diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-05-24 20:50:17 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-05-24 20:50:17 +0900 |
| commit | caeba1197c4b66e10614ffbddce0a2e98b0b5ef3 (patch) | |
| tree | 929fbbcc92b9e24baa203fdb2e025f9e3e7e94f0 /src | |
| parent | Use tabs in json (diff) | |
| download | sharkey-caeba1197c4b66e10614ffbddce0a2e98b0b5ef3.tar.gz sharkey-caeba1197c4b66e10614ffbddce0a2e98b0b5ef3.tar.bz2 sharkey-caeba1197c4b66e10614ffbddce0a2e98b0b5ef3.zip | |
Refactoring
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/endpoints/aggregation/posts/reaction.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/aggregation/posts/reply.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/aggregation/posts/repost.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/aggregation/users/activity.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/aggregation/users/post.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/aggregation/users/reaction.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/i/appdata/set.ts | 2 | ||||
| -rw-r--r-- | src/api/endpoints/posts/create.ts | 6 | ||||
| -rw-r--r-- | src/api/event.ts | 16 | ||||
| -rw-r--r-- | src/config.ts | 8 | ||||
| -rw-r--r-- | src/index.ts | 4 | ||||
| -rw-r--r-- | src/utils/dependencyInfo.ts | 6 | ||||
| -rw-r--r-- | src/utils/environmentInfo.ts | 5 | ||||
| -rw-r--r-- | src/utils/logger.ts | 30 | ||||
| -rw-r--r-- | src/utils/machineInfo.ts | 4 |
15 files changed, 46 insertions, 47 deletions
diff --git a/src/api/endpoints/aggregation/posts/reaction.ts b/src/api/endpoints/aggregation/posts/reaction.ts index e4ca680c33..eb99b9d088 100644 --- a/src/api/endpoints/aggregation/posts/reaction.ts +++ b/src/api/endpoints/aggregation/posts/reaction.ts @@ -52,7 +52,7 @@ module.exports = (params) => new Promise(async (res, rej) => { const graph = []; for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); + const day = new Date(new Date().setDate(new Date().getDate() - i)); const data = datas.filter(d => d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() diff --git a/src/api/endpoints/aggregation/posts/reply.ts b/src/api/endpoints/aggregation/posts/reply.ts index c7ba413311..02a60c8969 100644 --- a/src/api/endpoints/aggregation/posts/reply.ts +++ b/src/api/endpoints/aggregation/posts/reply.ts @@ -51,7 +51,7 @@ module.exports = (params) => new Promise(async (res, rej) => { const graph = []; for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); + const day = new Date(new Date().setDate(new Date().getDate() - i)); const data = datas.filter(d => d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() diff --git a/src/api/endpoints/aggregation/posts/repost.ts b/src/api/endpoints/aggregation/posts/repost.ts index 88d21243f8..217159caa7 100644 --- a/src/api/endpoints/aggregation/posts/repost.ts +++ b/src/api/endpoints/aggregation/posts/repost.ts @@ -51,7 +51,7 @@ module.exports = (params) => new Promise(async (res, rej) => { const graph = []; for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); + const day = new Date(new Date().setDate(new Date().getDate() - i)); const data = datas.filter(d => d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() diff --git a/src/api/endpoints/aggregation/users/activity.ts b/src/api/endpoints/aggregation/users/activity.ts index 6c5efff4b3..5d2ce995f7 100644 --- a/src/api/endpoints/aggregation/users/activity.ts +++ b/src/api/endpoints/aggregation/users/activity.ts @@ -86,7 +86,7 @@ module.exports = (params) => new Promise(async (res, rej) => { const graph = []; for (let i = 0; i < 365; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); + const day = new Date(new Date().setDate(new Date().getDate() - i)); const data = datas.filter(d => d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() diff --git a/src/api/endpoints/aggregation/users/post.ts b/src/api/endpoints/aggregation/users/post.ts index cb6bae4970..c964815a0c 100644 --- a/src/api/endpoints/aggregation/users/post.ts +++ b/src/api/endpoints/aggregation/users/post.ts @@ -84,7 +84,7 @@ module.exports = (params) => new Promise(async (res, rej) => { const graph = []; for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); + const day = new Date(new Date().setDate(new Date().getDate() - i)); const data = datas.filter(d => d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() diff --git a/src/api/endpoints/aggregation/users/reaction.ts b/src/api/endpoints/aggregation/users/reaction.ts index 8f1919fa69..0a082ed1b7 100644 --- a/src/api/endpoints/aggregation/users/reaction.ts +++ b/src/api/endpoints/aggregation/users/reaction.ts @@ -56,7 +56,7 @@ module.exports = (params) => new Promise(async (res, rej) => { const graph = []; for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); + const day = new Date(new Date().setDate(new Date().getDate() - i)); const data = datas.filter(d => d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts index 07178d6507..24f192de6b 100644 --- a/src/api/endpoints/i/appdata/set.ts +++ b/src/api/endpoints/i/appdata/set.ts @@ -34,7 +34,7 @@ module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) = const [value, valueError] = $(params.value).optional.string().$; if (valueError) return rej('invalid value param'); - let set = {}; + const set = {}; if (data) { Object.entries(data).forEach(([k, v]) => { set[`data.${k}`] = v; diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index 26faee4713..535f850a6e 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -36,9 +36,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { // Fetch files // forEach だと途中でエラーなどがあっても return できないので // 敢えて for を使っています。 - for (let i = 0; i < mediaIds.length; i++) { - const mediaId = mediaIds[i]; - + for (const mediaId of mediaIds) { // Fetch file // SELECT _id const entity = await DriveFile.findOne({ @@ -188,7 +186,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { } }); - let mentions = []; + const mentions = []; function addMention(mentionee, type) { // Reject if already added diff --git a/src/api/event.ts b/src/api/event.ts index 39dc809bdc..9613a9f7cc 100644 --- a/src/api/event.ts +++ b/src/api/event.ts @@ -13,14 +13,6 @@ class MisskeyEvent { config.redis.port, config.redis.host); } - private publish(channel: string, type: string, value?: any): void { - const message = value == null ? - { type: type } : - { type: type, body: value }; - - this.redisClient.publish(`misskey:${channel}`, JSON.stringify(message)); - } - public publishUserStream(userId: ID, type: string, value?: any): void { this.publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value); } @@ -32,6 +24,14 @@ class MisskeyEvent { public publishMessagingStream(userId: ID, otherpartyId: ID, type: string, value?: any): void { this.publish(`messaging-stream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value); } + + private publish(channel: string, type: string, value?: any): void { + const message = value == null ? + { type: type } : + { type: type, body: value }; + + this.redisClient.publish(`misskey:${channel}`, JSON.stringify(message)); + } } const ev = new MisskeyEvent(); diff --git a/src/config.ts b/src/config.ts index f7b0095ee7..ca940420e8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,7 +22,7 @@ export const path = process.env.NODE_ENV == 'test' /** * ユーザーが設定する必要のある情報 */ -interface Source { +type Source = { maintainer: string; url: string; secondary_url: string; @@ -68,12 +68,12 @@ interface Source { hook_secret: string; username: string; }; -} +}; /** * Misskeyが自動的に(ユーザーが設定した情報から推論して)設定する情報 */ -interface Mixin { +type Mixin = { host: string; scheme: string; secondary_host: string; @@ -83,7 +83,7 @@ interface Mixin { about_url: string; dev_url: string; drive_url: string; -} +}; export type Config = Source & Mixin; diff --git a/src/index.ts b/src/index.ts index b8d9b35f41..b8ff525ba7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -87,7 +87,7 @@ async function init(): Promise<Config> { MachineInfo.show(); new DependencyInfo().showAll(); - let configLogger = new Logger('Config'); + const configLogger = new Logger('Config'); if (!fs.existsSync(configPath)) { throw 'Configuration not found - Please run "npm run config" command.'; } @@ -109,7 +109,7 @@ async function init(): Promise<Config> { */ // Try to connect to MongoDB - let mongoDBLogger = new Logger('MongoDB'); + const mongoDBLogger = new Logger('MongoDB'); const db = require('./db/mongodb').default; mongoDBLogger.info('Successfully connected'); db.close(); diff --git a/src/utils/dependencyInfo.ts b/src/utils/dependencyInfo.ts index 8c3014d302..818fa3136c 100644 --- a/src/utils/dependencyInfo.ts +++ b/src/utils/dependencyInfo.ts @@ -2,19 +2,19 @@ import Logger from './logger'; import { execSync } from 'child_process'; export default class { - logger: Logger; + private logger: Logger; constructor() { this.logger = new Logger('Deps'); } - showAll(): void { + public showAll(): void { this.show('MongoDB', 'mongo --version', x => x.match(/^MongoDB shell version:? (.*)\r?\n/)); this.show('Redis', 'redis-server --version', x => x.match(/v=([0-9\.]*)/)); this.show('GraphicsMagick', 'gm -version', x => x.match(/^GraphicsMagick ([0-9\.]*) .*/)); } - show(serviceName: string, command: string, transform: (x: string) => RegExpMatchArray): void { + public show(serviceName: string, command: string, transform: (x: string) => RegExpMatchArray): void { try { // ステータス0以外のときにexecSyncはstderrをコンソール上に出力してしまうので // プロセスからのstderrをすべて無視するように stdio オプションをセット diff --git a/src/utils/environmentInfo.ts b/src/utils/environmentInfo.ts index f8508d1d8b..e6084cde0e 100644 --- a/src/utils/environmentInfo.ts +++ b/src/utils/environmentInfo.ts @@ -1,10 +1,11 @@ import Logger from './logger'; export default class { - static show(): void { + public static show(): void { const env = process.env.NODE_ENV; - let logger = new Logger('Env'); + const logger = new Logger('Env'); logger.info(typeof env == 'undefined' ? 'NODE_ENV is not set' : `NODE_ENV: ${env}`); + if (env !== 'production') { logger.warn('The environment is not in production mode'); logger.warn('Do not use for production purpose'); diff --git a/src/utils/logger.ts b/src/utils/logger.ts index ebfa3c34fe..ecfacbc952 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -11,43 +11,43 @@ function toLevelColor(level: LogLevel): chalk.ChalkStyle { } export default class Logger { - domain: string; + private domain: string; - static log(level: LogLevel, message: string): void { - let color = toLevelColor(level); - let time = (new Date()).toLocaleTimeString('ja-JP'); + constructor(domain: string) { + this.domain = domain; + } + + public static log(level: LogLevel, message: string): void { + const color = toLevelColor(level); + const time = (new Date()).toLocaleTimeString('ja-JP'); console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${message}`); } - static error(message: string): void { + public static error(message: string): void { Logger.log('Error', message); } - static warn(message: string): void { + public static warn(message: string): void { Logger.log('Warn', message); } - static info(message: string): void { + public static info(message: string): void { Logger.log('Info', message); } - constructor(domain: string) { - this.domain = domain; - } - - log(level: LogLevel, message: string): void { + public log(level: LogLevel, message: string): void { Logger.log(level, `[${this.domain}] ${message}`); } - error(message: string): void { + public error(message: string): void { this.log('Error', message); } - warn(message: string): void { + public warn(message: string): void { this.log('Warn', message); } - info(message: string): void { + public info(message: string): void { this.log('Info', message); } } diff --git a/src/utils/machineInfo.ts b/src/utils/machineInfo.ts index acae773743..0c189cc7ca 100644 --- a/src/utils/machineInfo.ts +++ b/src/utils/machineInfo.ts @@ -2,10 +2,10 @@ import * as os from 'os'; import Logger from './logger'; export default class { - static show(): void { + public static show(): void { const totalmem = (os.totalmem() / 1024 / 1024 / 1024).toFixed(1); const freemem = (os.freemem() / 1024 / 1024 / 1024).toFixed(1); - let logger = new Logger('Machine'); + const logger = new Logger('Machine'); logger.info(`Hostname: ${os.hostname()}`); logger.info(`Platform: ${process.platform}`); logger.info(`Architecture: ${process.arch}`); |