actions.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import log from '../log';
  2. import { Strophe, $msg } from 'strophe.js';
  3. import api from './api/index.js';
  4. import converse from './api/public.js';
  5. const { u, stx } = converse.env;
  6. /**
  7. * Reject an incoming message by replying with an error message of type "cancel".
  8. * @param {Element} stanza
  9. * @param {string} text
  10. * @return void
  11. */
  12. export function rejectMessage(stanza, text) {
  13. api.send(
  14. $msg({
  15. 'to': stanza.getAttribute('from'),
  16. 'type': 'error',
  17. 'id': stanza.getAttribute('id'),
  18. })
  19. .c('error', { 'type': 'cancel' })
  20. .c('not-allowed', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' }).up()
  21. .c('text', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' }).t(text)
  22. );
  23. log.warn(`Rejecting message stanza with the following reason: ${text}`);
  24. log.warn(stanza);
  25. }
  26. /**
  27. * Send out a XEP-0333 chat marker
  28. * @param {string} to_jid
  29. * @param {string} id - The id of the message being marked
  30. * @param {string} type - The marker type
  31. * @param {string} [msg_type]
  32. * @return void
  33. */
  34. export function sendMarker(to_jid, id, type, msg_type) {
  35. const stanza = $msg({
  36. 'from': api.connection.get().jid,
  37. 'id': u.getUniqueId(),
  38. 'to': to_jid,
  39. 'type': msg_type ? msg_type : 'chat',
  40. }).c(type, { 'xmlns': Strophe.NS.MARKERS, 'id': id });
  41. api.send(stanza);
  42. }
  43. /**
  44. * @param {string} to_jid
  45. * @param {string} id
  46. * @return void
  47. */
  48. export function sendReceiptStanza(to_jid, id) {
  49. const receipt_stanza = $msg({
  50. 'from': api.connection.get().jid,
  51. 'id': u.getUniqueId(),
  52. 'to': to_jid,
  53. 'type': 'chat',
  54. })
  55. .c('received', { 'xmlns': Strophe.NS.RECEIPTS, 'id': id }).up()
  56. .c('store', { 'xmlns': Strophe.NS.HINTS }).up();
  57. api.send(receipt_stanza);
  58. }
  59. /**
  60. * Sends a message with the given XEP-0085 chat state.
  61. * @param {string} jid
  62. * @param {string} chat_state
  63. */
  64. export function sendChatState(jid, chat_state) {
  65. if (api.settings.get('send_chat_state_notifications') && chat_state) {
  66. const allowed = api.settings.get('send_chat_state_notifications');
  67. if (Array.isArray(allowed) && !allowed.includes(chat_state)) {
  68. return;
  69. }
  70. api.send(
  71. $msg({
  72. 'id': u.getUniqueId(),
  73. 'to': jid,
  74. 'type': 'chat',
  75. })
  76. .c(chat_state, { 'xmlns': Strophe.NS.CHATSTATES }).up()
  77. .c('no-store', { 'xmlns': Strophe.NS.HINTS }).up()
  78. .c('no-permanent-store', { 'xmlns': Strophe.NS.HINTS })
  79. );
  80. }
  81. }
  82. /**
  83. * Sends a message stanza to retract a message in this chat
  84. * @param {string} jid
  85. * @param {import('../shared/message').default} message - The message which we're retracting.
  86. * @param {string} retraction_id - Unique ID for the retraction message
  87. */
  88. export function sendRetractionMessage(jid, message, retraction_id) {
  89. const origin_id = message.get('origin_id');
  90. if (!origin_id) {
  91. throw new Error("Can't retract message without a XEP-0359 Origin ID");
  92. }
  93. const stanza = stx`
  94. <message id="${retraction_id}"
  95. to="${jid}"
  96. type="chat"
  97. xmlns="jabber:client">
  98. <retract id="${origin_id}" xmlns="${Strophe.NS.RETRACT}"/>
  99. <body>/me retracted a message</body>
  100. <store xmlns="${Strophe.NS.HINTS}"/>
  101. <fallback xmlns="${Strophe.NS.FALLBACK}" for="${Strophe.NS.RETRACT}" />
  102. </message>`;
  103. return api.connection.get().send(stanza);
  104. }