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

[backend] mute user in room #216

Merged
merged 19 commits into from
Jan 20, 2024
Merged

[backend] mute user in room #216

merged 19 commits into from
Jan 20, 2024

Conversation

lim396
Copy link
Collaborator

@lim396 lim396 commented Jan 18, 2024

  • PUT /room/:roomId/mutes/:userIdのテストを追加しました。
  • GET /room/:roomId/mutesのテストを追加しました。
  • DELETE /room/:roomId/mutes/:userIdのテストを追加しました。
  • mute中のユーザーからmessageが届かないことを確認するテストを追加しました。
  • mute期限後にmessageが届くことを確認するテストを追加しました。
  • unmute後にmessageが届くことを確認するテストを追加しました。
  • PUT /room/:roomId/mutes/:userIdを追加しました。
    durationの指定がない場合は無期限のmuteになる仕様です。
  • GET /room/:roomId/mutesを追加しました。
  • DELETE /room/:roomId/mutes/:userIdを追加しました。

タイマー関数をmockに置き換えなどはまだ出来ていません。
テストの追加や修正により大きなPRになってしまっています🙏

追加したテスト
room.e2e-spec
スクリーンショット 2024-01-21 1 34 04

chat-gateway.e2e-spec
スクリーンショット 2024-01-19 7 46 00

修正したテスト
chat-gateway.e2e-specを少し修正
スクリーンショット 2024-01-19 18 26 18

Copy link
Contributor

coderabbitai bot commented Jan 18, 2024

Warning

Rate Limit Exceeded

@lim396 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 16 minutes and 13 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 8dd04ee and be7f8b3.

Walkthrough

The recent update introduces a MuteUserOnRoom feature to the chat application. It involves creating a new database table and model to handle user mutes within chat rooms, updating the schema, and integrating the mute functionality into the existing modules. The backend logic now includes mute checks before message creation, and the ability to manage mute durations. New endpoints and services have been added for muting operations, alongside tests to ensure proper functionality.

Changes

File Path Change Summary
backend/.../migrations/20240118204714_add_mute_user_on_room_model/migration.sql
backend/prisma/schema.prisma
Added MuteUserOnRoom table and model with fields for user and room IDs, creation and expiration timestamps, and necessary constraints.
backend/src/app.module.ts
backend/src/chat/chat.module.ts
backend/src/room/mute/mute.module.ts
Integrated MuteModule into the application and chat modules, and created a new MuteModule.
backend/src/chat/chat.gateway.ts Modified to include MuteService to check for muted users before message creation.
backend/src/room/e2e-spec.ts
backend/test/room.e2e-spec.ts
Updated RoomController logic for muting users with different scenarios and added tests for new mute functionality.
backend/src/room/utils/app.ts Added getMutedUsers method and updated muteUser method to include an optional duration parameter.
backend/src/room/mute/dto/create-mute.dto.ts
backend/src/room/mute/dto/update-mute.dto.ts
Introduced DTOs for creating and updating mute settings with validation and documentation decorators.
backend/src/room/mute/entities/mute.entity.ts Introduced the Mute entity.
backend/src/room/mute/mute.controller.spec.ts
backend/src/room/mute/mute.service.spec.ts
Added tests for MuteController and MuteService to ensure proper integration and functionality.
backend/test/chat-gateway.e2e-spec.ts Included tests for new mute functionality, checking the effects on user message reception.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

backend/test/utils/app.ts Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
Comment on lines 2102 to 2116
it('should get empty array after the duration (200 OK)', (done) => {
setTimeout(async () => {
const res = await app
.getMutedUsers(_publicRoom.id, owner.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
expect(res.body).toHaveLength(0);
done();
// const res2 = await app
// .getMutedUsers(_publicRoom.id, admin.accessToken)
// .expect(200);
// expect(res2.body).toBeInstanceOf(Array);
// expect(res2.body).toHaveLength(0);
}, 2000);
});
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case for checking the empty array of muted users after the mute duration has passed is implemented correctly. However, the use of setTimeout in tests is not recommended due to potential flakiness. Consider using fake timers or other time-mocking libraries for more reliable and faster tests.

- it('should get empty array after the duration (200 OK)', (done) => {
+ it('should get empty array after the duration (200 OK)', async () => {
+   jest.useFakeTimers();
+   // ... setup the mute ...
+   jest.advanceTimersByTime(2000);
+   const res = await app
+     .getMutedUsers(_publicRoom.id, owner.accessToken)
+     .expect(200);
+   expect(res.body).toBeInstanceOf(Array);
+   expect(res.body).toHaveLength(0);
+   jest.useRealTimers();
+ });

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
it('should get empty array after the duration (200 OK)', (done) => {
setTimeout(async () => {
const res = await app
.getMutedUsers(_publicRoom.id, owner.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
expect(res.body).toHaveLength(0);
done();
// const res2 = await app
// .getMutedUsers(_publicRoom.id, admin.accessToken)
// .expect(200);
// expect(res2.body).toBeInstanceOf(Array);
// expect(res2.body).toHaveLength(0);
}, 2000);
});
it('should get empty array after the duration (200 OK)', async () => {
jest.useFakeTimers();
// ... setup the mute ...
jest.advanceTimersByTime(2000);
const res = await app
.getMutedUsers(_publicRoom.id, owner.accessToken)
.expect(200);
expect(res.body).toBeInstanceOf(Array);
expect(res.body).toHaveLength(0);
jest.useRealTimers();
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mockタイマーは色々試行錯誤しましたが、まだ上手く使えなかったので後ほど試してみます。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lim396: Understood! If you encounter any issues or have questions when you revisit the mock timers, feel free to reach out. Good luck!

backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
@lim396 lim396 changed the title [WIP] mute user in room [backend] mute user in room Jan 18, 2024
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Show resolved Hide resolved
@lim396 lim396 changed the title [backend] mute user in room [WIP] mute user in room Jan 19, 2024
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
backend/test/room.e2e-spec.ts Outdated Show resolved Hide resolved
@lim396 lim396 force-pushed the feat/backend/mute-user-in-room branch from 482c05c to b528900 Compare January 20, 2024 12:10
backend/test/room.e2e-spec.ts Show resolved Hide resolved
backend/test/room.e2e-spec.ts Show resolved Hide resolved
backend/test/room.e2e-spec.ts Show resolved Hide resolved
@lim396 lim396 merged commit 7ae0df0 into main Jan 20, 2024
4 checks passed
@lim396 lim396 deleted the feat/backend/mute-user-in-room branch January 20, 2024 16:47
@lim396 lim396 changed the title [WIP] mute user in room [backend] mute user in room Jan 20, 2024
} else {
return false;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こういう名前の関数で、これが書かれない気がする

}
if (await this.isExpired(roomId, userId)) {
await this.remove(roomId, userId);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここまでまとめてremoveIfAlreadyExistsAndExpiredみたいな(もっといい名前考えてほしいですが)ふうに切り分けた方が自然かな

      if (await this.isExpired(roomId, userId)) {
        await this.remove(roomId, userId);
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants