diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts index 4109519ca5..cf2bdef1dc 100644 --- a/src/api/bot/core.ts +++ b/src/api/bot/core.ts @@ -3,6 +3,8 @@ import * as bcrypt from 'bcryptjs'; import User, { IUser } from '../models/user'; +import getPostSummary from '../../common/get-post-summary.js'; + export default class BotCore extends EventEmitter { public user: IUser = null; @@ -14,7 +16,7 @@ export default class BotCore extends EventEmitter { this.user = user; } - private setContect(context: Context) { + public setContext(context: Context) { this.context = context; this.emit('updated'); @@ -35,7 +37,7 @@ export default class BotCore extends EventEmitter { public static import(data) { const core = new BotCore(); core.user = data.user ? data.user : null; - core.setContect(data.context ? Context.import(core, data.context) : null); + core.setContext(data.context ? Context.import(core, data.context) : null); return core; } @@ -47,22 +49,74 @@ export default class BotCore extends EventEmitter { switch (query) { case 'ping': return 'PONG'; + + case 'help': + case 'ヘルプ': + return 'コマンド一覧です:' + + 'help: これです\n' + + 'me: アカウント情報を見ます\n' + + 'login, signin: サインインします\n' + + 'logout, signout: サインアウトします\n' + + 'post: 投稿します\n' + + 'tl: タイムラインを見ます\n'; + case 'me': return this.user ? `${this.user.name}としてサインインしています` : 'サインインしていません'; + + case 'login': + case 'signin': case 'ログイン': case 'サインイン': - this.setContect(new SigninContext(this)); + this.setContext(new SigninContext(this)); return await this.context.greet(); - default: + + 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 'タイムライン': + return await this.getTl(); + + default: return '?'; } } - public setUser(user: IUser) { + public signin(user: IUser) { this.user = user; - this.emit('set-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 getTl() { + if (this.user == null) return 'まずサインインしてください。'; + + const tl = await require('../endpoints/posts/timeline')({}, this.user); + + const text = tl + .map(post => getPostSummary(post)) + .join('\n-----\n'); + + return text; + } } abstract class Context extends EventEmitter { @@ -78,6 +132,7 @@ abstract class Context extends EventEmitter { } public static import(core: BotCore, data: any) { + if (data.type == 'post') return PostContext.import(core, data.content); if (data.type == 'signin') return SigninContext.import(core, data.content); return null; } @@ -114,7 +169,8 @@ class SigninContext extends Context { const same = bcrypt.compareSync(query, this.temporaryUser.password); if (same) { - this.core.setUser(this.temporaryUser); + this.core.signin(this.temporaryUser); + this.core.setContext(null); return `${this.temporaryUser.name}さん、おかえりなさい!`; } else { return `パスワードが違います... もう一度教えてください:`; @@ -125,7 +181,9 @@ class SigninContext extends Context { public export() { return { type: 'signin', - temporaryUser: this.temporaryUser + content: { + temporaryUser: this.temporaryUser + } }; } @@ -135,3 +193,28 @@ class SigninContext extends Context { return context; } } + +class PostContext extends Context { + public async greet(): Promise { + return '内容:'; + } + + public async q(query: string): Promise { + await require('../endpoints/posts/create')({ + text: query + }, this.core.user); + this.core.setContext(null); + return '投稿しましたよ!'; + } + + public export() { + return { + type: 'post' + }; + } + + public static import(core: BotCore, data: any) { + const context = new PostContext(core); + return context; + } +} diff --git a/src/api/bot/interfaces/line.ts b/src/api/bot/interfaces/line.ts index 52559eaeff..437f29cb3c 100644 --- a/src/api/bot/interfaces/line.ts +++ b/src/api/bot/interfaces/line.ts @@ -33,7 +33,8 @@ module.exports = async (app: express.Application) => { }); session = new BotCore(user); - session.on('set-user', user => { + + session.on('signin', user => { User.update(user._id, { $set: { line: { @@ -43,6 +44,16 @@ module.exports = async (app: express.Application) => { }); }); + session.on('signout', user => { + User.update(user._id, { + $set: { + line: { + user_id: null + } + } + }); + }); + redis.set(sessionId, JSON.stringify(session.export())); } else { session = BotCore.import(JSON.parse(_session)); diff --git a/src/tsconfig.json b/src/tsconfig.json index ecff047a74..36600eed2b 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "allowJs": true, "noEmitOnError": false, "noImplicitAny": false, "noImplicitReturns": true, diff --git a/tsconfig.json b/tsconfig.json index 064a04e4d2..a38ff220b2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "allowJs": true, "noEmitOnError": false, "noImplicitAny": false, "noImplicitReturns": true,