-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.js
370 lines (311 loc) · 12 KB
/
command.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
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
function doPost(e) {
const props = PropertiesService.getScriptProperties();
const propToken = props.getProperty('TOKEN');
const propId = props.getProperty('SPREADSHEET_ID');
// TODO: hmac による検証のほうが推奨らしいのでいつか書き換える
const verificationToken = e.parameter.token
if (verificationToken !== propToken) {
throw new Error('Invalid token error');
}
const help = `
/kzlt entry 'LTタイトル' -- 自分のLTを登録する
/kzlt my -- 自分のエントリしたLTを自分にだけ表示する
/kzlt list -- エントリー済みのLTをchannelに出力する(順番を決めたものを除く)
/kzlt all -- エントリー済みのLTを出力する(順番決めた/決めてない関係なく)
/kzlt shuffle -- 順番を決め、channelに出力する (次のshuffleに出てこない)
/kzlt reset -- 順番決めたものすべてを順番決めていないことにする
/kzlt remove 'エントリ番号' -- エントリ時に返ってきた番号を指定してエントリを削除する
/kzlt delimit -- 一旦区切ってすでに順番を決めたエントリをshuffle対象外とする
※ delimit 後に shuffle することで、前回 shuffle 後にエントリしたものだけで shuffle できます
`;
const argText = e.parameter.text;
if (!argText) {
return ContentService.createTextOutput(help);
}
const spreadsheet = SpreadsheetApp.openById(propId);
if (!spreadsheet) {
throw new Error('Spreadsheet: kzrb-LT-entries is not found');
}
const idx = argText.search(/\s+/);
const cmd = argText.slice(0, idx == -1 ? argText.length : idx);
const sheetName = e.parameter.channel_name;
const startRowNum = 2;
const startColNum = 2;
const maxRowSize = 100; // TODO: さぽって 100 行に限定してる
const status = { ORDERED: 'ordered', UNORDERED: 'unordered', REMOVED: 'removed', DELIMITED: 'delimited' };
const index = { DATE: 0, NAME: 1, TITLE: 2, STATUS: 3 };
const messages = {
no_entry: "エントリーはありません",
reset_order: "すべてのエントリーを順番を決めてない状態に戻しました",
full_entry: "満席です",
delimit_time: "ここまで順番を決めたエントリは発表済みとみなします"
}
// なければ sheet を作る
const sheet = function(name) {
const targetSheet = spreadsheet.getSheetByName(name);
return targetSheet ? targetSheet : spreadsheet.insertSheet(name);
}(sheetName);
console.log({ cmd: cmd, user_name: e.parameter.user_name });
switch (cmd) {
case 'entry': { // エントリする
const current = new Date().toLocaleString();
const userName = e.parameter.user_name;
const title = argText.slice(idx + 1, argText.length).trim();
const entryLine = [current, userName, title, status.UNORDERED];
const entryValues = sheet.getRange(
startRowNum,
startColNum,
maxRowSize,
entryLine.length,
).getValues();
for (let row = 0; row < maxRowSize; row++) {
if (!entryValues[row][index.DATE]) {
sheet.getRange(
startRowNum + row,
startColNum,
1,
entryLine.length,
).setValues([entryLine]);
SpreadsheetApp.flush()
console.log({ cmd: cmd, user_name: e.parameter.user_name, title: title, argText: argText });
const payload = createMessagePayload(
`${userName} さんから LT: 「${title}」のエントリがありました。entryId: ${startRowNum + row}`
);
return createPublicTextOutput(payload);
}
}
return ContentService.createTextOutput(messages.full_entry);
}
case 'remove': { // 番号指定でエントリを削除扱いにする
const entryId = argText.slice(idx + 1, argText.length).trim();
if (Number(entryId) === 0 || Number.isNaN === Number(entryId) || typeof (Number(entryId)) !== 'number') {
return ContentService.createTextOutput('entry 時に返ってきた entryId を指定してください /kzlt remove 1');
}
const targetRowNum = Number(entryId);
const entry = sheet.getRange(targetRowNum, startColNum, 1, 4).getValues()[0];
if (entry[index.NAME] !== e.parameter.user_name) {
return ContentService.createTextOutput(`entry が自身のものではありません。 ${entry[index.NAME]}`);
}
sheet.getRange(targetRowNum, startColNum + index.STATUS).setValue(status.REMOVED);
console.log({ cmd: cmd, user_name: e.parameter.user_name, entryId: entryId });
const payload = createMessagePayload(
`LT title: ${entry[index.TITLE]} のエントリが取り消されました。`
);
return createPublicTextOutput(payload);
}
case 'my': { // 自分がエントリしたものを出力する
const entries = sheet.getRange(
startRowNum,
startColNum,
maxRowSize,
4,
).getValues();
const userName = e.parameter.user_name;
let text = `${userName}のエントリー\n`;
let entryCount = 0;
for (let i = 0; i < maxRowSize; i++) {
if (!entries[i][index.DATE]) break;
if (entries[i][index.NAME] !== userName) continue;
if (entries[i][index.STATUS] === status.REMOVED) continue;
const title = entries[i][index.TITLE];
text += `- ${title}, entryId: ${startRowNum + i}\n`;
entryCount++;
}
if (entryCount === 0) text = messages.no_entry;
console.log({ cmd: cmd, user_name: e.parameter.user_name, text: text });
return ContentService.createTextOutput(text);
}
case 'list': { // shuffle されていないエントリを出力する
const entries = sheet.getRange(
startRowNum,
startColNum,
maxRowSize,
4,
).getValues();
let text = "現在までのエントリー\n";
let entryCount = 0;
for (let i = 0; i < maxRowSize; i++) {
if (!entries[i][index.DATE]) break;
if (entries[i][index.STATUS] !== status.UNORDERED) continue;
const name = entries[i][index.NAME];
const title = entries[i][index.TITLE];
text += `- ${title} by ${name}, entryId: ${startRowNum + i}\n`;
entryCount++;
}
console.log({ cmd: cmd, user_name: e.parameter.user_name });
if (entryCount === 0) {
return ContentService.createTextOutput(messages.no_entry);
} else {
const payload = createMessagePayload(text);
return createPublicTextOutput(payload);
}
}
case 'all': { // 削除扱いのエントリ以外すべてを出力する
const entries = sheet.getRange(
startRowNum,
startColNum,
maxRowSize,
4,
).getValues();
let allText = "";
for (let i = 0; i < maxRowSize; i++) {
if (!entries[i][index.DATE]) break;
if (entries[i][index.STATUS] === status.REMOVED) continue
const entry = entries[i];
const badge = (entry[index.STATUS] === status.ORDERED || entry[index.STATUS] === status.DELIMITED) ? '[done]' : '';
allText += `- ${badge} ${entry[index.TITLE]} by ${entry[index.NAME]}, entryId: ${startRowNum + i}\n`;
}
if (!allText) allText = messages.no_entry;
console.log({ cmd: cmd, user_name: e.parameter.user_name, text: allText });
return ContentService.createTextOutput(allText);
}
case 'shuffle': { // shuffle されていないエントリをシャッフルする
const entries = sheet.getRange(
startRowNum,
startColNum,
maxRowSize,
4,
).getValues();
const container = [];
for (let i = 0; i < maxRowSize; i++) {
if (!entries[i][index.DATE]) break;
const entry = entries[i];
container.push(entry);
}
if (container.length === 0) {
return createPublicTextOutput(createMessagePayload(messages.no_entry));
}
// 並び替え対象としたものに印をつける
const statuses = container.map((v) => {
switch (v[index.STATUS]) {
case status.REMOVED: {
return [status.REMOVED];
}
case status.DELIMITED: {
return [status.DELIMITED];
}
default: {
return [status.ORDERED];
}
}
});
sheet.getRange(
startRowNum,
5,
container.length,
1,
).setValues([...statuses]);
SpreadsheetApp.flush()
// シャッフルした番号の配列をつくる
const orderNumbers = indexesNumbers(container.length);
// markdown を作り、レスポンスを返す
const mdText = makeMarkdown(orderNumbers, container, status, index);
console.log({ cmd: cmd, user_name: e.parameter.user_name, text: mdText });
const payload = createMessagePayload(mdText);
return createPublicTextOutput(payload);
}
case 'reset': { // すでにシャッフルされたエントリの状態をシャッフルされていない状態に戻す
const entries = sheet.getRange(
startRowNum,
startColNum,
startRowNum + maxRowSize,
4,
).getValues();
const container = [];
for (let i = 0; i < maxRowSize; i++) {
if (!entries[i][index.DATE]) break;
container.push(entries[i]);
}
const values = container.map((v) => {
switch (v[index.STATUS]) {
case status.REMOVED: {
return [status.REMOVED];
}
case status.DELIMITED: {
return [status.DELIMITED];
}
default: {
return [status.UNORDERED];
}
}
});
sheet.getRange(
startRowNum,
5,
container.length,
1,
).setValues([...values]);
SpreadsheetApp.flush()
console.log({ cmd: cmd, user_name: e.parameter.user_name, text: message.reset_order });
return ContentService.createTextOutput(messages.reset_order);
}
case 'delimit': {
const entries = sheet.getRange(
startRowNum,
5,
maxRowSize,
1,
).getValues();
for (let i = 0; i < maxRowSize; i++) {
if (!entries[i][0]) break;
if (entries[i][0] !== status.ORDERED) continue;
sheet.getRange(
startRowNum + i,
5,
1,
).setValue(status.DELIMITED)
}
SpreadsheetApp.flush()
console.log({ cmd: cmd, user_name: e.parameter.user_name, text: messages.delimit_time });
const payload = createMessagePayload(messages.delimit_time);
return createPublicTextOutput(payload);
}
default:
console.log({ cmd: cmd, user_name: e.parameter.user_name });
return ContentService.createTextOutput(cmd + "\n" + help);
}
}
function shuffle([...ary]) {
for (let i = ary.length - 1; i >= 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[ary[i], ary[j]] = [ary[j], ary[i]];
}
return ary;
}
function indexesNumbers(num = 10) {
const nums = [...Array(num).keys()];
return shuffle(nums);
}
function makeMarkdown(orderNumbers, container, status, index) {
let count = 0;
let mdTable = "```\n"; // | タイトル | 時刻 | 時間 | 担当 |
let mdList = "";
for (const num of orderNumbers) {
const ary = container[num];
if (ary[index.STATUS] === status.REMOVED) continue;
if (ary[index.STATUS] === status.DELIMITED) continue;
count++;
mdTable += `| ${ary[index.TITLE]} | | | ${ary[index.NAME]} |\n`;
mdList += `- ${ary[index.TITLE]} by ${ary[index.NAME]}\n`;
if (count % 4 === 0) {
mdTable += `| 休憩 | | | |\n`;
mdList += `- 【休憩】`;
}
};
mdTable += "```";
return mdList + "\n" + mdTable;
}
function createPublicTextOutput(payload) {
const response = ContentService.createTextOutput();
response.setMimeType(ContentService.MimeType.JSON);
response.setContent(JSON.stringify(payload));
return response;
}
function createMessagePayload(text) {
return {
response_type: "in_channel",
text: text,
username: "kzrb",
icon_url: "https://meetup.kzrb.org/images/logo_kzrb.png"
}
}