2017-10-07 03:36:46 +09:00
|
|
|
import * as EventEmitter from 'events';
|
|
|
|
import * as bcrypt from 'bcryptjs';
|
|
|
|
|
2017-10-07 07:23:00 +09:00
|
|
|
import User, { IUser, init as initUser } from '../models/user';
|
2017-10-07 03:36:46 +09:00
|
|
|
|
2017-10-07 06:58:50 +09:00
|
|
|
import getPostSummary from '../../common/get-post-summary';
|
2017-10-07 06:43:36 +09:00
|
|
|
|
2017-10-07 03:36:46 +09:00
|
|
|
export default class BotCore extends EventEmitter {
|
2017-10-07 04:30:57 +09:00
|
|
|
public user: IUser = null;
|
2017-10-07 03:36:46 +09:00
|
|
|
|
|
|
|
private context: Context = null;
|
|
|
|
|
2017-10-07 04:30:57 +09:00
|
|
|
constructor(user?: IUser) {
|
2017-10-07 03:36:46 +09:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.user = user;
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:43:36 +09:00
|
|
|
public setContext(context: Context) {
|
2017-10-07 05:50:01 +09:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static import(data) {
|
|
|
|
const core = new BotCore();
|
2017-10-07 07:23:00 +09:00
|
|
|
core.user = data.user ? initUser(data.user) : null;
|
2017-10-07 06:43:36 +09:00
|
|
|
core.setContext(data.context ? Context.import(core, data.context) : null);
|
2017-10-07 05:50:01 +09:00
|
|
|
return core;
|
|
|
|
}
|
|
|
|
|
2017-10-07 03:36:46 +09:00
|
|
|
public async q(query: string): Promise<string> {
|
|
|
|
if (this.context != null) {
|
|
|
|
return await this.context.q(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (query) {
|
|
|
|
case 'ping':
|
|
|
|
return 'PONG';
|
2017-10-07 06:43:36 +09:00
|
|
|
|
|
|
|
case 'help':
|
|
|
|
case 'ヘルプ':
|
2017-10-07 07:23:00 +09:00
|
|
|
return 'コマンド一覧です:\n' +
|
2017-10-07 06:43:36 +09:00
|
|
|
'help: これです\n' +
|
|
|
|
'me: アカウント情報を見ます\n' +
|
|
|
|
'login, signin: サインインします\n' +
|
|
|
|
'logout, signout: サインアウトします\n' +
|
|
|
|
'post: 投稿します\n' +
|
|
|
|
'tl: タイムラインを見ます\n';
|
|
|
|
|
2017-10-07 05:50:01 +09:00
|
|
|
case 'me':
|
|
|
|
return this.user ? `${this.user.name}としてサインインしています` : 'サインインしていません';
|
2017-10-07 06:43:36 +09:00
|
|
|
|
|
|
|
case 'login':
|
|
|
|
case 'signin':
|
2017-10-07 03:36:46 +09:00
|
|
|
case 'ログイン':
|
|
|
|
case 'サインイン':
|
2017-10-07 06:43:36 +09:00
|
|
|
this.setContext(new SigninContext(this));
|
2017-10-07 03:36:46 +09:00
|
|
|
return await this.context.greet();
|
2017-10-07 06:43:36 +09:00
|
|
|
|
|
|
|
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:
|
2017-10-07 03:36:46 +09:00
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:43:36 +09:00
|
|
|
public signin(user: IUser) {
|
2017-10-07 03:36:46 +09:00
|
|
|
this.user = user;
|
2017-10-07 06:43:36 +09:00
|
|
|
this.emit('signin', user);
|
2017-10-07 05:50:01 +09:00
|
|
|
this.emit('updated');
|
2017-10-07 03:36:46 +09:00
|
|
|
}
|
2017-10-07 06:43:36 +09:00
|
|
|
|
|
|
|
public signout() {
|
|
|
|
const user = this.user;
|
|
|
|
this.user = null;
|
|
|
|
this.emit('signout', user);
|
|
|
|
this.emit('updated');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getTl() {
|
|
|
|
if (this.user == null) return 'まずサインインしてください。';
|
|
|
|
|
2017-10-07 06:58:50 +09:00
|
|
|
const tl = await require('../endpoints/posts/timeline')({
|
|
|
|
limit: 5
|
|
|
|
}, this.user);
|
2017-10-07 06:43:36 +09:00
|
|
|
|
|
|
|
const text = tl
|
|
|
|
.map(post => getPostSummary(post))
|
|
|
|
.join('\n-----\n');
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
2017-10-07 03:36:46 +09:00
|
|
|
}
|
|
|
|
|
2017-10-07 05:50:01 +09:00
|
|
|
abstract class Context extends EventEmitter {
|
2017-10-07 03:36:46 +09:00
|
|
|
protected core: BotCore;
|
|
|
|
|
|
|
|
public abstract async greet(): Promise<string>;
|
|
|
|
public abstract async q(query: string): Promise<string>;
|
2017-10-07 05:50:01 +09:00
|
|
|
public abstract export(): any;
|
2017-10-07 03:36:46 +09:00
|
|
|
|
|
|
|
constructor(core: BotCore) {
|
2017-10-07 05:50:01 +09:00
|
|
|
super();
|
2017-10-07 03:36:46 +09:00
|
|
|
this.core = core;
|
|
|
|
}
|
2017-10-07 05:50:01 +09:00
|
|
|
|
|
|
|
public static import(core: BotCore, data: any) {
|
2017-10-07 06:43:36 +09:00
|
|
|
if (data.type == 'post') return PostContext.import(core, data.content);
|
2017-10-07 05:50:01 +09:00
|
|
|
if (data.type == 'signin') return SigninContext.import(core, data.content);
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-07 03:36:46 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
class SigninContext extends Context {
|
2017-10-07 06:03:16 +09:00
|
|
|
private temporaryUser: IUser = null;
|
2017-10-07 03:36:46 +09:00
|
|
|
|
|
|
|
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()
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
data: false,
|
|
|
|
profile: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
return `${query}というユーザーは存在しませんでした... もう一度教えてください:`;
|
|
|
|
} else {
|
|
|
|
this.temporaryUser = user;
|
2017-10-07 05:50:01 +09:00
|
|
|
this.emit('updated');
|
2017-10-07 03:36:46 +09:00
|
|
|
return `パスワードを教えてください:`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Compare password
|
|
|
|
const same = bcrypt.compareSync(query, this.temporaryUser.password);
|
|
|
|
|
|
|
|
if (same) {
|
2017-10-07 06:43:36 +09:00
|
|
|
this.core.signin(this.temporaryUser);
|
|
|
|
this.core.setContext(null);
|
2017-10-07 03:36:46 +09:00
|
|
|
return `${this.temporaryUser.name}さん、おかえりなさい!`;
|
|
|
|
} else {
|
|
|
|
return `パスワードが違います... もう一度教えてください:`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 05:50:01 +09:00
|
|
|
|
|
|
|
public export() {
|
|
|
|
return {
|
2017-10-07 06:03:16 +09:00
|
|
|
type: 'signin',
|
2017-10-07 06:43:36 +09:00
|
|
|
content: {
|
|
|
|
temporaryUser: this.temporaryUser
|
|
|
|
}
|
2017-10-07 05:50:01 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static import(core: BotCore, data: any) {
|
|
|
|
const context = new SigninContext(core);
|
|
|
|
context.temporaryUser = data.temporaryUser;
|
|
|
|
return context;
|
|
|
|
}
|
2017-10-07 03:36:46 +09:00
|
|
|
}
|
2017-10-07 06:43:36 +09:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|