Skip to content

Commit

Permalink
ブロックしたら相互に見れないように、Stream muteなどの自動更新
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Apr 28, 2020
1 parent 3507874 commit a1ae6d9
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 81 deletions.
19 changes: 15 additions & 4 deletions src/server/api/common/get-hide-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import * as mongo from 'mongodb';
import Mute from '../../../models/mute';
import User, { IUser } from '../../../models/user';
import { unique } from '../../../prelude/array';
import Blocking from '../../../models/blocking';

export async function getHideUserIds(me: IUser, includeSilenced = false, includeSuspended = true) {
return await getHideUserIdsById(me ? me._id : null, includeSilenced, includeSuspended);
}

export async function getHideUserIdsById(meId?: mongo.ObjectID, includeSilenced = false, includeSuspended = true) {
const [suspended, silenced, muted] = await Promise.all([
export async function getHideUserIdsById(meId?: mongo.ObjectID | null, includeSilenced = false, includeSuspended = true) {
const [suspended, silenced, muted, blocking, blocked] = await Promise.all([
includeSuspended ? (User.find({
isSuspended: true
}, {
Expand All @@ -26,8 +27,18 @@ export async function getHideUserIdsById(meId?: mongo.ObjectID, includeSilenced
})) : [],
meId ? Mute.find({
muterId: meId
}) : Promise.resolve([])
}) : [],
meId ? Blocking.find({
blockerId: meId
}) : [],
meId ? Blocking.find({
blockeeId: meId
}) : [],
]);

return unique(suspended.map(user => user._id).concat(silenced.map(user => user._id)).concat(muted.map(mute => mute.muteeId)));
return unique(suspended.map(user => user._id)
.concat(silenced.map(user => user._id))
.concat(muted.map(mute => mute.muteeId)))
.concat(blocking.map(block => block.blockeeId))
.concat(blocked.map(block => block.blockerId));
}
4 changes: 4 additions & 0 deletions src/server/api/stream/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default abstract class Channel {
return this.connection.user;
}

protected get mutedUserIds() {
return this.connection.muting;
}

protected get subscriber() {
return this.connection.subscriber;
}
Expand Down
13 changes: 0 additions & 13 deletions src/server/api/stream/channels/global-timeline.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import autobind from 'autobind-decorator';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel';
import fetchMeta from '../../../../misc/fetch-meta';
import User from '../../../../models/user';

export default class extends Channel {
public readonly chName = 'globalTimeline';
public static requireCredential = false;

private showReplayInPublicTimeline = false;
private mutedUserIds: string[] = [];

@autobind
public async init(params: any) {
Expand All @@ -23,16 +20,6 @@ export default class extends Channel {

// Subscribe events
this.subscriber.on('notesStream', this.onNote);

const mute = await Mute.find({ muterId: this.user._id });
this.mutedUserIds = mute.map(m => m.muteeId.toString());

const silences = await User.find({
isSilenced: true,
_id: { $nin: this.user ? [ this.user._id ] : [] }
});

this.mutedUserIds = this.mutedUserIds.concat(silences.map(x => x._id.toString()));
}

@autobind
Expand Down
4 changes: 0 additions & 4 deletions src/server/api/stream/channels/hashtag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import autobind from 'autobind-decorator';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel';
Expand All @@ -10,9 +9,6 @@ export default class extends Channel {

@autobind
public async init(params: any) {
const mute = this.user ? await Mute.find({ muterId: this.user._id }) : null;
const mutedUserIds = mute ? mute.map(m => m.muteeId.toString()) : [];

const q: string[][] = params.q;

if (q == null) return;
Expand Down
14 changes: 0 additions & 14 deletions src/server/api/stream/channels/hot-timeline.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
import autobind from 'autobind-decorator';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel';
import User from '../../../../models/user';

export default class extends Channel {
public readonly chName = 'hotTimeline';
public static requireCredential = true;

private mutedUserIds: string[] = [];

@autobind
public async init(params: any) {
// Subscribe events
this.subscriber.on('hotStream', this.onNewNote);

const mute = await Mute.find({ muterId: this.user._id });
this.mutedUserIds = mute.map(m => m.muteeId.toString());

const silences = await User.find({
isSilenced: true,
_id: { $nin: this.user ? [ this.user._id ] : [] }
});

this.mutedUserIds = this.mutedUserIds.concat(silences.map(x => x._id.toString()));
}

@autobind
Expand Down
13 changes: 0 additions & 13 deletions src/server/api/stream/channels/hybrid-timeline.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import autobind from 'autobind-decorator';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel';
import fetchMeta from '../../../../misc/fetch-meta';
import UserList from '../../../../models/user-list';
import { concat } from '../../../../prelude/array';
import { isSelfHost } from '../../../../misc/convert-host';
import User from '../../../../models/user';
import Following from '../../../../models/following';
import { oidEquals, oidIncludes } from '../../../../prelude/oid';
import UserFilter from '../../../../models/user-filter';
Expand All @@ -16,7 +14,6 @@ export default class extends Channel {
public readonly chName = 'hybridTimeline';
public static requireCredential = true;

private mutedUserIds: string[] = [];
private hideFromUsers: string[] = [];
private hideFromHosts: string[] = [];
private hideRenoteUsers: string[] = [];
Expand All @@ -39,16 +36,6 @@ export default class extends Channel {

this.excludeForeignReply = !!params?.excludeForeignReply;

const mute = await Mute.find({ muterId: this.user._id });
this.mutedUserIds = mute.map(m => m.muteeId.toString());

const silences = await User.find({
isSilenced: true,
_id: { $nin: this.user ? [ this.user._id ] : [] }
});

this.mutedUserIds = this.mutedUserIds.concat(silences.map(x => x._id.toString()));

// Homeから隠すリストユーザー
const lists = await UserList.find({
userId: this.user._id,
Expand Down
12 changes: 0 additions & 12 deletions src/server/api/stream/channels/local-timeline.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import autobind from 'autobind-decorator';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel';
import fetchMeta from '../../../../misc/fetch-meta';
import User from '../../../../models/user';

export default class extends Channel {
public readonly chName = 'localTimeline';
public static requireCredential = false;

private showReplayInPublicTimeline = false;
private mutedUserIds: string[] = [];

@autobind
public async init(params: any) {
Expand All @@ -24,15 +21,6 @@ export default class extends Channel {
// Subscribe events
this.subscriber.on('notesStream', this.onNote);

const mute = this.user ? await Mute.find({ muterId: this.user._id }) : null;
this.mutedUserIds = mute ? mute.map(m => m.muteeId.toString()) : [];

const silences = await User.find({
isSilenced: true,
_id: { $nin: this.user ? [ this.user._id ] : [] }
});

this.mutedUserIds = this.mutedUserIds.concat(silences.map(x => x._id.toString()));
}

@autobind
Expand Down
14 changes: 1 addition & 13 deletions src/server/api/stream/channels/locao-timeline.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
import autobind from 'autobind-decorator';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import Channel from '../channel';
import fetchMeta from '../../../../misc/fetch-meta';
import User from '../../../../models/user';

export default class extends Channel {
public readonly chName = 'locaoTimeline';
public static requireCredential = false;

private showReplayInPublicTimeline = false;
private mutedUserIds: string[] = [];

@autobind
public async init(params: any) {
const meta = await fetchMeta();
if (meta.disableLocalTimeline) {
return;
}
this.showReplayInPublicTimeline = meta.showReplayInPublicTimeline;
this.showReplayInPublicTimeline = !!meta.showReplayInPublicTimeline;

// Subscribe events
this.subscriber.on('notesStream', this.onNote);

const mute = this.user ? await Mute.find({ muterId: this.user._id }) : null;
this.mutedUserIds = mute ? mute.map(m => m.muteeId.toString()) : [];

const silences = await User.find({
isSilenced: true,
_id: { $nin: this.user ? [ this.user._id ] : [] }
});

this.mutedUserIds = this.mutedUserIds.concat(silences.map(x => x._id.toString()));
}

@autobind
Expand Down
4 changes: 0 additions & 4 deletions src/server/api/stream/channels/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import autobind from 'autobind-decorator';
import Mute from '../../../../models/mute';
import Channel from '../channel';

export default class extends Channel {
Expand All @@ -8,9 +7,6 @@ export default class extends Channel {

@autobind
public async init(params: any) {
const mute = await Mute.find({ muterId: this.user._id });
const mutedUserIds = mute.map(m => m.muteeId.toString());

// Subscribe main stream channel
this.subscriber.on(`mainStream:${this.user._id}`, async data => {
const { type, body } = data;
Expand Down
4 changes: 0 additions & 4 deletions src/server/api/stream/channels/user-list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import autobind from 'autobind-decorator';
import Channel from '../channel';
import { pack } from '../../../../models/note';
import Mute from '../../../../models/mute';
import shouldMuteThisNote from '../../../../misc/should-mute-this-note';
import UserList, { IUserList } from '../../../../models/user-list';
import config from '../../../../config';
Expand All @@ -15,7 +14,6 @@ export default class extends Channel {
private listId: string;
public list: IUserList = null;

private mutedUserIds: string[] = [];
private hideRenoteUsers: string[] = [];
private followingIds: string[] = [];
private excludeForeignReply = false;
Expand All @@ -25,8 +23,6 @@ export default class extends Channel {
@autobind
public async init(params: any) {
this.listId = params.listId;
const mute = this.user ? await Mute.find({ muterId: this.user._id }) : null;
this.mutedUserIds = mute ? mute.map(m => m.muteeId.toString()) : [];

await this.refreshLists();

Expand Down
15 changes: 15 additions & 0 deletions src/server/api/stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Channel from './channel';
import channels from './channels';
import { EventEmitter } from 'events';
import { ApiError } from '../error';
import { getHideUserIdsById } from '../common/get-hide-users';

/**
* Main stream connection
Expand All @@ -23,6 +24,8 @@ export default class Connection {
private channels: Channel[] = [];
private subscribingNotes: any = {};
public sendMessageToWsOverride: any = null; // 後方互換性のため
public muting: string[] = [];
private mutingClock: any;

constructor(
wsConnection: websocket.connection,
Expand All @@ -36,6 +39,11 @@ export default class Connection {
this.subscriber = subscriber;

this.wsConnection.on('message', this.onWsConnectionMessage);

if (this.user) {
this.updateMuting();
this.mutingClock = setInterval(this.updateMuting, 60 * 1000);
}
}

/**
Expand Down Expand Up @@ -212,6 +220,12 @@ export default class Connection {
}
}

@autobind
private async updateMuting() {
const hides = await getHideUserIdsById(this.user?._id);
this.muting = hides.map(x => `${x}`);
}

/**
* ストリームが切れたとき
*/
Expand All @@ -220,5 +234,6 @@ export default class Connection {
for (const c of this.channels.filter(c => c.dispose)) {
c.dispose();
}
if (this.mutingClock) clearInterval(this.mutingClock);
}
}

0 comments on commit a1ae6d9

Please sign in to comment.