Browse Source

Use the stx tagged template literal

JC Brand 3 months ago
parent
commit
df69f90bcd

+ 49 - 39
src/headless/shared/actions.js

@@ -1,9 +1,10 @@
 import log from "@converse/log";
-import { Strophe, $msg } from 'strophe.js';
-import api from './api/index.js';
-import converse from './api/public.js';
+import { Strophe } from "strophe.js";
+import api from "./api/index.js";
+import converse from "./api/public.js";
+import {CHAT_STATES, MARKER_TYPES} from "./constants.js";
 
-const { u, stx } = converse.env;
+const { u, stx, Stanza } = converse.env;
 
 /**
  * Reject an incoming message by replying with an error message of type "cancel".
@@ -13,14 +14,15 @@ const { u, stx } = converse.env;
  */
 export function rejectMessage(stanza, text) {
     api.send(
-        $msg({
-            'to': stanza.getAttribute('from'),
-            'type': 'error',
-            'id': stanza.getAttribute('id'),
-        })
-            .c('error', { 'type': 'cancel' })
-            .c('not-allowed', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' }).up()
-            .c('text', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' }).t(text)
+        stx`<message to="${stanza.getAttribute("from")}"
+                    type="error"
+                    id="${stanza.getAttribute("id")}"
+                    xmlns="jabber:client">
+                <error type="cancel">
+                    <not-allowed xmlns="${Strophe.NS.STANZAS}"/>
+                    <text xmlns="${Strophe.NS.STANZAS}">${text}</text>
+                </error>
+            </message>`
     );
     log.warn(`Rejecting message stanza with the following reason: ${text}`);
     log.warn(stanza);
@@ -30,17 +32,23 @@ export function rejectMessage(stanza, text) {
  * Send out a XEP-0333 chat marker
  * @param {string} to_jid
  * @param {string} id - The id of the message being marked
- * @param {string} type - The marker type
+ * @param {import("./types").MessageMarkerType} type - The marker type
  * @param {string} [msg_type]
  * @return void
  */
 export function sendMarker(to_jid, id, type, msg_type) {
-    const stanza = $msg({
-        'from': api.connection.get().jid,
-        'id': u.getUniqueId(),
-        'to': to_jid,
-        'type': msg_type ? msg_type : 'chat',
-    }).c(type, { 'xmlns': Strophe.NS.MARKERS, 'id': id });
+    if (!MARKER_TYPES.includes(type)) {
+        log.error(`Invalid marker type: ${type}`);
+        return;
+    }
+    const stanza = stx`
+        <message from="${api.connection.get().jid}"
+                id="${u.getUniqueId()}"
+                to="${to_jid}"
+                type="${msg_type ? msg_type : "chat"}"
+                xmlns="jabber:client">
+            <${Stanza.unsafeXML(type)} xmlns="${Strophe.NS.MARKERS}" id="${id}"/>
+        </message>`;
     api.send(stanza);
 }
 
@@ -50,37 +58,39 @@ export function sendMarker(to_jid, id, type, msg_type) {
  * @return void
  */
 export function sendReceiptStanza(to_jid, id) {
-    const receipt_stanza = $msg({
-        'from': api.connection.get().jid,
-        'id': u.getUniqueId(),
-        'to': to_jid,
-        'type': 'chat',
-    })
-        .c('received', { 'xmlns': Strophe.NS.RECEIPTS, 'id': id }).up()
-        .c('store', { 'xmlns': Strophe.NS.HINTS }).up();
+    const receipt_stanza = stx`
+        <message from="${api.connection.get().jid}"
+                id="${u.getUniqueId()}"
+                to="${to_jid}"
+                type="chat"
+                xmlns="jabber:client">
+            <received xmlns="${Strophe.NS.RECEIPTS}" id="${id}"/>
+            <store xmlns="${Strophe.NS.HINTS}"/>
+        </message>`;
     api.send(receipt_stanza);
 }
 
 /**
  * Sends a message with the given XEP-0085 chat state.
  * @param {string} jid
- * @param {string} chat_state
+ * @param {import("./types").ChatStateType} chat_state
  */
 export function sendChatState(jid, chat_state) {
-    if (api.settings.get('send_chat_state_notifications') && chat_state) {
-        const allowed = api.settings.get('send_chat_state_notifications');
+    if (api.settings.get("send_chat_state_notifications") && chat_state) {
+        const allowed = api.settings.get("send_chat_state_notifications");
         if (Array.isArray(allowed) && !allowed.includes(chat_state)) {
             return;
         }
+        if (!CHAT_STATES.includes(chat_state)) {
+            log.error(`Invalid chat state: ${chat_state}`);
+            return;
+        }
         api.send(
-            $msg({
-                'id': u.getUniqueId(),
-                'to': jid,
-                'type': 'chat',
-            })
-                .c(chat_state, { 'xmlns': Strophe.NS.CHATSTATES }).up()
-                .c('no-store', { 'xmlns': Strophe.NS.HINTS }).up()
-                .c('no-permanent-store', { 'xmlns': Strophe.NS.HINTS })
+            stx`<message id="${u.getUniqueId()}" to="${jid}" type="chat" xmlns="jabber:client">
+                <${Stanza.unsafeXML(chat_state)} xmlns="${Strophe.NS.CHATSTATES}"/>
+                <no-store xmlns="${Strophe.NS.HINTS}"/>
+                <no-permanent-store xmlns="${Strophe.NS.HINTS}"/>
+            </message>`
         );
     }
 }
@@ -92,7 +102,7 @@ export function sendChatState(jid, chat_state) {
  * @param {string} retraction_id - Unique ID for the retraction message
  */
 export function sendRetractionMessage(jid, message, retraction_id) {
-    const origin_id = message.get('origin_id');
+    const origin_id = message.get("origin_id");
     if (!origin_id) {
         throw new Error("Can't retract message without a XEP-0359 Origin ID");
     }

+ 1 - 0
src/headless/shared/constants.js

@@ -134,6 +134,7 @@ export const CORE_PLUGINS = [
 ];
 
 export const CHAT_STATES = ['active', 'composing', 'gone', 'inactive', 'paused'];
+export const MARKER_TYPES = ['displayed', 'received', 'acknowledged'];
 
 export const KEYCODES = {
     TAB: "Tab",

+ 3 - 0
src/headless/shared/types.ts

@@ -216,3 +216,6 @@ export type FileUploadMessageAttributes = {
     oob_url: string;
     upload: 'success' | 'failure';
 };
+
+export type MessageMarkerType = "displayed" | "received" | "acknowledged";
+export type ChatStateType = "active" | "composing" | "paused" | "inactive" | "gone";

+ 4 - 4
src/headless/types/shared/actions.d.ts

@@ -9,11 +9,11 @@ export function rejectMessage(stanza: Element, text: string): void;
  * Send out a XEP-0333 chat marker
  * @param {string} to_jid
  * @param {string} id - The id of the message being marked
- * @param {string} type - The marker type
+ * @param {import("./types").MessageMarkerType} type - The marker type
  * @param {string} [msg_type]
  * @return void
  */
-export function sendMarker(to_jid: string, id: string, type: string, msg_type?: string): void;
+export function sendMarker(to_jid: string, id: string, type: import("./types").MessageMarkerType, msg_type?: string): void;
 /**
  * @param {string} to_jid
  * @param {string} id
@@ -23,9 +23,9 @@ export function sendReceiptStanza(to_jid: string, id: string): void;
 /**
  * Sends a message with the given XEP-0085 chat state.
  * @param {string} jid
- * @param {string} chat_state
+ * @param {import("./types").ChatStateType} chat_state
  */
-export function sendChatState(jid: string, chat_state: string): void;
+export function sendChatState(jid: string, chat_state: import("./types").ChatStateType): void;
 /**
  * Sends a message stanza to retract a message in this chat
  * @param {string} jid

+ 3 - 6
src/headless/types/shared/api/send.d.ts

@@ -7,12 +7,9 @@ declare namespace _default {
      * @param {Element|Builder} stanza
      * @returns {void}
      * @example
-     * const msg = converse.env.$msg({
-     *     'from': 'juliet@example.com/balcony',
-     *     'to': 'romeo@example.net',
-     *     'type':'chat'
-     * });
-     * _converse.api.send(msg);
+     *     const { stx } = _converse.env;
+     *     const msg = stx`<message from="juliet@example.com/balcony" to="romeo@example.net" type="chat"/>`;
+     *     _converse.api.send(msg);
      */
     function send(stanza: Element | import("strophe.js").Builder): void;
     /**

+ 1 - 0
src/headless/types/shared/constants.d.ts

@@ -31,6 +31,7 @@ export const CONTROLBOX_TYPE: "controlbox";
 export const CONNECTION_STATUS: typeof CONNECTION_STATUS;
 export const CORE_PLUGINS: string[];
 export const CHAT_STATES: string[];
+export const MARKER_TYPES: string[];
 export namespace KEYCODES {
     let TAB: string;
     let ENTER: string;

+ 2 - 0
src/headless/types/shared/types.d.ts

@@ -156,5 +156,7 @@ export type FileUploadMessageAttributes = {
     oob_url: string;
     upload: 'success' | 'failure';
 };
+export type MessageMarkerType = "displayed" | "received" | "acknowledged";
+export type ChatStateType = "active" | "composing" | "paused" | "inactive" | "gone";
 export {};
 //# sourceMappingURL=types.d.ts.map