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

Fix Chat Room Bugs When Changing Rooms and in Direct Messages #160

Open
wants to merge 3 commits into
base: development
Choose a base branch
from

Conversation

odkhang
Copy link
Collaborator

@odkhang odkhang commented Jul 22, 2024

Fixes #139 Fix Chat Room Bugs When Changing Rooms and in Direct Messages

Short description of what this resolves:

Fix Chat Room Bugs When Changing Rooms and in Direct Messages

Checklist

  • I have read the Contribution & Best practices Guide.
  • My branch is up-to-date with the Upstream development branch.
  • The acceptance, integration, unit tests and linter pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by Sourcery

This pull request addresses bugs related to chat room notifications and direct messages. It ensures notifications are correctly updated when changing rooms and marks direct message notifications as read. Additionally, it enhances the handling of direct message channel names and introduces a new state property to track message fetching status.

  • Bug Fixes:
    • Fixed issues with chat room notifications not updating correctly when changing rooms.
    • Resolved bugs in direct message channels where notifications were not being marked as read.
  • Enhancements:
    • Improved the handling of direct message channel names by using getters instead of direct method calls.
    • Added a new state property 'fetchingMessages' to track the message fetching status.

@odkhang odkhang marked this pull request as ready for review July 22, 2024 08:35
Copy link

sourcery-ai bot commented Jul 22, 2024

Reviewer's Guide by Sourcery

This pull request addresses bugs in the chat room functionality, specifically when changing rooms and handling direct messages. The changes include adding logic to handle event_id in the server-side notification publishing, fixing state management issues in the client-side chat store, and ensuring notifications are correctly managed and displayed.

File-Level Changes

Files Changes
server/venueless/live/modules/chat.py
webapp/src/store/chat.js
Fixed bugs related to chat room notifications and direct messages by adding event_id handling and correcting state management.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @odkhang - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

if event_id:
async with aredis() as redis:
read_pointer = await redis.hget(f"chat:read:{self.consumer.user.id}", body["data"]["event"]["channel"])
read_pointer = int(read_pointer.decode()) if read_pointer else 0
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Handle potential decoding errors

Consider adding error handling for the decode method in case the data is not in the expected format.

Comment on lines +359 to +361
notification_counts = await database_sync_to_async(
self.service.get_notification_counts
)(self.consumer.user.id)
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider using a more descriptive variable name

The variable notification_counts could be more descriptive to indicate what kind of notifications it is counting, e.g., unread_notification_counts.

Suggested change
notification_counts = await database_sync_to_async(
self.service.get_notification_counts
)(self.consumer.user.id)
unread_notification_counts = await database_sync_to_async(
self.service.get_unread_notification_counts
)(self.consumer.user.id)

self.service.get_notification_counts
)(self.consumer.user.id)
await self.consumer.send_json(["chat.notification_counts", notification_counts])
return
Copy link

Choose a reason for hiding this comment

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

nitpick: Unnecessary return statement

The return statement here is redundant since the function will exit after this block anyway.

@@ -349,6 +349,18 @@

@event("notification")
async def publish_notification(self, body):
event_id = body.get("data", {}).get("event", {}).get("event_id")
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the code to reduce nesting and encapsulate specific logic into helper methods.

The new code introduces increased complexity due to additional nested conditionals, asynchronous context management, multiple return points, and potential error handling issues. To improve readability and maintainability, consider refactoring the code to reduce nesting and encapsulate specific logic into helper methods. Here's a suggested refactor:

@event("notification")
async def publish_notification(self, body):
    event_data = body.get("data", {}).get("event", {})
    event_id = event_data.get("event_id")
    channel = event_data.get("channel")

    if event_id and channel:
        read_pointer = await self._get_read_pointer(channel)
        if read_pointer is not None and read_pointer >= event_id:
            if await self.service.remove_notifications(self.consumer.user.id, self.channel_id, read_pointer):
                await self._send_notification_counts()
                return

    await self.consumer.send_json(["chat.notification", body.get("data")])

async def _get_read_pointer(self, channel):
    async with aredis() as redis:
        read_pointer = await redis.hget(f"chat:read:{self.consumer.user.id}", channel)
        return int(read_pointer.decode()) if read_pointer else None

async def _send_notification_counts(self):
    notification_counts = await database_sync_to_async(
        self.service.get_notification_counts
    )(self.consumer.user.id)
    await self.consumer.send_json(["chat.notification_counts", notification_counts])

This refactor reduces complexity by introducing helper methods, making the main method cleaner and easier to follow. It also adheres to the Single Responsibility Principle, making future maintenance simpler.

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.

Fix Chat Room Bugs When Changing Rooms and in Direct Messages
2 participants