-
Notifications
You must be signed in to change notification settings - Fork 0
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
[backend] mute user in room #216
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2fa8f63
[backend] Add test for PUT /room/:id/mutes/:userId
lim396 0650dfe
[backend] Add test for GET /room/:id/mutes
lim396 c228c46
[backend] Add test to ensure that messages are not received from user…
lim396 3f7bcfc
[backend] Add MuteUserOnRoom model and migrate
lim396 e1505e5
[backend] Add API PUT /room/:roomId/mutes/:userId and GET /room/:room…
lim396 6260f2f
[backend] Modified to not save and send messages from users in mute
lim396 64e12aa
[backend] Add test for DELETE /room/:roomId/mutes/:userId
lim396 d1e9397
[backend] Add test to confirm messages from unmuted users can be rece…
lim396 5fbb6ef
[backend] Add API DELETE /room/:roomId/mutes/:userId
lim396 e8d0959
[backend] Separate test items and add status code to description
lim396 adbe1c4
[backend] e2e test on chat-gateway made a little easier to see
lim396 072ff1c
[backend] Removed tests implemented in a separate file from room e2e …
lim396 be06885
[backend] Fix test description
lim396 13deab7
[backend] Add test to confirm that it is possible to remute after a m…
lim396 6c23d21
[backend] Changed to be able to remute without unmuting after mute pe…
lim396 d21c5ce
[backend] Add test to confirm that it can be muted indefinitely
lim396 b528900
[backend] Fixed to be able to mute indefinitely
lim396 8dd04ee
[backend] Fix test
lim396 be7f8b3
[backend] Refactored code for indefinite mute when no duration is spe…
lim396 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
backend/prisma/migrations/20240118204714_add_mute_user_on_room_model/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- CreateTable | ||
CREATE TABLE "MuteUserOnRoom" ( | ||
"userId" INTEGER NOT NULL, | ||
"roomId" INTEGER NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"expiresAt" TIMESTAMP(3) | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "MuteUserOnRoom_userId_roomId_key" ON "MuteUserOnRoom"("userId", "roomId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "MuteUserOnRoom" ADD CONSTRAINT "MuteUserOnRoom_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "MuteUserOnRoom" ADD CONSTRAINT "MuteUserOnRoom_roomId_fkey" FOREIGN KEY ("roomId") REFERENCES "Room"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNumber, IsOptional } from 'class-validator'; | ||
|
||
export class CreateMuteDto { | ||
@IsNumber() | ||
@IsOptional() | ||
@ApiProperty({ required: false }) | ||
duration: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { CreateMuteDto } from './create-mute.dto'; | ||
|
||
export class UpdateMuteDto extends PartialType(CreateMuteDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Mute {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import { MuteController } from './mute.controller'; | ||
import { MuteService } from './mute.service'; | ||
|
||
describe('MuteController', () => { | ||
let controller: MuteController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [MuteController], | ||
providers: [MuteService, PrismaService], | ||
}).compile(); | ||
|
||
controller = module.get<MuteController>(MuteController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Put, | ||
Body, | ||
Delete, | ||
Param, | ||
ParseIntPipe, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { CreateMuteDto } from './dto/create-mute.dto'; | ||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; | ||
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'; | ||
import { AdminGuard } from '../guards/admin.guard'; | ||
import { MuteService } from './mute.service'; | ||
|
||
@ApiTags('mute') | ||
@ApiBearerAuth() | ||
@UseGuards(JwtAuthGuard) | ||
@Controller('room/:roomId/mutes') | ||
export class MuteController { | ||
constructor(private readonly muteService: MuteService) {} | ||
|
||
@Put(':userId') | ||
@UseGuards(AdminGuard) | ||
create( | ||
@Param('roomId', ParseIntPipe) roomId: number, | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Body() createMuteDto: CreateMuteDto, | ||
) { | ||
return this.muteService.create(roomId, userId, createMuteDto); | ||
} | ||
|
||
@Get() | ||
@UseGuards(AdminGuard) | ||
findAll(@Param('roomId', ParseIntPipe) roomId: number) { | ||
return this.muteService.findAll(roomId); | ||
} | ||
|
||
@Delete(':userId') | ||
@UseGuards(AdminGuard) | ||
remove( | ||
@Param('roomId', ParseIntPipe) roomId: number, | ||
@Param('userId', ParseIntPipe) userId: number, | ||
) { | ||
return this.muteService.remove(roomId, userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaModule } from 'src/prisma/prisma.module'; | ||
import { MuteService } from './mute.service'; | ||
import { MuteController } from './mute.controller'; | ||
|
||
@Module({ | ||
controllers: [MuteController], | ||
providers: [MuteService], | ||
imports: [PrismaModule], | ||
}) | ||
export class MuteModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import { MuteService } from './mute.service'; | ||
|
||
describe('MuteService', () => { | ||
let service: MuteService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [MuteService, PrismaService], | ||
}).compile(); | ||
|
||
service = module.get<MuteService>(MuteService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { | ||
BadRequestException, | ||
ForbiddenException, | ||
Injectable, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { CreateMuteDto } from './dto/create-mute.dto'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class MuteService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async isExpired(roomId: number, userId: number) { | ||
const now = new Date(); | ||
const mute = await this.prisma.muteUserOnRoom.findUnique({ | ||
where: { | ||
userId_roomId_unique: { | ||
userId, | ||
roomId, | ||
}, | ||
}, | ||
}); | ||
if (!mute) { | ||
return false; | ||
} else if (mute.expiresAt === null) { | ||
return false; | ||
} else if (mute.expiresAt <= now) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
async create(roomId: number, userId: number, createMuteDto: CreateMuteDto) { | ||
await this.prisma.$transaction(async (prisma) => { | ||
const room = await prisma.room.findUnique({ | ||
where: { | ||
id: roomId, | ||
}, | ||
}); | ||
if (room.accessLevel === 'DIRECT') { | ||
throw new BadRequestException('Cannot mute user in DIRECT room'); | ||
} | ||
const user = await prisma.userOnRoom.findUnique({ | ||
where: { | ||
userId_roomId_unique: { | ||
userId, | ||
roomId, | ||
}, | ||
}, | ||
}); | ||
if (!user) { | ||
throw new NotFoundException('User does not exist in the room'); | ||
} | ||
if (user.role === 'OWNER') { | ||
throw new ForbiddenException('Cannot mute owner'); | ||
} | ||
if (await this.isExpired(roomId, userId)) { | ||
await this.remove(roomId, userId); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここまでまとめて
|
||
let expiresAt; | ||
if (!createMuteDto.duration) { | ||
expiresAt = null; | ||
} else { | ||
expiresAt = new Date(); | ||
expiresAt.setSeconds(expiresAt.getSeconds() + createMuteDto.duration); | ||
} | ||
await prisma.muteUserOnRoom.create({ | ||
data: { | ||
userId, | ||
roomId, | ||
expiresAt, | ||
}, | ||
}); | ||
}); | ||
return { | ||
message: 'Mute user successfully', | ||
}; | ||
} | ||
|
||
async findAll(roomId: number) { | ||
const now = new Date(); | ||
const tmp = await this.prisma.muteUserOnRoom.findMany({ | ||
where: { | ||
roomId, | ||
OR: [ | ||
{ | ||
expiresAt: { | ||
gt: now, | ||
}, | ||
}, | ||
{ | ||
expiresAt: null, | ||
}, | ||
], | ||
}, | ||
include: { | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
avatarURL: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
const users = tmp.map((item) => item.user); | ||
return users; | ||
} | ||
|
||
async remove(roomId: number, userId: number) { | ||
await this.prisma.$transaction(async (prisma) => { | ||
const user = await prisma.muteUserOnRoom.findUnique({ | ||
where: { | ||
userId_roomId_unique: { | ||
userId, | ||
roomId, | ||
}, | ||
}, | ||
}); | ||
if (!user) { | ||
throw new NotFoundException('User not found in the Mute list'); | ||
} | ||
await prisma.muteUserOnRoom.delete({ | ||
where: { | ||
userId_roomId_unique: { | ||
userId, | ||
roomId, | ||
}, | ||
}, | ||
}); | ||
return { message: 'Unmute user successfully' }; | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こういう名前の関数で、これが書かれない気がする