forked from mirrors/misskey
feat: チャンネルミュートの実装 (#14105)
* add channel_muting table and entities * add channel_muting services * タイムライン取得処理への組み込み * misskey-jsの型とインターフェース生成 * Channelスキーマにミュート情報を追加 * フロントエンドの実装 * 条件が逆だったのを修正 * 期限切れミュートを掃除する機能を実装 * TLの抽出条件調節 * 名前の変更と変更不要の差分をロールバック * 修正漏れ * isChannelRelatedの条件に誤りがあった * [wip] テスト追加 * テストの追加と検出した不備の修正 * fix test * fix CHANGELOG.md * 通常はFTTにしておく * 実装忘れ対応 * fix merge * fix merge * add channel tl test * fix CHANGELOG.md * remove unused import * fix lint * fix test * fix favorite -> favorited * exclude -> include * fix CHANGELOG.md * fix CHANGELOG.md * maintenance * fix CHANGELOG.md * fix * fix ci * regenerate * fix * Revert "fix" This reverts commit 699d50c6ec798777d8e9667cb5d45a26b06bfc93. * fixed --------- Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { ChannelFollowingsRepository } from '@/models/_.js';
|
||||
import type { ChannelFollowingsRepository, ChannelsRepository, MiUser } from '@/models/_.js';
|
||||
import { MiChannel } from '@/models/_.js';
|
||||
import { IdService } from '@/core/IdService.js';
|
||||
import { GlobalEvents, GlobalEventService } from '@/core/GlobalEventService.js';
|
||||
@@ -23,6 +23,8 @@ export class ChannelFollowingService implements OnModuleInit {
|
||||
private redisClient: Redis.Redis,
|
||||
@Inject(DI.redisForSub)
|
||||
private redisForSub: Redis.Redis,
|
||||
@Inject(DI.channelsRepository)
|
||||
private channelsRepository: ChannelsRepository,
|
||||
@Inject(DI.channelFollowingsRepository)
|
||||
private channelFollowingsRepository: ChannelFollowingsRepository,
|
||||
private idService: IdService,
|
||||
@@ -45,6 +47,50 @@ export class ChannelFollowingService implements OnModuleInit {
|
||||
onModuleInit() {
|
||||
}
|
||||
|
||||
/**
|
||||
* フォローしているチャンネルの一覧を取得する.
|
||||
* @param params
|
||||
* @param [opts]
|
||||
* @param {(boolean|undefined)} [opts.idOnly=false] チャンネルIDのみを取得するかどうか. ID以外のフィールドに値がセットされなくなり、他テーブルとのJOINも一切されなくなるので注意.
|
||||
* @param {(boolean|undefined)} [opts.joinUser=undefined] チャンネルオーナーのユーザ情報をJOINするかどうか(falseまたは省略時はJOINしない).
|
||||
* @param {(boolean|undefined)} [opts.joinBannerFile=undefined] バナー画像のドライブファイルをJOINするかどうか(falseまたは省略時はJOINしない).
|
||||
*/
|
||||
@bindThis
|
||||
public async list(
|
||||
params: {
|
||||
requestUserId: MiUser['id'],
|
||||
},
|
||||
opts?: {
|
||||
idOnly?: boolean;
|
||||
joinUser?: boolean;
|
||||
joinBannerFile?: boolean;
|
||||
},
|
||||
): Promise<MiChannel[]> {
|
||||
if (opts?.idOnly) {
|
||||
const q = this.channelFollowingsRepository.createQueryBuilder('channel_following')
|
||||
.select('channel_following.followeeId')
|
||||
.where('channel_following.followerId = :userId', { userId: params.requestUserId });
|
||||
|
||||
return q
|
||||
.getRawMany<{ channel_following_followeeId: string }>()
|
||||
.then(xs => xs.map(x => ({ id: x.channel_following_followeeId } as MiChannel)));
|
||||
} else {
|
||||
const q = this.channelsRepository.createQueryBuilder('channel')
|
||||
.innerJoin('channel_following', 'channel_following', 'channel_following.followeeId = channel.id')
|
||||
.where('channel_following.followerId = :userId', { userId: params.requestUserId });
|
||||
|
||||
if (opts?.joinUser) {
|
||||
q.innerJoinAndSelect('channel.user', 'user');
|
||||
}
|
||||
|
||||
if (opts?.joinBannerFile) {
|
||||
q.leftJoinAndSelect('channel.banner', 'drive_file');
|
||||
}
|
||||
|
||||
return q.getMany();
|
||||
}
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public async follow(
|
||||
requestUser: MiLocalUser,
|
||||
|
||||
224
packages/backend/src/core/ChannelMutingService.ts
Normal file
224
packages/backend/src/core/ChannelMutingService.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
import { Brackets, In } from 'typeorm';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { ChannelMutingRepository, ChannelsRepository, MiChannel, MiChannelMuting, 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 mutingChannelsCache: RedisKVCache<Set<string>>;
|
||||
|
||||
constructor(
|
||||
@Inject(DI.redis)
|
||||
private redisClient: Redis.Redis,
|
||||
@Inject(DI.redisForSub)
|
||||
private redisForSub: Redis.Redis,
|
||||
@Inject(DI.channelsRepository)
|
||||
private channelsRepository: ChannelsRepository,
|
||||
@Inject(DI.channelMutingRepository)
|
||||
private channelMutingRepository: ChannelMutingRepository,
|
||||
private idService: IdService,
|
||||
private globalEventService: GlobalEventService,
|
||||
) {
|
||||
this.mutingChannelsCache = 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* ミュートしているチャンネルの一覧を取得する.
|
||||
* @param params
|
||||
* @param [opts]
|
||||
* @param {(boolean|undefined)} [opts.idOnly=false] チャンネルIDのみを取得するかどうか. ID以外のフィールドに値がセットされなくなり、他テーブルとのJOINも一切されなくなるので注意.
|
||||
* @param {(boolean|undefined)} [opts.joinUser=undefined] チャンネルオーナーのユーザ情報をJOINするかどうか(falseまたは省略時はJOINしない).
|
||||
* @param {(boolean|undefined)} [opts.joinBannerFile=undefined] バナー画像のドライブファイルをJOINするかどうか(falseまたは省略時はJOINしない).
|
||||
*/
|
||||
@bindThis
|
||||
public async list(
|
||||
params: {
|
||||
requestUserId: MiUser['id'],
|
||||
},
|
||||
opts?: {
|
||||
idOnly?: boolean;
|
||||
joinUser?: boolean;
|
||||
joinBannerFile?: boolean;
|
||||
},
|
||||
): Promise<MiChannel[]> {
|
||||
if (opts?.idOnly) {
|
||||
const q = this.channelMutingRepository.createQueryBuilder('channel_muting')
|
||||
.select('channel_muting.channelId')
|
||||
.where('channel_muting.userId = :userId', { userId: params.requestUserId })
|
||||
.andWhere(new Brackets(qb => {
|
||||
qb.where('channel_muting.expiresAt IS NULL')
|
||||
.orWhere('channel_muting.expiresAt > :now', { now: new Date() });
|
||||
}));
|
||||
|
||||
return q
|
||||
.getRawMany<{ channel_muting_channelId: string }>()
|
||||
.then(xs => xs.map(x => ({ id: x.channel_muting_channelId } as MiChannel)));
|
||||
} else {
|
||||
const q = this.channelsRepository.createQueryBuilder('channel')
|
||||
.innerJoin('channel_muting', 'channel_muting', 'channel_muting.channelId = channel.id')
|
||||
.where('channel_muting.userId = :userId', { userId: params.requestUserId })
|
||||
.andWhere(new Brackets(qb => {
|
||||
qb.where('channel_muting.expiresAt IS NULL')
|
||||
.orWhere('channel_muting.expiresAt > :now', { now: new Date() });
|
||||
}));
|
||||
|
||||
if (opts?.joinUser) {
|
||||
q.innerJoinAndSelect('channel.user', 'user');
|
||||
}
|
||||
|
||||
if (opts?.joinBannerFile) {
|
||||
q.leftJoinAndSelect('channel.banner', 'drive_file');
|
||||
}
|
||||
|
||||
return q.getMany();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 期限切れのチャンネルミュート情報を取得する.
|
||||
*
|
||||
* @param [opts]
|
||||
* @param {(boolean|undefined)} [opts.joinUser=undefined] チャンネルミュートを設定したユーザ情報をJOINするかどうか(falseまたは省略時はJOINしない).
|
||||
* @param {(boolean|undefined)} [opts.joinChannel=undefined] ミュート先のチャンネル情報をJOINするかどうか(falseまたは省略時はJOINしない).
|
||||
*/
|
||||
public async findExpiredMutings(opts?: {
|
||||
joinUser?: boolean;
|
||||
joinChannel?: boolean;
|
||||
}): Promise<MiChannelMuting[]> {
|
||||
const now = new Date();
|
||||
const q = this.channelMutingRepository.createQueryBuilder('channel_muting')
|
||||
.where('channel_muting.expiresAt < :now', { now });
|
||||
|
||||
if (opts?.joinUser) {
|
||||
q.innerJoinAndSelect('channel_muting.user', 'user');
|
||||
}
|
||||
|
||||
if (opts?.joinChannel) {
|
||||
q.leftJoinAndSelect('channel_muting.channel', 'channel');
|
||||
}
|
||||
|
||||
return q.getMany();
|
||||
}
|
||||
|
||||
/**
|
||||
* 既にミュートされているかどうかをキャッシュから取得する.
|
||||
* @param params
|
||||
* @param params.requestUserId
|
||||
*/
|
||||
@bindThis
|
||||
public async isMuted(params: {
|
||||
requestUserId: MiUser['id'],
|
||||
targetChannelId: MiChannel['id'],
|
||||
}): Promise<boolean> {
|
||||
const mutedChannels = await this.mutingChannelsCache.get(params.requestUserId);
|
||||
return (mutedChannels?.has(params.targetChannelId) ?? false);
|
||||
}
|
||||
|
||||
/**
|
||||
* チャンネルをミュートする.
|
||||
* @param params
|
||||
* @param {(Date|null|undefined)} [params.expiresAt] ミュートの有効期限. nullまたは省略時は無期限.
|
||||
*/
|
||||
@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,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* チャンネルのミュートを解除する.
|
||||
* @param params
|
||||
*/
|
||||
@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
|
||||
public async eraseExpiredMutings(): Promise<void> {
|
||||
const expiredMutings = await this.findExpiredMutings();
|
||||
await this.channelMutingRepository.delete({ id: In(expiredMutings.map(x => x.id)) });
|
||||
|
||||
const userIds = [...new Set(expiredMutings.map(x => x.userId))];
|
||||
for (const userId of userIds) {
|
||||
this.mutingChannelsCache.refresh(userId).then();
|
||||
}
|
||||
}
|
||||
|
||||
@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.mutingChannelsCache.refresh(body.userId).then();
|
||||
break;
|
||||
}
|
||||
case 'unmuteChannel': {
|
||||
this.mutingChannelsCache.delete(body.userId).then();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public dispose(): void {
|
||||
this.mutingChannelsCache.dispose();
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public onApplicationShutdown(signal?: string | undefined): void {
|
||||
this.dispose();
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import { SystemWebhookService } from '@/core/SystemWebhookService.js';
|
||||
import { UserSearchService } from '@/core/UserSearchService.js';
|
||||
import { WebhookTestService } from '@/core/WebhookTestService.js';
|
||||
import { FlashService } from '@/core/FlashService.js';
|
||||
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
|
||||
import { AccountMoveService } from './AccountMoveService.js';
|
||||
import { AccountUpdateService } from './AccountUpdateService.js';
|
||||
import { AiService } from './AiService.js';
|
||||
@@ -225,6 +226,7 @@ const $FeaturedService: Provider = { provide: 'FeaturedService', useExisting: Fe
|
||||
const $FanoutTimelineService: Provider = { provide: 'FanoutTimelineService', useExisting: FanoutTimelineService };
|
||||
const $FanoutTimelineEndpointService: Provider = { provide: 'FanoutTimelineEndpointService', useExisting: FanoutTimelineEndpointService };
|
||||
const $ChannelFollowingService: Provider = { provide: 'ChannelFollowingService', useExisting: ChannelFollowingService };
|
||||
const $ChannelMutingService: Provider = { provide: 'ChannelMutingService', useExisting: ChannelMutingService };
|
||||
const $ChatService: Provider = { provide: 'ChatService', useExisting: ChatService };
|
||||
const $RegistryApiService: Provider = { provide: 'RegistryApiService', useExisting: RegistryApiService };
|
||||
const $ReversiService: Provider = { provide: 'ReversiService', useExisting: ReversiService };
|
||||
@@ -378,6 +380,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
||||
FanoutTimelineService,
|
||||
FanoutTimelineEndpointService,
|
||||
ChannelFollowingService,
|
||||
ChannelMutingService,
|
||||
ChatService,
|
||||
RegistryApiService,
|
||||
ReversiService,
|
||||
@@ -527,6 +530,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
||||
$FanoutTimelineService,
|
||||
$FanoutTimelineEndpointService,
|
||||
$ChannelFollowingService,
|
||||
$ChannelMutingService,
|
||||
$ChatService,
|
||||
$RegistryApiService,
|
||||
$ReversiService,
|
||||
@@ -677,6 +681,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
||||
FanoutTimelineService,
|
||||
FanoutTimelineEndpointService,
|
||||
ChannelFollowingService,
|
||||
ChannelMutingService,
|
||||
ChatService,
|
||||
RegistryApiService,
|
||||
ReversiService,
|
||||
@@ -824,6 +829,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
|
||||
$FanoutTimelineService,
|
||||
$FanoutTimelineEndpointService,
|
||||
$ChannelFollowingService,
|
||||
$ChannelMutingService,
|
||||
$ChatService,
|
||||
$RegistryApiService,
|
||||
$ReversiService,
|
||||
|
||||
@@ -19,6 +19,8 @@ import { isQuote, isRenote } from '@/misc/is-renote.js';
|
||||
import { CacheService } from '@/core/CacheService.js';
|
||||
import { isReply } from '@/misc/is-reply.js';
|
||||
import { isInstanceMuted } from '@/misc/is-instance-muted.js';
|
||||
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
|
||||
import { isChannelRelated } from '@/misc/is-channel-related.js';
|
||||
|
||||
type NoteFilter = (note: MiNote) => boolean;
|
||||
|
||||
@@ -35,6 +37,7 @@ type TimelineOptions = {
|
||||
ignoreAuthorFromBlock?: boolean;
|
||||
ignoreAuthorFromMute?: boolean;
|
||||
ignoreAuthorFromInstanceBlock?: boolean;
|
||||
ignoreAuthorChannelFromMute?: boolean;
|
||||
excludeNoFiles?: boolean;
|
||||
excludeReplies?: boolean;
|
||||
excludePureRenotes: boolean;
|
||||
@@ -55,6 +58,7 @@ export class FanoutTimelineEndpointService {
|
||||
private cacheService: CacheService,
|
||||
private fanoutTimelineService: FanoutTimelineService,
|
||||
private utilityService: UtilityService,
|
||||
private channelMutingService: ChannelMutingService,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -111,11 +115,13 @@ export class FanoutTimelineEndpointService {
|
||||
userIdsWhoMeMutingRenotes,
|
||||
userIdsWhoBlockingMe,
|
||||
userMutedInstances,
|
||||
userMutedChannels,
|
||||
] = await Promise.all([
|
||||
this.cacheService.userMutingsCache.fetch(ps.me.id),
|
||||
this.cacheService.renoteMutingsCache.fetch(ps.me.id),
|
||||
this.cacheService.userBlockedCache.fetch(ps.me.id),
|
||||
this.cacheService.userProfileCache.fetch(me.id).then(p => new Set(p.mutedInstances)),
|
||||
this.channelMutingService.mutingChannelsCache.fetch(me.id),
|
||||
]);
|
||||
|
||||
const parentFilter = filter;
|
||||
@@ -126,6 +132,7 @@ export class FanoutTimelineEndpointService {
|
||||
if (isUserRelated(note.renote, userIdsWhoMeMuting, ps.ignoreAuthorFromMute)) return false;
|
||||
if (!ps.ignoreAuthorFromMute && isRenote(note) && !isQuote(note) && userIdsWhoMeMutingRenotes.has(note.userId)) return false;
|
||||
if (isInstanceMuted(note, userMutedInstances)) return false;
|
||||
if (isChannelRelated(note, userMutedChannels, ps.ignoreAuthorChannelFromMute)) return false;
|
||||
|
||||
return parentFilter(note);
|
||||
};
|
||||
|
||||
@@ -255,6 +255,8 @@ export interface InternalEventTypes {
|
||||
metaUpdated: { before?: MiMeta; after: MiMeta; };
|
||||
followChannel: { 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;
|
||||
mute: { muterId: MiUser['id']; muteeId: MiUser['id']; };
|
||||
unmute: { muterId: MiUser['id']; muteeId: MiUser['id']; };
|
||||
|
||||
@@ -604,6 +604,7 @@ export class NoteCreateService implements OnApplicationShutdown {
|
||||
replyUserHost: data.reply ? data.reply.userHost : null,
|
||||
renoteUserId: data.renote ? data.renote.userId : null,
|
||||
renoteUserHost: data.renote ? data.renote.userHost : null,
|
||||
renoteChannelId: data.renote ? data.renote.channelId : null,
|
||||
userHost: user.host,
|
||||
});
|
||||
|
||||
|
||||
@@ -106,6 +106,7 @@ function generateDummyNote(override?: Partial<MiNote>): MiNote {
|
||||
replyUserHost: null,
|
||||
renoteUserId: null,
|
||||
renoteUserHost: null,
|
||||
renoteChannelId: null,
|
||||
...override,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,36 +4,40 @@
|
||||
*/
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { In } from 'typeorm';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import type { ChannelFavoritesRepository, ChannelFollowingsRepository, ChannelsRepository, DriveFilesRepository, NotesRepository } from '@/models/_.js';
|
||||
import type {
|
||||
ChannelFavoritesRepository,
|
||||
ChannelFollowingsRepository, ChannelMutingRepository,
|
||||
ChannelsRepository,
|
||||
DriveFilesRepository,
|
||||
MiDriveFile,
|
||||
MiNote,
|
||||
NotesRepository,
|
||||
} from '@/models/_.js';
|
||||
import type { Packed } from '@/misc/json-schema.js';
|
||||
import type { } from '@/models/Blocking.js';
|
||||
import type { MiUser } from '@/models/User.js';
|
||||
import type { MiChannel } from '@/models/Channel.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { IdService } from '@/core/IdService.js';
|
||||
import { DriveFileEntityService } from './DriveFileEntityService.js';
|
||||
import { NoteEntityService } from './NoteEntityService.js';
|
||||
import { In } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class ChannelEntityService {
|
||||
constructor(
|
||||
@Inject(DI.channelsRepository)
|
||||
private channelsRepository: ChannelsRepository,
|
||||
|
||||
@Inject(DI.channelFollowingsRepository)
|
||||
private channelFollowingsRepository: ChannelFollowingsRepository,
|
||||
|
||||
@Inject(DI.channelFavoritesRepository)
|
||||
private channelFavoritesRepository: ChannelFavoritesRepository,
|
||||
|
||||
@Inject(DI.channelMutingRepository)
|
||||
private channelMutingRepository: ChannelMutingRepository,
|
||||
@Inject(DI.notesRepository)
|
||||
private notesRepository: NotesRepository,
|
||||
|
||||
@Inject(DI.driveFilesRepository)
|
||||
private driveFilesRepository: DriveFilesRepository,
|
||||
|
||||
private noteEntityService: NoteEntityService,
|
||||
private driveFileEntityService: DriveFileEntityService,
|
||||
private idService: IdService,
|
||||
@@ -45,31 +49,59 @@ export class ChannelEntityService {
|
||||
src: MiChannel['id'] | MiChannel,
|
||||
me?: { id: MiUser['id'] } | null | undefined,
|
||||
detailed?: boolean,
|
||||
opts?: {
|
||||
bannerFiles?: Map<MiDriveFile['id'], MiDriveFile>;
|
||||
followings?: Set<MiChannel['id']>;
|
||||
favorites?: Set<MiChannel['id']>;
|
||||
muting?: Set<MiChannel['id']>;
|
||||
pinnedNotes?: Map<MiNote['id'], MiNote>;
|
||||
},
|
||||
): Promise<Packed<'Channel'>> {
|
||||
const channel = typeof src === 'object' ? src : await this.channelsRepository.findOneByOrFail({ id: src });
|
||||
const meId = me ? me.id : null;
|
||||
|
||||
const banner = channel.bannerId ? await this.driveFilesRepository.findOneBy({ id: channel.bannerId }) : null;
|
||||
let bannerFile: MiDriveFile | null = null;
|
||||
if (channel.bannerId) {
|
||||
bannerFile = opts?.bannerFiles?.get(channel.bannerId)
|
||||
?? await this.driveFilesRepository.findOneByOrFail({ id: channel.bannerId });
|
||||
}
|
||||
|
||||
const isFollowing = meId ? await this.channelFollowingsRepository.exists({
|
||||
where: {
|
||||
followerId: meId,
|
||||
followeeId: channel.id,
|
||||
},
|
||||
}) : false;
|
||||
let isFollowing = false;
|
||||
let isFavorited = false;
|
||||
let isMuting = false;
|
||||
if (me) {
|
||||
isFollowing = opts?.followings?.has(channel.id) ?? await this.channelFollowingsRepository.exists({
|
||||
where: {
|
||||
followerId: me.id,
|
||||
followeeId: channel.id,
|
||||
},
|
||||
});
|
||||
|
||||
const isFavorited = meId ? await this.channelFavoritesRepository.exists({
|
||||
where: {
|
||||
userId: meId,
|
||||
channelId: channel.id,
|
||||
},
|
||||
}) : false;
|
||||
isFavorited = opts?.favorites?.has(channel.id) ?? await this.channelFavoritesRepository.exists({
|
||||
where: {
|
||||
userId: me.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
});
|
||||
|
||||
const pinnedNotes = channel.pinnedNoteIds.length > 0 ? await this.notesRepository.find({
|
||||
where: {
|
||||
id: In(channel.pinnedNoteIds),
|
||||
},
|
||||
}) : [];
|
||||
isMuting = opts?.muting?.has(channel.id) ?? await this.channelMutingRepository.exists({
|
||||
where: {
|
||||
userId: me.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const pinnedNotes = Array.of<MiNote>();
|
||||
if (channel.pinnedNoteIds.length > 0) {
|
||||
pinnedNotes.push(
|
||||
...(
|
||||
opts?.pinnedNotes
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
? channel.pinnedNoteIds.map(it => opts.pinnedNotes!.get(it)).filter(it => it != null)
|
||||
: await this.notesRepository.findBy({ id: In(channel.pinnedNoteIds) })
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
id: channel.id,
|
||||
@@ -78,7 +110,7 @@ export class ChannelEntityService {
|
||||
name: channel.name,
|
||||
description: channel.description,
|
||||
userId: channel.userId,
|
||||
bannerUrl: banner ? this.driveFileEntityService.getPublicUrl(banner) : null,
|
||||
bannerUrl: bannerFile ? this.driveFileEntityService.getPublicUrl(bannerFile) : null,
|
||||
pinnedNoteIds: channel.pinnedNoteIds,
|
||||
color: channel.color,
|
||||
isArchived: channel.isArchived,
|
||||
@@ -90,6 +122,7 @@ export class ChannelEntityService {
|
||||
...(me ? {
|
||||
isFollowing,
|
||||
isFavorited,
|
||||
isMuting,
|
||||
hasUnreadNote: false, // 後方互換性のため
|
||||
} : {}),
|
||||
|
||||
@@ -98,5 +131,72 @@ export class ChannelEntityService {
|
||||
} : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public async packMany(
|
||||
src: MiChannel['id'][] | MiChannel[],
|
||||
me?: { id: MiUser['id'] } | null | undefined,
|
||||
detailed?: boolean,
|
||||
): Promise<Packed<'Channel'>[]> {
|
||||
// IDのみの要素がある場合、DBからオブジェクトを取得して補う
|
||||
const channels = src.filter(it => typeof it === 'object') as MiChannel[];
|
||||
channels.push(
|
||||
...(await this.channelsRepository.find({
|
||||
where: {
|
||||
id: In(src.filter(it => typeof it !== 'object') as MiChannel['id'][]),
|
||||
},
|
||||
})),
|
||||
);
|
||||
channels.sort((a, b) => a.id.localeCompare(b.id));
|
||||
|
||||
const bannerFiles = await this.driveFilesRepository
|
||||
.findBy({
|
||||
id: In(channels.map(it => it.bannerId).filter(it => it != null)),
|
||||
})
|
||||
.then(it => new Map(it.map(it => [it.id, it])));
|
||||
|
||||
const followings = me
|
||||
? await this.channelFollowingsRepository
|
||||
.findBy({
|
||||
followerId: me.id,
|
||||
followeeId: In(channels.map(it => it.id)),
|
||||
})
|
||||
.then(it => new Set(it.map(it => it.followeeId)))
|
||||
: new Set<MiChannel['id']>();
|
||||
|
||||
const favorites = me
|
||||
? await this.channelFavoritesRepository
|
||||
.findBy({
|
||||
userId: me.id,
|
||||
channelId: In(channels.map(it => it.id)),
|
||||
})
|
||||
.then(it => new Set(it.map(it => it.channelId)))
|
||||
: new Set<MiChannel['id']>();
|
||||
|
||||
const muting = me
|
||||
? await this.channelMutingRepository
|
||||
.findBy({
|
||||
userId: me.id,
|
||||
channelId: In(channels.map(it => it.id)),
|
||||
})
|
||||
.then(it => new Set(it.map(it => it.channelId)))
|
||||
: new Set<MiChannel['id']>();
|
||||
|
||||
const pinnedNotes = await this.notesRepository
|
||||
.find({
|
||||
where: {
|
||||
id: In(channels.flatMap(it => it.pinnedNoteIds)),
|
||||
},
|
||||
})
|
||||
.then(it => new Map(it.map(it => [it.id, it])));
|
||||
|
||||
return Promise.all(channels.map(it => this.pack(it, me, detailed, {
|
||||
bannerFiles,
|
||||
followings,
|
||||
favorites,
|
||||
muting,
|
||||
pinnedNotes,
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user