Browse Source

Resolve the URLs the same way pictures are resolved for the chat messages

Reset the resolve status if the incoming or sent message is edited
Fix the CURL user agent
Fix stripTags to remove useless spaces
CSS fixes
Update the dependencies
pull/978/head
Timothée Jaussoin 5 years ago
parent
commit
9665944c3e
  1. 10
      app/Message.php
  2. 12
      app/Url.php
  3. 6
      app/helpers/StringHelper.php
  4. 2
      app/helpers/UtilsHelper.php
  5. 15
      app/widgets/Chat/Chat.php
  6. 27
      app/widgets/Chat/_chat_embed.tpl
  7. 14
      app/widgets/Chat/chat.css
  8. 22
      app/widgets/Chat/chat.js
  9. 8
      app/widgets/ChatActions/ChatActions.php
  10. 43
      composer.lock
  11. 49
      database/migrations/20201222194330_add_autoincrement_to_urls_table.php
  12. 1
      public/theme/css/icon.css
  13. 1
      public/theme/css/listn.css

10
app/Message.php

@ -17,7 +17,7 @@ class Message extends Model
protected $guarded = [];
protected $with = ['reactions', 'parent.from'];
protected $with = ['reactions', 'parent.from', 'resolvedUrl'];
protected $attributes = [
'type' => 'chat'
@ -42,6 +42,11 @@ class Message extends Model
return $this->belongsTo('App\Message', 'parentmid', 'mid');
}
public function resolvedUrl()
{
return $this->belongsTo('App\Url', 'urlid', 'id');
}
public function from()
{
return $this->belongsTo('App\Contact', 'jidfrom', 'id');
@ -141,6 +146,9 @@ class Message extends Model
public function set($stanza, $parent = false)
{
// We reset the URL resolution to refresh it once the message is displayed
$this->resolved = false;
$this->id = ($stanza->{'stanza-id'} && $stanza->{'stanza-id'}->attributes()->id)
? (string)$stanza->{'stanza-id'}->attributes()->id
: 'm_' . generateUUID();

12
app/Url.php

@ -9,17 +9,16 @@ use Embed\Http\CurlDispatcher;
class Url extends Model
{
protected $primaryKey = 'hash';
protected $keyType = 'string';
public $incrementing = false;
public static $id = 0;
public static function resolve($url)
{
if (Validator::url()->validate($url)) {
$hash = hash('sha256', $url);
$cached = \App\Url::find($hash);
$cached = \App\Url::where('hash', $hash)->first();
if ($cached) {
self::$id = $cached->id;
return $cached->cache;
} else {
$cached = new \App\Url;
@ -29,6 +28,8 @@ class Url extends Model
$cached->cache = $url;
$cached->save();
self::$id = $cached->id;
return $cached->cache;
}
}
@ -42,7 +43,8 @@ class Url extends Model
{
$dispatcher = new CurlDispatcher([
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5
CURLOPT_TIMEOUT => 5,
CURLOPT_USERAGENT => DEFAULT_HTTP_USER_AGENT,
]);
$embed = new EmbedLight(\Embed\Embed::create($url, null, $dispatcher));

6
app/helpers/StringHelper.php

@ -369,7 +369,11 @@ function stringToColor($string): string
*/
function stripTags($string): string
{
return strip_tags(preg_replace('/(<\/[^>]+?>)(<[^>\/][^>]*?>)/', '$1 $2', $string));
return strip_tags(
preg_replace('/\s+/', ' ',
preg_replace('/(<\/[^>]+?>)(<[^>\/][^>]*?>)/', '$1 $2', $string)
)
);
}
/**

2
app/helpers/UtilsHelper.php

@ -601,7 +601,7 @@ function generateKey($size)
return $hash;
}
define('DEFAULT_HTTP_USER_AGENT', 'Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0');
define('DEFAULT_HTTP_USER_AGENT', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://google.com/bot.html)');
/*
* @desc Request a simple url

15
app/widgets/Chat/Chat.php

@ -9,7 +9,7 @@ use Moxl\Xec\Action\Muc\SetConfig;
use App\Message;
use App\MessageFile;
use App\Reaction;
use App\Url;
use Moxl\Xec\Action\BOB\Request;
use Moxl\Xec\Action\Disco\Request as DiscoRequest;
@ -421,7 +421,8 @@ class Chat extends \Movim\Widget\Base
\App\Message::where('id', $oldid)->update([
'id' => $m->id,
'replaceid' => $m->id
'replaceid' => $m->id,
'resolved' => false
]);
} else {
$m = new \App\Message;
@ -1052,6 +1053,16 @@ class Chat extends \Movim\Widget\Base
}
}
if ($message->resolvedUrl && !$message->file
&& !$message->card && !$message->sticker) {
$resolved = $message->resolvedUrl->cache;
if ($resolved) {
$tpl = $this->tpl();
$tpl->assign('embed', $resolved);
$message->card = $tpl->draw('_chat_embed');
}
}
// Parent
if ($message->parent) {
if ($message->parent->file) {

27
app/widgets/Chat/_chat_embed.tpl

@ -0,0 +1,27 @@
<li class="block">
{if="!empty($embed->images)"}
<span class="primary icon thumb active color {$embed->url|stringToColor}"
onclick="Preview_ajaxShow('{$embed->images[0]['url']|protectPicture}')"
style="background-image: url({$embed->images[0]['url']|protectPicture})"
>
{if="count($embed->images) > 1"}
<i class="material-icons">photo_library</i>
{else}
<i class="material-icons">image</i>
{/if}
</span>
{else}
<span class="primary icon bubble gray">
{if="$embed->providerIcon"}
<img src="{$embed->providerIcon}"/>
{else}
<i class="material-icons">link</i>
{/if}
</span>
{/if}
<div>
<p class="line">{$embed->title}</p>
<p class="line">{$embed->description}</p>
</div>
</li>

14
app/widgets/Chat/chat.css

@ -476,7 +476,19 @@ main:not(.enabled) #chat_widget {
/* Card */
#chat_widget .bubble ul.card.list {
max-width: 55rem;
max-width: 45rem;
}
#chat_widget .bubble ul.card.list li span.primary:not(.thumb) {
margin-top: 1rem;
}
#chat_widget .bubble span.resource + ul.card.list li {
margin-top: 0.5rem;
}
#chat_widget .bubble ul.card.list li {
margin-bottom: 0.5rem;
}
#chat_widget .bubble ul.card.list .info {

22
app/widgets/Chat/chat.js

@ -822,11 +822,6 @@ var Chat = {
reactions.innerHTML = data.reactionsHtml;
}
if (data.card) {
bubble.querySelector('div.bubble').classList.add('file');
msg.appendChild(Chat.getCardHtml(data.card));
}
if (isMuc) {
var resourceSpan = document.createElement('span');
resourceSpan.classList.add('resource');
@ -836,6 +831,11 @@ var Chat = {
msg.appendChild(resourceSpan);
}
if (data.card) {
bubble.querySelector('div.bubble').classList.add('file');
msg.appendChild(Chat.getCardHtml(data.card));
}
// Parent
if (data.parent) {
msg.appendChild(Chat.getParentHtml(data.parent));
@ -867,8 +867,8 @@ var Chat = {
elem.parentElement.replaceChild(msg, elem);
mergeMsg = true;
// If the previous message was not a file and is replaced by it
if (data.file != null) {
// If the previous message was not a file or card and is replaced by it
if (data.file != null || data.card != null) {
msg.parentElement.classList.add('file');
}
@ -997,9 +997,13 @@ var Chat = {
},
getCardHtml: function(card) {
var ul = document.createElement('ul');
ul.setAttribute('class', 'card list noanim active');
ul.setAttribute('class', 'card list middle noanim');
ul.innerHTML = card;
if (ul.querySelector('li').getAttribute('onclick')) {
ul.classList.add('active');
}
return ul;
},
getFileHtml: function(file, sticker) {

8
app/widgets/ChatActions/ChatActions.php

@ -1,5 +1,7 @@
<?php
use App\Url;
use Moxl\Xec\Action\Message\Retract;
include_once WIDGETS_PATH.'ContactActions/ContactActions.php';
@ -88,6 +90,12 @@ class ChatActions extends \Movim\Widget\Base
if ($picture != false) {
$message->file = (array)$picture;
$this->rpc('Chat.refreshMessage', $message->mid);
} else {
try {
Url::resolve(trim($message->body));
$message->urlid = Url::$id;
$this->rpc('Chat.refreshMessage', $message->mid);
} catch (\Exception $e) {}
}
$message->resolved = true;

43
composer.lock

@ -8,16 +8,16 @@
"packages": [
{
"name": "cakephp/core",
"version": "4.1.7",
"version": "4.2.0",
"source": {
"type": "git",
"url": "https://github.com/cakephp/core.git",
"reference": "6584be57b8c39b7e0c5cc0400cc796a55c8e5344"
"reference": "635a924ff9ddc2e2f81e9073643c59e7ed6f7f55"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/cakephp/core/zipball/6584be57b8c39b7e0c5cc0400cc796a55c8e5344",
"reference": "6584be57b8c39b7e0c5cc0400cc796a55c8e5344",
"url": "https://api.github.com/repos/cakephp/core/zipball/635a924ff9ddc2e2f81e9073643c59e7ed6f7f55",
"reference": "635a924ff9ddc2e2f81e9073643c59e7ed6f7f55",
"shasum": ""
},
"require": {
@ -26,7 +26,8 @@
},
"suggest": {
"cakephp/cache": "To use Configure::store() and restore().",
"cakephp/event": "To use PluginApplicationInterface or plugin applications."
"cakephp/event": "To use PluginApplicationInterface or plugin applications.",
"league/container": "To use Container and ServiceProvider classes"
},
"type": "library",
"autoload": {
@ -60,20 +61,20 @@
"issues": "https://github.com/cakephp/cakephp/issues",
"source": "https://github.com/cakephp/core"
},
"time": "2020-10-21T22:39:20+00:00"
"time": "2020-12-03T14:30:58+00:00"
},
{
"name": "cakephp/database",
"version": "4.1.7",
"version": "4.2.0",
"source": {
"type": "git",
"url": "https://github.com/cakephp/database.git",
"reference": "1088b6bb2b45f6e84aa2e31636934f5f8c70f229"
"reference": "376b9c7c1bfe364452839fd351cf7a10bf4d75ae"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/cakephp/database/zipball/1088b6bb2b45f6e84aa2e31636934f5f8c70f229",
"reference": "1088b6bb2b45f6e84aa2e31636934f5f8c70f229",
"url": "https://api.github.com/repos/cakephp/database/zipball/376b9c7c1bfe364452839fd351cf7a10bf4d75ae",
"reference": "376b9c7c1bfe364452839fd351cf7a10bf4d75ae",
"shasum": ""
},
"require": {
@ -115,20 +116,20 @@
"issues": "https://github.com/cakephp/cakephp/issues",
"source": "https://github.com/cakephp/database"
},
"time": "2020-12-05T06:01:57+00:00"
"time": "2020-12-14T09:13:32+00:00"
},
{
"name": "cakephp/datasource",
"version": "4.1.7",
"version": "4.2.0",
"source": {
"type": "git",
"url": "https://github.com/cakephp/datasource.git",
"reference": "8fdf6abbb84fb8a098f91b8e2ef2d18397d83f11"
"reference": "e1f9483f6d57e2799a34654d156bc08d5ff930ac"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/cakephp/datasource/zipball/8fdf6abbb84fb8a098f91b8e2ef2d18397d83f11",
"reference": "8fdf6abbb84fb8a098f91b8e2ef2d18397d83f11",
"url": "https://api.github.com/repos/cakephp/datasource/zipball/e1f9483f6d57e2799a34654d156bc08d5ff930ac",
"reference": "e1f9483f6d57e2799a34654d156bc08d5ff930ac",
"shasum": ""
},
"require": {
@ -173,20 +174,20 @@
"issues": "https://github.com/cakephp/cakephp/issues",
"source": "https://github.com/cakephp/datasource"
},
"time": "2020-10-19T19:03:43+00:00"
"time": "2020-12-09T02:44:24+00:00"
},
{
"name": "cakephp/utility",
"version": "4.1.7",
"version": "4.2.0",
"source": {
"type": "git",
"url": "https://github.com/cakephp/utility.git",
"reference": "3d1b4a39e85a2b277d1308bae3e6398dd4ed4c57"
"reference": "d8e667953f13f399fea08a7e204b9e0f022d5d3c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/cakephp/utility/zipball/3d1b4a39e85a2b277d1308bae3e6398dd4ed4c57",
"reference": "3d1b4a39e85a2b277d1308bae3e6398dd4ed4c57",
"url": "https://api.github.com/repos/cakephp/utility/zipball/d8e667953f13f399fea08a7e204b9e0f022d5d3c",
"reference": "d8e667953f13f399fea08a7e204b9e0f022d5d3c",
"shasum": ""
},
"require": {
@ -232,7 +233,7 @@
"issues": "https://github.com/cakephp/cakephp/issues",
"source": "https://github.com/cakephp/utility"
},
"time": "2020-11-28T00:16:31+00:00"
"time": "2020-12-04T14:13:14+00:00"
},
{
"name": "cboden/ratchet",

49
database/migrations/20201222194330_add_autoincrement_to_urls_table.php

@ -0,0 +1,49 @@
<?php
use Movim\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddAutoincrementToUrlsTable extends Migration
{
public function up()
{
$this->disableForeignKeyCheck();
$this->schema->table('urls', function (Blueprint $table) {
$table->dropPrimary();
});
$this->schema->table('urls', function (Blueprint $table) {
$table->increments('id')->first();
$table->unique('hash');
});
$this->schema->table('messages', function (Blueprint $table) {
$table->integer('urlid')->unsigned()->nullable();
$table->foreign('urlid')
->references('id')->on('urls')
->onDelete('set null');
});
$this->enableForeignKeyCheck();
}
public function down()
{
$this->disableForeignKeyCheck();
$this->schema->table('messages', function (Blueprint $table) {
$table->dropForeign('messages_urlid_foreign');
$table->dropColumn('urlid');
});
$this->schema->table('urls', function (Blueprint $table) {
$table->dropColumn('id');
$table->dropUnique('urls_hash_unique');
$table->primary('hash');
});
$this->enableForeignKeyCheck();
}
}

1
public/theme/css/icon.css

@ -66,6 +66,7 @@ span.icon.primary.thumb {
margin-left: -1.5rem;
align-self: stretch;
height: auto;
text-shadow: 0 0 1rem rgba(0, 0, 0, 0.5);
}
ul.fill li span.icon.primary.thumb {

1
public/theme/css/listn.css

@ -374,6 +374,7 @@ ul li div.bubble p {
font-size: 1.75rem;
word-wrap: break-word;
white-space: pre-wrap;
word-break: normal;
position: relative;
}

Loading…
Cancel
Save