From fdf78d53f1fee2cbda16c6e14059dbf41ea62044 Mon Sep 17 00:00:00 2001 From: ahmedGamalEllabban Date: Sat, 29 Nov 2025 20:26:47 +0200 Subject: [PATCH] Add flags to determine block/mute statuses in profile responses --- src/profile/dto/profile-response.dto.ts | 18 +++++ src/profile/profile.service.ts | 103 +++++++++++++++++++++++- 2 files changed, 118 insertions(+), 3 deletions(-) diff --git a/src/profile/dto/profile-response.dto.ts b/src/profile/dto/profile-response.dto.ts index 63fd53e..be0fc12 100644 --- a/src/profile/dto/profile-response.dto.ts +++ b/src/profile/dto/profile-response.dto.ts @@ -94,6 +94,24 @@ export class ProfileResponseDto { }) is_deactivated?: boolean; + @ApiPropertyOptional({ + description: 'Whether the user has been blocked by the profile owner', + example: false, + }) + is_been_blocked?: boolean; + + @ApiPropertyOptional({ + description: 'Whether the profile owner is blocked by the current user', + example: false, + }) + is_blocked_by_me?: boolean; + + @ApiPropertyOptional({ + description: 'Whether the profile owner is muted by the current user', + example: false, + }) + is_muted_by_me?: boolean; + @ApiProperty({ description: 'Profile creation timestamp', example: '2025-01-01T00:00:00.000Z', diff --git a/src/profile/profile.service.ts b/src/profile/profile.service.ts index 401c1bb..597ab88 100644 --- a/src/profile/profile.service.ts +++ b/src/profile/profile.service.ts @@ -39,7 +39,13 @@ export class ProfileService { }; } - private formatProfileResponseWithFollowStatus(profile: any, isFollowedByMe: boolean) { + private formatProfileResponseWithFollowStatus( + profile: any, + isFollowedByMe: boolean, + isBeenBlocked: boolean = false, + isBlockedByMe: boolean = false, + isMutedByMe: boolean = false, + ) { const { User, ...profileData } = profile; const { _count, ...userData } = User; @@ -49,6 +55,9 @@ export class ProfileService { followers_count: _count.Followers, following_count: _count.Following, is_followed_by_me: isFollowedByMe, + is_been_blocked: isBeenBlocked, + is_blocked_by_me: isBlockedByMe, + is_muted_by_me: isMutedByMe, }; } @@ -70,7 +79,12 @@ export class ProfileService { } let isFollowedByMe = false; + let isBeenBlocked = false; + let isBlockedByMe = false; + let isMutedByMe = false; + if (currentUserId && currentUserId !== userId) { + // Check if current user follows the profile user const followRelation = await this.prismaService.follow.findUnique({ where: { followerId_followingId: { @@ -80,9 +94,48 @@ export class ProfileService { }, }); isFollowedByMe = !!followRelation; + + // Check if the profile user has blocked the current user + const blockByProfile = await this.prismaService.block.findUnique({ + where: { + blockerId_blockedId: { + blockerId: userId, + blockedId: currentUserId, + }, + }, + }); + isBeenBlocked = !!blockByProfile; + + // Check if current user has blocked the profile user + const blockByCurrentUser = await this.prismaService.block.findUnique({ + where: { + blockerId_blockedId: { + blockerId: currentUserId, + blockedId: userId, + }, + }, + }); + isBlockedByMe = !!blockByCurrentUser; + + // Check if current user has muted the profile user + const muteByCurrentUser = await this.prismaService.mute.findUnique({ + where: { + muterId_mutedId: { + muterId: currentUserId, + mutedId: userId, + }, + }, + }); + isMutedByMe = !!muteByCurrentUser; } - return this.formatProfileResponseWithFollowStatus(profile, isFollowedByMe); + return this.formatProfileResponseWithFollowStatus( + profile, + isFollowedByMe, + isBeenBlocked, + isBlockedByMe, + isMutedByMe, + ); } public async getProfileByUsername(username: string, currentUserId?: number) { @@ -105,7 +158,12 @@ export class ProfileService { } let isFollowedByMe = false; + let isBeenBlocked = false; + let isBlockedByMe = false; + let isMutedByMe = false; + if (currentUserId && currentUserId !== profile.user_id) { + // Check if current user follows the profile user const followRelation = await this.prismaService.follow.findUnique({ where: { followerId_followingId: { @@ -115,9 +173,48 @@ export class ProfileService { }, }); isFollowedByMe = !!followRelation; + + // Check if the profile user has blocked the current user + const blockByProfile = await this.prismaService.block.findUnique({ + where: { + blockerId_blockedId: { + blockerId: profile.user_id, + blockedId: currentUserId, + }, + }, + }); + isBeenBlocked = !!blockByProfile; + + // Check if current user has blocked the profile user + const blockByCurrentUser = await this.prismaService.block.findUnique({ + where: { + blockerId_blockedId: { + blockerId: currentUserId, + blockedId: profile.user_id, + }, + }, + }); + isBlockedByMe = !!blockByCurrentUser; + + // Check if current user has muted the profile user + const muteByCurrentUser = await this.prismaService.mute.findUnique({ + where: { + muterId_mutedId: { + muterId: currentUserId, + mutedId: profile.user_id, + }, + }, + }); + isMutedByMe = !!muteByCurrentUser; } - return this.formatProfileResponseWithFollowStatus(profile, isFollowedByMe); + return this.formatProfileResponseWithFollowStatus( + profile, + isFollowedByMe, + isBeenBlocked, + isBlockedByMe, + isMutedByMe, + ); } public async updateProfile(userId: number, updateProfileDto: UpdateProfileDto) {