sharing button
This commit is contained in:
@@ -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();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user