This commit is contained in:
syuilo 2017-10-07 06:43:36 +09:00
parent f5fe36825b
commit 1d4f9378ca
4 changed files with 105 additions and 9 deletions

View File

@ -3,6 +3,8 @@ import * as bcrypt from 'bcryptjs';
import User, { IUser } from '../models/user'; import User, { IUser } from '../models/user';
import getPostSummary from '../../common/get-post-summary.js';
export default class BotCore extends EventEmitter { export default class BotCore extends EventEmitter {
public user: IUser = null; public user: IUser = null;
@ -14,7 +16,7 @@ export default class BotCore extends EventEmitter {
this.user = user; this.user = user;
} }
private setContect(context: Context) { public setContext(context: Context) {
this.context = context; this.context = context;
this.emit('updated'); this.emit('updated');
@ -35,7 +37,7 @@ export default class BotCore extends EventEmitter {
public static import(data) { public static import(data) {
const core = new BotCore(); const core = new BotCore();
core.user = data.user ? data.user : null; 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; return core;
} }
@ -47,22 +49,74 @@ export default class BotCore extends EventEmitter {
switch (query) { switch (query) {
case 'ping': case 'ping':
return 'PONG'; return 'PONG';
case 'help':
case 'ヘルプ':
return 'コマンド一覧です:' +
'help: これです\n' +
'me: アカウント情報を見ます\n' +
'login, signin: サインインします\n' +
'logout, signout: サインアウトします\n' +
'post: 投稿します\n' +
'tl: タイムラインを見ます\n';
case 'me': case 'me':
return this.user ? `${this.user.name}としてサインインしています` : 'サインインしていません'; return this.user ? `${this.user.name}としてサインインしています` : 'サインインしていません';
case 'login':
case 'signin':
case 'ログイン': case 'ログイン':
case 'サインイン': case 'サインイン':
this.setContect(new SigninContext(this)); this.setContext(new SigninContext(this));
return await this.context.greet(); 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 'タイムライン':
return await this.getTl();
default: default:
return '?'; return '?';
} }
} }
public setUser(user: IUser) { public signin(user: IUser) {
this.user = user; this.user = user;
this.emit('set-user', user); this.emit('signin', user);
this.emit('updated'); 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 { abstract class Context extends EventEmitter {
@ -78,6 +132,7 @@ abstract class Context extends EventEmitter {
} }
public static import(core: BotCore, data: any) { 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); if (data.type == 'signin') return SigninContext.import(core, data.content);
return null; return null;
} }
@ -114,7 +169,8 @@ class SigninContext extends Context {
const same = bcrypt.compareSync(query, this.temporaryUser.password); const same = bcrypt.compareSync(query, this.temporaryUser.password);
if (same) { if (same) {
this.core.setUser(this.temporaryUser); this.core.signin(this.temporaryUser);
this.core.setContext(null);
return `${this.temporaryUser.name}さん、おかえりなさい!`; return `${this.temporaryUser.name}さん、おかえりなさい!`;
} else { } else {
return `パスワードが違います... もう一度教えてください:`; return `パスワードが違います... もう一度教えてください:`;
@ -125,7 +181,9 @@ class SigninContext extends Context {
public export() { public export() {
return { return {
type: 'signin', type: 'signin',
content: {
temporaryUser: this.temporaryUser temporaryUser: this.temporaryUser
}
}; };
} }
@ -135,3 +193,28 @@ class SigninContext extends Context {
return context; 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.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;
}
}

View File

@ -33,7 +33,8 @@ module.exports = async (app: express.Application) => {
}); });
session = new BotCore(user); session = new BotCore(user);
session.on('set-user', user => {
session.on('signin', user => {
User.update(user._id, { User.update(user._id, {
$set: { $set: {
line: { 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())); redis.set(sessionId, JSON.stringify(session.export()));
} else { } else {
session = BotCore.import(JSON.parse(_session)); session = BotCore.import(JSON.parse(_session));

View File

@ -1,5 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"allowJs": true,
"noEmitOnError": false, "noEmitOnError": false,
"noImplicitAny": false, "noImplicitAny": false,
"noImplicitReturns": true, "noImplicitReturns": true,

View File

@ -1,5 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"allowJs": true,
"noEmitOnError": false, "noEmitOnError": false,
"noImplicitAny": false, "noImplicitAny": false,
"noImplicitReturns": true, "noImplicitReturns": true,