sharing button

This commit is contained in:
nnduc
2026-11-18 18:44:40 +07:00
parent 0d8107a0a7
commit a0499ade59
8 changed files with 227 additions and 2 deletions
+20
View File
@@ -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())) {
@@ -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();
},