@@ -19,8 +19,7 @@ Cadence.addCommand('admin', arg => {
19
19
20
20
let sessionid ;
21
21
22
- xmpp . command ( command )
23
- . then ( ( stanza ) => {
22
+ const process = stanza => {
24
23
sessionid = $ ( 'command' , stanza ) . attr ( 'sessionid' ) ;
25
24
return new Promise ( ( resolve , reject ) => {
26
25
if ( interactive ) {
@@ -39,32 +38,33 @@ Cadence.addCommand('admin', arg => {
39
38
resolve ( args ) ;
40
39
}
41
40
} ) ;
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 ) => {
47
46
const result = [ ] ;
48
47
$ ( 'field[type!="hidden"]' , stanza ) . each ( function ( ) {
49
48
const label = $ ( this ) . attr ( 'label' ) ;
50
49
const value = $ ( this ) . text ( ) ;
51
50
result . push ( $ ( '<strong>' ) . text ( value + ':' ) , ' ' , value , $ ( '<br>' ) ) ;
52
- } ) ;
53
-
51
+ } )
54
52
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 )
56
56
. catch ( error => {
57
57
switch ( error . condition ) {
58
58
case 'forbidden' :
59
59
throw new Cadence . Error ( strings . error . admin . forbidden , { command} ) ;
60
60
case 'service-unavailable' :
61
61
throw new Cadence . Error ( strings . error . admin . badCommand , { command} ) ;
62
62
}
63
- throw new Cadence . Error ( strings . error . admin . unknown , { command } ) ;
63
+ throw error ;
64
64
} ) ;
65
65
} )
66
66
. parse ( string => {
67
- const m = Cadence . parseArgs ( string ) ;
67
+ const arg = Cadence . parseArgs ( string ) ;
68
68
69
69
// Make single-argument commands more convenient:
70
70
const defaultArgs = {
@@ -75,11 +75,11 @@ Cadence.addCommand('admin', arg => {
75
75
}
76
76
77
77
// Use first positional argument as command.
78
- if ( m [ 0 ] . length ) m . cmd = m [ 0 ] [ 0 ] ;
79
78
// 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 ;
83
83
} )
84
84
. require ( Cadence . requirements . online ) ;
85
85
@@ -96,7 +96,7 @@ Cadence.addCommand('affiliate', ({type, nick, jid}) => {
96
96
97
97
// List users with a specific affiliation.
98
98
if ( ! target ) {
99
- return xmpp . getUsers ( { affiliation : type } ) . then ( ( stanza ) => {
99
+ const list = stanza => {
100
100
// Create a dictionary of non-occupant users:
101
101
const users = { } ;
102
102
$ ( 'item' , stanza ) . map ( function ( ) {
@@ -118,8 +118,9 @@ Cadence.addCommand('affiliate', ({type, nick, jid}) => {
118
118
for ( let jid in users ) users [ jid ] = visual . format . user ( users [ jid ] ) ;
119
119
120
120
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 => {
123
124
if ( error . condition == 'forbidden' )
124
125
throw new Cadence . Error ( strings . error . affiliations . forbidden , { type} ) ;
125
126
throw error ;
@@ -397,28 +398,30 @@ Cadence.addCommand('create', arg => {
397
398
} ,
398
399
error => {
399
400
// 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 ;
401
402
}
402
403
) ;
403
404
404
- const create = ( ) => {
405
+ const join = ( ) => {
405
406
ui . messageInfo ( strings . info . creating , {
406
407
room,
407
408
user : { nick : xmpp . nick . target , jid : xmpp . jid }
408
409
} ) ;
409
410
return xmpp . joinRoom ( { room : name } ) ;
410
411
} ;
411
412
412
- const configure = ( ) => xmpp . roomConfig ( name ) . then ( processForm ) ;
413
- const processForm = form => fillForm ( form ) . then ( submit , cancel ) ;
413
+ const getForm = ( ) => xmpp . roomConfig ( name ) ;
414
414
const fillForm = form => {
415
415
// 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
+ }
417
420
418
421
// Unlike /configure, this form is in the promise chain. It can only be submitted once.
419
422
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 } ) ;
422
425
} ) ;
423
426
} ;
424
427
const submit = data => xmpp . roomConfigSubmit ( name , data ) ;
@@ -427,6 +430,9 @@ Cadence.addCommand('create', arg => {
427
430
throw error || new Cadence . Error ( strings . error . roomCreateCancel ) ;
428
431
} ;
429
432
433
+ const processForm = form => fillForm ( form ) . then ( submit ) . catch ( cancel ) ;
434
+ const configure = ( ) => getForm ( ) . then ( processForm ) ;
435
+
430
436
const success = ( ) => {
431
437
xmpp . setRoom ( name ) ;
432
438
Cadence . setSetting ( 'xmpp.room' , name ) ;
@@ -442,14 +448,16 @@ Cadence.addCommand('create', arg => {
442
448
throw error ;
443
449
} ;
444
450
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 ) ;
446
454
} )
447
455
. parse ( string => {
448
456
const arg = Cadence . parseArgs ( string ) ;
449
457
// Use --name or positional args or --title as name.
450
458
const name = arg . name || arg [ 0 ] . join ( ' ' ) || arg . title ;
451
459
// Use --title or --name as title.
452
- arg . title = arg . title || arg . name ;
460
+ arg . title = arg . title || name ;
453
461
// The name must be lowercase.
454
462
arg . name = name . toLowerCase ( ) ;
455
463
return arg ;
0 commit comments