add channel_muting services

This commit is contained in:
samunohito 2024-06-10 20:36:30 +09:00
parent cbc256b7ce
commit 94ededa68d
5 changed files with 166 additions and 14 deletions

View File

@ -0,0 +1,106 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';
import { DI } from '@/di-symbols.js';
import type { ChannelMutingRepository, MiChannel, MiUser } from '@/models/_.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEvents, GlobalEventService } from '@/core/GlobalEventService.js';
import { bindThis } from '@/decorators.js';
import { RedisKVCache } from '@/misc/cache.js';
@Injectable()
export class ChannelMutingService {
public userMutingChannelsCache: RedisKVCache<Set<string>>;
constructor(
@Inject(DI.redis)
private redisClient: Redis.Redis,
@Inject(DI.redisForSub)
private redisForSub: Redis.Redis,
@Inject(DI.channelMutingRepository)
private channelMutingRepository: ChannelMutingRepository,
private idService: IdService,
private globalEventService: GlobalEventService,
) {
this.userMutingChannelsCache = new RedisKVCache<Set<string>>(this.redisClient, 'channelMutingChannels', {
lifetime: 1000 * 60 * 30, // 30m
memoryCacheLifetime: 1000 * 60, // 1m
fetcher: (userId) => this.channelMutingRepository.find({
where: { userId: userId },
select: ['channelId'],
}).then(xs => new Set(xs.map(x => x.channelId))),
toRedisConverter: (value) => JSON.stringify(Array.from(value)),
fromRedisConverter: (value) => new Set(JSON.parse(value)),
});
this.redisForSub.on('message', this.onMessage);
}
@bindThis
public async mute(params: {
requestUserId: MiUser['id'],
targetChannelId: MiChannel['id'],
expiresAt?: Date | null,
}): Promise<void> {
await this.channelMutingRepository.insert({
id: this.idService.gen(),
userId: params.requestUserId,
channelId: params.targetChannelId,
expiresAt: params.expiresAt,
});
this.globalEventService.publishInternalEvent('muteChannel', {
userId: params.requestUserId,
channelId: params.targetChannelId,
});
}
@bindThis
public async unmute(params: {
requestUserId: MiUser['id'],
targetChannelId: MiChannel['id'],
}): Promise<void> {
await this.channelMutingRepository.delete({
userId: params.requestUserId,
channelId: params.targetChannelId,
});
this.globalEventService.publishInternalEvent('unmuteChannel', {
userId: params.requestUserId,
channelId: params.targetChannelId,
});
}
@bindThis
private async onMessage(_: string, data: string): Promise<void> {
const obj = JSON.parse(data);
if (obj.channel === 'internal') {
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
switch (type) {
case 'muteChannel': {
this.userMutingChannelsCache.refresh(body.userId).then();
break;
}
case 'unmuteChannel': {
this.userMutingChannelsCache.delete(body.userId).then();
break;
}
}
}
}
@bindThis
public dispose(): void {
this.userMutingChannelsCache.dispose();
}
@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}

View File

