From da94dbee0030fb8ae60370a04ab542721f3df253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B4=87=E5=B3=B0=20=E6=9C=94=E8=8F=AF?= Date: Mon, 18 Nov 2024 21:46:28 +0900 Subject: [PATCH] =?UTF-8?q?Mod:=20UserReactionBlockingService=E3=81=A8User?= =?UTF-8?q?BlockingService=E3=82=92=E7=B5=B1=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/core/CoreModule.ts | 6 - packages/backend/src/core/ReactionService.ts | 5 +- .../backend/src/core/UserBlockingService.ts | 74 ++++++++++- .../src/core/UserReactionBlockingService.ts | 120 ------------------ .../blocking-reaction-user/create.ts | 6 +- .../blocking-reaction-user/delete.ts | 6 +- packages/misskey-js/src/autogen/types.ts | 3 +- 7 files changed, 81 insertions(+), 139 deletions(-) delete mode 100644 packages/backend/src/core/UserReactionBlockingService.ts diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts index 393bdf26f7..734d135648 100644 --- a/packages/backend/src/core/CoreModule.ts +++ b/packages/backend/src/core/CoreModule.ts @@ -58,7 +58,6 @@ import { S3Service } from './S3Service.js'; import { SignupService } from './SignupService.js'; import { WebAuthnService } from './WebAuthnService.js'; import { UserBlockingService } from './UserBlockingService.js'; -import { UserReactionBlockingService } from './UserReactionBlockingService.js'; import { CacheService } from './CacheService.js'; import { UserService } from './UserService.js'; import { UserFollowingService } from './UserFollowingService.js'; @@ -203,7 +202,6 @@ const $S3Service: Provider = { provide: 'S3Service', useExisting: S3Service }; const $SignupService: Provider = { provide: 'SignupService', useExisting: SignupService }; const $WebAuthnService: Provider = { provide: 'WebAuthnService', useExisting: WebAuthnService }; const $UserBlockingService: Provider = { provide: 'UserBlockingService', useExisting: UserBlockingService }; -const $UserReactionBlockingService: Provider = { provide: 'UserReactionBlockingService', useExisting: UserReactionBlockingService }; const $CacheService: Provider = { provide: 'CacheService', useExisting: CacheService }; const $UserService: Provider = { provide: 'UserService', useExisting: UserService }; const $UserFollowingService: Provider = { provide: 'UserFollowingService', useExisting: UserFollowingService }; @@ -355,7 +353,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting SignupService, WebAuthnService, UserBlockingService, - UserReactionBlockingService, CacheService, UserService, UserFollowingService, @@ -503,7 +500,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting $SignupService, $WebAuthnService, $UserBlockingService, - $UserReactionBlockingService, $CacheService, $UserService, $UserFollowingService, @@ -652,7 +648,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting SignupService, WebAuthnService, UserBlockingService, - UserReactionBlockingService, CacheService, UserService, UserFollowingService, @@ -799,7 +794,6 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting $SignupService, $WebAuthnService, $UserBlockingService, - $UserReactionBlockingService, $CacheService, $UserService, $UserFollowingService, diff --git a/packages/backend/src/core/ReactionService.ts b/packages/backend/src/core/ReactionService.ts index 72c027233a..6924841eca 100644 --- a/packages/backend/src/core/ReactionService.ts +++ b/packages/backend/src/core/ReactionService.ts @@ -30,7 +30,6 @@ import { trackPromise } from '@/misc/promise-tracker.js'; import { isQuote, isRenote } from '@/misc/is-renote.js'; import { ReactionsBufferingService } from '@/core/ReactionsBufferingService.js'; import { PER_NOTE_REACTION_USER_PAIR_CACHE_MAX } from '@/const.js'; -import { BlockingReactionUserService } from '@/core/BlockingReactionUserService.js'; const FALLBACK = '\u2764'; @@ -92,7 +91,7 @@ export class ReactionService { private userEntityService: UserEntityService, private noteEntityService: NoteEntityService, private userBlockingService: UserBlockingService, - private blockingReactionUserService: BlockingReactionUserService, + private userReactionBlockingService: UserBlockingService, private reactionsBufferingService: ReactionsBufferingService, private idService: IdService, private featuredService: FeaturedService, @@ -109,7 +108,7 @@ export class ReactionService { // Check blocking if (note.userId !== user.id) { const blocked = await this.userBlockingService.checkBlocked(note.userId, user.id); - const reactionBlocked = await this.blockingReactionUserService.checkBlocked(note.userId, user.id); + const reactionBlocked = await this.userReactionBlockingService.checkBlocked(note.userId, user.id); if (blocked || reactionBlocked) { throw new IdentifiableError('e70412a4-7197-4726-8e74-f3e0deb92aa7'); } diff --git a/packages/backend/src/core/UserBlockingService.ts b/packages/backend/src/core/UserBlockingService.ts index 3de99baf0c..328205d645 100644 --- a/packages/backend/src/core/UserBlockingService.ts +++ b/packages/backend/src/core/UserBlockingService.ts @@ -26,7 +26,6 @@ import { UserWebhookService } from '@/core/UserWebhookService.js'; import { bindThis } from '@/decorators.js'; import { CacheService } from '@/core/CacheService.js'; import { UserFollowingService } from '@/core/UserFollowingService.js'; -import { UserReactionBlockingService } from '@/core/UserReactionBlockingService.js'; @Injectable() export class UserBlockingService implements OnModuleInit { @@ -50,7 +49,6 @@ export class UserBlockingService implements OnModuleInit { private cacheService: CacheService, private userEntityService: UserEntityService, - private userReactionBlockingService: UserReactionBlockingService, private idService: IdService, private queueService: QueueService, private globalEventService: GlobalEventService, @@ -93,7 +91,7 @@ export class UserBlockingService implements OnModuleInit { }); if (blocking.blockType === MiBlockingType.Reaction) { - await this.userReactionBlockingService.unblock(blocker, blockee); + await this.reactionUnblock(blocker, blockee); } blocking.blockType = MiBlockingType.User; await this.blockingsRepository.insert(blocking); @@ -216,4 +214,74 @@ export class UserBlockingService implements OnModuleInit { public async checkBlocked(blockerId: MiUser['id'], blockeeId: MiUser['id']): Promise { return (await this.cacheService.userBlockingCache.fetch(blockerId)).has(blockeeId); } + + + @bindThis + public async reactionBlock(blocker: MiUser, blockee: MiUser, silent = false) { + const blocking = await this.blockingsRepository.findOneBy({ + blockerId: blocker.id, + blockeeId: blockee.id, + }).then(blocking => { + if (blocking) { + return blocking; + } + return { + id: this.idService.gen(), + blocker, + blockerId: blocker.id, + blockee, + blockeeId: blockee.id, + blockType: MiBlockingType.Reaction, + } as MiBlocking; + }); + + if (blocking.blockType === MiBlockingType.User) { + await this.unblock(blocker, blockee); + } + blocking.blockType = MiBlockingType.Reaction; + await this.blockingsRepository.insert(blocking); + + this.cacheService.userReactionBlockingCache.refresh(blocker.id); + this.cacheService.userReactionBlockedCache.refresh(blockee.id); + + this.globalEventService.publishInternalEvent('blockingReactionCreated', { + blockerId: blocker.id, + blockeeId: blockee.id, + }); + } + + @bindThis + public async reactionUnblock(blocker: MiUser, blockee: MiUser) { + const blocking = await this.blockingsRepository.findOneBy({ + blockerId: blocker.id, + blockeeId: blockee.id, + blockType: MiBlockingType.Reaction, + }); + + if (blocking == null) { + this.logger.warn('Unblock requested, but the target was not blocked.'); + return; + } + + // Since we already have the blocker and blockee, we do not need to fetch + // them in the query above and can just manually insert them here. + blocking.blocker = blocker; + blocking.blockee = blockee; + + await this.blockingsRepository.delete(blocking.id); + + this.cacheService.userReactionBlockingCache.refresh(blocker.id); + this.cacheService.userReactionBlockedCache.refresh(blockee.id); + + this.globalEventService.publishInternalEvent('blockingReactionDeleted', { + blockerId: blocker.id, + blockeeId: blockee.id, + }); + } + + @bindThis + public async checkReactionBlocked(blockerId: MiUser['id'], blockeeId: MiUser['id']): Promise { + return (await this.cacheService.userReactionBlockingCache.fetch(blockerId)).has(blockeeId); + } } + diff --git a/packages/backend/src/core/UserReactionBlockingService.ts b/packages/backend/src/core/UserReactionBlockingService.ts deleted file mode 100644 index 750f77a85a..0000000000 --- a/packages/backend/src/core/UserReactionBlockingService.ts +++ /dev/null @@ -1,120 +0,0 @@ -/* - * SPDX-FileCopyrightText: syuilo and misskey-project - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { Inject, Injectable } from '@nestjs/common'; -import { ModuleRef } from '@nestjs/core'; -import { IdService } from '@/core/IdService.js'; -import type { MiUser } from '@/models/User.js'; -import type { MiBlocking } from '@/models/Blocking.js'; -import { QueueService } from '@/core/QueueService.js'; -import { GlobalEventService } from '@/core/GlobalEventService.js'; -import { DI } from '@/di-symbols.js'; -import type { BlockingsRepository, UserListsRepository } from '@/models/_.js'; -import Logger from '@/logger.js'; -import { UserEntityService } from '@/core/entities/UserEntityService.js'; -import { ApRendererService } from '@/core/activitypub/ApRendererService.js'; -import { LoggerService } from '@/core/LoggerService.js'; -import { UserWebhookService } from '@/core/UserWebhookService.js'; -import { bindThis } from '@/decorators.js'; -import { CacheService } from '@/core/CacheService.js'; -import { UserFollowingService } from '@/core/UserFollowingService.js'; -import { MiBlockingType } from '@/models/Blocking.js'; -import { UserBlockingService } from '@/core/UserBlockingService.js'; - -@Injectable() -export class UserReactionBlockingService { - private logger: Logger; - private userFollowingService: UserFollowingService; - - constructor( - private moduleRef: ModuleRef, - - @Inject(DI.blockingsRepository) - private blockingsRepository: BlockingsRepository, - - @Inject(DI.userListsRepository) - private userListsRepository: UserListsRepository, - - private cacheService: CacheService, - private userEntityService: UserEntityService, - private userBlockingService: UserBlockingService, - private idService: IdService, - private queueService: QueueService, - private globalEventService: GlobalEventService, - private webhookService: UserWebhookService, - private apRendererService: ApRendererService, - private loggerService: LoggerService, - ) { - this.logger = this.loggerService.getLogger('user-block'); - } - - @bindThis - public async block(blocker: MiUser, blockee: MiUser, silent = false) { - const blocking = await this.blockingsRepository.findOneBy({ - blockerId: blocker.id, - blockeeId: blockee.id, - }).then(blocking => { - if (blocking) { - return blocking; - } - return { - id: this.idService.gen(), - blocker, - blockerId: blocker.id, - blockee, - blockeeId: blockee.id, - blockType: MiBlockingType.Reaction, - } as MiBlocking; - }); - - if (blocking.blockType === MiBlockingType.User) { - await this.userBlockingService.unblock(blocker, blockee); - } - blocking.blockType = MiBlockingType.Reaction; - await this.blockingsRepository.insert(blocking); - - this.cacheService.userReactionBlockingCache.refresh(blocker.id); - this.cacheService.userReactionBlockedCache.refresh(blockee.id); - - this.globalEventService.publishInternalEvent('blockingReactionCreated', { - blockerId: blocker.id, - blockeeId: blockee.id, - }); - } - - @bindThis - public async unblock(blocker: MiUser, blockee: MiUser) { - const blocking = await this.blockingsRepository.findOneBy({ - blockerId: blocker.id, - blockeeId: blockee.id, - blockType: MiBlockingType.Reaction, - }); - - if (blocking == null) { - this.logger.warn('Unblock requested, but the target was not blocked.'); - return; - } - - // Since we already have the blocker and blockee, we do not need to fetch - // them in the query above and can just manually insert them here. - blocking.blocker = blocker; - blocking.blockee = blockee; - - await this.blockingsRepository.delete(blocking.id); - - this.cacheService.userReactionBlockingCache.refresh(blocker.id); - this.cacheService.userReactionBlockedCache.refresh(blockee.id); - - this.globalEventService.publishInternalEvent('blockingReactionDeleted', { - blockerId: blocker.id, - blockeeId: blockee.id, - }); - } - - @bindThis - public async checkBlocked(blockerId: MiUser['id'], blockeeId: MiUser['id']): Promise { - return (await this.cacheService.userReactionBlockingCache.fetch(blockerId)).has(blockeeId); - } -} diff --git a/packages/backend/src/server/api/endpoints/blocking-reaction-user/create.ts b/packages/backend/src/server/api/endpoints/blocking-reaction-user/create.ts index d9ec2f0503..8074bd0a1d 100644 --- a/packages/backend/src/server/api/endpoints/blocking-reaction-user/create.ts +++ b/packages/backend/src/server/api/endpoints/blocking-reaction-user/create.ts @@ -11,7 +11,7 @@ import { MiBlockingType } from '@/models/_.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { DI } from '@/di-symbols.js'; import { GetterService } from '@/server/api/GetterService.js'; -import { UserReactionBlockingService } from '@/core/UserReactionBlockingService.js'; +import { UserBlockingService } from '@/core/UserBlockingService.js'; import { ApiError } from '../../error.js'; export const meta = { @@ -72,7 +72,7 @@ export default class extends Endpoint { // eslint- private userEntityService: UserEntityService, private getterService: GetterService, - private userReactionBlockingService: UserReactionBlockingService, + private userBlockingService: UserBlockingService, ) { super(meta, paramDef, async (ps, me) => { const blocker = await this.usersRepository.findOneByOrFail({ id: me.id }); @@ -101,7 +101,7 @@ export default class extends Endpoint { // eslint- throw new ApiError(meta.errors.alreadyBlocking); } - await this.userReactionBlockingService.block(blocker, blockee); + await this.userBlockingService.reactionBlock(blocker, blockee); return await this.userEntityService.pack(blockee.id, blocker, { schema: 'UserDetailedNotMe', diff --git a/packages/backend/src/server/api/endpoints/blocking-reaction-user/delete.ts b/packages/backend/src/server/api/endpoints/blocking-reaction-user/delete.ts index c608fa486d..02c89fe0ae 100644 --- a/packages/backend/src/server/api/endpoints/blocking-reaction-user/delete.ts +++ b/packages/backend/src/server/api/endpoints/blocking-reaction-user/delete.ts @@ -8,9 +8,9 @@ import { Inject, Injectable } from '@nestjs/common'; import { Endpoint } from '@/server/api/endpoint-base.js'; import type { UsersRepository, BlockingsRepository, MiBlockingType } from '@/models/_.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; -import { UserReactionBlockingService } from '@/core/UserReactionBlockingService.js'; import { DI } from '@/di-symbols.js'; import { GetterService } from '@/server/api/GetterService.js'; +import { UserBlockingService } from '@/core/UserBlockingService.js'; import { ApiError } from '../../error.js'; export const meta = { @@ -71,7 +71,7 @@ export default class extends Endpoint { // eslint- private userEntityService: UserEntityService, private getterService: GetterService, - private userReactionBlockingService: UserReactionBlockingService, + private userBlockingService: UserBlockingService, ) { super(meta, paramDef, async (ps, me) => { const blocker = await this.usersRepository.findOneByOrFail({ id: me.id }); @@ -101,7 +101,7 @@ export default class extends Endpoint { // eslint- } // Delete blocking - await this.userReactionBlockingService.unblock(blocker, blockee); + await this.userBlockingService.reactionUnblock(blocker, blockee); return await this.userEntityService.pack(blockee.id, blocker, { schema: 'UserDetailedNotMe', diff --git a/packages/misskey-js/src/autogen/types.ts b/packages/misskey-js/src/autogen/types.ts index 4a574781ab..4d2126a149 100644 --- a/packages/misskey-js/src/autogen/types.ts +++ b/packages/misskey-js/src/autogen/types.ts @@ -4515,7 +4515,8 @@ export type components = { /** Format: id */ blockeeId: string; blockee: components['schemas']['UserDetailedNotMe']; - isReactionBlock: boolean; + /** @enum {string} */ + blockType: 'user' | 'reaction'; }; Hashtag: { /** @example misskey */