11import 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 */
1415export 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 */
3739export 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 */
5260export 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 */
6978export 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 */
94104export 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 }
0 commit comments