タイムライン取得処理への組み込み

This commit is contained in:
samunohito 2024-06-11 21:13:31 +09:00
parent 94ededa68d
commit 7d7c2d4daf
18 changed files with 512 additions and 43 deletions

View file

@ -6,7 +6,7 @@
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 type { ChannelMutingRepository, ChannelsRepository, MiChannel, MiUser } from '@/models/_.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEvents, GlobalEventService } from '@/core/GlobalEventService.js';
import { bindThis } from '@/decorators.js';
@ -21,6 +21,8 @@ export class ChannelMutingService {
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,
@ -40,6 +42,61 @@ export class ChannelMutingService {
this.redisForSub.on('message', this.onMessage);
}
/**
* .
* @param params
* @param [opts]
* @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?: {
joinUser?: boolean;
joinBannerFile?: boolean;
},
): Promise<MiChannel[]> {
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(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 params
* @param params.requestUserId
*/
@bindThis
public async isMuted(params: {
requestUserId: MiUser['id'],
targetChannelId: MiChannel['id'],
}): Promise<boolean> {
const mutedChannels = await this.userMutingChannelsCache.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'],
@ -59,6 +116,10 @@ export class ChannelMutingService {
});
}
/**
* .
* @param params
*/
@bindThis
public async unmute(params: {
requestUserId: MiUser['id'],

View file

@ -17,6 +17,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 TimelineOptions = {
untilId: string | null,
@ -33,6 +35,7 @@ type TimelineOptions = {
excludeNoFiles?: boolean;
excludeReplies?: boolean;
excludePureRenotes: boolean;
excludeMutedChannels?: boolean;
dbFallback: (untilId: string | null, sinceId: string | null, limit: number) => Promise<MiNote[]>,
};
@ -45,6 +48,7 @@ export class FanoutTimelineEndpointService {
private noteEntityService: NoteEntityService,
private cacheService: CacheService,
private fanoutTimelineService: FanoutTimelineService,
private channelMutingService: ChannelMutingService,
) {
}
@ -101,11 +105,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)),
ps.excludeMutedChannels ? this.channelMutingService.userMutingChannelsCache.fetch(me.id) : Promise.resolve(new Set<string>()),
]);
const parentFilter = filter;
@ -114,6 +120,7 @@ export class FanoutTimelineEndpointService {
if (isUserRelated(note, 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 (ps.excludeMutedChannels && isChannelRelated(note, userMutedChannels)) return false;
return parentFilter(note);
};

View file

@ -434,6 +434,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,
});

View file

@ -4,36 +4,39 @@
*/
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,
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 { isNotNull } from '@/misc/is-not-null.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.notesRepository)
private notesRepository: NotesRepository,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private noteEntityService: NoteEntityService,
private driveFileEntityService: DriveFileEntityService,
private idService: IdService,
@ -45,31 +48,50 @@ 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']>;
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 isFavorite = 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;
isFavorite = 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),
},
}) : [];
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(isNotNull)
: await this.notesRepository.findBy({ id: In(channel.pinnedNoteIds) })
),
);
}
return {
id: channel.id,
@ -78,7 +100,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,
@ -89,7 +111,7 @@ export class ChannelEntityService {
...(me ? {
isFollowing,
isFavorited,
isFavorite,
hasUnreadNote: false, // 後方互換性のため
} : {}),
@ -98,5 +120,62 @@ 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 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,
pinnedNotes,
})));
}
}