1
|
|
|
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1'; |
2
|
|
|
import BaseHandler from './BaseHandler'; |
3
|
|
|
import NewPollFormCard from '../cards/NewPollFormCard'; |
4
|
|
|
import {addOptionToState, getConfigFromInput, getStateFromCard} from '../helpers/state'; |
5
|
|
|
import {callMessageApi} from '../helpers/api'; |
6
|
|
|
import {createDialogActionResponse, createStatusActionResponse} from '../helpers/response'; |
7
|
|
|
import PollCard from '../cards/PollCard'; |
8
|
|
|
import {ClosableType, MessageDialogConfig, PollFormInputs, PollState, Voter} from '../helpers/interfaces'; |
9
|
|
|
import AddOptionFormCard from '../cards/AddOptionFormCard'; |
10
|
|
|
import {saveVotes} from '../helpers/vote'; |
11
|
|
|
import {PROHIBITED_ICON_URL} from '../config/default'; |
12
|
|
|
import ClosePollFormCard from '../cards/ClosePollFormCard'; |
13
|
|
|
import MessageDialogCard from '../cards/MessageDialogCard'; |
14
|
|
|
import {createAutoCloseTask} from '../helpers/task'; |
15
|
|
|
import ScheduleClosePollFormCard from '../cards/ScheduleClosePollFormCard'; |
16
|
|
|
|
17
|
|
|
/* |
18
|
|
|
This list methods are used in the poll chat message |
19
|
|
|
*/ |
20
|
|
|
interface PollAction { |
21
|
|
|
saveOption(): Promise<chatV1.Schema$Message>; |
22
|
|
|
|
23
|
|
|
getEventPollState(): PollState; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
export default class ActionHandler extends BaseHandler implements PollAction { |
27
|
|
|
async process(): Promise<chatV1.Schema$Message> { |
28
|
|
|
const action = this.event.common?.invokedFunction; |
29
|
|
|
switch (action) { |
30
|
|
|
case 'start_poll': |
31
|
|
|
return await this.startPoll(); |
32
|
|
|
case 'vote': |
33
|
|
|
return this.recordVote(); |
34
|
|
|
case 'add_option_form': |
35
|
|
|
return this.addOptionForm(); |
36
|
|
|
case 'add_option': |
37
|
|
|
return await this.saveOption(); |
38
|
|
|
case 'show_form': |
39
|
|
|
const pollForm = new NewPollFormCard({topic: '', choices: []}, this.getUserTimezone()).create(); |
40
|
|
|
return createDialogActionResponse(pollForm); |
41
|
|
|
case 'new_poll_on_change': |
42
|
|
|
return this.newPollOnChange(); |
43
|
|
|
case 'close_poll_form': |
44
|
|
|
return this.closePollForm(); |
45
|
|
|
case 'close_poll': |
46
|
|
|
return await this.closePoll(); |
47
|
|
|
case 'schedule_close_poll_form': |
48
|
|
|
return this.scheduleClosePollForm(); |
49
|
|
|
case 'schedule_close_poll': |
50
|
|
|
return this.scheduleClosePoll(); |
51
|
|
|
default: |
52
|
|
|
return createStatusActionResponse('Unknown action!', 'UNKNOWN'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Handle the custom start_poll action. |
58
|
|
|
* |
59
|
|
|
* @returns {object} Response to send back to Chat |
60
|
|
|
*/ |
61
|
|
|
async startPoll(): Promise<chatV1.Schema$Message> { |
62
|
|
|
// Get the form values |
63
|
|
|
const formValues: PollFormInputs = this.event.common!.formInputs! as PollFormInputs; |
64
|
|
|
const config = getConfigFromInput(formValues); |
65
|
|
|
|
66
|
|
|
if (!config.topic || config.choices.length === 0) { |
67
|
|
|
// Incomplete form submitted, rerender |
68
|
|
|
const dialog = new NewPollFormCard(config, this.getUserTimezone()).create(); |
69
|
|
|
return createDialogActionResponse(dialog); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (config.closedTime) { |
73
|
|
|
// because previously in the form, we marked up the time with user timezone offset |
74
|
|
|
const utcClosedTime = config.closedTime + this.getUserTimezone().offset; |
75
|
|
|
if (utcClosedTime < Date.now() - 360000) { |
76
|
|
|
const dialog = new NewPollFormCard(config, this.getUserTimezone()).create(); |
77
|
|
|
return createDialogActionResponse(dialog); |
78
|
|
|
} |
79
|
|
|
config.closedTime = utcClosedTime; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
const pollCardMessage = new PollCard({author: this.event.user, ...config}, |
83
|
|
|
this.getUserTimezone()).createMessage(); |
84
|
|
|
const request = { |
85
|
|
|
parent: this.event.space?.name, |
86
|
|
|
requestBody: pollCardMessage, |
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
const apiResponse = await callMessageApi('create', request); |
90
|
|
|
if (apiResponse.data?.name) { |
91
|
|
|
await createAutoCloseTask(config, apiResponse.data.name); |
92
|
|
|
return createStatusActionResponse('Poll started.', 'OK'); |
93
|
|
|
} else { |
94
|
|
|
return createStatusActionResponse('Failed to start poll.', 'UNKNOWN'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Handle the custom vote action. Updates the state to record |
100
|
|
|
* the user's vote then rerenders the card. |
101
|
|
|
* |
102
|
|
|
* @returns {object} Response to send back to Chat |
103
|
|
|
*/ |
104
|
|
|
recordVote() { |
105
|
|
|
const parameters = this.event.common?.parameters; |
106
|
|
|
if (!(parameters?.['index'])) { |
107
|
|
|
throw new Error('Index Out of Bounds'); |
108
|
|
|
} |
109
|
|
|
const choice = parseInt(parameters['index']); |
110
|
|
|
const userId = this.event.user?.name ?? ''; |
111
|
|
|
const userName = this.event.user?.displayName ?? ''; |
112
|
|
|
const voter: Voter = {uid: userId, name: userName}; |
113
|
|
|
const state = this.getEventPollState(); |
114
|
|
|
|
115
|
|
|
// Add or update the user's selected option |
116
|
|
|
state.votes = saveVotes(choice, voter, state.votes!, state.anon); |
117
|
|
|
const card = new PollCard(state, this.getUserTimezone()); |
118
|
|
|
return { |
119
|
|
|
thread: this.event.message?.thread, |
120
|
|
|
actionResponse: { |
121
|
|
|
type: 'UPDATE_MESSAGE', |
122
|
|
|
}, |
123
|
|
|
cardsV2: [card.createCardWithId()], |
124
|
|
|
}; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Opens and starts a dialog that allows users to add details about a contact. |
129
|
|
|
* |
130
|
|
|
* @returns {object} open a dialog. |
131
|
|
|
*/ |
132
|
|
|
addOptionForm() { |
133
|
|
|
const state = this.getEventPollState(); |
134
|
|
|
const dialog = new AddOptionFormCard(state).create(); |
135
|
|
|
return createDialogActionResponse(dialog); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Handle add new option input to the poll state |
140
|
|
|
* the user's vote then rerenders the card. |
141
|
|
|
* |
142
|
|
|
* @returns {object} Response to send back to Chat |
143
|
|
|
*/ |
144
|
|
|
async saveOption(): Promise<chatV1.Schema$Message> { |
145
|
|
|
const userName = this.event.user?.displayName ?? ''; |
146
|
|
|
const state = this.getEventPollState(); |
147
|
|
|
const formValues = this.event.common?.formInputs; |
148
|
|
|
const optionValue = formValues?.['value']?.stringInputs?.value?.[0]?.trim() || ''; |
149
|
|
|
addOptionToState(optionValue, state, userName); |
150
|
|
|
|
151
|
|
|
const cardMessage = new PollCard(state, this.getUserTimezone()).createMessage(); |
152
|
|
|
|
153
|
|
|
const request = { |
154
|
|
|
name: this.event.message!.name, |
155
|
|
|
requestBody: cardMessage, |
156
|
|
|
updateMask: 'cardsV2', |
157
|
|
|
}; |
158
|
|
|
const apiResponse = await callMessageApi('update', request); |
159
|
|
|
if (apiResponse.status === 200) { |
160
|
|
|
return createStatusActionResponse('Option is added', 'OK'); |
161
|
|
|
} else { |
162
|
|
|
return createStatusActionResponse('Failed to add option.', 'UNKNOWN'); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
getEventPollState(): PollState { |
167
|
|
|
const stateJson = getStateFromCard(this.event); |
168
|
|
|
if (!stateJson) { |
169
|
|
|
throw new ReferenceError('no valid state in the event'); |
170
|
|
|
} |
171
|
|
|
return JSON.parse(stateJson); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
async closePoll(): Promise<chatV1.Schema$Message> { |
175
|
|
|
const state = this.getEventPollState(); |
176
|
|
|
state.closedTime = Date.now(); |
177
|
|
|
state.closedBy = this.event.user?.displayName ?? ''; |
178
|
|
|
const cardMessage = new PollCard(state, this.getUserTimezone()).createMessage(); |
179
|
|
|
const request = { |
180
|
|
|
name: this.event.message!.name, |
181
|
|
|
requestBody: cardMessage, |
182
|
|
|
updateMask: 'cardsV2', |
183
|
|
|
}; |
184
|
|
|
const apiResponse = await callMessageApi('update', request); |
185
|
|
|
if (apiResponse.status === 200) { |
186
|
|
|
return createStatusActionResponse('Poll is closed', 'OK'); |
187
|
|
|
} else { |
188
|
|
|
return createStatusActionResponse('Failed to close poll.', 'UNKNOWN'); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
closePollForm() { |
193
|
|
|
const state = this.getEventPollState(); |
194
|
|
|
if (state.type === ClosableType.CLOSEABLE_BY_ANYONE || state.author!.name === this.event.user?.name) { |
195
|
|
|
return createDialogActionResponse(new ClosePollFormCard(state, this.getUserTimezone()).create()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
const dialogConfig: MessageDialogConfig = { |
199
|
|
|
title: 'Sorry, you can not close this poll', |
200
|
|
|
message: `The poll setting restricts the ability to close the poll to only the creator(${state.author!.displayName}).`, |
201
|
|
|
imageUrl: PROHIBITED_ICON_URL, |
202
|
|
|
}; |
203
|
|
|
return createDialogActionResponse(new MessageDialogCard(dialogConfig).create()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
async scheduleClosePoll(): Promise<chatV1.Schema$Message> { |
207
|
|
|
const formValues: PollFormInputs = this.event.common!.formInputs! as PollFormInputs; |
208
|
|
|
const config = getConfigFromInput(formValues); |
209
|
|
|
|
210
|
|
|
// because previously in the form, we marked up the time with user timezone offset |
211
|
|
|
const utcClosedTime = config.closedTime! + this.getUserTimezone()?.offset; |
212
|
|
|
if (utcClosedTime < Date.now() - 360000) { |
213
|
|
|
const dialog = new ScheduleClosePollFormCard(config, this.getUserTimezone()).create(); |
214
|
|
|
return createDialogActionResponse(dialog); |
215
|
|
|
} |
216
|
|
|
config.closedTime = utcClosedTime; |
217
|
|
|
const messageId = this.event.message!.name!; |
218
|
|
|
config.autoClose = true; |
219
|
|
|
await createAutoCloseTask(config, messageId); |
220
|
|
|
|
221
|
|
|
const state = this.getEventPollState(); |
222
|
|
|
state.closedTime = utcClosedTime; |
223
|
|
|
const cardMessage = new PollCard(state, this.getUserTimezone()).createMessage(); |
224
|
|
|
|
225
|
|
|
const request = { |
226
|
|
|
name: this.event.message!.name, |
227
|
|
|
requestBody: cardMessage, |
228
|
|
|
updateMask: 'cardsV2', |
229
|
|
|
}; |
230
|
|
|
const apiResponse = await callMessageApi('update', request); |
231
|
|
|
if (apiResponse.status === 200) { |
232
|
|
|
return createStatusActionResponse('Poll is scheduled to close', 'OK'); |
233
|
|
|
} else { |
234
|
|
|
return createStatusActionResponse('Failed to schedule close poll.', 'UNKNOWN'); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
scheduleClosePollForm() { |
239
|
|
|
const state = this.getEventPollState(); |
240
|
|
|
return createDialogActionResponse(new ScheduleClosePollFormCard(state, this.getUserTimezone()).create()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
newPollOnChange() { |
244
|
|
|
const formValues: PollFormInputs = this.event.common!.formInputs! as PollFormInputs; |
245
|
|
|
const config = getConfigFromInput(formValues); |
246
|
|
|
return createDialogActionResponse(new NewPollFormCard(config, this.getUserTimezone()).create()); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|