From a0499ade599747cc2f14b851af163ba01df02b1c Mon Sep 17 00:00:00 2001 From: nnduc Date: Wed, 18 Nov 2026 18:44:40 +0700 Subject: [PATCH] sharing button --- frontend/locales/extra.ini | 5 + frontend/locales/extra/vi.po | 15 +++ frontend/widgets/Chat/chat.js | 20 ++++ frontend/widgets/ChatActions/chatactions.js | 94 +++++++++++++++++++ src/widgets/chat.rs | 1 + src/widgets/chatactions.rs | 39 +++++++- .../ChatActions/_chatactions_forward.html | 38 ++++++++ .../_chatactions_message_dialog.html | 17 ++++ 8 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 templates/ChatActions/_chatactions_forward.html diff --git a/frontend/locales/extra.ini b/frontend/locales/extra.ini index 1b5c090..794aabc 100644 --- a/frontend/locales/extra.ini +++ b/frontend/locales/extra.ini @@ -53,6 +53,11 @@ files_recordings = Recordings files_files = Files files_message = Show the message files_empty = Nothing shared yet +forward = Forward +forward_title = Forward to… +forward_filter = Search a discussion +forward_error = This message cannot be forwarded +forward_encrypted_file = This encrypted file can only be sent in an encrypted discussion ; Upstream strings still in use, their widgets are not part of this edition diff --git a/frontend/locales/extra/vi.po b/frontend/locales/extra/vi.po index 7fd24d6..aa811cc 100644 --- a/frontend/locales/extra/vi.po +++ b/frontend/locales/extra/vi.po @@ -3110,3 +3110,18 @@ msgstr "Chọn một mật khẩu tốt" msgid "Type your password again" msgstr "Nhập lại mật khẩu" + +msgid "Forward" +msgstr "Chuyển tiếp" + +msgid "Forward to…" +msgstr "Chuyển tiếp tới…" + +msgid "Search a discussion" +msgstr "Tìm cuộc trò chuyện" + +msgid "This message cannot be forwarded" +msgstr "Không thể chuyển tiếp tin nhắn này" + +msgid "This encrypted file can only be sent in an encrypted discussion" +msgstr "Tệp mã hóa này chỉ có thể gửi trong cuộc trò chuyện được mã hóa" diff --git a/frontend/widgets/Chat/chat.js b/frontend/widgets/Chat/chat.js index cc9e70d..4052d82 100644 --- a/frontend/widgets/Chat/chat.js +++ b/frontend/widgets/Chat/chat.js @@ -70,6 +70,7 @@ var Chat = { Chat.pagination = config['pagination']; Chat.delivery_error = config['delivery_error']; Chat.action_impossible_encrypted_error = config['action_impossible_encrypted_error']; + Chat.encrypted_file_error = config['encrypted_file_error']; }, autocomplete: function (event, jid) { RoomsUtils_ajaxMucUsersAutocomplete(jid); @@ -292,6 +293,13 @@ var Chat = { }); } } else { + // The key of an encrypted file (a forwarded one) must not go in clear + if (typeof AESGCM_REGEX != 'undefined' && AESGCM_REGEX.test(text.trim())) { + Toast.send(Chat.encrypted_file_error); + Chat.disableSending(); + return; + } + Chat.lastSentUUID = id = crypto.randomUUID(); let request = Chat_ajaxHttpDaemonSendMessage(id, jid, text, isMuc, null, replyMid, mucReceipts); request.then(response => { @@ -451,6 +459,18 @@ var Chat = { Chat.toggleAction(); var textarea = Chat.getTextarea(); + + // A decrypted file forwarded to this discussion: through the upload + // panel, it is encrypted again if this discussion is encrypted + let forwarded = window.movimForwardFile; + if (forwarded && forwarded.jid == textarea.dataset.jid + && typeof Upload != 'undefined' && Upload.file == null) { + delete window.movimForwardFile; + Upload.file = forwarded.file; + Upload.name = forwarded.file.name; + Upload_ajaxGetPanel(); + } + textarea.onkeydown = function (event) { if ((event.key == 'ArrowLeft' && Chat.tryPreviousEmoji()) || (event.key == 'ArrowRight' && Chat.tryNextEmoji())) { diff --git a/frontend/widgets/ChatActions/chatactions.js b/frontend/widgets/ChatActions/chatactions.js index bb9cec7..2bf75d1 100644 --- a/frontend/widgets/ChatActions/chatactions.js +++ b/frontend/widgets/ChatActions/chatactions.js @@ -1,5 +1,6 @@ var ChatActions = { message: null, + forwardError: null, // OMEMO messages of the open conversation: {jid, entries: [[mid, id, messageid, published]], bodies} index: null, keywords: '', @@ -7,6 +8,99 @@ var ChatActions = { setMessage: function (message) { ChatActions.message = message; }, + initMessageDialog: function (forwardError) { + ChatActions.forwardError = forwardError; + + // The system share sheet (mostly on mobile), to send to other apps + if (navigator.share) { + document.querySelector('#chat_actions li.share')?.classList.remove('hide'); + } + }, + + // The text of the message, decrypted by this browser for OMEMO + messageText: async function () { + let message = ChatActions.message; + if (!message) return null; + + if (message.encrypted) { + if (typeof ChatOmemoDB == 'undefined') return null; + let text = await ChatOmemoDB.getMessage(message.id); + if (typeof text != 'string' && message.messageid) text = await ChatOmemoDB.getMessage(message.messageid); + return (typeof text == 'string' && text.trim()) ? text : null; + } + + let text = MovimUtils.decodeHTMLEntities(message.body ?? ''); + return text.trim() ? text : null; + }, + + filterForward: function (keyword) { + let words = ChatActions.normalize(keyword).split(/\s+/).filter(w => w); + document.querySelectorAll('#forward_targets li').forEach(li => { + let haystack = ChatActions.normalize(li.dataset.filter); + li.classList.toggle('hide', !words.every(w => haystack.includes(w))); + }); + }, + + // Put the message in the draft of the other discussion and open it, the + // user can review it and send it as usual (encrypted if enabled there) + forwardTo: async function (jid, muc) { + let text = await ChatActions.messageText(); + if (!text) { + Toast.send(ChatActions.forwardError); + return; + } + + if (typeof AESGCM_REGEX != 'undefined' && AESGCM_REGEX.test(text.trim())) { + // An encrypted file: decrypted here and uploaded again from the + // other discussion, encrypted with a new key only if it is + // encrypted. Kept on window, the widget scripts are reloaded. + try { + window.movimForwardFile = { jid: jid, file: await ChatActions.decryptedFile(text.trim()) }; + } catch (e) { + console.error(e); + Toast.send(ChatActions.forwardError); + return; + } + } else { + localStorage.setItem(jid + '_message', text); + } + + Dialog_ajaxClear(); + ChatActions_ajaxForwardTo(jid, muc); + }, + + decryptedFile: async function (url) { + let [, , filename] = url.match(AESGCM_REGEX); + let blob = await (await fetch(await ChatOmemo.decryptFile(url))).blob(); + return new File([blob], filename, { type: blob.type }); + }, + + share: async function () { + let text = await ChatActions.messageText(); + if (!text || !navigator.share) return; + + try { + let url = text.trim(); + if (typeof AESGCM_REGEX != 'undefined' && AESGCM_REGEX.test(url)) { + // An encrypted file: share the decrypted file, never the key + let file = await ChatActions.decryptedFile(url); + + if (!navigator.canShare || !navigator.canShare({ files: [file] })) { + Toast.send(ChatActions.forwardError); + return; + } + await navigator.share({ files: [file] }); + } else if (/^https?:\/\/\S+$/.test(url)) { + await navigator.share({ url: url }); + } else { + await navigator.share({ text: text }); + } + } catch (e) { + // Cancelled by the user + if (e.name != 'AbortError') Toast.send(ChatActions.forwardError); + } + }, + focusSearch: function () { document.querySelector('form[name=search] input').focus(); }, diff --git a/src/widgets/chat.rs b/src/widgets/chat.rs index 632b67a..afa52db 100644 --- a/src/widgets/chat.rs +++ b/src/widgets/chat.rs @@ -31,6 +31,7 @@ pub async fn call(ctx: &Ctx, func: &str, p: &P<'_>) -> bool { "pagination": PAGINATION, "delivery_error": ctx.t("message.error", &[]), "action_impossible_encrypted_error": ctx.t("chat.action_impossible_encrypted", &[]), + "encrypted_file_error": ctx.t("chatactions.forward_encrypted_file", &[]), }) ); } diff --git a/src/widgets/chatactions.rs b/src/widgets/chatactions.rs index e3ebc23..7c2fe0e 100644 --- a/src/widgets/chatactions.rs +++ b/src/widgets/chatactions.rs @@ -1,5 +1,5 @@ -//! ChatActions widget: actions on a message (react, copy, reply, edit, -//! retract, moderate), search in a discussion, shared files, URL previews. +//! ChatActions widget: actions on a message (react, copy, reply, forward, +//! share, edit, retract, moderate), search in a discussion, shared files, URL previews. use serde_json::json; @@ -30,6 +30,8 @@ pub async fn call(ctx: &Ctx, func: &str, p: &P<'_>) -> bool { search(ctx, &p.str(0), &p.str(1), p.b(2), &encrypted).await; } "ajaxShowFiles" => show_files(ctx, &p.str(0), p.b(1)).await, + "ajaxShowForwardDialog" => show_forward_dialog(ctx).await, + "ajaxForwardTo" => super::search::chat(ctx, &p.str(0), p.b(1)).await, "ajaxCopiedMessageText" => ctx.toast(&ctx.t("chatactions.copied_text", &[])), "ajaxHttpDaemonRetract" => retract(ctx, p.i(0).unwrap_or(0)).await, "ajaxHttpDaemonModerate" => moderate(ctx, p.i(0).unwrap_or(0)).await, @@ -119,6 +121,39 @@ async fn show_message_dialog(ctx: &Ctx, mid: i64) { }), ); super::dialog(ctx, &html, false, false); + rpc!(ctx, "ChatActions.initMessageDialog", ctx.t("chatactions.forward_error", &[])); +} + +/// Where to forward a message: the open discussions (most recent first), +/// the bookmarked rooms, then the rest of the roster. The browser fills the +/// draft of the chosen discussion and opens it, the normal sending (with +/// OMEMO when enabled there) does the rest. +async fn show_forward_dialog(ctx: &Ctx) { + let me = ctx.jid(); + let mut open = db::open_chats(ctx.db(), &me).await.unwrap_or_default(); + open.reverse(); + open.retain(|j| *j != me); + + let mut roster: Vec = ctx.st(|st| st.roster.keys().filter(|j| !open.contains(j)).cloned().collect()).unwrap_or_default(); + let views = super::contact_views(ctx, &[open.clone(), roster.clone()].concat()).await; + roster.sort_by_key(|j| views.get(j).map(|v| v.truename.to_lowercase()).unwrap_or_default()); + + let contact = |jid: &String| { + views.get(jid).map(|c| json!({ "jid": c.jid, "name": c.truename, "picture": c.picture, "muc": false })) + }; + + let mut rooms: Vec<_> = ctx + .st(|st| st.conferences.values().cloned().collect::>()) + .unwrap_or_default() + .into_iter() + .map(|c| json!({ "jid": c.jid, "name": c.title(), "picture": rooms::room_picture(ctx, &c), "muc": true })) + .collect(); + rooms.sort_by_key(|r| r["name"].as_str().unwrap_or("").to_lowercase()); + + let targets: Vec<_> = open.iter().filter_map(contact).chain(rooms).chain(roster.iter().filter_map(contact)).collect(); + + let html = ctx.render("ChatActions/_chatactions_forward.html", json!({ "targets": targets })); + super::dialog(ctx, &html, true, false); } /// The browser searches and lists the OMEMO messages itself (the server diff --git a/templates/ChatActions/_chatactions_forward.html b/templates/ChatActions/_chatactions_forward.html new file mode 100644 index 0000000..8c0077c --- /dev/null +++ b/templates/ChatActions/_chatactions_forward.html @@ -0,0 +1,38 @@ +
+

{{ __('chatactions.forward_title') }}

+
    + +
+
    + {% for t in targets %} +
  • + + + + {% if t.muc %} + + forum + + {% endif %} +
    +

    {{ t.name }}

    +

    {{ t.jid }}

    +
    +
  • + {% endfor %} +
+
+
+ +
diff --git a/templates/ChatActions/_chatactions_message_dialog.html b/templates/ChatActions/_chatactions_message_dialog.html index 444ff90..47742d3 100644 --- a/templates/ChatActions/_chatactions_message_dialog.html +++ b/templates/ChatActions/_chatactions_message_dialog.html @@ -38,6 +38,23 @@ +
  • + + forward + +
    +

    {{ __('chatactions.forward') }}

    +
    +
  • + + {% if editable %}