From de723831561c807e3196bcf00a5d2687934b388f Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 14 Aug 2018 06:14:47 +0900 Subject: [PATCH] Add welcome module --- src/ai.ts | 34 ++++++++++++++++++++++++++++++++-- src/index.ts | 2 ++ src/module.ts | 1 + src/modules/welcome/index.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/modules/welcome/index.ts diff --git a/src/ai.ts b/src/ai.ts index 39b3889..6b4a2c7 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -20,11 +20,17 @@ export default class 藍 { */ private connection: any; + /** + * ローカルタイムラインストリーム + */ + private localTimelineConnection: any; + private modules: IModule[] = []; constructor(account: any) { this.account = account; + //#region Home stream this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], { WebSocket: WebSocket }); @@ -42,9 +48,27 @@ export default class 藍 { this.onMessage(msg); }); + //#endregion - if (config.reversiEnabled) { - } + //#region Local timeline stream + this.localTimelineConnection = new ReconnectingWebSocket(`${config.wsUrl}/local-timeline?i=${config.i}`, [], { + WebSocket: WebSocket + }); + + this.localTimelineConnection.addEventListener('open', () => { + console.log('local-timeline stream opened'); + }); + + this.localTimelineConnection.addEventListener('close', () => { + console.log('local-timeline stream closed'); + }); + + this.localTimelineConnection.addEventListener('message', message => { + const msg = JSON.parse(message.data); + + this.onLocalNote(msg.body); + }); + //#endregion } public install = (module: IModule) => { @@ -82,6 +106,12 @@ export default class 藍 { } } + private onLocalNote = (note: any) => { + this.modules.filter(m => m.hasOwnProperty('onLocalNote')).forEach(m => { + return m.onLocalNote(note); + }); + } + private onMention = (msg: MessageLike) => { console.log(`mention received: ${msg.id}`); diff --git a/src/index.ts b/src/index.ts index 0ea4fbd..3d5da75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import EmojiModule from './modules/emoji'; import FortuneModule from './modules/fortune'; import GuessingGameModule from './modules/guessing-game'; import KeywordModule from './modules/keyword'; +import WelcomeModule from './modules/welcome'; import * as request from 'request-promise-native'; const promiseRetry = require('promise-retry'); @@ -20,6 +21,7 @@ promiseRetry(retry => { const ai = new 藍(account); ai.install(new PingModule()); + ai.install(new WelcomeModule()); ai.install(new EmojiModule()); ai.install(new FortuneModule()); ai.install(new GuessingGameModule()); diff --git a/src/module.ts b/src/module.ts index fd0702e..27b6622 100644 --- a/src/module.ts +++ b/src/module.ts @@ -5,5 +5,6 @@ export default interface IModule { name: string; install?: (ai: 藍) => void; onMention?: (msg: MessageLike) => boolean; + onLocalNote?: (note: any) => void; onReplyThisModule?: (msg: MessageLike) => void; } diff --git a/src/modules/welcome/index.ts b/src/modules/welcome/index.ts new file mode 100644 index 0000000..6b84be9 --- /dev/null +++ b/src/modules/welcome/index.ts @@ -0,0 +1,29 @@ +import 藍 from '../../ai'; +import IModule from '../../module'; + +export default class WelcomeModule implements IModule { + public name = 'welcome'; + + private ai: 藍; + + public install = (ai: 藍) => { + this.ai = ai; + } + + public onLocalNote = (note: any) => { + if (note.user.notesCount == 1) { + setTimeout(() => { + this.ai.api('notes/create', { + renoteId: note.id + }); + }, 3000); + + setTimeout(() => { + this.ai.api('notes/reactions/create', { + noteId: note.id, + reaction: 'congrats' + }); + }, 5000); + } + } +}