Browse Source

Improve the URL resolution flow in the Chat

pull/1443/head
Timothée Jaussoin 6 months ago
parent
commit
701739e971
  1. 2
      app/Widgets/Chat/_chat.tpl
  2. 64
      app/Widgets/Chat/chat.js
  3. 5
      app/Widgets/ChatActions/ChatActions.php
  4. 10
      app/Widgets/Chats/_chats_item.tpl
  5. 2
      src/Movim/EmbedLight.php

2
app/Widgets/Chat/_chat.tpl

@ -37,7 +37,7 @@
</a>
<ul class="list fill">
<div id="reply"></div>
<ul class="list" id="embed"></ul>
<ul class="list middle" id="embed"></ul>
<div id="dictaphone"></div>
<li class="emojis"></li>
<li class="main">

64
app/Widgets/Chat/chat.js

@ -20,6 +20,9 @@ var Chat = {
since: null,
sent: false,
// Timer
typingTimer: null,
// Autocomplete vars.
autocompleteList: null,
lastAutocomplete: null,
@ -464,6 +467,12 @@ var Chat = {
}
}, 3000);
if (Chat.typingTimer != null) clearTimeout(Chat.typingTimer);
if (this.value) {
if (Chat.lastResolvedUrl != this.value) Chat.lastResolvedUrl = null;
Chat.typingTimer = setTimeout(e => Chat.checkResolveUrl(this.value), 1000);
}
MovimUtils.textareaAutoheight(this);
Chat.checkEmojis(this.value);
Chat.scrollRestore();
@ -473,30 +482,12 @@ var Chat = {
}
textarea.addEventListener('paste', function (e) {
let url;
clearTimeout(Chat.typingTimer);
let clipboardData = e.clipboardData || window.clipboardData;
let pastedData = clipboardData.getData('Text');
try {
url = new URL(pastedData);
} catch (_) {
return false;
}
if ((url.protocol === "http:" || url.protocol === "https:")
&& textarea.value == '' && !Chat.isEncrypted()) {
Chat.enableSending();
let resolve = ChatActions_ajaxHttpResolveUrl(pastedData);
resolve.then(e => {
Chat.disableSending();
Chat.finishedSending();
}).catch(e => {
Chat.disableSending();
Chat.finishedSending();
});
}
Chat.checkResolveUrl(pastedData);
Chat.toggleAction();
});
@ -506,6 +497,33 @@ var Chat = {
Chat.autocompleteList = null;
},
checkResolveUrl(content) {
let url;
try {
url = new URL(content);
} catch (_) {
return false;
}
if ((url.protocol === "http:" || url.protocol === "https:")
&& !Chat.isEncrypted()
&& !content.includes(' ')
&& Chat.lastResolvedUrl != content) {
Chat.enableSending();
let resolve = ChatActions_ajaxHttpResolveUrl(content);
resolve.then(e => {
Chat.disableSending();
Chat.finishedSending();
}).catch(e => {
Chat.disableSending();
Chat.finishedSending();
});
Chat.lastResolvedUrl = content;
}
},
checkEmojis: function (value, reaction, noColon) {
value = value.toLowerCase();
@ -1692,14 +1710,14 @@ MovimEvents.registerWindow('loaded', 'chat', () => {
Upload.initiate((file) => {
if (MovimUtils.urlParts().page == 'chat'
&& (typeof(PublishStories) == 'undefined' || PublishStories.main == undefined)) {
&& (typeof (PublishStories) == 'undefined' || PublishStories.main == undefined)) {
Upload.prependName = 'chat';
}
});
Upload.attach((file) => {
if (MovimUtils.urlParts().page == 'chat'
&& (typeof(PublishStories) == 'undefined' || PublishStories.main == undefined)) {
&& (typeof (PublishStories) == 'undefined' || PublishStories.main == undefined)) {
Chat_ajaxHttpDaemonSendMessage(
Chat.getTextarea().dataset.jid,
false,

5
app/Widgets/ChatActions/ChatActions.php

@ -200,7 +200,10 @@ class ChatActions extends \Movim\Widget\Base
{
try {
$embed = (new Url)->resolve(trim($url));
$this->rpc('MovimTpl.fill', '#embed', (new Chat)->prepareEmbed($embed));
if ($embed != null) {
$this->rpc('MovimTpl.fill', '#embed', (new Chat)->prepareEmbed($embed));
}
} catch (\Exception $e) {
}
$this->rpc('Chat.disableSending');

10
app/Widgets/Chats/_chats_item.tpl

@ -116,9 +116,15 @@
<span class="moderator">{$c->__('chats.me')}:</span>
{/if}
{if="$message->resolvedUrl && $message->resolvedUrl->cache"}
<i class="material-symbols">link</i> {$message->resolvedUrl->cache->title}
<i class="material-symbols">link</i>
{if="$message->resolvedUrl->cache->providerName"}
{$message->resolvedUrl->cache->providerName}
{/if}
{$message->resolvedUrl->cache->title}
{if="!empty($message->resolvedUrl->cache->description)"}
| {$message->resolvedUrl->cache->description}
{$message->resolvedUrl->cache->description}
{/if}
{else}
{autoescape="off"}

2
src/Movim/EmbedLight.php

@ -6,8 +6,6 @@
namespace Movim;
use DateTime;
class EmbedLight
{
public string $title;

Loading…
Cancel
Save