muc-commands.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import BootstrapModal from "./base.js";
  2. import { __ } from '../i18n';
  3. import { api, converse } from "@converse/headless/core";
  4. import log from "@converse/headless/log";
  5. import tpl_muc_commands_modal from "../templates/muc_commands_modal.js";
  6. const { Strophe, $iq, sizzle } = converse.env;
  7. const u = converse.env.utils;
  8. export default BootstrapModal.extend({
  9. id: "muc-commands-modal",
  10. initialize () {
  11. this.commands = [];
  12. BootstrapModal.prototype.initialize.apply(this, arguments);
  13. this.listenTo(this.model, 'change', this.render);
  14. this.getCommands();
  15. },
  16. toHTML () {
  17. return tpl_muc_commands_modal(Object.assign(
  18. this.model.toJSON(), {
  19. 'commands': this.commands,
  20. 'display_name': __('Ad-hoc commands for %1$s', this.model.getDisplayName()),
  21. 'toggleCommandForm': ev => this.toggleCommandForm(ev)
  22. })
  23. );
  24. },
  25. async getCommands () {
  26. this.commands = await api.adhoc.getCommands(Strophe.getDomainFromJid(this.model.get('jid')));
  27. this.render();
  28. },
  29. async toggleCommandForm (ev) {
  30. ev.preventDefault();
  31. const node = ev.target.getAttribute('data-command-node');
  32. this.commands.filter(c => (c.node !== node)).forEach(c => (c.show_form = false));
  33. const cmd = this.commands.filter(c => c.node === node)[0];
  34. cmd.show_form = !cmd.show_form;
  35. cmd.show_form && await this.fetchCommandForm(cmd);
  36. this.render();
  37. },
  38. async 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. command.fields;
  50. try {
  51. const iq = await api.sendIQ(stanza);
  52. command.fields = sizzle('field', iq).map(f => u.xForm2TemplateResult(f, iq))
  53. } catch (e) {
  54. if (e === null) {
  55. log.error(`Error: timeout while trying to execute command for ${jid}`);
  56. } else {
  57. log.error(`Error while trying to execute command for ${jid}`);
  58. log.error(e);
  59. }
  60. command.fields = [];
  61. }
  62. /*
  63. <iq xmlns="jabber:client" id="72c21b57-5e9f-4b63-9e53-c6e69ed3337e:sendIQ" type="result" from="conference.chat.example.org" to="arzu.horsten@chat.example.org/converse.js-138545405">
  64. <command xmlns="http://jabber.org/protocol/commands" node="http://prosody.im/protocol/hats#add" sessionid="141a571b-37e2-4891-824f-72ca4b64806f" status="executing">
  65. <x xmlns="jabber:x:data" type="form">
  66. <title>Add a hat</title>
  67. <instructions>Assign a hat to a room member</instructions>
  68. <field label="User JID" type="jid-single" var="user"><required/></field>
  69. <field label="Room JID" type="jid-single" var="room"><required/></field>
  70. <field label="Hat title" type="text-single" var="title"/>
  71. <field label="Hat URI" type="text-single" var="uri"><required/></field>
  72. </x>
  73. <actions execute="complete"><next/><complete/></actions>
  74. </command>
  75. </iq>
  76. */
  77. }
  78. });