2020-12-10 11:08:17 +01:00
|
|
|
import 'shared/autocomplete/index.js';
|
2020-05-15 14:33:31 +02:00
|
|
|
import log from "@converse/headless/log";
|
2021-02-16 13:51:59 +01:00
|
|
|
import tpl_adhoc from './templates/ad-hoc.js';
|
2021-03-24 11:59:09 +01:00
|
|
|
import { CustomElement } from 'shared/components/element.js';
|
2021-02-16 13:43:13 +01:00
|
|
|
import { __ } from 'i18n';
|
2020-12-03 13:40:30 +01:00
|
|
|
import { api, converse } from "@converse/headless/core";
|
2021-02-16 13:51:59 +01:00
|
|
|
import { fetchCommandForm } from './utils.js';
|
2020-04-15 13:59:55 +02:00
|
|
|
|
2021-02-16 13:51:59 +01:00
|
|
|
const { Strophe, $iq, sizzle, u } = converse.env;
|
2020-04-15 13:59:55 +02:00
|
|
|
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
export default class AdHocCommands extends CustomElement {
|
2020-04-15 13:59:55 +02:00
|
|
|
|
|
|
|
static get properties () {
|
|
|
|
return {
|
2020-05-14 16:12:00 +02:00
|
|
|
'alert': { type: String },
|
|
|
|
'alert_type': { type: String },
|
|
|
|
'nonce': { type: String }, // Used to force re-rendering
|
2020-04-15 13:59:55 +02:00
|
|
|
'showform': { type: String },
|
2020-05-14 16:12:00 +02:00
|
|
|
'view': { type: String },
|
2020-04-15 13:59:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.view = 'choose-service';
|
|
|
|
this.showform = '';
|
|
|
|
this.commands = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return tpl_adhoc({
|
2020-05-14 16:12:00 +02:00
|
|
|
'alert': this.alert,
|
|
|
|
'alert_type': this.alert_type,
|
2020-04-15 13:59:55 +02:00
|
|
|
'commands': this.commands,
|
|
|
|
'fetchCommands': ev => this.fetchCommands(ev),
|
|
|
|
'hideCommandForm': ev => this.hideCommandForm(ev),
|
|
|
|
'runCommand': ev => this.runCommand(ev),
|
|
|
|
'showform': this.showform,
|
|
|
|
'toggleCommandForm': ev => this.toggleCommandForm(ev),
|
|
|
|
'view': this.view,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchCommands (ev) {
|
|
|
|
ev.preventDefault();
|
2020-05-14 16:12:00 +02:00
|
|
|
delete this.alert_type;
|
|
|
|
delete this.alert;
|
|
|
|
|
2020-04-15 13:59:55 +02:00
|
|
|
const form_data = new FormData(ev.target);
|
|
|
|
const jid = form_data.get('jid').trim();
|
2020-05-14 16:12:00 +02:00
|
|
|
let supported;
|
|
|
|
try {
|
|
|
|
supported = await api.disco.supports(Strophe.NS.ADHOC, jid)
|
|
|
|
} catch (e) {
|
|
|
|
log.error(e);
|
|
|
|
}
|
|
|
|
if (supported) {
|
|
|
|
try {
|
|
|
|
this.commands = await api.adhoc.getCommands(jid);
|
|
|
|
this.view = 'list-commands';
|
|
|
|
} catch (e) {
|
|
|
|
log.error(e);
|
|
|
|
this.alert_type = 'danger';
|
|
|
|
this.alert = __('Sorry, an error occurred while looking for commands on that entity.');
|
|
|
|
this.commands = [];
|
|
|
|
log.error(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.alert_type = 'danger';
|
|
|
|
this.alert = __("The specified entity doesn't support ad-hoc commands");
|
2020-04-15 13:59:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async toggleCommandForm (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
const node = ev.target.getAttribute('data-command-node');
|
|
|
|
const cmd = this.commands.filter(c => c.node === node)[0];
|
|
|
|
this.showform !== node && await fetchCommandForm(cmd);
|
|
|
|
this.showform = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
hideCommandForm (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.showform = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
async runCommand (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
const form_data = new FormData(ev.target);
|
|
|
|
const jid = form_data.get('command_jid').trim();
|
|
|
|
const node = form_data.get('command_node').trim();
|
|
|
|
|
|
|
|
const cmd = this.commands.filter(c => c.node === node)[0];
|
2021-01-11 15:06:51 +01:00
|
|
|
cmd.alert = null;
|
|
|
|
this.nonce = u.getUniqueId();
|
|
|
|
|
2020-04-15 13:59:55 +02:00
|
|
|
const inputs = sizzle(':input:not([type=button]):not([type=submit])', ev.target);
|
2020-09-25 18:43:37 +02:00
|
|
|
const config_array = inputs
|
2020-04-15 13:59:55 +02:00
|
|
|
.filter(i => !['command_jid', 'command_node'].includes(i.getAttribute('name')))
|
2020-09-25 18:43:37 +02:00
|
|
|
.map(u.webForm2xForm)
|
|
|
|
.filter(n => n);
|
2020-04-15 13:59:55 +02:00
|
|
|
|
|
|
|
const iq = $iq({to: jid, type: "set"})
|
|
|
|
.c("command", {
|
2020-05-31 11:13:19 +02:00
|
|
|
'sessionid': cmd.sessionid,
|
2020-04-15 13:59:55 +02:00
|
|
|
'node': cmd.node,
|
|
|
|
'xmlns': Strophe.NS.ADHOC
|
|
|
|
}).c("x", {xmlns: Strophe.NS.XFORM, type: "submit"});
|
2020-09-25 18:43:37 +02:00
|
|
|
config_array.forEach(node => iq.cnode(node).up());
|
2020-04-15 13:59:55 +02:00
|
|
|
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = await api.sendIQ(iq);
|
|
|
|
} catch (e) {
|
|
|
|
cmd.alert_type = 'danger';
|
|
|
|
cmd.alert = __('Sorry, an error occurred while trying to execute the command. See the developer console for details');
|
|
|
|
log.error('Error while trying to execute an ad-hoc command');
|
|
|
|
log.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
cmd.alert = result.querySelector('note')?.textContent;
|
|
|
|
} else {
|
|
|
|
cmd.alert = 'Done';
|
|
|
|
}
|
|
|
|
cmd.alert_type = 'primary';
|
|
|
|
this.nonce = u.getUniqueId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:45:18 +02:00
|
|
|
api.elements.define('converse-adhoc-commands', AdHocCommands);
|