summaryrefslogtreecommitdiff
path: root/src/api/bot/core.ts
blob: d6706e9a1c787311565fe6490893300c1f65ab70 (plain)
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
import * as EventEmitter from 'events';
import * as bcrypt from 'bcryptjs';

import User, { ILocalAccount, IUser, init as initUser } from '../models/user';

import getPostSummary from '../../common/get-post-summary';
import getUserSummary from '../../common/user/get-summary';
import parseAcct from '../../common/user/parse-acct';
import getNotificationSummary from '../../common/get-notification-summary';

const hmm = [
	'?',
	'ふぅ~む...?',
	'ちょっと何言ってるかわからないです',
	'「ヘルプ」と言うと利用可能な操作が確認できますよ'
];

/**
 * Botの頭脳
 */
export default class BotCore extends EventEmitter {
	public user: IUser = null;

	private context: Context = null;

	constructor(user?: IUser) {
		super();

		this.user = user;
	}

	public clearContext() {
		this.setContext(null);
	}

	public setContext(context: Context) {
		this.context = context;
		this.emit('updated');

		if (context) {
			context.on('updated', () => {
				this.emit('updated');
			});
		}
	}

	public export() {
		return {
			user: this.user,
			context: this.context ? this.context.export() : null
		};
	}

	protected _import(data) {
		this.user = data.user ? initUser(data.user) : null;
		this.setContext(data.context ? Context.import(this, data.context) : null);
	}

	public static import(data) {
		const bot = new BotCore();
		bot._import(data);
		return bot;
	}

	public async q(query: string): Promise<string> {
		if (this.context != null) {
			return await this.context.q(query);
		}

		if (/^@[a-zA-Z0-9_]+$/.test(query)) {
			return await this.showUserCommand(query);
		}

		switch (query) {
			case 'ping':
				return 'PONG';

			case 'help':
			case 'ヘルプ':
				return '利用可能なコマンド一覧です:\n' +
					'help: これです\n' +
					'me: アカウント情報を見ます\n' +
					'login, signin: サインインします\n' +
					'logout, signout: サインアウトします\n' +
					'post: 投稿します\n' +
					'tl: タイムラインを見ます\n' +
					'no: 通知を見ます\n' +
					'@<ユーザー名>: ユーザーを表示します\n' +
					'\n' +
					'タイムラインや通知を見た後、「次」というとさらに遡ることができます。';

			case 'me':
				return this.user ? `${this.user.name}としてサインインしています。\n\n${getUserSummary(this.user)}` : 'サインインしていません';

			case 'login':
			case 'signin':
			case 'ログイン':
			case 'サインイン':
				if (this.user != null) return '既にサインインしていますよ!';
				this.setContext(new SigninContext(this));
				return await this.context.greet();

			case 'logout':
			case 'signout':
			case 'ログアウト':
			case 'サインアウト':
				if (this.user == null) return '今はサインインしてないですよ!';
				this.signout();
				return 'ご利用ありがとうございました <3';

			case 'post':
			case '投稿':
				if (this.user == null) return 'まずサインインしてください。';
				this.setContext(new PostContext(this));
				return await this.context.greet();

			case 'tl':
			case 'タイムライン':
				if (this.user == null) return 'まずサインインしてください。';
				this.setContext(new TlContext(this));
				return await this.context.greet();

			case 'no':
			case 'notifications':
			case '通知':
				if (this.user == null) return 'まずサインインしてください。';
				this.setContext(new NotificationsContext(this));
				return await this.context.greet();

			case 'guessing-game':
			case '数当てゲーム':
				this.setContext(new GuessingGameContext(this));
				return await this.context.greet();

			default:
				return hmm[Math.floor(Math.random() * hmm.length)];
		}
	}

	public signin(user: IUser) {
		this.user = user;
		this.emit('signin', user);
		this.emit('updated');
	}

	public signout() {
		const user = this.user;
		this.user = null;
		this.emit('signout', user);
		this.emit('updated');
	}

	public async refreshUser() {
		this.user = await User.findOne({
			_id: this.user._id
		}, {
			fields: {
				data: false
			}
		});

		this.emit('updated');
	}

	public async showUserCommand(q: string): Promise<string> {
		try {
			const user = await require('../endpoints/users/show')(parseAcct(q.substr(1)), this.user);

			const text = getUserSummary(user);

			return text;
		} catch (e) {
			return `問題が発生したようです...: ${e}`;
		}
	}
}

abstract class Context extends EventEmitter {
	protected bot: BotCore;

	public abstract async greet(): Promise<string>;
	public abstract async q(query: string): Promise<string>;
	public abstract export(): any;

	constructor(bot: BotCore) {
		super();
		this.bot = bot;
	}

	public static import(bot: BotCore, data: any) {
		if (data.type == 'guessing-game') return GuessingGameContext.import(bot, data.content);
		if (data.type == 'post') return PostContext.import(bot, data.content);
		if (data.type == 'tl') return TlContext.import(bot, data.content);
		if (data.type == 'notifications') return NotificationsContext.import(bot, data.content);
		if (data.type == 'signin') return SigninContext.import(bot, data.content);
		return null;
	}
}

class SigninContext extends Context {
	private temporaryUser: IUser = null;

