Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: mute room member #47

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 },
});
}
}
Loading