2
0

utils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { _converse, api, converse } from "@converse/headless/core";
  2. import log from "@converse/headless/log";
  3. const { Strophe, $iq, sizzle, u } = converse.env;
  4. export function getAutoCompleteListItem (text, input) {
  5. input = input.trim();
  6. const element = document.createElement('li');
  7. element.setAttribute('aria-selected', 'false');
  8. if (api.settings.get('muc_mention_autocomplete_show_avatar')) {
  9. const img = document.createElement('img');
  10. let dataUri = 'data:' + _converse.DEFAULT_IMAGE_TYPE + ';base64,' + _converse.DEFAULT_IMAGE;
  11. if (_converse.vcards) {
  12. const vcard = _converse.vcards.findWhere({ 'nickname': text });
  13. if (vcard) dataUri = 'data:' + vcard.get('image_type') + ';base64,' + vcard.get('image');
  14. }
  15. img.setAttribute('src', dataUri);
  16. img.setAttribute('width', '22');
  17. img.setAttribute('class', 'avatar avatar-autocomplete');
  18. element.appendChild(img);
  19. }
  20. const regex = new RegExp('(' + input + ')', 'ig');
  21. const parts = input ? text.split(regex) : [text];
  22. parts.forEach(txt => {
  23. if (input && txt.match(regex)) {
  24. const match = document.createElement('mark');
  25. match.textContent = txt;
  26. element.appendChild(match);
  27. } else {
  28. element.appendChild(document.createTextNode(txt));
  29. }
  30. });
  31. return element;
  32. }
  33. export async function getAutoCompleteList () {
  34. const models = [...(await api.rooms.get()), ...(await api.contacts.get())];
  35. const jids = [...new Set(models.map(o => Strophe.getDomainFromJid(o.get('jid'))))];
  36. return jids;
  37. }
  38. export async function fetchCommandForm (command) {
  39. const node = command.node;
  40. const jid = command.jid;
  41. const stanza = $iq({
  42. 'type': 'set',
  43. 'to': jid
  44. }).c('command', {
  45. 'xmlns': Strophe.NS.ADHOC,
  46. 'node': node,
  47. 'action': 'execute'
  48. });
  49. try {
  50. const iq = await api.sendIQ(stanza);
  51. const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
  52. command.sessionid = cmd_el.getAttribute('sessionid');
  53. command.instructions = sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent;
  54. command.fields = sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
  55. .map(f => u.xForm2TemplateResult(f, cmd_el));
  56. } catch (e) {
  57. if (e === null) {
  58. log.error(`Error: timeout while trying to execute command for ${jid}`);
  59. } else {
  60. log.error(`Error while trying to execute command for ${jid}`);
  61. log.error(e);
  62. }
  63. command.fields = [];
  64. }
  65. }