From d1f5d62251526c01b23e741ec363152cc21cc58c Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 7 Oct 2017 17:20:47 +0900 Subject: [PATCH] Refactor --- src/api/bot/core.ts | 18 ++++++++++-------- src/common/get-user-summary.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 src/common/get-user-summary.ts diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts index 5038665573..4970f24469 100644 --- a/src/api/bot/core.ts +++ b/src/api/bot/core.ts @@ -4,14 +4,12 @@ import * as bcrypt from 'bcryptjs'; import User, { IUser, init as initUser } from '../models/user'; import getPostSummary from '../../common/get-post-summary'; +import getUserSummary from '../../common/get-user-summary'; -function getUserSummary(user: IUser): string { - return `${user.name} (@${user.username})\n` + - `${user.posts_count}投稿、${user.following_count}フォロー、${user.followers_count}フォロワー\n` + - `場所: ${user.profile.location}、誕生日: ${user.profile.birthday}\n` + - `「${user.description}」`; -} +/** + * Botの頭脳 + */ export default class BotCore extends EventEmitter { public user: IUser = null; @@ -23,6 +21,10 @@ export default class BotCore extends EventEmitter { this.user = user; } + public clearContext() { + this.setContext(null); + } + public setContext(context: Context) { this.context = context; this.emit('updated'); @@ -178,7 +180,7 @@ class SigninContext extends Context { if (same) { this.core.signin(this.temporaryUser); - this.core.setContext(null); + this.core.clearContext(); return `${this.temporaryUser.name}さん、おかえりなさい!`; } else { return `パスワードが違います... もう一度教えてください:`; @@ -211,7 +213,7 @@ class PostContext extends Context { await require('../endpoints/posts/create')({ text: query }, this.core.user); - this.core.setContext(null); + this.core.clearContext(); return '投稿しましたよ!'; } diff --git a/src/common/get-user-summary.ts b/src/common/get-user-summary.ts new file mode 100644 index 0000000000..1bec2f9a26 --- /dev/null +++ b/src/common/get-user-summary.ts @@ -0,0 +1,12 @@ +import { IUser } from '../api/models/user'; + +/** + * ユーザーを表す文字列を取得します。 + * @param user ユーザー + */ +export default function(user: IUser): string { + return `${user.name} (@${user.username})\n` + + `${user.posts_count}投稿、${user.following_count}フォロー、${user.followers_count}フォロワー\n` + + `場所: ${user.profile.location}、誕生日: ${user.profile.birthday}\n` + + `「${user.description}」`; +}