Skip to content

Commit df69f90

Browse files
committed
Use the stx tagged template literal
1 parent 336df63 commit df69f90

File tree

7 files changed

+63
-49
lines changed

7 files changed

+63
-49
lines changed

src/headless/shared/actions.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import log from "@converse/log";
2-
import { Strophe, $msg } from 'strophe.js';
3-
import api from './api/index.js';
4-
import converse from './api/public.js';
2+
import { Strophe } from "strophe.js";
3+
import api from "./api/index.js";
4+
import converse from "./api/public.js";
5+
import {CHAT_STATES, MARKER_TYPES} from "./constants.js";
56

6-
const { u, stx } = converse.env;
7+
const { u, stx, Stanza } = converse.env;
78

89
/**
910
* Reject an incoming message by replying with an error message of type "cancel".
@@ -13,14 +14,15 @@ const { u, stx } = converse.env;
1314
*/
1415
export function rejectMessage(stanza, text) {
1516
api.send(
16-
$msg({
17-
'to': stanza.getAttribute('from'),
18-
'type': 'error',
19-
'id': stanza.getAttribute('id'),
20-
})
21-
.c('error', { 'type': 'cancel' })
22-
.c('not-allowed', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' }).up()
23-
.c('text', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' }).t(text)
17+
stx`<message to="${stanza.getAttribute("from")}"
18+
type="error"
19+
id="${stanza.getAttribute("id")}"
20+
xmlns="jabber:client">
21+
<error type="cancel">
22+
<not-allowed xmlns="${Strophe.NS.STANZAS}"/>
23+
<text xmlns="${Strophe.NS.STANZAS}">${text}</text>
24+
</error>
25+
</message>`
2426
);
2527
log.warn(`Rejecting message stanza with the following reason: ${text}`);
2628
log.warn(stanza);
@@ -30,17 +32,23 @@ export function rejectMessage(stanza, text) {
3032
* Send out a XEP-0333 chat marker
3133
* @param {string} to_jid
3234
* @param {string} id - The id of the message being marked
33-
* @param {string} type - The marker type
35+
* @param {import("./types").MessageMarkerType} type - The marker type
3436
* @param {string} [msg_type]
3537
* @return void
3638
*/
3739
export function sendMarker(to_jid, id, type, msg_type) {
38-
const stanza = $msg({
39-
'from': api.connection.get().jid,
40-
'id': u.getUniqueId(),
41-
'to': to_jid,
42-
'type': msg_type ? msg_type : 'chat',
43-
}).c(type, { 'xmlns': Strophe.NS.MARKERS, 'id': id });
40+
if (!MARKER_TYPES.includes(type)) {
41+
log.error(`Invalid marker type: ${type}`);
42+
return;
43+
}
44+
const stanza = stx`
45+
<message from="${api.connection.get().jid}"
46+
id="${u.getUniqueId()}"
47+
to="${to_jid}"
48+
type="${msg_type ? msg_type : "chat"}"
49+
xmlns="jabber:client">
50+
<${Stanza.unsafeXML(type)} xmlns="${Strophe.NS.MARKERS}" id="${id}"/>
51+
</message>`;
4452
api.send(stanza);
4553
}
4654

@@ -50,37 +58,39 @@ export function sendMarker(to_jid, id, type, msg_type) {
5058
* @return void
5159
*/
5260
export function sendReceiptStanza(to_jid, id) {
53-
const receipt_stanza = $msg({
54-
'from': api.connection.get().jid,
55-
'id': u.getUniqueId(),
56-
'to': to_jid,
57-
'type': 'chat',
58-
})
59-
.c('received', { 'xmlns': Strophe.NS.RECEIPTS, 'id': id }).up()
60-
.c('store', { 'xmlns': Strophe.NS.HINTS }).up();
61+
const receipt_stanza = stx`
62+
<message from="${api.connection.get().jid}"
63+
id="${u.getUniqueId()}"
64+
to="${to_jid}"
65+
type="chat"
66+
xmlns="jabber:client">
67+
<received xmlns="${Strophe.NS.RECEIPTS}" id="${id}"/>
68+
<store xmlns="${Strophe.NS.HINTS}"/>
69+
</message>`;
6170
api.send(receipt_stanza);
6271
}
6372

6473
/**
6574
* Sends a message with the given XEP-0085 chat state.
6675
* @param {string} jid
67-
* @param {string} chat_state
76+
* @param {import("./types").ChatStateType} chat_state
6877
*/
6978
export function sendChatState(jid, chat_state) {
70-
if (api.settings.get('send_chat_state_notifications') && chat_state) {
71-
const allowed = api.settings.get('send_chat_state_notifications');
79+
if (api.settings.get("send_chat_state_notifications") && chat_state) {
80+
const allowed = api.settings.get("send_chat_state_notifications");
7281
if (Array.isArray(allowed) && !allowed.includes(chat_state)) {
7382
return;
7483
}
84+
if (!CHAT_STATES.includes(chat_state)) {
85+
log.error(`Invalid chat state: ${chat_state}`);
86+
return;
87+
}
7588
api.send(
76-
$msg({
77-
'id': u.getUniqueId(),
78-
'to': jid,
79-
'type': 'chat',
80-
})
81-
.c(chat_state, { 'xmlns': Strophe.NS.CHATSTATES }).up()
82-
.c('no-store', { 'xmlns': Strophe.NS.HINTS }).up()
83-
.c('no-permanent-store', { 'xmlns': Strophe.NS.HINTS })
89+
stx`<message id="${u.getUniqueId()}" to="${jid}" type="chat" xmlns="jabber:client">
90+
<${Stanza.unsafeXML(chat_state)} xmlns="${Strophe.NS.CHATSTATES}"/>
91+
<no-store xmlns="${Strophe.NS.HINTS}"/>
92+
<no-permanent-store xmlns="${Strophe.NS.HINTS}"/>
93+
</message>`
8494
);
8595
}
8696
}
@@ -92,7 +102,7 @@ export function sendChatState(jid, chat_state) {
92102
* @param {string} retraction_id - Unique ID for the retraction message
93103
*/
94104
export function sendRetractionMessage(jid, message, retraction_id) {
95-
const origin_id = message.get('origin_id');
105+
const origin_id = message.get("origin_id");
96106
if (!origin_id) {
97107
throw new Error("Can't retract message without a XEP-0359 Origin ID");
98108
}

src/headless/shared/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export const CORE_PLUGINS = [
134134
];
135135

136136
export const CHAT_STATES = ['active', 'composing', 'gone', 'inactive', 'paused'];
137+
export const MARKER_TYPES = ['displayed', 'received', 'acknowledged'];
137138

138139
export const KEYCODES = {
139140
TAB: "Tab",

src/headless/shared/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,6 @@ export type FileUploadMessageAttributes = {
216216
oob_url: string;
217217
upload: 'success' | 'failure';
218218
};
219+
220+
export type MessageMarkerType = "displayed" | "received" | "acknowledged";
221+
export type ChatStateType = "active" | "composing" | "paused" | "inactive" | "gone";

src/headless/types/shared/actions.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export function rejectMessage(stanza: Element, text: string): void;
99
* Send out a XEP-0333 chat marker
1010
* @param {string} to_jid
1111
* @param {string} id - The id of the message being marked
12-
* @param {string} type - The marker type
12+
* @param {import("./types").MessageMarkerType} type - The marker type
1313
* @param {string} [msg_type]
1414
* @return void
1515
*/
16-
export function sendMarker(to_jid: string, id: string, type: string, msg_type?: string): void;
16+
export function sendMarker(to_jid: string, id: string, type: import("./types").MessageMarkerType, msg_type?: string): void;
1717
/**
1818
* @param {string} to_jid
1919
* @param {string} id
@@ -23,9 +23,9 @@ export function sendReceiptStanza(to_jid: string, id: string): void;
2323
/**
2424
* Sends a message with the given XEP-0085 chat state.
2525
* @param {string} jid
26-
* @param {string} chat_state
26+
* @param {import("./types").ChatStateType} chat_state
2727
*/
28-
export function sendChatState(jid: string, chat_state: string): void;
28+
export function sendChatState(jid: string, chat_state: import("./types").ChatStateType): void;
2929
/**
3030
* Sends a message stanza to retract a message in this chat
3131
* @param {string} jid

src/headless/types/shared/api/send.d.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ declare namespace _default {
77
* @param {Element|Builder} stanza
88
* @returns {void}
99
* @example
10-
* const msg = converse.env.$msg({
11-
* 'from': 'juliet@example.com/balcony',
12-
* 'to': 'romeo@example.net',
13-
* 'type':'chat'
14-
* });
15-
* _converse.api.send(msg);
10+
* const { stx } = _converse.env;
11+
* const msg = stx`<message from="juliet@example.com/balcony" to="romeo@example.net" type="chat"/>`;
12+
* _converse.api.send(msg);
1613
*/
1714
function send(stanza: Element | import("strophe.js").Builder): void;
1815
/**

src/headless/types/shared/constants.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const CONTROLBOX_TYPE: "controlbox";
3131
export const CONNECTION_STATUS: typeof CONNECTION_STATUS;
3232
export const CORE_PLUGINS: string[];
3333
export const CHAT_STATES: string[];
34+
export const MARKER_TYPES: string[];
3435
export namespace KEYCODES {
3536
let TAB: string;
3637
let ENTER: string;

src/headless/types/shared/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,7 @@ export type FileUploadMessageAttributes = {
156156
oob_url: string;
157157
upload: 'success' | 'failure';
158158
};
159+
export type MessageMarkerType = "displayed" | "received" | "acknowledged";
160+
export type ChatStateType = "active" | "composing" | "paused" | "inactive" | "gone";
159161
export {};
160162
//# sourceMappingURL=types.d.ts.map

0 commit comments

Comments
 (0)