forked from sampaiodiego/rocket.chat.app-poll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPollApp.ts
202 lines (176 loc) · 7.34 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
import {
IConfigurationExtend,
IHttp,
IModify,
IPersistence,
IRead,
} from '@rocket.chat/apps-engine/definition/accessors';
import {App} from '@rocket.chat/apps-engine/definition/App';
import {SettingType} from '@rocket.chat/apps-engine/definition/settings';
import {
IUIKitInteractionHandler,
UIKitBlockInteractionContext,
UIKitViewSubmitInteractionContext,
} from '@rocket.chat/apps-engine/definition/uikit';
import {createPollMessage} from './src/lib/createPollMessage';
import {createPollModal} from './src/lib/createPollModal';
import {finishPollMessage} from './src/lib/finishPollMessage';
import {votePoll} from './src/lib/votePoll';
import {CreatePollCommand} from "./src/CreatePollCommand";
import {getPoll} from "./src/lib/getPoll";
import {IJobContext} from "@rocket.chat/apps-engine/definition/scheduler";
export class PollApp extends App implements IUIKitInteractionHandler {
public async executeViewSubmitHandler(context: UIKitViewSubmitInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify) {
const data = context.getInteractionData();
const {state}: {
state: {
poll: {
question: string,
[option: string]: string,
},
config?: {
mode?: string,
visibility?: string,
showResults?: string,
},
},
} = data.view as any;
if (!state) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: {
question: 'Error creating poll',
},
});
}
try {
await createPollMessage(data, read, modify, persistence, data.user.id);
} catch (err) {
return context.getInteractionResponder().viewErrorResponse({
viewId: data.view.id,
errors: err,
});
}
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': {
const modal = await createPollModal({
id: data.container.id,
data,
persistence,
modify,
options: parseInt(String(data.value), 10)
});
return context.getInteractionResponder().updateModalViewResponse(modal);
}
case "overflow":
switch (data.value) {
case "finish":
try {
await finishPollMessage({data, read, persistence, modify});
} 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);
}
await modify.getNotifier().notifyUser(
context.getInteractionData().user,
errorMessage.getMessage(),
);
}
break;
case "not-voted":
const {room} = context.getInteractionData();
if (room === undefined) {
throw new Error('Unexpected error');
}
const id = data.message?.id
if (id === undefined) {
throw new Error('Unexpected error');
}
const poll = await getPoll(id, read);
const userIds = (room.userIds || new Array<string>());
userIds.push(context.getInteractionData().user.id)
const notVoted = await Promise.all(userIds.filter(id => {
return poll.votes.filter(vote => {
return vote.voters.filter(person => person.id === id).length > 0
}).length === 0;
}).map(async id => await read.getUserReader().getById(id).then(user => "@" + user.username)));
if (notVoted.length === 0) {
const builder = modify.getCreator()
.startMessage()
.setText("Everybody has voted")
.setParseUrls(true)
.setRoom(room)
await modify.getCreator().finish(builder)
} else {
const builder = modify.getCreator()
.startMessage()
.setText(notVoted.join(", ") + " didn't vote")
.setParseUrls(true)
.setRoom(room)
await modify.getCreator().finish(builder)
}
}
}
return {
success: true,
triggerId: data.triggerId,
};
}
public async initialize(configuration: IConfigurationExtend): Promise<void> {
await configuration.slashCommands.provideSlashCommand(new CreatePollCommand());
await configuration.settings.provideSetting({
id: 'use-user-name',
i18nLabel: 'Use name attribute to display voters, instead of username',
i18nDescription: 'When checked, display voters as full user names instead of username',
required: false,
type: SettingType.BOOLEAN,
public: true,
packageValue: false,
});
await configuration.scheduler.registerProcessors([
{
id: 'poll_timeout',
processor: this.processor,
},
]);
}
private async processor(jobContext: IJobContext, read: IRead, modify: IModify, http: IHttp, persistence: IPersistence) {
await finishPollMessage({
data: {
message: {
id: jobContext.poll.msgId,
},
user: {
id: jobContext.poll.uid,
}
},
read,
persistence,
modify
})
}
}