共有可能チャンネルに接続しようとしていて、かつそのチャンネルに既に接続していたら無意味なので無視するように

This commit is contained in:
syuilo 2018-10-11 23:01:57 +09:00
parent ad67886f96
commit e85f9f4aa5
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
17 changed files with 59 additions and 11 deletions

View File

@ -7,6 +7,8 @@ import Connection from '.';
export default abstract class Channel { export default abstract class Channel {
protected connection: Connection; protected connection: Connection;
public id: string; public id: string;
public abstract readonly chName: string;
public abstract readonly shouldShare: boolean;
protected get user() { protected get user() {
return this.connection.user; return this.connection.user;

View File

@ -2,6 +2,9 @@ import autobind from 'autobind-decorator';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'drive';
public readonly shouldShare = true;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
// Subscribe drive stream // Subscribe drive stream

View File

@ -8,6 +8,9 @@ import * as maps from '../../../../../games/reversi/maps';
import Channel from '../../channel'; import Channel from '../../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'gamesReversiGame';
public readonly shouldShare = false;
private gameId: mongo.ObjectID; private gameId: mongo.ObjectID;
@autobind @autobind

View File

@ -5,6 +5,9 @@ import { publishMainStream } from '../../../../../stream';
import Channel from '../../channel'; import Channel from '../../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'gamesReversi';
public readonly shouldShare = true;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
// Subscribe reversi stream // Subscribe reversi stream

View File

@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'globalTimeline';
public readonly shouldShare = true;
private mutedUserIds: string[] = []; private mutedUserIds: string[] = [];
@autobind @autobind

View File

@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'hashtag';
public readonly shouldShare = false;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
const mute = this.user ? await Mute.find({ muterId: this.user._id }) : null; const mute = this.user ? await Mute.find({ muterId: this.user._id }) : null;

View File

@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'homeTimeline';
public readonly shouldShare = true;
private mutedUserIds: string[] = []; private mutedUserIds: string[] = [];
@autobind @autobind

View File

@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'hybridTimeline';
public readonly shouldShare = true;
private mutedUserIds: string[] = []; private mutedUserIds: string[] = [];
@autobind @autobind

View File

@ -5,6 +5,9 @@ import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'localTimeline';
public readonly shouldShare = true;
private mutedUserIds: string[] = []; private mutedUserIds: string[] = [];
@autobind @autobind

View File

@ -3,6 +3,9 @@ import Mute from '../../../../models/mute';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'main';
public readonly shouldShare = true;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
const mute = await Mute.find({ muterId: this.user._id }); const mute = await Mute.find({ muterId: this.user._id });

View File

@ -2,6 +2,9 @@ import autobind from 'autobind-decorator';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'messagingIndex';
public readonly shouldShare = true;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
// Subscribe messaging index stream // Subscribe messaging index stream

View File

@ -3,6 +3,9 @@ import read from '../../common/read-messaging-message';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'messaging';
public readonly shouldShare = false;
private otherpartyId: string; private otherpartyId: string;
@autobind @autobind

View File

@ -5,6 +5,9 @@ import Channel from '../channel';
const ev = new Xev(); const ev = new Xev();
export default class extends Channel { export default class extends Channel {
public readonly chName = 'notesStats';
public readonly shouldShare = true;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
ev.addListener('notesStats', this.onStats); ev.addListener('notesStats', this.onStats);

View File

@ -5,6 +5,9 @@ import Channel from '../channel';
const ev = new Xev(); const ev = new Xev();
export default class extends Channel { export default class extends Channel {
public readonly chName = 'serverStats';
public readonly shouldShare = true;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
ev.addListener('serverStats', this.onStats); ev.addListener('serverStats', this.onStats);

View File

@ -2,6 +2,9 @@ import autobind from 'autobind-decorator';
import Channel from '../channel'; import Channel from '../channel';
export default class extends Channel { export default class extends Channel {
public readonly chName = 'userList';
public readonly shouldShare = false;
@autobind @autobind
public async init(params: any) { public async init(params: any) {
const listId = params.listId as string; const listId = params.listId as string;

View File

@ -148,7 +148,7 @@ export default class Connection {
private onChannelConnectRequested(payload: any) { private onChannelConnectRequested(payload: any) {
const { channel, id, params } = payload; const { channel, id, params } = payload;
log(`CH CONNECT: ${id} ${channel} by @${this.user.username}`); 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 @autobind
public connectChannel(id: string, params: any, channelClass: { new(id: string, connection: Connection): Channel }) { public connectChannel(id: string, params: any, channel: string) {
const channel = new channelClass(id, this); // 共有可能チャンネルに接続しようとしていて、かつそのチャンネルに既に接続していたら無意味なので無視
this.channels.push(channel); if ((channels as any)[channel].shouldShare && this.channels.some(c => c.chName === channel)) {
channel.init(params); return;
}
const ch: Channel = new (channels as any)[channel](id, this);
this.channels.push(ch);
ch.init(params);
this.sendMessageToWs('connected', { this.sendMessageToWs('connected', {
id: id id: id
}); });

View File

@ -6,7 +6,6 @@ import Xev from 'xev';
import MainStreamConnection from './stream'; import MainStreamConnection from './stream';
import { ParsedUrlQuery } from 'querystring'; import { ParsedUrlQuery } from 'querystring';
import authenticate from './authenticate'; import authenticate from './authenticate';
import channels from './stream/channels';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import config from '../../config'; import config from '../../config';
@ -66,13 +65,13 @@ module.exports = (server: http.Server) => {
}; };
main.connectChannel(Math.random().toString(), null, main.connectChannel(Math.random().toString(), null,
request.resourceURL.pathname === '/' ? channels.homeTimeline : request.resourceURL.pathname === '/' ? 'homeTimeline' :
request.resourceURL.pathname === '/local-timeline' ? channels.localTimeline : request.resourceURL.pathname === '/local-timeline' ? 'localTimeline' :
request.resourceURL.pathname === '/hybrid-timeline' ? channels.hybridTimeline : request.resourceURL.pathname === '/hybrid-timeline' ? 'hybridTimeline' :
request.resourceURL.pathname === '/global-timeline' ? channels.globalTimeline : null); request.resourceURL.pathname === '/global-timeline' ? 'globalTimeline' : null);
if (request.resourceURL.pathname === '/') { if (request.resourceURL.pathname === '/') {
main.connectChannel(Math.random().toString(), null, channels.main); main.connectChannel(Math.random().toString(), null, 'main');
} }
} }