From 971d78a9577823f718330f7a8f0d926cbb43e9af Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 24 Jan 2019 09:34:03 +0900 Subject: [PATCH] Refactoring --- src/ai.ts | 21 ++++++++++++++++++--- src/modules/guessing-game/index.ts | 5 +---- src/modules/kazutori/index.ts | 3 +-- src/modules/keyword/index.ts | 5 +---- src/utils/get-collection.ts | 13 ------------- 5 files changed, 21 insertions(+), 26 deletions(-) delete mode 100644 src/utils/get-collection.ts diff --git a/src/ai.ts b/src/ai.ts index b46db52..42e85b6 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -11,7 +11,6 @@ import Module from './module'; import Message from './message'; import { FriendDoc } from './friend'; import { User } from './misskey/user'; -import getCollection from './utils/get-collection'; import Stream from './stream'; import log from './utils/log'; @@ -83,11 +82,11 @@ export default class 藍 { @autobind private run() { //#region Init DB - this.contexts = getCollection(this.db, 'contexts', { + this.contexts = this.getCollection('contexts', { indices: ['key'] }); - this.friends = getCollection(this.db, 'friends', { + this.friends = this.getCollection('friends', { indices: ['userId'] }); //#endregion @@ -207,6 +206,22 @@ export default class 藍 { } } + /** + * データベースのコレクションを取得します + */ + @autobind + public getCollection(name: string, opts?: any): loki.Collection { + let collection: loki.Collection; + + collection = this.db.getCollection(name); + + if (collection == null) { + collection = this.db.addCollection(name, opts); + } + + return collection; + } + /** * 投稿します */ diff --git a/src/modules/guessing-game/index.ts b/src/modules/guessing-game/index.ts index 805290e..e6d386a 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -3,7 +3,6 @@ import * as loki from 'lokijs'; import Module from '../../module'; import Message from '../../message'; import serifs from '../../serifs'; -import getCollection from '../../utils/get-collection'; export default class extends Module { public readonly name = 'guessingGame'; @@ -19,11 +18,9 @@ export default class extends Module { @autobind public install() { - //#region Init DB - this.guesses = getCollection(this.ai.db, 'guessingGame', { + this.guesses = this.ai.getCollection('guessingGame', { indices: ['userId'] }); - //#endregion return { mentionHook: this.mentionHook, diff --git a/src/modules/kazutori/index.ts b/src/modules/kazutori/index.ts index cb3a7ac..0802f01 100644 --- a/src/modules/kazutori/index.ts +++ b/src/modules/kazutori/index.ts @@ -3,7 +3,6 @@ import * as loki from 'lokijs'; import Module from '../../module'; import Message from '../../message'; import serifs from '../../serifs'; -import getCollection from '../../utils/get-collection'; import { User } from '../../misskey/user'; type Game = { @@ -23,7 +22,7 @@ export default class extends Module { @autobind public install() { - this.games = getCollection(this.ai.db, 'kazutori'); + this.games = this.ai.getCollection('kazutori'); this.crawleGameEnd(); setInterval(this.crawleGameEnd, 1000); diff --git a/src/modules/keyword/index.ts b/src/modules/keyword/index.ts index 7f8bd40..ae44b99 100644 --- a/src/modules/keyword/index.ts +++ b/src/modules/keyword/index.ts @@ -3,7 +3,6 @@ import * as loki from 'lokijs'; import Module from '../../module'; import config from '../../config'; import serifs from '../../serifs'; -import getCollection from '../../utils/get-collection'; const MeCab = require('mecab-async'); function kanaToHira(str: string) { @@ -26,11 +25,9 @@ export default class extends Module { public install() { if (!config.keywordEnabled) return {}; - //#region Init DB - this.learnedKeywords = getCollection(this.ai.db, '_keyword_learnedKeywords', { + this.learnedKeywords = this.ai.getCollection('_keyword_learnedKeywords', { indices: ['userId'] }); - //#endregion this.tokenizer = new MeCab(); this.tokenizer.command = config.mecab; diff --git a/src/utils/get-collection.ts b/src/utils/get-collection.ts deleted file mode 100644 index 90ce705..0000000 --- a/src/utils/get-collection.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as loki from 'lokijs'; - -export default function(db: loki, name: string, opts?: any): loki.Collection { - let collection; - - collection = db.getCollection(name); - - if (collection == null) { - collection = db.addCollection(name, opts); - } - - return collection; -}