utils.js 826 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * @typedef {import('../chat/model.js').default} ChatBox
  3. */
  4. import _converse from '../../shared/_converse.js';
  5. import log from "../../log";
  6. import { Strophe } from 'strophe.js';
  7. /**
  8. * @param {string} jid
  9. * @param {object} attrs
  10. * @param {new (attrs: object, options: object) => ChatBox} Model
  11. */
  12. export async function createChatBox (jid, attrs, Model) {
  13. jid = Strophe.getBareJidFromJid(jid.toLowerCase());
  14. Object.assign(attrs, {'jid': jid, 'id': jid});
  15. let chatbox;
  16. try {
  17. chatbox = new Model(attrs, {'collection': _converse.state.chatboxes});
  18. } catch (e) {
  19. log.error(e);
  20. return null;
  21. }
  22. await chatbox.initialized;
  23. if (!chatbox.isValid()) {
  24. chatbox.destroy();
  25. return null;
  26. }
  27. _converse.state.chatboxes.add(chatbox);
  28. return chatbox;
  29. }