presence.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { _converse, api } from '../../core.js';
  2. export default {
  3. /**
  4. * @namespace _converse.api.user.presence
  5. * @memberOf _converse.api.user
  6. */
  7. presence: {
  8. /**
  9. * Send out a presence stanza
  10. * @method _converse.api.user.presence.send
  11. * @param { String } type
  12. * @param { String } to
  13. * @param { String } [status] - An optional status message
  14. * @param { Array<Element>|Array<Strophe.Builder>|Element|Strophe.Builder } [child_nodes]
  15. * Nodes(s) to be added as child nodes of the `presence` XML element.
  16. */
  17. async send (type, to, status, child_nodes) {
  18. await api.waitUntil('statusInitialized');
  19. if (child_nodes && !Array.isArray(child_nodes)) {
  20. child_nodes = [child_nodes];
  21. }
  22. const model = _converse.xmppstatus
  23. const presence = await model.constructPresence(type, to, status);
  24. child_nodes?.map(c => c?.tree() ?? c).forEach(c => presence.cnode(c).up());
  25. api.send(presence);
  26. if (['away', 'chat', 'dnd', 'online', 'xa', undefined].includes(type)) {
  27. const mucs = await api.rooms.get();
  28. mucs.forEach(muc => muc.sendStatusPresence(type, status, child_nodes));
  29. }
  30. }
  31. }
  32. }