-
Notifications
You must be signed in to change notification settings - Fork 344
/
attachmentActions.js
324 lines (303 loc) · 10.5 KB
/
attachmentActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*!
* Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
*/
import {
WebexPlugin
} from '@webex/webex-core';
import {
SDK_EVENT,
createEventEnvelope,
constructHydraId,
getHydraClusterString,
hydraTypes
} from '@webex/common';
import {cloneDeep} from 'lodash';
const debug = require('debug')('attachmentActions');
/**
* @typedef {Object} AttachmentActionObject
* @property {string} id - (server generated) Unique identifier for the attachment action
* @property {string} messageId - The ID of the message in which attachment action is to be performed
* @property {string} type - The type of attachment action eg., submit
* @property {Object} inputs - The inputs for form fields in attachment message
* @property {string} personId - (server generated) The ID for the author of the attachment action
* @property {string} roomId - (server generated) The ID for the room of the message
* @property {isoDate} created - (server generated) The date and time that the message was created
*/
/**
* AttachmentActions are events that communicate information when a user clicks on an
* Action.Submit button in a card displayed in Webex
* Information conveyed in an AttachmentAction includes details about the user that
* clicked the button along with any card specific inputs. See the
* {@link https://developer.webex.com/docs/api/v1/attachment-actions|Attachments Actions API Documentation}
* for more details
* @class
*/
const AttachmentActions = WebexPlugin.extend({
/**
* Initializer used to generate AttachmentActions
* as a plugin wrapped around the provided arguments.
* @private
* @see WebexPlugin.initialize
* @param {...any} args
* @returns {undefined}
*/
initialize(...args) {
Reflect.apply(WebexPlugin.prototype.initialize, this, args);
},
/**
* Register to listen for incoming attachmentAction events
* This is an alternate approach to registering for attachmentAction webhooks.
* The events passed to any registered handlers will be similar to the webhook JSON,
* but will omit webhook specific fields such as name, secret, url, etc.
* The attachmentActions.listen() event objects can also include additional fields not
* available in the webhook's JSON payload, specifically: `inputs`.
* To utilize the `listen()` method, the authorization token used
* will need to have `spark:all` and `spark:kms` scopes enabled.
* Note that by configuring your application to enable or disable `spark:all`
* via its configuration page will also enable or disable `spark:kms`.
* See the <a href="https://webex.github.io/webex-js-sdk/samples/browser-socket/">Sample App</a>
* for more details.
* @instance
* @memberof Messages
* @returns {Promise}
* @example
* webex.attachmentActions.listen()
* .then(() => {
* console.log('listening to attachmentActions events');
* webex.attachmentActions.on('created', (event) => console.log(`Got an attachmentActions:created event:\n${event}`));
* })
* .catch((e) => console.error(`Unable to register for attachmentAction events: ${e}`));
* // Some app logic...
* // WHen it is time to cleanup
* webex.attachmentActions.stopListening();
* webex.attachmentActions.off('created');
*/
listen() {
// Create a common envelope that we will wrap all events in
return createEventEnvelope(this.webex,
SDK_EVENT.EXTERNAL.RESOURCE.ATTACHMENT_ACTIONS)
.then((envelope) => {
this.eventEnvelope = envelope;
// Register to listen to events
return this.webex.internal.mercury.connect().then(() => {
this.listenTo(this.webex.internal.mercury,
SDK_EVENT.INTERNAL.WEBEX_ACTIVITY,
(event) => this.onWebexApiEvent(event));
});
});
},
/**
* Post a new attachment action for a message with attachment.
* @instance
* @memberof AttachmentActions
* @param {AttachmentActionObject} attachmentAction
* @returns {Promise<AttachmentActionObject>}
* @example
* webex.rooms.create({title: 'Create Message with card Example'})
* .then(function(room) {
* return webex.messages.create({
* text: 'Howdy!',
* roomId: room.id,
* attachments:[ {
* contentType: 'application/vnd.microsoft.card.adaptive',
* content: {
* type: 'AdaptiveCard',
* version: '1.0',
* body: [
* {
* type: 'TextBlock',
* text: '',
* size: 'large'
* },
* {
* type: 'TextBlock',
* text: 'Adaptive Cards',
* separation: 'none'
* }
* {
* type: 'Input.Date',
* id: 'dueDate'
* }
* ],
* actions: [
* {
* type: 'Action.Submit',
* title: 'Due Date'
* }
* ]
* }
* }]
* });
* })
* .then(function(message) {
* return webex.attachmentActions.create({
* type: 'submit',
* messageId: message.id,
* inputs:{
* dueDate: '26/06/1995'
* }
* })
* .then(function(attachmentAction)){
* var assert = require('assert');
* assert(attachmentAction.id);
* assert(attachmentAction.type);
* assert(attachmentAction.personId);
* assert(attachmentAction.inputs);
* assert(attachmentAction.messageId);
* assert(attachmentAction.roomId);
* assert(attachmentAction.created);
* return 'success';
* }
* });
* // => success
*/
create(attachmentAction) {
return this.request({
method: 'POST',
service: 'hydra',
resource: 'attachment/actions',
body: attachmentAction
})
.then((res) => res.body);
},
/**
* Returns a single attachment action.
* @instance
* @memberof AttachmentActions
* @param {string} attachmentAction
* @returns {Promise<AttachmentActionObject>}
* @example
* var attachmentAction;
* webex.rooms.create({title: 'Get Message Example'})
* .then(function(room) {
* return webex.messages.create({
* text: 'Howdy!',
* roomId: room.id,
* attachments:[ {
* contentType: 'application/vnd.microsoft.card.adaptive',
* content: {
* type: 'AdaptiveCard',
* version: '1.0',
* body: [
* {
* type: 'TextBlock',
* text: '',
* size: 'large'
* },
* {
* type: 'TextBlock',
* text: 'Adaptive Cards',
* separation: 'none'
* },
* {
* type: 'Input.Date',
* id: 'dueDate'
* }
* ],
* actions: [
* {
* type: 'Action.Submit',
* title: 'Due Date'
* }
* ]
* }
* }]
* });
* })
* .then(function(message) {
* return webex.attachmentActions.create({
* type: 'submit',
* messageId: message.id,
* inputs:{
* dueDate: '26/06/1995'
* });
* })
* .then(function(attachmentAction) {
* return webex.attachmentActions.get(attachmentAction.id)
* })
* .then(function(attachmentAction){
* var assert = require('assert');
* assert.deepEqual(attachmentAction, attachmentAction);
* return 'success';
* })
* // => success
*/
get(attachmentAction) {
const id = attachmentAction.id || attachmentAction;
return this.request({
service: 'hydra',
resource: `attachment/actions/${id}`
})
.then((res) => res.body.items || res.body);
},
/**
* This function is called when an internal mercury events fires,
* if the user registered for these events with the listen() function.
* External users of the SDK should not call this function
* @private
* @memberof AttachmentAction
* @param {Object} event
* @returns {void}
*/
onWebexApiEvent(event) {
const {activity} = event.data;
/* eslint-disable no-case-declarations */
switch (activity.verb) {
case SDK_EVENT.INTERNAL.ACTIVITY_VERB.CARD_ACTION:
const createdEvent =
this.getattachmentActionEvent(activity,
SDK_EVENT.EXTERNAL.EVENT_TYPE.CREATED);
if (createdEvent) {
debug(`attachmentAction "created" payload: \
${JSON.stringify(createdEvent)}`);
this.trigger(SDK_EVENT.EXTERNAL.EVENT_TYPE.CREATED, createdEvent);
}
break;
default: {
break;
}
}
},
/**
* Constructs the data object for an event on the attachmentAction resource,
* adhering to Hydra's Webehook data structure messages.
* External users of the SDK should not call this function
* @private
* @memberof AttachmentAction
* @param {Object} activity from mercury
* @param {Object} event type of "webhook" event
* @returns {Object} constructed event
*/
getattachmentActionEvent(activity, event) {
try {
const sdkEvent = cloneDeep(this.eventEnvelope);
const cluster = getHydraClusterString(this.webex, activity.target.url);
sdkEvent.event = event;
sdkEvent.data.created = activity.published;
sdkEvent.actorId =
constructHydraId(hydraTypes.PEOPLE, activity.actor.entryUUID, cluster);
sdkEvent.data.roomId =
constructHydraId(hydraTypes.ROOM, activity.target.id, cluster);
sdkEvent.data.messageId =
constructHydraId(hydraTypes.MESSAGE, activity.parent.id, cluster);
sdkEvent.data.personId =
constructHydraId(hydraTypes.PEOPLE, activity.actor.entryUUID, cluster);
// Seems like it would be nice to have this, but its not in the hydra webhook
// sdkEvent.data.personEmail =
// activity.actor.emailAddress || activity.actor.entryEmail;
sdkEvent.data.id =
constructHydraId(hydraTypes.ATTACHMENT_ACTION, activity.id, cluster);
if (activity.object.inputs) {
sdkEvent.data.inputs = activity.object.inputs;
}
sdkEvent.data.type = activity.object.objectType;
return sdkEvent;
}
catch (e) {
this.webex.logger.error(`Unable to generate SDK event from mercury \
'socket activity for attachmentAction:${event} event: ${e.message}`);
return null;
}
}
});
export default AttachmentActions;