Skip to content

Commit

Permalink
ADD: mute room member (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
automerge-pingpong[bot] committed Oct 15, 2023
2 parents 277f350 + 2ecd0fa commit 88a055b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/code/src/rooms/dto/mute-member.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class MuteMemberDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
roomId: string;
@ApiProperty()
@IsNotEmpty()
@IsString()
memberId: string;
}
10 changes: 10 additions & 0 deletions backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { UpdateRoomDto } from './dto/update-room.dto';
import { ChangeOwnerDto } from './dto/change-owner.dto';
import { SetAdminDto } from './dto/set-admin.dto';
import { KickMemberDto } from './dto/kick-member.dto';
import { MuteMemberDto } from './dto/mute-member.dto';
import { ApiCookieAuth, ApiTags } from '@nestjs/swagger';

@ApiTags('rooms')
Expand Down Expand Up @@ -100,4 +101,13 @@ export class RoomsController {
) {
return await this.roomsService.kickMember(memberdata, userId);
}
@Post('mute')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
async muteMember(
@Body() memberdata: MuteMemberDto,
@GetCurrentUser('userId') userId: string,
) {
return await this.roomsService.muteMember(memberdata, userId);
}
}
42 changes: 42 additions & 0 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DeleteRoomDto } from './dto/delete-room.dto';
import { ChangeOwnerDto } from './dto/change-owner.dto';
import { SetAdminDto } from './dto/set-admin.dto';
import { KickMemberDto } from './dto/kick-member.dto';
import { MuteMemberDto } from './dto/mute-member.dto';
import * as bcrypt from 'bcrypt';
import { UpdateRoomDto } from './dto/update-room.dto';

Expand Down Expand Up @@ -237,4 +238,45 @@ export class RoomsService {
},
});
}
async muteMember(roomData: MuteMemberDto, userId: string) {
// check if user is admin or owner
// check if member is in room
// check if member is not admin or owner
// check if member is not muted
const room = await this.prisma.room.findUnique({
where: { id: roomData.roomId },
select: { ownerId: true },
});
const user = await this.prisma.roomMember.findUnique({
where: { unique_user_room: { userId: userId, roomId: roomData.roomId } },
select: { is_admin: true },
});
const member = await this.prisma.roomMember.findUnique({
where: {
unique_user_room: {
userId: roomData.memberId,
roomId: roomData.roomId,
},
},
});
if (!room) throw new HttpException('room not found', HttpStatus.NOT_FOUND);
if (!member)
throw new HttpException('member not found', HttpStatus.NOT_FOUND);
if (!user.is_admin)
throw new UnauthorizedException('You are not admin of this room');
if (member.is_mueted)
throw new UnauthorizedException('member is already muted');
if (member.userId === userId)
throw new UnauthorizedException('You can not mute yourself');
const afterFiveMin = new Date(Date.now() + 5 * 60 * 1000);
await this.prisma.roomMember.update({
where: {
unique_user_room: {
userId: roomData.memberId,
roomId: roomData.roomId,
},
},
data: { is_mueted: true, mute_expires: afterFiveMin },
});
}
}

0 comments on commit 88a055b

Please sign in to comment.