@@ -8,22 +8,19 @@ async function about(ctx){
8
8
await ctx . render ( 'about' )
9
9
}
10
10
/**
11
- * Activate a bot for the member
12
- * @module
11
+ * Activate a specific Bot.
13
12
* @public
14
- * @api no associated view
15
- * @param {object } ctx Koa Context object
16
- * @returns {object } Koa Context object
13
+ * @async
14
+ * @param {object } ctx - Koa Context object
15
+ * @returns {object } - Activated Response object: { bot_id, greeting, success, version, versionUpdate, }
17
16
*/
18
- function activateBot ( ctx ) {
19
- const { avatar, } = ctx . state
20
- avatar . activeBotId = ctx . params . bid
21
- const { activeBotId, activeBotVersion, activeBotNewestVersion, } = avatar
22
- ctx . body = {
23
- activeBotId,
24
- activeBotVersion,
25
- version : activeBotNewestVersion ,
26
- }
17
+ async function activateBot ( ctx ) {
18
+ const { bid, } = ctx . params
19
+ if ( ! ctx . Globals . isValidGuid ( bid ) )
20
+ ctx . throw ( 400 , `missing bot id` )
21
+ const { avatar : Avatar , } = ctx . state
22
+ const response = await Avatar . setActiveBot ( bid )
23
+ ctx . body = response
27
24
}
28
25
async function alerts ( ctx ) {
29
26
// @todo : put into ctx the _type_ of alert to return, system use dataservices, member use personal
@@ -34,52 +31,55 @@ async function alerts(ctx){
34
31
ctx . body = await ctx . state . MemberSession . alerts ( ctx . request . body )
35
32
}
36
33
}
34
+ /**
35
+ * Manage bots for the member.
36
+ * @param {Koa } ctx - Koa Context object
37
+ * @returns {object } - Koa Context object
38
+ */
37
39
async function bots ( ctx ) {
38
40
const { bid, } = ctx . params // bot_id sent in url path
39
- const { avatar } = ctx . state
41
+ const { avatar : Avatar , } = ctx . state
40
42
const bot = ctx . request . body
41
43
?? { }
42
44
switch ( ctx . method ) {
43
45
case 'DELETE' : // retire bot
44
46
if ( ! ctx . Globals . isValidGuid ( bid ) )
45
47
ctx . throw ( 400 , `missing bot id` )
46
- ctx . body = await avatar . retireBot ( bid )
48
+ ctx . body = await Avatar . retireBot ( bid )
47
49
break
48
50
case 'POST' : // create new bot
49
- ctx . body = await avatar . createBot ( bot )
51
+ ctx . body = await Avatar . createBot ( bot )
50
52
break
51
53
case 'PUT' : // update bot
52
- ctx . body = await avatar . updateBot ( bot )
54
+ ctx . body = await Avatar . updateBot ( bot )
53
55
break
54
56
case 'GET' :
55
57
default :
56
58
if ( bid ?. length ) { // specific bot
57
- ctx . body = await avatar . getBot ( ctx . params . bid )
59
+ ctx . body = await Avatar . getBot ( ctx . params . bid )
58
60
} else {
59
- const { activeBotId, } = avatar
60
- const bots = await avatar . getBots ( )
61
+ const bots = await Avatar . getBots ( )
62
+ let { activeBotId, greeting, } = Avatar
63
+ if ( ! activeBotId ) {
64
+ const { bot_id, greeting : activeGreeting } = await Avatar . setActiveBot ( )
65
+ activeBotId = bot_id
66
+ greeting = activeGreeting
67
+ }
61
68
ctx . body = { // wrap bots
62
69
activeBotId,
63
70
bots,
71
+ greeting,
64
72
}
65
73
}
66
74
break
67
75
}
68
76
}
69
- function category ( ctx ) { // sets category for avatar
70
- ctx . state . category = ctx . request . body
71
- const { avatar, } = ctx . state
72
- avatar . setActiveCategory ( ctx . state . category )
73
- ctx . body = avatar . category
74
- }
75
77
/**
76
78
* Challenge the member session with a passphrase.
77
- * @module
78
79
* @public
79
80
* @async
80
- * @api - No associated view
81
81
* @param {Koa } ctx - Koa Context object
82
- * @returns {object } Koa Context object
82
+ * @returns {object } - Koa Context object
83
83
* @property {object } ctx.body - The result of the challenge.
84
84
*/
85
85
async function challenge ( ctx ) {
@@ -99,6 +99,8 @@ async function challenge(ctx){
99
99
}
100
100
/**
101
101
* Chat with the Member or System Avatar's intelligence.
102
+ * @public
103
+ * @async
102
104
* @param {Koa } ctx - Koa Context object
103
105
* @returns {object } - The response from the chat in `ctx.body`
104
106
* @property {object } instruction - Instructionset for the frontend to execute (optional)
@@ -131,41 +133,38 @@ async function createBot(ctx){
131
133
ctx . body = await avatar . createBot ( bot )
132
134
}
133
135
/**
134
- * Delete an item from collection via the member's avatar.
135
- * @async
136
- * @public
137
- * @param {object } ctx - Koa Context object
138
- * @returns {boolean } - Under `ctx.body`, status of deletion.
136
+ * Save feedback from the member.
137
+ * @param {Koa } ctx - Koa Context object
138
+ * @returns {Boolean } - Whether or not the feedback was saved
139
139
*/
140
- async function deleteItem ( ctx ) {
141
- const { iid, } = ctx . params
142
- const { avatar, } = ctx . state
143
- if ( ! iid ?. length )
144
- ctx . throw ( 400 , `missing item id` )
145
- ctx . body = await avatar . deleteItem ( iid )
140
+ async function feedback ( ctx ) {
141
+ const { mid : message_id , } = ctx . params
142
+ const { avatar : Avatar , } = ctx . state
143
+ const { isPositive= true , message, } = ctx . request . body
144
+ ctx . body = await Avatar . feedback ( message_id , isPositive , message )
146
145
}
147
146
/**
148
- * Get greetings for this bot/active bot.
149
- * @todo - move dynamic system responses to a separate function (route /system)
150
- * @param {Koa } ctx - Koa Context object.
151
- * @returns {object } - Greetings response message object: { success: false, messages: [], }.
147
+ * Get greetings for active situation.
148
+ * @public
149
+ * @async
150
+ * @param {Koa } ctx - Koa Context object
151
+ * @returns {object } - Greetings response message object: { responses, success, }
152
152
*/
153
153
async function greetings ( ctx ) {
154
154
const { vld : validateId , } = ctx . request . query
155
155
let { dyn : dynamic , } = ctx . request . query
156
156
if ( typeof dynamic === 'string' )
157
157
dynamic = JSON . parse ( dynamic )
158
- const { avatar, } = ctx . state
159
- let response = { success : false , messages : [ ] , }
160
- if ( validateId ?. length )
161
- response . messages . push ( ...await avatar . validateRegistration ( validateId ) )
162
- else
163
- response . messages . push ( ...await avatar . greeting ( dynamic ) )
164
- response . success = response . messages . length > 0
158
+ const { avatar : Avatar , } = ctx . state
159
+ const response = validateId ?. length && Avatar . isMyLife
160
+ ? await Avatar . validateRegistration ( validateId )
161
+ : await Avatar . greeting ( dynamic )
165
162
ctx . body = response
166
163
}
167
164
/**
168
165
* Request help about MyLife.
166
+ * @public
167
+ * @async
169
168
* @param {Koa } ctx - Koa Context object, body={ request: string|required, mbr_id, type: string, }.
170
169
* @returns {object } - Help response message object.
171
170
*/
@@ -179,8 +178,8 @@ async function help(ctx){
179
178
}
180
179
/**
181
180
* Index page for the application.
182
- * @async
183
181
* @public
182
+ * @async
184
183
* @param {object } ctx - Koa Context object
185
184
*/
186
185
async function index ( ctx ) {
@@ -210,18 +209,12 @@ async function item(ctx){
210
209
const { avatar, } = ctx . state
211
210
const { globals, } = avatar
212
211
const { method, } = ctx . request
213
- const item = ctx . request . body
214
- /* validate payload */
215
- if ( ! id ?. length )
216
- ctx . throw ( 400 , `missing item id` )
217
- if ( ! item || typeof item !== 'object' || ! Object . keys ( item ) . length )
218
- ctx . throw ( 400 , `missing item data` )
219
- const { id : itemId , } = item
220
- if ( itemId && itemId !== id ) // ensure item.id is set to id
221
- throw new Error ( `item.id must match /:iid` )
222
- else if ( ! itemId )
212
+ const item = ctx . request . body // always `{}` by default
213
+ if ( ! item ?. id && id ?. length )
223
214
item . id = id
224
- ctx . body = await avatar . item ( item , method )
215
+ const response = await avatar . item ( item , method )
216
+ delete avatar . frontendInstruction // already embedded in response
217
+ ctx . body = response
225
218
}
226
219
async function logout ( ctx ) {
227
220
ctx . session = null
@@ -354,17 +347,15 @@ async function signup(ctx) {
354
347
const { mbr_id, ..._registrationData } = signupPacket // do not display theoretical memberId
355
348
ctx . status = 200 // OK
356
349
ctx . body = {
350
+ message : 'Signup successful' ,
357
351
payload : _registrationData ,
358
352
success,
359
- message : 'Signup successful' ,
360
353
}
361
354
}
362
355
async function summarize ( ctx ) {
363
- const { avatar, } = ctx . state
356
+ const { avatar : Avatar , } = ctx . state
364
357
const { fileId, fileName, } = ctx . request . body
365
- if ( avatar . isMyLife )
366
- throw new Error ( 'Only logged in members may summarize text' )
367
- ctx . body = await avatar . summarize ( fileId , fileName )
358
+ ctx . body = await Avatar . summarize ( fileId , fileName )
368
359
}
369
360
/**
370
361
* Get a specified team, its details and bots, by id for the member.
@@ -384,8 +375,8 @@ async function team(ctx){
384
375
* @returns {Object[] } - List of team objects.
385
376
*/
386
377
async function teams ( ctx ) {
387
- const { avatar, } = ctx . state
388
- ctx . body = await avatar . teams ( )
378
+ const { avatar : Avatar , } = ctx . state
379
+ ctx . body = await Avatar . teams ( )
389
380
}
390
381
async function updateBotInstructions ( ctx ) {
391
382
const { bid, } = ctx . params
@@ -417,14 +408,13 @@ export {
417
408
activateBot ,
418
409
alerts ,
419
410
bots ,
420
- category ,
421
411
challenge ,
422
412
chat ,
423
413
collections ,
424
414
createBot ,
425
- deleteItem ,
426
- help ,
415
+ feedback ,
427
416
greetings ,
417
+ help ,
428
418
index ,
429
419
interfaceMode ,
430
420
item ,
0 commit comments