From e85f9f4aa57023ab7c63b9b2f94dc15a910a46a9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 11 Oct 2018 23:01:57 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=B1=E6=9C=89=E5=8F=AF=E8=83=BD=E3=83=81?= =?UTF-8?q?=E3=83=A3=E3=83=B3=E3=83=8D=E3=83=AB=E3=81=AB=E6=8E=A5=E7=B6=9A?= =?UTF-8?q?=E3=81=97=E3=82=88=E3=81=86=E3=81=A8=E3=81=97=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=81=A6=E3=80=81=E3=81=8B=E3=81=A4=E3=81=9D=E3=81=AE=E3=83=81?= =?UTF-8?q?=E3=83=A3=E3=83=B3=E3=83=8D=E3=83=AB=E3=81=AB=E6=97=A2=E3=81=AB?= =?UTF-8?q?=E6=8E=A5=E7=B6=9A=E3=81=97=E3=81=A6=E3=81=84=E3=81=9F=E3=82=89?= =?UTF-8?q?=E7=84=A1=E6=84=8F=E5=91=B3=E3=81=AA=E3=81=AE=E3=81=A7=E7=84=A1?= =?UTF-8?q?=E8=A6=96=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/api/stream/channel.ts | 2 ++ src/server/api/stream/channels/drive.ts | 3 +++ .../api/stream/channels/games/reversi-game.ts | 3 +++ src/server/api/stream/channels/games/reversi.ts | 3 +++ src/server/api/stream/channels/global-timeline.ts | 3 +++ src/server/api/stream/channels/hashtag.ts | 3 +++ src/server/api/stream/channels/home-timeline.ts | 3 +++ src/server/api/stream/channels/hybrid-timeline.ts | 3 +++ src/server/api/stream/channels/local-timeline.ts | 3 +++ src/server/api/stream/channels/main.ts | 3 +++ src/server/api/stream/channels/messaging-index.ts | 3 +++ src/server/api/stream/channels/messaging.ts | 3 +++ src/server/api/stream/channels/notes-stats.ts | 3 +++ src/server/api/stream/channels/server-stats.ts | 3 +++ src/server/api/stream/channels/user-list.ts | 3 +++ src/server/api/stream/index.ts | 15 ++++++++++----- src/server/api/streaming.ts | 11 +++++------ 17 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/server/api/stream/channel.ts b/src/server/api/stream/channel.ts index e2726060dc..3548e4c374 100644 --- a/src/server/api/stream/channel.ts +++ b/src/server/api/stream/channel.ts @@ -7,6 +7,8 @@ import Connection from '.'; export default abstract class Channel { protected connection: Connection; public id: string; + public abstract readonly chName: string; + public abstract readonly shouldShare: boolean; protected get user() { return this.connection.user; diff --git a/src/server/api/stream/channels/drive.ts b/src/server/api/stream/channels/drive.ts index 807fc93cd0..4b428a0081 100644 --- a/src/server/api/stream/channels/drive.ts +++ b/src/server/api/stream/channels/drive.ts @@ -2,6 +2,9 @@ import autobind from 'autobind-decorator'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'drive'; + public readonly shouldShare = true; + @autobind public async init(params: any) { // Subscribe drive stream diff --git a/src/server/api/stream/channels/games/reversi-game.ts b/src/server/api/stream/channels/games/reversi-game.ts index d3d3c0f5ab..9004cdfc62 100644 --- a/src/server/api/stream/channels/games/reversi-game.ts +++ b/src/server/api/stream/channels/games/reversi-game.ts @@ -8,6 +8,9 @@ import * as maps from '../../../../../games/reversi/maps'; import Channel from '../../channel'; export default class extends Channel { + public readonly chName = 'gamesReversiGame'; + public readonly shouldShare = false; + private gameId: mongo.ObjectID; @autobind diff --git a/src/server/api/stream/channels/games/reversi.ts b/src/server/api/stream/channels/games/reversi.ts index d75025c944..0a76847035 100644 --- a/src/server/api/stream/channels/games/reversi.ts +++ b/src/server/api/stream/channels/games/reversi.ts @@ -5,6 +5,9 @@ import { publishMainStream } from '../../../../../stream'; import Channel from '../../channel'; export default class extends Channel { + public readonly chName = 'gamesReversi'; + public readonly shouldShare = true; + @autobind public async init(params: any) { // Subscribe reversi stream diff --git a/src/server/api/stream/channels/global-timeline.ts b/src/server/api/stream/channels/global-timeline.ts index ab0fe5d094..00f9e01baf 100644 --- a/src/server/api/stream/channels/global-timeline.ts +++ b/src/server/api/stream/channels/global-timeline.ts @@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'globalTimeline'; + public readonly shouldShare = true; + private mutedUserIds: string[] = []; @autobind diff --git a/src/server/api/stream/channels/hashtag.ts b/src/server/api/stream/channels/hashtag.ts index 652b0caa5b..3f4f37ca58 100644 --- a/src/server/api/stream/channels/hashtag.ts +++ b/src/server/api/stream/channels/hashtag.ts @@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'hashtag'; + public readonly shouldShare = false; + @autobind public async init(params: any) { const mute = this.user ? await Mute.find({ muterId: this.user._id }) : null; diff --git a/src/server/api/stream/channels/home-timeline.ts b/src/server/api/stream/channels/home-timeline.ts index 4c674e75ef..abdd356127 100644 --- a/src/server/api/stream/channels/home-timeline.ts +++ b/src/server/api/stream/channels/home-timeline.ts @@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'homeTimeline'; + public readonly shouldShare = true; + private mutedUserIds: string[] = []; @autobind diff --git a/src/server/api/stream/channels/hybrid-timeline.ts b/src/server/api/stream/channels/hybrid-timeline.ts index 0b12ab3a8f..f26668b49c 100644 --- a/src/server/api/stream/channels/hybrid-timeline.ts +++ b/src/server/api/stream/channels/hybrid-timeline.ts @@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'hybridTimeline'; + public readonly shouldShare = true; + private mutedUserIds: string[] = []; @autobind diff --git a/src/server/api/stream/channels/local-timeline.ts b/src/server/api/stream/channels/local-timeline.ts index 769ec6392f..13285604b1 100644 --- a/src/server/api/stream/channels/local-timeline.ts +++ b/src/server/api/stream/channels/local-timeline.ts @@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'localTimeline'; + public readonly shouldShare = true; + private mutedUserIds: string[] = []; @autobind diff --git a/src/server/api/stream/channels/main.ts b/src/server/api/stream/channels/main.ts index fd0984c833..f19f5d92a5 100644 --- a/src/server/api/stream/channels/main.ts +++ b/src/server/api/stream/channels/main.ts @@ -3,6 +3,9 @@ import Mute from '../../../../models/mute'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'main'; + public readonly shouldShare = true; + @autobind public async init(params: any) { const mute = await Mute.find({ muterId: this.user._id }); diff --git a/src/server/api/stream/channels/messaging-index.ts b/src/server/api/stream/channels/messaging-index.ts index 6e87cca7f4..43acaf9ab1 100644 --- a/src/server/api/stream/channels/messaging-index.ts +++ b/src/server/api/stream/channels/messaging-index.ts @@ -2,6 +2,9 @@ import autobind from 'autobind-decorator'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'messagingIndex'; + public readonly shouldShare = true; + @autobind public async init(params: any) { // Subscribe messaging index stream diff --git a/src/server/api/stream/channels/messaging.ts b/src/server/api/stream/channels/messaging.ts index e1a78c8678..c862438b06 100644 --- a/src/server/api/stream/channels/messaging.ts +++ b/src/server/api/stream/channels/messaging.ts @@ -3,6 +3,9 @@ import read from '../../common/read-messaging-message'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'messaging'; + public readonly shouldShare = false; + private otherpartyId: string; @autobind diff --git a/src/server/api/stream/channels/notes-stats.ts b/src/server/api/stream/channels/notes-stats.ts index cc68d9886d..3171ada78d 100644 --- a/src/server/api/stream/channels/notes-stats.ts +++ b/src/server/api/stream/channels/notes-stats.ts @@ -5,6 +5,9 @@ import Channel from '../channel'; const ev = new Xev(); export default class extends Channel { + public readonly chName = 'notesStats'; + public readonly shouldShare = true; + @autobind public async init(params: any) { ev.addListener('notesStats', this.onStats); diff --git a/src/server/api/stream/channels/server-stats.ts b/src/server/api/stream/channels/server-stats.ts index 28a566e8ae..4d01765780 100644 --- a/src/server/api/stream/channels/server-stats.ts +++ b/src/server/api/stream/channels/server-stats.ts @@ -5,6 +5,9 @@ import Channel from '../channel'; const ev = new Xev(); export default class extends Channel { + public readonly chName = 'serverStats'; + public readonly shouldShare = true; + @autobind public async init(params: any) { ev.addListener('serverStats', this.onStats); diff --git a/src/server/api/stream/channels/user-list.ts b/src/server/api/stream/channels/user-list.ts index 4ace308923..85df4b4f16 100644 --- a/src/server/api/stream/channels/user-list.ts +++ b/src/server/api/stream/channels/user-list.ts @@ -2,6 +2,9 @@ import autobind from 'autobind-decorator'; import Channel from '../channel'; export default class extends Channel { + public readonly chName = 'userList'; + public readonly shouldShare = false; + @autobind public async init(params: any) { const listId = params.listId as string; diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts index afedd4362c..6d3675c94b 100644 --- a/src/server/api/stream/index.ts +++ b/src/server/api/stream/index.ts @@ -148,7 +148,7 @@ export default class Connection { private onChannelConnectRequested(payload: any) { const { channel, id, params } = payload; log(`CH CONNECT: ${id} ${channel} by @${this.user.username}`); - this.connectChannel(id, params, (channels as any)[channel]); + this.connectChannel(id, params, channel); } /** @@ -177,10 +177,15 @@ export default class Connection { * チャンネルに接続 */ @autobind - public connectChannel(id: string, params: any, channelClass: { new(id: string, connection: Connection): Channel }) { - const channel = new channelClass(id, this); - this.channels.push(channel); - channel.init(params); + public connectChannel(id: string, params: any, channel: string) { + // 共有可能チャンネルに接続しようとしていて、かつそのチャンネルに既に接続していたら無意味なので無視 + if ((channels as any)[channel].shouldShare && this.channels.some(c => c.chName === channel)) { + return; + } + + const ch: Channel = new (channels as any)[channel](id, this); + this.channels.push(ch); + ch.init(params); this.sendMessageToWs('connected', { id: id }); diff --git a/src/server/api/streaming.ts b/src/server/api/streaming.ts index 5710257d39..1a0543b364 100644 --- a/src/server/api/streaming.ts +++ b/src/server/api/streaming.ts @@ -6,7 +6,6 @@ import Xev from 'xev'; import MainStreamConnection from './stream'; import { ParsedUrlQuery } from 'querystring'; import authenticate from './authenticate'; -import channels from './stream/channels'; import { EventEmitter } from 'events'; import config from '../../config'; @@ -66,13 +65,13 @@ module.exports = (server: http.Server) => { }; main.connectChannel(Math.random().toString(), null, - request.resourceURL.pathname === '/' ? channels.homeTimeline : - request.resourceURL.pathname === '/local-timeline' ? channels.localTimeline : - request.resourceURL.pathname === '/hybrid-timeline' ? channels.hybridTimeline : - request.resourceURL.pathname === '/global-timeline' ? channels.globalTimeline : null); + request.resourceURL.pathname === '/' ? 'homeTimeline' : + request.resourceURL.pathname === '/local-timeline' ? 'localTimeline' : + request.resourceURL.pathname === '/hybrid-timeline' ? 'hybridTimeline' : + request.resourceURL.pathname === '/global-timeline' ? 'globalTimeline' : null); if (request.resourceURL.pathname === '/') { - main.connectChannel(Math.random().toString(), null, channels.main); + main.connectChannel(Math.random().toString(), null, 'main'); } }