fix: Try using CacheService to avoid excess db lookups

This isn't perfect, theoretically if some massive number of users
blocked the user making this request the set lookup could take a long
amount of time, but eh, it works, and that scenario is highly unlikely.
This commit is contained in:
Julia Johannesen 2024-11-14 23:43:19 -05:00
parent 5764fa55cb
commit cc4e99fdde
No known key found for this signature in database
GPG Key ID: 4A1377AF3E7FBC46

View File

@ -11,12 +11,13 @@ import type { Packed } from '@/misc/json-schema.js';
import { awaitAll } from '@/misc/prelude/await-all.js'; import { awaitAll } from '@/misc/prelude/await-all.js';
import type { MiUser } from '@/models/User.js'; import type { MiUser } from '@/models/User.js';
import type { MiNote } from '@/models/Note.js'; import type { MiNote } from '@/models/Note.js';
import type { BlockingsRepository, UsersRepository, NotesRepository, FollowingsRepository, PollsRepository, PollVotesRepository, NoteReactionsRepository, ChannelsRepository, MiMeta } from '@/models/_.js'; import type { UsersRepository, NotesRepository, FollowingsRepository, PollsRepository, PollVotesRepository, NoteReactionsRepository, ChannelsRepository, MiMeta } from '@/models/_.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { DebounceLoader } from '@/misc/loader.js'; import { DebounceLoader } from '@/misc/loader.js';
import { IdService } from '@/core/IdService.js'; import { IdService } from '@/core/IdService.js';
import { ReactionsBufferingService } from '@/core/ReactionsBufferingService.js'; import { ReactionsBufferingService } from '@/core/ReactionsBufferingService.js';
import type { OnModuleInit } from '@nestjs/common'; import type { OnModuleInit } from '@nestjs/common';
import type { CacheService } from '../CacheService.js';
import type { CustomEmojiService } from '../CustomEmojiService.js'; import type { CustomEmojiService } from '../CustomEmojiService.js';
import type { ReactionService } from '../ReactionService.js'; import type { ReactionService } from '../ReactionService.js';
import type { UserEntityService } from './UserEntityService.js'; import type { UserEntityService } from './UserEntityService.js';
@ -27,6 +28,7 @@ import type { Config } from '@/config.js';
export class NoteEntityService implements OnModuleInit { export class NoteEntityService implements OnModuleInit {
private userEntityService: UserEntityService; private userEntityService: UserEntityService;
private driveFileEntityService: DriveFileEntityService; private driveFileEntityService: DriveFileEntityService;
private cacheService: CacheService;
private customEmojiService: CustomEmojiService; private customEmojiService: CustomEmojiService;
private reactionService: ReactionService; private reactionService: ReactionService;
private reactionsBufferingService: ReactionsBufferingService; private reactionsBufferingService: ReactionsBufferingService;
@ -39,9 +41,6 @@ export class NoteEntityService implements OnModuleInit {
@Inject(DI.meta) @Inject(DI.meta)
private meta: MiMeta, private meta: MiMeta,
@Inject(DI.blockingsRepository)
private blockingsRepository: BlockingsRepository,
@Inject(DI.usersRepository) @Inject(DI.usersRepository)
private usersRepository: UsersRepository, private usersRepository: UsersRepository,
@ -78,6 +77,7 @@ export class NoteEntityService implements OnModuleInit {
onModuleInit() { onModuleInit() {
this.userEntityService = this.moduleRef.get('UserEntityService'); this.userEntityService = this.moduleRef.get('UserEntityService');
this.driveFileEntityService = this.moduleRef.get('DriveFileEntityService'); this.driveFileEntityService = this.moduleRef.get('DriveFileEntityService');
this.cacheService = this.moduleRef.get('CacheService');
this.customEmojiService = this.moduleRef.get('CustomEmojiService'); this.customEmojiService = this.moduleRef.get('CustomEmojiService');
this.reactionService = this.moduleRef.get('ReactionService'); this.reactionService = this.moduleRef.get('ReactionService');
this.reactionsBufferingService = this.moduleRef.get('ReactionsBufferingService'); this.reactionsBufferingService = this.moduleRef.get('ReactionsBufferingService');
@ -146,12 +146,7 @@ export class NoteEntityService implements OnModuleInit {
} }
if (!hide && meId && packedNote.userId !== meId) { if (!hide && meId && packedNote.userId !== meId) {
const isBlocked = await this.blockingsRepository.exists({ const isBlocked = (await this.cacheService.userBlockedCache.fetch(meId)).has(packedNote.userId);
where: {
blockeeId: meId,
blockerId: packedNote.userId,
},
});
if (isBlocked) hide = true; if (isBlocked) hide = true;
} }
@ -283,12 +278,7 @@ export class NoteEntityService implements OnModuleInit {
} else { } else {
// フォロワーかどうか // フォロワーかどうか
const [blocked, following, user] = await Promise.all([ const [blocked, following, user] = await Promise.all([
this.blockingsRepository.exists({ this.cacheService.userBlockingCache.fetch(meId).then((ids) => ids.has(note.userId)),
where: {
blockeeId: meId,
blockerId: note.userId,
},
}),
this.followingsRepository.count({ this.followingsRepository.count({
where: { where: {
followeeId: note.userId, followeeId: note.userId,
@ -313,12 +303,7 @@ export class NoteEntityService implements OnModuleInit {
} }
if (meId != null) { if (meId != null) {
const isBlocked = await this.blockingsRepository.exists({ const isBlocked = (await this.cacheService.userBlockedCache.fetch(meId)).has(note.userId);
where: {
blockeeId: meId,
blockerId: note.userId,
},
});
if (isBlocked) return false; if (isBlocked) return false;
} }