	public async greet(): Promise<string> {
		return 'まずユーザー名を教えてください:';
	}

	public async q(query: string): Promise<string> {
		if (this.temporaryUser == null) {
			// Fetch user
			const user: IUser = await User.findOne({
				username_lower: query.toLowerCase(),
				host: null
			}, {
				fields: {
					data: false
				}
			});

			if (user === null) {
				return `${query}というユーザーは存在しませんでした... もう一度教えてください:`;
			} else {
				this.temporaryUser = user;
				this.emit('updated');
				return `パスワードを教えてください:`;
			}
		} else {
			// Compare password
			const same = await bcrypt.compare(query, (this.temporaryUser.account as ILocalAccount).password);

			if (same) {
				this.bot.signin(this.temporaryUser);
				this.bot.clearContext();
				return `${this.temporaryUser.name}さん、おかえりなさい!`;
			} else {
				return `パスワードが違います... もう一度教えてください:`;
			}
		}
	}

	public export() {
		return {
			type: 'signin',
			content: {
				temporaryUser: this.temporaryUser
			}
		};
	}

	public static import(bot: BotCore, data: any) {
		const context = new SigninContext(bot);
		context.temporaryUser = data.temporaryUser;
		return context;
	}
}

class PostContext extends Context {
	public async greet(): Promise<string> {
		return '内容:';
	}

	public async q(query: string): Promise<string> {
		await require('../endpoints/posts/create')({
			text: query
		}, this.bot.user);
		this.bot.clearContext();
		return '投稿しましたよ!';
	}

	public export() {
		return {
			type: 'post'
		};
	}

	public static import(bot: BotCore, data: any) {
		const context = new PostContext(bot);
		return context;
	}
}

class TlContext extends Context {
	private next: string = null;

	public async greet(): Promise<string> {
		return await this.getTl();
	}

	public async q(query: string): Promise<string> {
		if (query == '次') {
			return await this.getTl();
		} else {
			this.bot.clearContext();
			return await this.bot.q(query);
		}
	}

	private async getTl() {
		const tl = await require('../endpoints/posts/timeline')({
			limit: 5,
			until_id: this.next ? this.next : undefined
		}, this.bot.user);

		if (tl.length > 0) {
			this.next = tl[tl.length - 1].id;
			this.emit('updated');

			const text = tl
				.map(post => `${post.user.name}\n「${getPostSummary(post)}」`)
				.join('\n-----\n');

			return text;
		} else {
			return 'タイムラインに表示するものがありません...';
		}
	}

	public export() {
		return {
			type: 'tl',
			content: {
				next: this.next,
			}
		};
	}

	public static import(bot: BotCore, data: any) {
		const context = new TlContext(bot);
		context.next = data.next;
		return context;
	}
}

class NotificationsContext extends Context {
	private next: string = null;

	public async greet(): Promise<string> {
		return await this.getNotifications();
	}

	public async q(query: string): Promise<string> {
		if (query == '次') {
			return await this.getNotifications();
		} else {
			this.bot.clearContext();
			return await this.bot.q(query);
		}
	}

	private async getNotifications() {
		const notifications = await require('../endpoints/i/notifications')({
			limit: 5,
			until_id: this.next ? this.next : undefined
		}, this.bot.user);

		if (notifications.length > 0) {
			this.next = notifications[notifications.length - 1].id;
			this.emit('updated');

			const text = notifications
				.map(notification => getNotificationSummary(notification))
				.join('\n-----\n');

			return text;
		} else {
			return '通知はありません';
		}
	}

	public export() {
		return {
			type: 'notifications',
			content: {
				next: this.next,
			}
		};
	}

	public static import(bot: BotCore, data: any) {
		const context = new NotificationsContext(bot);
		context.next = data.next;
		return context;
	}
}

class GuessingGameContext extends Context {
	private secret: number;
	private history: number[] = [];

	public async greet(): Promise<string> {
		this.secret = Math.floor(Math.random() * 100);
		this.emit('updated');
		return '0~100の秘密の数を当ててみてください:';
	}

	public async q(query: string): Promise<string> {
		if (query == 'やめる') {
			this.bot.clearContext();
			return 'やめました。';
		}

		const guess = parseInt(query, 10);

		if (isNaN(guess)) {
			return '整数で推測してください。「やめる」と言うとゲームをやめます。';
		}

		const firsttime = this.history.indexOf(guess) === -1;

		this.history.push(guess);
		this.emit('updated');

		if (this.secret < guess) {
			return firsttime ? `${guess}よりも小さいですね` : `もう一度言いますが${guess}より小さいですよ`;
		} else if (this.secret > guess) {
			return firsttime ? `${guess}よりも大きいですね` : `もう一度言いますが${guess}より大きいですよ`;
		} else {
			this.bot.clearContext();
			return `正解です🎉 (${this.history.length}回目で当てました)`;
		}
	}

	public export() {
		return {
			type: 'guessing-game',
			content: {
				secret: this.secret,
				history: this.history
			}
		};
	}

	public static import(bot: BotCore, data: any) {
		const context = new GuessingGameContext(bot);
		context.secret = data.secret;
		context.history = data.history;
		return context;
	}
}