import BootstrapModal from "./base.js"; import { __ } from '../i18n'; import { api, converse } from "@converse/headless/core"; import log from "@converse/headless/log"; import tpl_muc_commands_modal from "../templates/muc_commands-modal.js"; const { Strophe, $iq, sizzle } = converse.env; const u = converse.env.utils; export default BootstrapModal.extend({ id: "muc-commands-modal", initialize () { this.commands = []; BootstrapModal.prototype.initialize.apply(this, arguments); this.listenTo(this.model, 'change', this.render); this.getCommands(); }, toHTML () { return tpl_muc_commands_modal(Object.assign( this.model.toJSON(), { 'commands': this.commands, 'display_name': __('Ad-hoc commands for %1$s', this.model.getDisplayName()), 'toggleCommandForm': ev => this.toggleCommandForm(ev) }) ); }, async getCommands () { this.commands = await api.adhoc.getCommands(Strophe.getDomainFromJid(this.model.get('jid'))); this.render(); }, async toggleCommandForm (ev) { ev.preventDefault(); const node = ev.target.getAttribute('data-command-node'); this.commands.filter(c => (c.node !== node)).forEach(c => (c.show_form = false)); const cmd = this.commands.filter(c => c.node === node)[0]; cmd.show_form = !cmd.show_form; cmd.show_form && await this.fetchCommandForm(cmd); this.render(); }, async fetchCommandForm (command) { const node = command.node; const jid = command.jid; const stanza = $iq({ 'type': 'set', 'to': jid }).c('command', { 'xmlns': Strophe.NS.ADHOC, 'node': node, 'action': 'execute' }); command.fields; try { const iq = await api.sendIQ(stanza); command.fields = sizzle('field', iq).map(f => u.xForm2TemplateResult(f, iq)) } catch (e) { if (e === null) { log.error(`Error: timeout while trying to execute command for ${jid}`); } else { log.error(`Error while trying to execute command for ${jid}`); log.error(e); } command.fields = []; } /* Add a hat Assign a hat to a room member */ } });