Skip to content

Commit b93ad09

Browse files
committed
[#391] Many small fixes:
- reverse $.extend in handleError to avoid clobbering {command}. - rename {name} to {room} in room selection menu. - destructure Error object in fromError. - return the promise chain in /admin. - fix the /admin arg parser. - fix the /create promise chain. - return true after showing a server announcement, to keep the handler. - [].each -> [].forEach. - Use instanceof instead of .constructor, which is null/undefined safe.
1 parent 6d541fb commit b93ad09

File tree

6 files changed

+53
-40
lines changed

6 files changed

+53
-40
lines changed

RELEASE-NOTES

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
1.12.0dev - Trixie
2-
==================
1+
2.0dev - Tantabus
2+
=================
33

44
This version is under development.
55

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION=1.12.0dev
1+
VERSION=2.0dev

js/core/chat.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ var Cadence = {
4949
}
5050

5151
static fromError(error) {
52-
if (error instanceof Error) return new Cadence.Error(strings.error.javascript, error);
52+
if (error instanceof Error) {
53+
const {name, message, stack} = error;
54+
return new Cadence.Error(strings.error.javascript, {name, message, stack});
55+
}
5356
// Catch whatever crazy stuff has been thrown at us.
5457
return new Cadence.Error(strings.error.unknown, {error: JSON.stringify(error)});
5558
}
@@ -104,7 +107,7 @@ var Cadence = {
104107
if (!(error instanceof Cadence.Error)) {
105108
error = Cadence.Error.fromError(error);
106109
}
107-
error.data = $.extend(error.data, {command});
110+
error.data = $.extend({command}, error.data);
108111
error.output();
109112
},
110113

js/core/commands.js

+36-28
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ Cadence.addCommand('admin', arg => {
1919

2020
let sessionid;
2121

22-
xmpp.command(command)
23-
.then((stanza) => {
22+
const process = stanza => {
2423
sessionid = $('command', stanza).attr('sessionid');
2524
return new Promise((resolve, reject) => {
2625
if (interactive) {
@@ -39,32 +38,33 @@ Cadence.addCommand('admin', arg => {
3938
resolve(args);
4039
}
4140
});
42-
})
43-
.then((data) => {
44-
return xmpp.commandSubmit(command, sessionid, data);
45-
})
46-
.then((stanza) => {
41+
};
42+
43+
const submit = data => xmpp.commandSubmit(command, sessionid, data);
44+
45+
const result = (stanza) => {
4746
const result = [];
4847
$('field[type!="hidden"]', stanza).each(function() {
4948
const label = $(this).attr('label');
5049
const value = $(this).text();
5150
result.push($('<strong>').text(value + ':'), ' ', value, $('<br>'));
52-
});
53-
51+
})
5452
ui.messageInfo(strings.info.admin[result.length ? 'result' : 'completed'], {command, result});
55-
})
53+
};
54+
55+
return xmpp.command(command).then(process).then(submit).then(result)
5656
.catch(error => {
5757
switch (error.condition) {
5858
case 'forbidden':
5959
throw new Cadence.Error(strings.error.admin.forbidden, {command});
6060
case 'service-unavailable':
6161
throw new Cadence.Error(strings.error.admin.badCommand, {command});
6262
}
63-
throw new Cadence.Error(strings.error.admin.unknown, {command});
63+
throw error;
6464
});
6565
})
6666
.parse(string => {
67-
const m = Cadence.parseArgs(string);
67+
const arg = Cadence.parseArgs(string);
6868

6969
// Make single-argument commands more convenient:
7070
const defaultArgs = {
@@ -75,11 +75,11 @@ Cadence.addCommand('admin', arg => {
7575
}
7676

7777
// Use first positional argument as command.
78-
if (m[0].length) m.cmd = m[0][0];
7978
// If there is more, use the remaining text as an argument.
80-
if (m[0].length > 1 && m.cmd in defaultArgs)
81-
m[defaultArgs[m.cmd]] = arg.substring(m[1][0][0]).trim();
82-
return m;
79+
if (arg[0].length) arg.cmd = arg[0][0];
80+
if (arg[0].length > 1 && arg.cmd in defaultArgs)
81+
arg[defaultArgs[arg.cmd]] = string.substring(arg[1][0][0]).trim();
82+
return arg;
8383
})
8484
.require(Cadence.requirements.online);
8585

@@ -96,7 +96,7 @@ Cadence.addCommand('affiliate', ({type, nick, jid}) => {
9696

9797
// List users with a specific affiliation.
9898
if (!target) {
99-
return xmpp.getUsers({affiliation: type}).then((stanza) => {
99+
const list = stanza => {
100100
// Create a dictionary of non-occupant users:
101101
const users = {};
102102
$('item', stanza).map(function() {
@@ -118,8 +118,9 @@ Cadence.addCommand('affiliate', ({type, nick, jid}) => {
118118
for (let jid in users) users[jid] = visual.format.user(users[jid]);
119119

120120
ui.messageInfo(strings.info.affiliations[type], {type, list: users});
121-
})
122-
.catch(error => {
121+
};
122+
123+
return xmpp.getUsers({affiliation: type}).then(list).catch(error => {
123124
if (error.condition == 'forbidden')
124125
throw new Cadence.Error(strings.error.affiliations.forbidden, {type});
125126
throw error;
@@ -397,28 +398,30 @@ Cadence.addCommand('create', arg => {
397398
},
398399
error => {
399400
// Catch only an <item-not-found> error.
400-
if (!$('item-not-found', error).length) throw error;
401+
if (error.condition != 'item-not-found') throw error;
401402
}
402403
);
403404

404-
const create = () => {
405+
const join = () => {
405406
ui.messageInfo(strings.info.creating, {
406407
room,
407408
user: {nick: xmpp.nick.target, jid: xmpp.jid}
408409
});
409410
return xmpp.joinRoom({room: name});
410411
};
411412

412-
const configure = () => xmpp.roomConfig(name).then(processForm);
413-
const processForm = form => fillForm(form).then(submit, cancel);
413+
const getForm = () => xmpp.roomConfig(name);
414414
const fillForm = form => {
415415
// Use command-line arguments or just set the room title.
416-
if (!interactive) return Cadence.roomConf(arg) || {'muc#roomconfig_roomname': title};
416+
if (!interactive) {
417+
const conf = Cadence.roomConf(arg) || {'muc#roomconfig_roomname': title};
418+
return Promise.resolve(conf);
419+
}
417420

418421
// Unlike /configure, this form is in the promise chain. It can only be submitted once.
419422
return new Promise((resolve, reject) => {
420-
const form = ui.dataForm(conf, resolve);
421-
return ui.formDialog(form, {cancel: reject, apply: false});
423+
const htmlForm = ui.dataForm(form, resolve);
424+
return ui.formDialog(htmlForm, {cancel: reject, apply: false});
422425
});
423426
};
424427
const submit = data => xmpp.roomConfigSubmit(name, data);
@@ -427,6 +430,9 @@ Cadence.addCommand('create', arg => {
427430
throw error || new Cadence.Error(strings.error.roomCreateCancel);
428431
};
429432

433+
const processForm = form => fillForm(form).then(submit).catch(cancel);
434+
const configure = () => getForm().then(processForm);
435+
430436
const success = () => {
431437
xmpp.setRoom(name);
432438
Cadence.setSetting('xmpp.room', name);
@@ -442,14 +448,16 @@ Cadence.addCommand('create', arg => {
442448
throw error;
443449
};
444450

445-
return checkExists.then(create).then(configure).then(success, error);
451+
const create = () => join().then(configure).then(success, error);
452+
453+
return checkExists.then(create);
446454
})
447455
.parse(string => {
448456
const arg = Cadence.parseArgs(string);
449457
// Use --name or positional args or --title as name.
450458
const name = arg.name || arg[0].join(' ') || arg.title;
451459
// Use --title or --name as title.
452-
arg.title = arg.title || arg.name;
460+
arg.title = arg.title || name;
453461
// The name must be lowercase.
454462
arg.name = name.toLowerCase();
455463
return arg;

js/core/ui.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ var ui = {
234234
// The room selection menu listens for changes.
235235
this.dom.roomSelection.change(function() {
236236
if (this.value != xmpp.room.current) {
237-
if (this.value) Cadence.tryCommand('join', {name: this.value});
237+
if (this.value) Cadence.tryCommand('join', {room: this.value});
238238
else Cadence.tryCommand('part');
239239
}
240240
});

js/core/xmpp.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ var xmpp = {
502502
$.each(data, (name, values) => {
503503
if (values !== undefined) {
504504
form.c('field', {'var': name});
505-
if (!values || values.constructor != Array) values = [values];
505+
if (!(values instanceof Array)) values = [values];
506506
values.forEach(value => form.c('value', {}, String(value)));
507507
form.up();
508508
}
@@ -586,8 +586,8 @@ var xmpp = {
586586
$.each(data, (name, values) => {
587587
if (values !== undefined) {
588588
form.c('field', {'var': name});
589-
if (!values || values.constructor != Array) values = [values];
590-
values.each(value => form.c('value', {}, String(value)));
589+
if (!values || !(values instanceof Array)) values = [values];
590+
values.forEach(value => form.c('value', {}, String(value)));
591591
form.up();
592592
}
593593
});
@@ -958,8 +958,10 @@ var xmpp = {
958958
const time = delay.attr('stamp') || (new Date()).toISOString();
959959

960960
// Message of the Day.
961-
if (!from.node && !from.resource)
962-
return ui.messageError(strings.info.motd, {domain: from.domain, text: body});
961+
if (!from.node && !from.resource) {
962+
ui.messageError(strings.info.motd, {domain: from.domain, text: body});
963+
return true;
964+
}
963965

964966
if (muc) {
965967
const codes = $.makeArray($('x status', stanza).map(function() {
@@ -1154,7 +1156,7 @@ var xmpp = {
11541156
query.attrs({type: typeof val});
11551157
if (val !== undefined) query.t(String(val));
11561158
}
1157-
else if (val.constructor === Array) {
1159+
else if (val instanceof Array) {
11581160
query.attrs({type: 'array'});
11591161
encodeArray(val);
11601162
}

0 commit comments

Comments
 (0)