@ -12,6 +12,7 @@ import {
} from '@/core/entities/AbuseReportNotificationRecipientEntityService.js'; } from '@/core/entities/AbuseReportNotificationRecipientEntityService.js';
import { AbuseReportNotificationService } from '@/core/AbuseReportNotificationService.js'; import { AbuseReportNotificationService } from '@/core/AbuseReportNotificationService.js';
import { SystemWebhookService } from '@/core/SystemWebhookService.js'; import { SystemWebhookService } from '@/core/SystemWebhookService.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import { AccountMoveService } from './AccountMoveService.js'; import { AccountMoveService } from './AccountMoveService.js';
import { AccountUpdateService } from './AccountUpdateService.js'; import { AccountUpdateService } from './AccountUpdateService.js';
import { AiService } from './AiService.js'; import { AiService } from './AiService.js';
@ -215,6 +216,7 @@ const $FeaturedService: Provider = { provide: 'FeaturedService', useExisting: Fe
const $FanoutTimelineService: Provider = { provide: 'FanoutTimelineService', useExisting: FanoutTimelineService }; const $FanoutTimelineService: Provider = { provide: 'FanoutTimelineService', useExisting: FanoutTimelineService };
const $FanoutTimelineEndpointService: Provider = { provide: 'FanoutTimelineEndpointService', useExisting: FanoutTimelineEndpointService }; const $FanoutTimelineEndpointService: Provider = { provide: 'FanoutTimelineEndpointService', useExisting: FanoutTimelineEndpointService };
const $ChannelFollowingService: Provider = { provide: 'ChannelFollowingService', useExisting: ChannelFollowingService }; const $ChannelFollowingService: Provider = { provide: 'ChannelFollowingService', useExisting: ChannelFollowingService };
const $ChannelMutingService: Provider = { provide: 'ChannelMutingService', useExisting: ChannelMutingService };
const $RegistryApiService: Provider = { provide: 'RegistryApiService', useExisting: RegistryApiService }; const $RegistryApiService: Provider = { provide: 'RegistryApiService', useExisting: RegistryApiService };
const $ReversiService: Provider = { provide: 'ReversiService', useExisting: ReversiService }; const $ReversiService: Provider = { provide: 'ReversiService', useExisting: ReversiService };
@ -361,6 +363,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
FanoutTimelineService, FanoutTimelineService,
FanoutTimelineEndpointService, FanoutTimelineEndpointService,
ChannelFollowingService, ChannelFollowingService,
ChannelMutingService,
RegistryApiService, RegistryApiService,
ReversiService, ReversiService,
@ -503,6 +506,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$FanoutTimelineService, $FanoutTimelineService,
$FanoutTimelineEndpointService, $FanoutTimelineEndpointService,
$ChannelFollowingService, $ChannelFollowingService,
$ChannelMutingService,
$RegistryApiService, $RegistryApiService,
$ReversiService, $ReversiService,
@ -646,6 +650,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
FanoutTimelineService, FanoutTimelineService,
FanoutTimelineEndpointService, FanoutTimelineEndpointService,
ChannelFollowingService, ChannelFollowingService,
ChannelMutingService,
RegistryApiService, RegistryApiService,
ReversiService, ReversiService,
@ -787,6 +792,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$FanoutTimelineService, $FanoutTimelineService,
$FanoutTimelineEndpointService, $FanoutTimelineEndpointService,
$ChannelFollowingService, $ChannelFollowingService,
$ChannelMutingService,
$RegistryApiService, $RegistryApiService,
$ReversiService, $ReversiService,

View File

@ -240,6 +240,8 @@ export interface InternalEventTypes {
metaUpdated: MiMeta; metaUpdated: MiMeta;
followChannel: { userId: MiUser['id']; channelId: MiChannel['id']; }; followChannel: { userId: MiUser['id']; channelId: MiChannel['id']; };
unfollowChannel: { userId: MiUser['id']; channelId: MiChannel['id']; }; unfollowChannel: { userId: MiUser['id']; channelId: MiChannel['id']; };
muteChannel: { userId: MiUser['id']; channelId: MiChannel['id']; };
unmuteChannel: { userId: MiUser['id']; channelId: MiChannel['id']; };
updateUserProfile: MiUserProfile; updateUserProfile: MiUserProfile;
mute: { muterId: MiUser['id']; muteeId: MiUser['id']; }; mute: { muterId: MiUser['id']; muteeId: MiUser['id']; };
unmute: { muterId: MiUser['id']; muteeId: MiUser['id']; }; unmute: { muterId: MiUser['id']; muteeId: MiUser['id']; };

View File

@ -12,8 +12,9 @@ import type { NotificationService } from '@/core/NotificationService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { CacheService } from '@/core/CacheService.js'; import { CacheService } from '@/core/CacheService.js';
import { MiFollowing, MiUserProfile } from '@/models/_.js'; import { MiFollowing, MiUserProfile } from '@/models/_.js';
import type { StreamEventEmitter, GlobalEvents } from '@/core/GlobalEventService.js'; import type { GlobalEvents, StreamEventEmitter } from '@/core/GlobalEventService.js';
import { ChannelFollowingService } from '@/core/ChannelFollowingService.js'; import { ChannelFollowingService } from '@/core/ChannelFollowingService.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import type { ChannelsService } from './ChannelsService.js'; import type { ChannelsService } from './ChannelsService.js';
import type { EventEmitter } from 'events'; import type { EventEmitter } from 'events';
import type Channel from './channel.js'; import type Channel from './channel.js';
@ -33,10 +34,12 @@ export default class Connection {
public userProfile: MiUserProfile | null = null; public userProfile: MiUserProfile | null = null;
public following: Record<string, Pick<MiFollowing, 'withReplies'> | undefined> = {}; public following: Record<string, Pick<MiFollowing, 'withReplies'> | undefined> = {};
public followingChannels: Set<string> = new Set(); public followingChannels: Set<string> = new Set();
public mutingChannels: Set<string> = new Set();
public userIdsWhoMeMuting: Set<string> = new Set(); public userIdsWhoMeMuting: Set<string> = new Set();
public userIdsWhoBlockingMe: Set<string> = new Set(); public userIdsWhoBlockingMe: Set<string> = new Set();
public userIdsWhoMeMutingRenotes: Set<string> = new Set(); public userIdsWhoMeMutingRenotes: Set<string> = new Set();
public userMutedInstances: Set<string> = new Set(); public userMutedInstances: Set<string> = new Set();
public userMutedChannels: Set<string> = new Set();
private fetchIntervalId: NodeJS.Timeout | null = null; private fetchIntervalId: NodeJS.Timeout | null = null;
constructor( constructor(
@ -45,7 +48,7 @@ export default class Connection {
private notificationService: NotificationService, private notificationService: NotificationService,
private cacheService: CacheService, private cacheService: CacheService,
private channelFollowingService: ChannelFollowingService, private channelFollowingService: ChannelFollowingService,
private channelMutingService: ChannelMutingService,
user: MiUser | null | undefined, user: MiUser | null | undefined,
token: MiAccessToken | null | undefined, token: MiAccessToken | null | undefined,
) { ) {
@ -56,10 +59,19 @@ export default class Connection {
@bindThis @bindThis
public async fetch() { public async fetch() {
if (this.user == null) return; if (this.user == null) return;
const [userProfile, following, followingChannels, userIdsWhoMeMuting, userIdsWhoBlockingMe, userIdsWhoMeMutingRenotes] = await Promise.all([ const [
userProfile,
following,
followingChannels,
mutingChannels,
userIdsWhoMeMuting,
userIdsWhoBlockingMe,
userIdsWhoMeMutingRenotes,
] = await Promise.all([
this.cacheService.userProfileCache.fetch(this.user.id), this.cacheService.userProfileCache.fetch(this.user.id),
this.cacheService.userFollowingsCache.fetch(this.user.id), this.cacheService.userFollowingsCache.fetch(this.user.id),
this.channelFollowingService.userFollowingChannelsCache.fetch(this.user.id), this.channelFollowingService.userFollowingChannelsCache.fetch(this.user.id),
this.channelMutingService.userMutingChannelsCache.fetch(this.user.id),
this.cacheService.userMutingsCache.fetch(this.user.id), this.cacheService.userMutingsCache.fetch(this.user.id),
this.cacheService.userBlockedCache.fetch(this.user.id), this.cacheService.userBlockedCache.fetch(this.user.id),
this.cacheService.renoteMutingsCache.fetch(this.user.id), this.cacheService.renoteMutingsCache.fetch(this.user.id),
@ -67,6 +79,7 @@ export default class Connection {
this.userProfile = userProfile; this.userProfile = userProfile;
this.following = following; this.following = following;
this.followingChannels = followingChannels; this.followingChannels = followingChannels;
this.mutingChannels = mutingChannels;
this.userIdsWhoMeMuting = userIdsWhoMeMuting; this.userIdsWhoMeMuting = userIdsWhoMeMuting;
this.userIdsWhoBlockingMe = userIdsWhoBlockingMe; this.userIdsWhoBlockingMe = userIdsWhoBlockingMe;
this.userIdsWhoMeMutingRenotes = userIdsWhoMeMutingRenotes; this.userIdsWhoMeMutingRenotes = userIdsWhoMeMutingRenotes;
@ -112,16 +125,37 @@ export default class Connection {
const { type, body } = obj; const { type, body } = obj;
switch (type) { switch (type) {
case 'readNotification': this.onReadNotification(body); break; case 'readNotification':
case 'subNote': this.onSubscribeNote(body); break; this.onReadNotification(body);
case 's': this.onSubscribeNote(body); break; // alias break;
case 'sr': this.onSubscribeNote(body); this.readNote(body); break; case 'subNote':
case 'unsubNote': this.onUnsubscribeNote(body); break; this.onSubscribeNote(body);
case 'un': this.onUnsubscribeNote(body); break; // alias break;
case 'connect': this.onChannelConnectRequested(body); break; case 's':
case 'disconnect': this.onChannelDisconnectRequested(body); break; this.onSubscribeNote(body);
case 'channel': this.onChannelMessageRequested(body); break; break; // alias
case 'ch': this.onChannelMessageRequested(body); break; // alias case 'sr':
this.onSubscribeNote(body);
this.readNote(body);
break;
case 'unsubNote':
this.onUnsubscribeNote(body);
break;
case 'un':
this.onUnsubscribeNote(body);
break; // alias
case 'connect':
this.onChannelConnectRequested(body);
break;
case 'disconnect':
this.onChannelDisconnectRequested(body);
break;
case 'channel':
this.onChannelMessageRequested(body);
break;
case 'ch':
this.onChannelMessageRequested(body);
break; // alias
} }
} }

View File

@ -6,7 +6,7 @@
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { isInstanceMuted } from '@/misc/is-instance-muted.js'; import { isInstanceMuted } from '@/misc/is-instance-muted.js';
import { isUserRelated } from '@/misc/is-user-related.js'; import { isUserRelated } from '@/misc/is-user-related.js';
import { isRenotePacked, isQuotePacked } from '@/misc/is-renote.js'; import { isQuotePacked, isRenotePacked } from '@/misc/is-renote.js';
import type { Packed } from '@/misc/json-schema.js'; import type { Packed } from '@/misc/json-schema.js';
import type Connection from './Connection.js'; import type Connection from './Connection.js';
@ -54,6 +54,10 @@ export default abstract class Channel {
return this.connection.followingChannels; return this.connection.followingChannels;
} }
protected get mutingChannels() {
return this.connection.mutingChannels;
}
protected get subscriber() { protected get subscriber() {
return this.connection.subscriber; return this.connection.subscriber;
} }