Prechádzať zdrojové kódy

Work on making emojis plugin self containing

Use hook to set `is_only_emojis` in the emojis plugin.

Updates #3421
JC Brand 1 rok pred
rodič
commit
5ea996f0fc

+ 0 - 2
src/headless/plugins/chat/model.js

@@ -849,7 +849,6 @@ class ChatBox extends ModelWithContact {
     }
 
     async getOutgoingMessageAttributes (attrs) {
-        await api.emojis.initialize();
         const is_spoiler = !!this.get('composing_spoiler');
         const origin_id = u.getUniqueId();
         const text = attrs?.body;
@@ -858,7 +857,6 @@ class ChatBox extends ModelWithContact {
             'from': _converse.session.get('bare_jid'),
             'fullname': _converse.state.xmppstatus.get('fullname'),
             'id': origin_id,
-            'is_only_emojis': text ? u.isOnlyEmojis(text) : false,
             'jid': this.get('jid'),
             'message': body,
             'msgid': origin_id,

+ 45 - 47
src/headless/plugins/chat/parsers.js

@@ -110,51 +110,51 @@ export async function parseMessage (stanza) {
     /**
      * The object which {@link parseMessage} returns
      * @typedef {Object} MessageAttributes
-     * @property { ('me'|'them') } sender - Whether the message was sent by the current user or someone else
-     * @property { Array<Object> } references - A list of objects representing XEP-0372 references
-     * @property { Boolean } editable - Is this message editable via XEP-0308?
-     * @property { Boolean } is_archived -  Is this message from a XEP-0313 MAM archive?
-     * @property { Boolean } is_carbon - Is this message a XEP-0280 Carbon?
-     * @property { Boolean } is_delayed - Was delivery of this message was delayed as per XEP-0203?
-     * @property { Boolean } is_encrypted -  Is this message XEP-0384  encrypted?
-     * @property { Boolean } is_error - Whether an error was received for this message
-     * @property { Boolean } is_headline - Is this a "headline" message?
-     * @property { Boolean } is_markable - Can this message be marked with a XEP-0333 chat marker?
-     * @property { Boolean } is_marker - Is this message a XEP-0333 Chat Marker?
-     * @property { Boolean } is_only_emojis - Does the message body contain only emojis?
-     * @property { Boolean } is_spoiler - Is this a XEP-0382 spoiler message?
-     * @property { Boolean } is_tombstone - Is this a XEP-0424 tombstone?
-     * @property { Boolean } is_unstyled - Whether XEP-0393 styling hints should be ignored
-     * @property { Boolean } is_valid_receipt_request - Does this message request a XEP-0184 receipt (and is not from us or a carbon or archived message)
-     * @property { Object } encrypted -  XEP-0384 encryption payload attributes
-     * @property { String } body - The contents of the <body> tag of the message stanza
-     * @property { String } chat_state - The XEP-0085 chat state notification contained in this message
-     * @property { String } contact_jid - The JID of the other person or entity
-     * @property { String } edited - An ISO8601 string recording the time that the message was edited per XEP-0308
-     * @property { String } error_condition - The defined error condition
-     * @property { String } error_text - The error text received from the server
-     * @property { String } error_type - The type of error received from the server
-     * @property { String } from - The sender JID
-     * @property { String } fullname - The full name of the sender
-     * @property { String } marker - The XEP-0333 Chat Marker value
-     * @property { String } marker_id - The `id` attribute of a XEP-0333 chat marker
-     * @property { String } msgid - The root `id` attribute of the stanza
-     * @property { String } nick - The roster nickname of the sender
-     * @property { String } oob_desc - The description of the XEP-0066 out of band data
-     * @property { String } oob_url - The URL of the XEP-0066 out of band data
-     * @property { String } origin_id - The XEP-0359 Origin ID
-     * @property { String } receipt_id - The `id` attribute of a XEP-0184 <receipt> element
-     * @property { String } received - An ISO8601 string recording the time that the message was received
-     * @property { String } replace_id - The `id` attribute of a XEP-0308 <replace> element
-     * @property { String } retracted - An ISO8601 string recording the time that the message was retracted
-     * @property { String } retracted_id - The `id` attribute of a XEP-424 <retracted> element
-     * @property { String } spoiler_hint  The XEP-0382 spoiler hint
-     * @property { String } stanza_id - The XEP-0359 Stanza ID. Note: the key is actualy `stanza_id ${by_jid}` and there can be multiple.
-     * @property { String } subject - The <subject> element value
-     * @property { String } thread - The <thread> element value
-     * @property { String } time - The time (in ISO8601 format), either given by the XEP-0203 <delay> element, or of receipt.
-     * @property { String } to - The recipient JID
-     * @property { String } type - The type of message
+     * @property {('me'|'them')} sender - Whether the message was sent by the current user or someone else
+     * @property {Array<Object>} references - A list of objects representing XEP-0372 references
+     * @property {Boolean} editable - Is this message editable via XEP-0308?
+     * @property {Boolean} is_archived -  Is this message from a XEP-0313 MAM archive?
+     * @property {Boolean} is_carbon - Is this message a XEP-0280 Carbon?
+     * @property {Boolean} is_delayed - Was delivery of this message was delayed as per XEP-0203?
+     * @property {Boolean} is_encrypted -  Is this message XEP-0384  encrypted?
+     * @property {Boolean} is_error - Whether an error was received for this message
+     * @property {Boolean} is_headline - Is this a "headline" message?
+     * @property {Boolean} is_markable - Can this message be marked with a XEP-0333 chat marker?
+     * @property {Boolean} is_marker - Is this message a XEP-0333 Chat Marker?
+     * @property {Boolean} is_only_emojis - Does the message body contain only emojis?
+     * @property {Boolean} is_spoiler - Is this a XEP-0382 spoiler message?
+     * @property {Boolean} is_tombstone - Is this a XEP-0424 tombstone?
+     * @property {Boolean} is_unstyled - Whether XEP-0393 styling hints should be ignored
+     * @property {Boolean} is_valid_receipt_request - Does this message request a XEP-0184 receipt (and is not from us or a carbon or archived message)
+     * @property {Object} encrypted -  XEP-0384 encryption payload attributes
+     * @property {String} body - The contents of the <body> tag of the message stanza
+     * @property {String} chat_state - The XEP-0085 chat state notification contained in this message
+     * @property {String} contact_jid - The JID of the other person or entity
+     * @property {String} edited - An ISO8601 string recording the time that the message was edited per XEP-0308
+     * @property {String} error_condition - The defined error condition
+     * @property {String} error_text - The error text received from the server
+     * @property {String} error_type - The type of error received from the server
+     * @property {String} from - The sender JID
+     * @property {String} fullname - The full name of the sender
+     * @property {String} marker - The XEP-0333 Chat Marker value
+     * @property {String} marker_id - The `id` attribute of a XEP-0333 chat marker
+     * @property {String} msgid - The root `id` attribute of the stanza
+     * @property {String} nick - The roster nickname of the sender
+     * @property {String} oob_desc - The description of the XEP-0066 out of band data
+     * @property {String} oob_url - The URL of the XEP-0066 out of band data
+     * @property {String} origin_id - The XEP-0359 Origin ID
+     * @property {String} receipt_id - The `id` attribute of a XEP-0184 <receipt> element
+     * @property {String} received - An ISO8601 string recording the time that the message was received
+     * @property {String} replace_id - The `id` attribute of a XEP-0308 <replace> element
+     * @property {String} retracted - An ISO8601 string recording the time that the message was retracted
+     * @property {String} retracted_id - The `id` attribute of a XEP-424 <retracted> element
+     * @property {String} spoiler_hint  The XEP-0382 spoiler hint
+     * @property {String} stanza_id - The XEP-0359 Stanza ID. Note: the key is actualy `stanza_id ${by_jid}` and there can be multiple.
+     * @property {String} subject - The <subject> element value
+     * @property {String} thread - The <thread> element value
+     * @property {String} time - The time (in ISO8601 format), either given by the XEP-0203 <delay> element, or of receipt.
+     * @property {String} to - The recipient JID
+     * @property {String} type - The type of message
      */
     const delay = sizzle(`delay[xmlns="${Strophe.NS.DELAY}"]`, original_stanza).pop();
     const marker = getChatMarker(stanza);
@@ -201,11 +201,9 @@ export async function parseMessage (stanza) {
             return new StanzaParseError(`Invalid Stanza: Forged MAM message from ${from}`, stanza);
         }
     }
-    await api.emojis.initialize();
     attrs = Object.assign(
         {
             'message': attrs.body || attrs.error, // TODO: Remove and use body and error attributes instead
-            'is_only_emojis': attrs.body ? u.isOnlyEmojis(attrs.body) : false,
             'is_valid_receipt_request': isValidReceiptRequest(stanza, attrs)
         },
         attrs

+ 22 - 2
src/headless/plugins/emoji/plugin.js

@@ -3,13 +3,13 @@
  * @copyright 2022, the Converse.js contributors
  * @license Mozilla Public License (MPLv2)
  */
+import { getOpenPromise } from '@converse/openpromise';
 import _converse from '../../shared/_converse.js';
 import api from '../../shared/api/index.js';
 import converse from '../../shared/api/public.js';
-import { getOpenPromise } from '@converse/openpromise';
-import './utils.js';
 import EmojiPicker from './picker.js';
 import emojis from './api.js';
+import { isOnlyEmojis } from './utils.js';
 
 converse.emojis = {
     'initialized': false,
@@ -61,5 +61,25 @@ converse.plugins.add('converse-emoji', {
         Object.assign(_converse, exports); // XXX: DEPRECATED
         Object.assign(_converse.exports, exports);
         Object.assign(api, emojis);
+
+        api.listen.on('getOutgoingMessageAttributes', async (_chat, attrs) => {
+            await api.emojis.initialize();
+            const { original_text: text } = attrs;
+            return {
+                ...attrs,
+                is_only_emojis: text ? isOnlyEmojis(text) : false,
+            };
+        });
+
+        async function parseMessage (_stanza, attrs) {
+            await api.emojis.initialize();
+            return {
+                ...attrs,
+                is_only_emojis: attrs.body ? isOnlyEmojis(attrs.body) : false
+            }
+        }
+
+        api.listen.on('parseMUCMessage', parseMessage);
+        api.listen.on('parseMessage', parseMessage);
     },
 });

+ 1 - 1
src/headless/plugins/emoji/utils.js

@@ -183,7 +183,7 @@ function shortnamesToUnicode (str) {
  * @param { String } text - A string which migh be just an emoji shortname
  * @returns { Boolean }
  */
-function isOnlyEmojis (text) {
+export function isOnlyEmojis (text) {
     const words = text.trim().split(/\s+/);
     if (words.length === 0 || words.length > 3) {
         return false;

+ 2 - 3
src/headless/plugins/mam/utils.js

@@ -10,8 +10,8 @@ import _converse from '../../shared/_converse.js';
 import api from '../../shared/api/index.js';
 import converse from '../../shared/api/public.js';
 import log from '../../log.js';
-import { parseMUCMessage } from '../../plugins/muc/parsers';
-import { parseMessage } from '../../plugins/chat/parsers';
+import { parseMUCMessage } from '../../plugins/muc/parsers.js';
+import { parseMessage } from '../../plugins/chat/parsers.js';
 import { CHATROOMS_TYPE } from '../../shared/constants.js';
 
 const { NS } = Strophe;
@@ -102,7 +102,6 @@ export function preMUCJoinMAMFetch(muc) {
  * @param {('forwards'|'backwards'|null)} [should_page=null]
  */
 export async function handleMAMResult(model, result, query, options, should_page) {
-    await api.emojis.initialize();
     const is_muc = model.get('type') === CHATROOMS_TYPE;
     const doParseMessage = /** @param {Element} s*/ (s) =>
         is_muc ? parseMUCMessage(s, /** @type {MUC} */ (model)) : parseMessage(s);

+ 6 - 3
src/headless/plugins/muc/muc.js

@@ -1064,8 +1064,11 @@ class MUC extends ChatBox {
         return [updated_message, updated_references];
     }
 
+    /**
+     * @param {Object} [attrs] - A map of attributes to be saved on the message
+     * @returns {Promise<MUCMessage>}
+     */
     async getOutgoingMessageAttributes (attrs) {
-        await api.emojis.initialize();
         const is_spoiler = this.get('composing_spoiler');
         let text = '', references;
         if (attrs?.body) {
@@ -1082,11 +1085,11 @@ class MUC extends ChatBox {
             'msgid': origin_id,
             'from': `${this.get('jid')}/${this.get('nick')}`,
             'fullname': this.get('nick'),
-            'is_only_emojis': text ? u.isOnlyEmojis(text) : false,
             'message': body,
             'nick': this.get('nick'),
             'sender': 'me',
-            'type': 'groupchat'
+            'type': 'groupchat',
+            'original_text': text,
         }, u.getMediaURLsMetadata(text));
 
         /**

+ 52 - 55
src/headless/plugins/muc/parsers.js

@@ -170,58 +170,58 @@ export async function parseMUCMessage (stanza, chatbox) {
     /**
      * @typedef {Object} MUCMessageAttributes
      * The object which {@link parseMUCMessage} returns
-     * @property { ('me'|'them') } sender - Whether the message was sent by the current user or someone else
-     * @property { Array<Object> } activities - A list of objects representing XEP-0316 MEP notification data
-     * @property { Array<Object> } references - A list of objects representing XEP-0372 references
-     * @property { Boolean } editable - Is this message editable via XEP-0308?
-     * @property { Boolean } is_archived -  Is this message from a XEP-0313 MAM archive?
-     * @property { Boolean } is_carbon - Is this message a XEP-0280 Carbon?
-     * @property { Boolean } is_delayed - Was delivery of this message was delayed as per XEP-0203?
-     * @property { Boolean } is_encrypted -  Is this message XEP-0384  encrypted?
-     * @property { Boolean } is_error - Whether an error was received for this message
-     * @property { Boolean } is_headline - Is this a "headline" message?
-     * @property { Boolean } is_markable - Can this message be marked with a XEP-0333 chat marker?
-     * @property { Boolean } is_marker - Is this message a XEP-0333 Chat Marker?
-     * @property { Boolean } is_only_emojis - Does the message body contain only emojis?
-     * @property { Boolean } is_spoiler - Is this a XEP-0382 spoiler message?
-     * @property { Boolean } is_tombstone - Is this a XEP-0424 tombstone?
-     * @property { Boolean } is_unstyled - Whether XEP-0393 styling hints should be ignored
-     * @property { Boolean } is_valid_receipt_request - Does this message request a XEP-0184 receipt (and is not from us or a carbon or archived message)
-     * @property { Object } encrypted -  XEP-0384 encryption payload attributes
-     * @property { String } body - The contents of the <body> tag of the message stanza
-     * @property { String } chat_state - The XEP-0085 chat state notification contained in this message
-     * @property { String } edited - An ISO8601 string recording the time that the message was edited per XEP-0308
-     * @property { String } error_condition - The defined error condition
-     * @property { String } error_text - The error text received from the server
-     * @property { String } error_type - The type of error received from the server
-     * @property { String } from - The sender JID (${muc_jid}/${nick})
-     * @property { String } from_muc - The JID of the MUC from which this message was sent
-     * @property { String } from_real_jid - The real JID of the sender, if available
-     * @property { String } fullname - The full name of the sender
-     * @property { String } marker - The XEP-0333 Chat Marker value
-     * @property { String } marker_id - The `id` attribute of a XEP-0333 chat marker
-     * @property { String } moderated - The type of XEP-0425 moderation (if any) that was applied
-     * @property { String } moderated_by - The JID of the user that moderated this message
-     * @property { String } moderated_id - The  XEP-0359 Stanza ID of the message that this one moderates
-     * @property { String } moderation_reason - The reason provided why this message moderates another
-     * @property { String } msgid - The root `id` attribute of the stanza
-     * @property { String } nick - The MUC nickname of the sender
-     * @property { String } occupant_id - The XEP-0421 occupant ID
-     * @property { String } oob_desc - The description of the XEP-0066 out of band data
-     * @property { String } oob_url - The URL of the XEP-0066 out of band data
-     * @property { String } origin_id - The XEP-0359 Origin ID
-     * @property { String } receipt_id - The `id` attribute of a XEP-0184 <receipt> element
-     * @property { String } received - An ISO8601 string recording the time that the message was received
-     * @property { String } replace_id - The `id` attribute of a XEP-0308 <replace> element
-     * @property { String } retracted - An ISO8601 string recording the time that the message was retracted
-     * @property { String } retracted_id - The `id` attribute of a XEP-424 <retracted> element
-     * @property { String } spoiler_hint  The XEP-0382 spoiler hint
-     * @property { String } stanza_id - The XEP-0359 Stanza ID. Note: the key is actualy `stanza_id ${by_jid}` and there can be multiple.
-     * @property { String } subject - The <subject> element value
-     * @property { String } thread - The <thread> element value
-     * @property { String } time - The time (in ISO8601 format), either given by the XEP-0203 <delay> element, or of receipt.
-     * @property { String } to - The recipient JID
-     * @property { String } type - The type of message
+     * @property {('me'|'them')} sender - Whether the message was sent by the current user or someone else
+     * @property {Array<Object>} activities - A list of objects representing XEP-0316 MEP notification data
+     * @property {Array<Object>} references - A list of objects representing XEP-0372 references
+     * @property {Boolean} editable - Is this message editable via XEP-0308?
+     * @property {Boolean} is_archived -  Is this message from a XEP-0313 MAM archive?
+     * @property {Boolean} is_carbon - Is this message a XEP-0280 Carbon?
+     * @property {Boolean} is_delayed - Was delivery of this message was delayed as per XEP-0203?
+     * @property {Boolean} is_encrypted -  Is this message XEP-0384  encrypted?
+     * @property {Boolean} is_error - Whether an error was received for this message
+     * @property {Boolean} is_headline - Is this a "headline" message?
+     * @property {Boolean} is_markable - Can this message be marked with a XEP-0333 chat marker?
+     * @property {Boolean} is_marker - Is this message a XEP-0333 Chat Marker?
+     * @property {Boolean} is_only_emojis - Does the message body contain only emojis?
+     * @property {Boolean} is_spoiler - Is this a XEP-0382 spoiler message?
+     * @property {Boolean} is_tombstone - Is this a XEP-0424 tombstone?
+     * @property {Boolean} is_unstyled - Whether XEP-0393 styling hints should be ignored
+     * @property {Boolean} is_valid_receipt_request - Does this message request a XEP-0184 receipt (and is not from us or a carbon or archived message)
+     * @property {Object} encrypted -  XEP-0384 encryption payload attributes
+     * @property {String} body - The contents of the <body> tag of the message stanza
+     * @property {String} chat_state - The XEP-0085 chat state notification contained in this message
+     * @property {String} edited - An ISO8601 string recording the time that the message was edited per XEP-0308
+     * @property {String} error_condition - The defined error condition
+     * @property {String} error_text - The error text received from the server
+     * @property {String} error_type - The type of error received from the server
+     * @property {String} from - The sender JID (${muc_jid}/${nick})
+     * @property {String} from_muc - The JID of the MUC from which this message was sent
+     * @property {String} from_real_jid - The real JID of the sender, if available
+     * @property {String} fullname - The full name of the sender
+     * @property {String} marker - The XEP-0333 Chat Marker value
+     * @property {String} marker_id - The `id` attribute of a XEP-0333 chat marker
+     * @property {String} moderated - The type of XEP-0425 moderation (if any) that was applied
+     * @property {String} moderated_by - The JID of the user that moderated this message
+     * @property {String} moderated_id - The  XEP-0359 Stanza ID of the message that this one moderates
+     * @property {String} moderation_reason - The reason provided why this message moderates another
+     * @property {String} msgid - The root `id` attribute of the stanza
+     * @property {String} nick - The MUC nickname of the sender
+     * @property {String} occupant_id - The XEP-0421 occupant ID
+     * @property {String} oob_desc - The description of the XEP-0066 out of band data
+     * @property {String} oob_url - The URL of the XEP-0066 out of band data
+     * @property {String} origin_id - The XEP-0359 Origin ID
+     * @property {String} receipt_id - The `id` attribute of a XEP-0184 <receipt> element
+     * @property {String} received - An ISO8601 string recording the time that the message was received
+     * @property {String} replace_id - The `id` attribute of a XEP-0308 <replace> element
+     * @property {String} retracted - An ISO8601 string recording the time that the message was retracted
+     * @property {String} retracted_id - The `id` attribute of a XEP-424 <retracted> element
+     * @property {String} spoiler_hint  The XEP-0382 spoiler hint
+     * @property {String} stanza_id - The XEP-0359 Stanza ID. Note: the key is actualy `stanza_id ${by_jid}` and there can be multiple.
+     * @property {String} subject - The <subject> element value
+     * @property {String} thread - The <thread> element value
+     * @property {String} time - The time (in ISO8601 format), either given by the XEP-0203 <delay> element, or of receipt.
+     * @property {String} to - The recipient JID
+     * @property {String} type - The type of message
      */
     let attrs = Object.assign(
         {
@@ -262,13 +262,10 @@ export async function parseMUCMessage (stanza, chatbox) {
         getEncryptionAttributes(stanza),
     );
 
-    await api.emojis.initialize();
-
     attrs.from_real_jid = attrs.is_archived && getJIDFromMUCUserData(stanza) ||
         chatbox.occupants.findOccupant(attrs)?.get('jid');
 
     attrs = Object.assign({
-        'is_only_emojis': attrs.body ? u.isOnlyEmojis(attrs.body) : false,
         'is_valid_receipt_request': isValidReceiptRequest(stanza, attrs),
         'message': attrs.body || attrs.error, // TODO: Should only be used for error and info messages
         'sender': getSender(attrs, chatbox),

+ 13 - 0
src/headless/types/plugins/emoji/api.d.ts

@@ -0,0 +1,13 @@
+export default emojis_api;
+declare namespace emojis_api {
+    export { emojis };
+}
+declare namespace emojis {
+    /**
+     * Initializes Emoji support by downloading the emojis JSON (and any applicable images).
+     * @method api.emojis.initialize
+     * @returns {Promise}
+     */
+    function initialize(): Promise<any>;
+}
+//# sourceMappingURL=api.d.ts.map

+ 8 - 0
src/headless/types/plugins/emoji/utils.d.ts

@@ -16,4 +16,12 @@ export function getShortnameReferences(text: string): {
  * @param {string} text
  */
 export function getCodePointReferences(text: string): any[];
+/**
+ * Determines whether the passed in string is just a single emoji shortname;
+ * @namespace u
+ * @method u.isOnlyEmojis
+ * @param { String } text - A string which migh be just an emoji shortname
+ * @returns { Boolean }
+ */
+export function isOnlyEmojis(text: string): boolean;
 //# sourceMappingURL=utils.d.ts.map

+ 5 - 0
src/headless/types/plugins/muc/muc.d.ts

@@ -231,6 +231,11 @@ declare class MUC extends ChatBox {
      * @param { String } text
      */
     parseTextForReferences(text: string): any[];
+    /**
+     * @param {Object} [attrs] - A map of attributes to be saved on the message
+     * @returns {Promise<MUCMessage>}
+     */
+    getOutgoingMessageAttributes(attrs?: any): Promise<MUCMessage>;
     /**
      * Utility method to construct the JID for the current user as occupant of the groupchat.
      * @private