forked from RocketChat/rocket.chat.app-poll
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PollApp.ts
465 lines (423 loc) · 18.6 KB
/
PollApp.ts
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import {
IConfigurationExtend,
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IAppInfo, RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-engine/definition/metadata';
import { SettingType } from '@rocket.chat/apps-engine/definition/settings';
import {
IUIKitInteractionHandler,
UIKitBlockInteractionContext,
UIKitViewSubmitInteractionContext,
} from '@rocket.chat/apps-engine/definition/uikit';
import { addOptionModal } from './src/lib/addOptionModal';
import { createLivePollMessage } from './src/lib/createLivePollMessage';
import { createLivePollModal } from './src/lib/createLivePollModal';
import timeZones from './src/assets/timezones';
import { IModalContext, pollVisibility } from './src/definition';
import { createMixedVisibilityModal } from './src/lib/createMixedVisibilityModal';
import { createPollMessage } from './src/lib/createPollMessage';
import { createPollModal } from './src/lib/createPollModal';
import { finishPollMessage } from './src/lib/finishPollMessage';
import { nextPollMessage } from './src/lib/nextPollMessage';
import { updatePollMessage } from './src/lib/updatePollMessage';
import { votePoll } from './src/lib/votePoll';
import { PollCommand } from './src/PollCommand';
export class PollApp extends App implements IUIKitInteractionHandler {
constructor(info: IAppInfo, logger: ILogger) {
super(info, logger);
}
public async executeViewSubmitHandler(context: UIKitViewSubmitInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify) {
const data = context.getInteractionData();
const id = data.view.id;
if (/create-poll-modal/i.test(id)) {
const { state }: {
state: {
poll: {
question: string,
[option: string]: string,
},
config?: {
mode?: string,
visibility?: string,
additionalChoices?: string,
wordCloud?: string;
},
},
config?: {
mode?: string,
visibility?: string,
},
} = data.view as any;
if (!state) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: {
question: 'Error creating poll',
},
});
}
if (state.config && state.config.visibility !== pollVisibility.mixed) {
try {
await createPollMessage(data, read, modify, persistence, data.user.id);
} catch (err) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: err,
});
}
} else {
// Open mixed visibility modal
try {
const modal = await createMixedVisibilityModal({ question: state.poll.question, persistence, modify, data });
await modify.getUiController().openModalView(modal, context.getInteractionData(), data.user);
return {
success: true,
};
} catch (err) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: err,
});
}
}
return {
success: true,
};
} else if (/create-live-poll-modal/.test(id)) {
const { state }: {
state: {
poll: {
question: string,
[option: string]: string,
},
config?: {
mode?: string,
visibility?: string,
},
},
} = data.view as any;
if (!state) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: {
option: 'Error creating poll',
},
});
}
const association = new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, data.view.id);
const [readData] = await read.getPersistenceReader().readByAssociation(association) as any;
const polls = readData.polls || [];
const pollIndex = readData.pollIndex 1;
const totalPolls = readData.totalPolls;
// Prompt user to enter values for poll if left blank
try {
if (!state.poll || !state.poll.question || state.poll.question.trim() === '') {
throw { question: 'Please type your question here' };
}
if (!state.poll || !state.poll.ttv || isNaN( state.poll.ttv)) {
throw { ttv: 'Please enter a valid time for the poll to end' };
}
if (!state.poll['option-0'] || state.poll['option-0'] === '') {
throw {
'option-0': 'Please provide one more option',
};
}
if (!state.poll['option-1'] || state.poll['option-1'] === '') {
throw {
'option-1': 'Please provide one more option',
};
}
} catch (err) {
this.getLogger().log(err);
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: err,
});
}
polls.push(state);
readData.polls = polls;
readData.pollIndex = pollIndex;
readData.user = data.user;
readData.appId = data.appId;
readData.view = data.view;
readData.triggerId = data.triggerId;
await persistence.updateByAssociation(association, readData, true);
if (pollIndex === totalPolls) {
const pollId = `live-${Math.random().toString(36).slice(7)}`;
const livePollAssociation = new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, pollId);
await persistence.createWithAssociation(readData, livePollAssociation);
try {
if (readData.save) {
const message = modify
.getCreator()
.startMessage()
.setSender(data.user)
.setText(`Live Poll has been saved with id ${pollId}. Use \`/poll live load ${pollId}\` to start.`)
.setUsernameAlias('Poll');
if (readData.room) {
message.setRoom(readData.room);
}
modify
.getNotifier()
.notifyUser(
data.user,
message.getMessage(),
);
} else {
await createLivePollMessage(data, read, modify, persistence, data.user.id, 0);
}
} catch (err) {
this.getLogger().log(err);
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: err,
});
}
} else {
const modal = await createLivePollModal({id: data.view.id, question: '', persistence, modify, data, pollIndex, totalPolls});
return context.getInteractionResponder().updateModalViewResponse(modal);
}
} else if (/create-mixed-visibility-modal/.test(id)) {
const { state }: {
state: {
mixedVisibility: {
anonymousOptions: any,
additionalChoices?: string;
},
},
} = data.view as any;
if (!state) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: {
question: 'Error building mixed visibility modal',
},
});
}
try {
await createPollMessage(data, read, modify, persistence, data.user.id);
} catch (err) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: err,
});
}
} else if (/add-option-modal/.test(id)) {
const { state }: {
state: {
addOption: {
option: string,
},
},
} = data.view as any;
if (!state) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: {
option: 'Error adding option',
},
});
}
try {
const logger = this.getLogger();
await updatePollMessage({data, read, modify, persistence, logger});
} catch (err) {
this.getLogger().log(err);
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: err,
});
}
return {
success: true,
};
}
return {
success: true,
};
}
public async executeBlockActionHandler(context: UIKitBlockInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify) {
const data = context.getInteractionData();
const { actionId } = data;
switch (actionId) {
case 'vote': {
await votePoll({ data, read, persistence, modify });
return {
success: true,
};
}
case 'create': {
const modal = await createPollModal({ data, persistence, modify });
return context.getInteractionResponder().openModalViewResponse(modal);
}
case 'addChoice': {
let modal;
const association = new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, data.container.id);
const [record] = await read.getPersistenceReader().readByAssociation(association) as Array<IModalContext>;
data.room = record.room;
(data as any).threadId = record.threadId;
if (data.value && data.value.includes('live-')) {
(data as any).totalPolls = (record as any).totalPolls;
(data as any).pollIndex = (record as any).pollIndex;
(data as any).save = (record as any).save;
modal = await createLivePollModal({
id: data.container.id,
data, persistence, modify,
options: parseInt(data.value.split('-')[1], 10),
pollIndex: parseInt(data.value.split('-')[2], 10),
totalPolls: parseInt(data.value.split('-')[3], 10),
});
} else {
modal = await createPollModal({ id: data.container.id, data, persistence, modify, options: parseInt(String(data.value), 10) });
}
return context.getInteractionResponder().updateModalViewResponse(modal);
}
case 'nextPoll': {
try {
const logger = this.getLogger();
await nextPollMessage({ data, read, persistence, modify, logger });
} catch (e) {
const { room } = context.getInteractionData();
const errorMessage = modify
.getCreator()
.startMessage()
.setSender(context.getInteractionData().user)
.setText(e.message)
.setUsernameAlias('Poll');
if (room) {
errorMessage.setRoom(room);
}
modify
.getNotifier()
.notifyUser(
context.getInteractionData().user,
errorMessage.getMessage(),
);
}
break;
}
case 'addUserChoice': {
const modal = await addOptionModal({ id: data.container.id, read, modify });
return context.getInteractionResponder().openModalViewResponse(modal);
}
case 'finish': {
try {
const logger = this.getLogger();
await finishPollMessage({ data, read, persistence, modify, http, logger });
} catch (e) {
const { room } = context.getInteractionData();
const errorMessage = modify
.getCreator()
.startMessage()
.setSender(context.getInteractionData().user)
.setText(e.message)
.setUsernameAlias('Poll');
if (room) {
errorMessage.setRoom(room);
}
modify
.getNotifier()
.notifyUser(
context.getInteractionData().user,
errorMessage.getMessage(),
);
}
break;
}
case 'mode': {
const viewId = data.container.id;
const viewAssociation = new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, viewId);
const optionsAssociation = new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'options');
const [record] = await read.getPersistenceReader().readByAssociation(viewAssociation) as Array<IModalContext>;
data.room = record.room;
(data as any).threadId = record.threadId;
try {
const [existingOptions] = await read.getPersistenceReader()
.readByAssociations([viewAssociation, optionsAssociation]) as any;
const modal = await createPollModal({ id: viewId,
data,
persistence,
modify,
mode: data.value,
...existingOptions && existingOptions.options && { options: existingOptions.options },
});
return context.getInteractionResponder().updateModalViewResponse(modal);
} catch (e) {
this.getLogger().log(e);
}
}
}
return {
success: true,
triggerId: data.triggerId,
};
}
public async initialize(configuration: IConfigurationExtend): Promise<void> {
configuration.scheduler.registerProcessors([
{
id: 'nextPoll',
processor: async (jobContext, read, modify, http, persis) => {
try {
const logger = this.getLogger();
await nextPollMessage({ data: jobContext, read, persistence: persis, modify, logger });
} catch (e) {
const errorMessage = modify
.getCreator()
.startMessage()
.setSender(jobContext.user)
.setText(e.message)
.setUsernameAlias('Poll');
if (jobContext.room) {
errorMessage.setRoom(jobContext.room);
}
await modify
.getNotifier()
.notifyUser(
jobContext.user,
errorMessage.getMessage(),
);
}
},
},
]);
await configuration.slashCommands.provideSlashCommand(new PollCommand(this));
await configuration.settings.provideSetting({
id : 'use-user-name',
i18nLabel: 'use_user_name_label',
i18nDescription: 'use_user_name_description',
required: false,
type: SettingType.BOOLEAN,
public: true,
packageValue: false,
});
await configuration.settings.provideSetting({
id : 'wordcloud-api',
i18nLabel: 'word_cloud_api_label',
i18nDescription: 'word_cloud_api_description',
required: false,
type: SettingType.STRING,
public: true,
packageValue: 'https://quickchart.io/wordcloud',
value: 'https://quickchart.io/wordcloud',
});
await configuration.settings.provideSetting({
id : 'timezone',
i18nLabel: 'timezone_label',
i18nDescription: 'timezone_description',
required: true,
type: SettingType.SELECT,
public: true,
packageValue: 'America/Danmarkshavn',
value: 'America/Danmarkshavn',
values: timeZones.timeZones.map((tz) => ({
i18nLabel: `${tz.value} (UTC ${tz.offset >= 0 ? ' ' tz.offset : '- ' Math.abs(tz.offset)} )`,
key: tz.utc[0],
})),
});
}
}