2018-08-11 03:19:34 +00:00
|
|
|
// AI CORE
|
|
|
|
|
2018-08-10 17:28:31 +00:00
|
|
|
import * as WebSocket from 'ws';
|
|
|
|
import * as request from 'request-promise-native';
|
|
|
|
import serifs from './serifs';
|
2018-08-11 01:42:06 +00:00
|
|
|
import config from './config';
|
|
|
|
import IModule from './module';
|
|
|
|
import MessageLike from './message-like';
|
|
|
|
import ReversiModule from './modules/reversi';
|
2018-08-11 03:19:34 +00:00
|
|
|
import ServerModule from './modules/server';
|
|
|
|
const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
|
2018-08-10 17:28:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 藍
|
|
|
|
*/
|
2018-08-11 01:42:06 +00:00
|
|
|
export default class 藍 {
|
2018-08-10 17:28:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ホームストリーム
|
|
|
|
*/
|
|
|
|
private connection: any;
|
|
|
|
|
2018-08-11 01:42:06 +00:00
|
|
|
private modules: IModule[] = [];
|
2018-08-10 17:28:31 +00:00
|
|
|
|
|
|
|
constructor() {
|
2018-08-11 01:42:06 +00:00
|
|
|
this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], {
|
2018-08-10 17:28:31 +00:00
|
|
|
WebSocket: WebSocket
|
|
|
|
});
|
|
|
|
|
|
|
|
this.connection.addEventListener('open', () => {
|
|
|
|
console.log('home stream opened');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.connection.addEventListener('close', () => {
|
|
|
|
console.log('home stream closed');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.connection.addEventListener('message', message => {
|
|
|
|
const msg = JSON.parse(message.data);
|
|
|
|
|
|
|
|
this.onMessage(msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (config.reversiEnabled) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 01:42:06 +00:00
|
|
|
public install = (module: IModule) => {
|
2018-08-11 03:19:34 +00:00
|
|
|
module.install(this);
|
2018-08-11 01:42:06 +00:00
|
|
|
this.modules.push(module);
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:28:31 +00:00
|
|
|
private onMessage = (msg: any) => {
|
|
|
|
switch (msg.type) {
|
|
|
|
// メンションされたとき
|
|
|
|
case 'mention': {
|
|
|
|
if (msg.body.userId == config.id) return; // 自分は弾く
|
|
|
|
this.onMention(new MessageLike(this, msg.body, false));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返信されたとき
|
|
|
|
case 'reply': {
|
|
|
|
if (msg.body.userId == config.id) return; // 自分は弾く
|
|
|
|
this.onMention(new MessageLike(this, msg.body, false));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// メッセージ
|
|
|
|
case 'messaging_message': {
|
|
|
|
if (msg.body.userId == config.id) return; // 自分は弾く
|
|
|
|
this.onMention(new MessageLike(this, msg.body, true));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 01:42:06 +00:00
|
|
|
private onMention = (msg: MessageLike) => {
|
2018-08-11 03:34:24 +00:00
|
|
|
console.log(`mention received: ${msg.id}`);
|
|
|
|
|
2018-08-10 17:28:31 +00:00
|
|
|
// リアクションする
|
2018-08-11 01:42:06 +00:00
|
|
|
if (!msg.isMessage) {
|
2018-08-10 17:28:31 +00:00
|
|
|
setTimeout(() => {
|
2018-08-11 01:42:06 +00:00
|
|
|
request.post(`${config.apiUrl}/notes/reactions/create`, {
|
2018-08-10 17:28:31 +00:00
|
|
|
json: {
|
|
|
|
i: config.i,
|
2018-08-11 01:42:06 +00:00
|
|
|
noteId: msg.id,
|
2018-08-10 17:28:31 +00:00
|
|
|
reaction: 'love'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2018-08-11 01:42:06 +00:00
|
|
|
this.modules.filter(m => m.hasOwnProperty('onMention')).some(m => {
|
|
|
|
return m.onMention(msg);
|
|
|
|
});
|
2018-08-10 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public post = (param: any) => {
|
2018-08-11 03:34:24 +00:00
|
|
|
request.post('notes/create', param);
|
2018-08-10 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public sendMessage = (userId: any, param: any) => {
|
2018-08-11 03:34:24 +00:00
|
|
|
this.api('messages/create', Object.assign({
|
|
|
|
userId: userId,
|
|
|
|
}, param));
|
2018-08-10 17:28:31 +00:00
|
|
|
}
|
2018-08-11 01:42:06 +00:00
|
|
|
|
|
|
|
public api = (endpoint: string, param) => {
|
|
|
|
return request.post(`${config.apiUrl}/${endpoint}`, {
|
|
|
|
json: Object.assign({
|
|
|
|
i: config.i
|
|
|
|
}, param)
|
|
|
|
});
|
|
|
|
};
|
2018-08-10 17:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ai = new 藍();
|
2018-08-11 01:42:06 +00:00
|
|
|
|
2018-08-11 03:19:34 +00:00
|
|
|
const serverModule = new ServerModule();
|
|
|
|
ai.install(serverModule);
|
|
|
|
|
2018-08-11 03:35:51 +00:00
|
|
|
const reversiModule = new ReversiModule();
|
|
|
|
ai.install(reversiModule);
|