From 9a9570ab6245df69d9d89c1f8733c1eb49da2995 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 13 Jul 2024 10:39:24 +0100 Subject: [PATCH] mute users when blocking them - fixes #558 we already unfollow them, and make them unfollow us, so muting them is a very sensible thing to want since the mute is handled separately, users can un-mute the people they block (same as they can re-follow them) --- .../server/api/endpoints/blocking/create.ts | 21 +++++++++++++++++-- .../server/api/endpoints/blocking/delete.ts | 19 +++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/blocking/create.ts b/packages/backend/src/server/api/endpoints/blocking/create.ts index 5066215749..fd6c164449 100644 --- a/packages/backend/src/server/api/endpoints/blocking/create.ts +++ b/packages/backend/src/server/api/endpoints/blocking/create.ts @@ -6,9 +6,10 @@ import ms from 'ms'; import { Inject, Injectable } from '@nestjs/common'; import { Endpoint } from '@/server/api/endpoint-base.js'; -import type { UsersRepository, BlockingsRepository } from '@/models/_.js'; +import type { UsersRepository, BlockingsRepository, MutingsRepository } from '@/models/_.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { UserBlockingService } from '@/core/UserBlockingService.js'; +import { UserMutingService } from '@/core/UserMutingService.js'; import { DI } from '@/di-symbols.js'; import { GetterService } from '@/server/api/GetterService.js'; import { ApiError } from '../../error.js'; @@ -69,9 +70,13 @@ export default class extends Endpoint { // eslint- @Inject(DI.blockingsRepository) private blockingsRepository: BlockingsRepository, + @Inject(DI.mutingsRepository) + private mutingsRepository: MutingsRepository, + private userEntityService: UserEntityService, private getterService: GetterService, private userBlockingService: UserBlockingService, + private userMutingService: UserMutingService, ) { super(meta, paramDef, async (ps, me) => { const blocker = await this.usersRepository.findOneByOrFail({ id: me.id }); @@ -99,7 +104,19 @@ export default class extends Endpoint { // eslint- throw new ApiError(meta.errors.alreadyBlocking); } - await this.userBlockingService.block(blocker, blockee); + await Promise.all([ + this.userBlockingService.block(blocker, blockee), + this.mutingsRepository.exists({ + where: { + muterId: blocker.id, + muteeId: blockee.id, + }, + }).then(exists => { + if (!exists) { + this.userMutingService.mute(blocker, blockee, null); + } + }), + ]); return await this.userEntityService.pack(blockee.id, blocker, { schema: 'UserDetailedNotMe', diff --git a/packages/backend/src/server/api/endpoints/blocking/delete.ts b/packages/backend/src/server/api/endpoints/blocking/delete.ts index cebb307338..a4cd1b1cde 100644 --- a/packages/backend/src/server/api/endpoints/blocking/delete.ts +++ b/packages/backend/src/server/api/endpoints/blocking/delete.ts @@ -6,9 +6,10 @@ import ms from 'ms'; import { Inject, Injectable } from '@nestjs/common'; import { Endpoint } from '@/server/api/endpoint-base.js'; -import type { UsersRepository, BlockingsRepository } from '@/models/_.js'; +import type { UsersRepository, BlockingsRepository, MutingsRepository } from '@/models/_.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { UserBlockingService } from '@/core/UserBlockingService.js'; +import { UserMutingService } from '@/core/UserMutingService.js'; import { DI } from '@/di-symbols.js'; import { GetterService } from '@/server/api/GetterService.js'; import { ApiError } from '../../error.js'; @@ -69,9 +70,13 @@ export default class extends Endpoint { // eslint- @Inject(DI.blockingsRepository) private blockingsRepository: BlockingsRepository, + @Inject(DI.mutingsRepository) + private mutingsRepository: MutingsRepository, + private userEntityService: UserEntityService, private getterService: GetterService, private userBlockingService: UserBlockingService, + private userMutingService: UserMutingService, ) { super(meta, paramDef, async (ps, me) => { const blocker = await this.usersRepository.findOneByOrFail({ id: me.id }); @@ -100,7 +105,17 @@ export default class extends Endpoint { // eslint- } // Delete blocking - await this.userBlockingService.unblock(blocker, blockee); + await Promise.all([ + this.userBlockingService.unblock(blocker, blockee), + this.mutingsRepository.findOneBy({ + muterId: blocker.id, + muteeId: blockee.id, + }).then(exists => { + if (exists) { + this.userMutingService.unmute([exists]); + } + }), + ]); return await this.userEntityService.pack(blockee.id, blocker, { schema: 'UserDetailedNotMe',