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

chore: restrict email notifications for removed users #6100

Merged
merged 1 commit into from
Nov 27, 2024

Conversation

NarayanBavisetti
Copy link
Collaborator

@NarayanBavisetti NarayanBavisetti commented Nov 27, 2024

chore:

  • restrict email notifications for removed users

Summary by CodeRabbit

  • New Features

    • Enhanced filtering for notifications based on active project membership, allowing users to receive more relevant updates.
    • Improved retrieval of user activities linked to active project members for CSV exports.
    • Streamlined notification handling for issue mentions, optimizing the process for determining valid notifications.
  • Bug Fixes

    • Refined query logic to ensure only appropriate notifications and activities are processed, reducing redundancy and improving clarity.

Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

Walkthrough

The pull request introduces enhancements to the notification filtering logic across several components. In the NotificationViewSet, new conditions are added to filter notifications based on active project members and various query parameters. The ExportWorkspaceUserActivityEndpoint is modified to ensure user activities are filtered by active project members. Additionally, the notification_task.py file is updated to optimize the retrieval of active project members for issue notifications, streamlining the logic for processing mentions. Overall, these changes improve the relevance and accuracy of notifications and user activities.

Changes

File Path Change Summary
apiserver/plane/app/views/notification/base.py Enhanced filtering logic in NotificationViewSet to include checks for active project members and various query parameters.
apiserver/plane/app/views/workspace/base.py Modified ExportWorkspaceUserActivityEndpoint to filter user activities based on active project members.
apiserver/plane/bgtasks/notification_task.py Updated notifications function to optimize retrieval of active project members using Subquery, and refined logic for handling mentions.

Possibly related PRs

Suggested reviewers

  • pablohashescobar
  • sriramveeraghanta

Poem

🐰 In the meadow where notifications bloom,
Active members gather, dispelling the gloom.
With filters refined, they hop and they play,
Ensuring each message finds its own way.
So let the updates ring clear and bright,
For every project member, a joyous delight! 🌼


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>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (2)
apiserver/plane/app/views/notification/base.py (1)

65-68: Consider adding an index to optimize the new filtering conditions.

Since we're now filtering on project__project_projectmember__member and project__project_projectmember__is_active, consider adding a composite index to optimize these queries, especially if the notifications table is expected to grow large.

Example Django migration to add the index:

class Migration(migrations.Migration):
    dependencies = [
        ('app', 'previous_migration'),
    ]

    operations = [
        migrations.AddIndex(
            model_name='ProjectMember',
            index=models.Index(
                fields=['member', 'is_active'],
                name='project_member_active_idx'
            ),
        ),
    ]
apiserver/plane/bgtasks/notification_task.py (1)

248-250: Reuse fetched project member IDs to avoid redundant queries

Since project_member_ids are fetched at the beginning, you can reuse this list in other parts of your code to avoid redundant queries. For example, when filtering mentions or subscribers, utilize project_member_ids for consistency and efficiency.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 05d3e3a and b159626.

📒 Files selected for processing (3)
  • apiserver/plane/app/views/notification/base.py (1 hunks)
  • apiserver/plane/app/views/workspace/base.py (1 hunks)
  • apiserver/plane/bgtasks/notification_task.py (5 hunks)
🔇 Additional comments (1)
apiserver/plane/app/views/workspace/base.py (1)

356-356: LGTM! Verify query performance impact.

The added filter correctly ensures that only activities from projects where the user is an active member are included, which aligns with the PR objective of restricting notifications for removed users.

Let's verify the query performance impact:

✅ Verification successful

Query performance impact is minimal - filter is widely used with proper indexes

The added filter project__project_projectmember__is_active=True is already extensively used across the codebase (50+ occurrences) in similar query patterns, particularly in views that handle user-specific data. The consistent usage pattern shows this is a well-established filtering approach in the codebase.

Key observations:

  • The filter is commonly paired with project__archived_at__isnull=True and other performance-optimized conditions
  • Most queries using this filter also use select_related() for optimizing related field access
  • The query in question is already limited to 10000 records and uses select_related() for performance
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the added filter significantly impacts the query performance
# by analyzing the query plan and checking for any related indexes.

# Check if there's an index on project_projectmember.is_active
ast-grep --pattern 'class Migration:
  $$$
  operations = [
    $$$
    migrations.AddIndex(
      model_name="projectmember",
      index=models.Index(
        fields=["is_active"],
        $$$
      ),
      $$$
    ),
    $$$
  ]'

# Look for similar patterns of filtering by is_active in other views
rg "project__project_projectmember__is_active\s*=" -A 2

Length of output: 15737

Comment on lines +65 to +68
.filter(
project__project_projectmember__member=request.user,
project__project_projectmember__is_active=True,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

The active member check is not consistently applied across all notification queries

The verification revealed that while the main list endpoint properly filters for active project members, other notification endpoints like UnreadNotificationEndpoint and MarkAllReadNotificationViewSet don't include the same active member check:

  • UnreadNotificationEndpoint.get() (lines 245-263): Missing active member check
  • MarkAllReadNotificationViewSet.create() (lines 284-286): Missing active member check

These endpoints should also include the same filtering to maintain consistency with the PR's objective of restricting notifications for removed users.

🔗 Analysis chain

LGTM! The changes effectively restrict notifications to active project members.

The added filters ensure that notifications are only shown to active project members, which aligns with the PR objective of restricting notifications for removed users.

Let's verify that the filters work as expected:


Let me gather more information to verify if there are any potential notification queries that might bypass the active member check.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that notifications are properly filtered for inactive project members

# Check if there are any notifications being sent to inactive project members
ast-grep --pattern 'Notification.objects.filter($$$).exclude(project__project_projectmember__is_active=True)' \
  --pattern 'Notification.objects.filter($$$).exclude(project__project_projectmember__member=True)'

# Check for any other notification queries that might bypass these filters
rg -l "Notification.objects" | xargs ast-grep --pattern 'Notification.objects.filter($$$)'

Length of output: 2374


Script:

#!/bin/bash
# Let's check the content of the notification base file to understand the context of these queries
cat apiserver/plane/app/views/notification/base.py

Length of output: 13739

apiserver/plane/bgtasks/notification_task.py Show resolved Hide resolved
Comment on lines +257 to +259
new_mentions = [
str(mention) for mention in new_mentions if mention in new_mentions
]
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix redundant and incorrect filtering of new mentions

The condition if mention in new_mentions in the list comprehension is redundant because each mention is inherently from new_mentions. This does not filter the mentions as intended. If the goal is to filter new_mentions to include only active project members, you should update the code to:

new_mentions = [
    str(mention) for mention in new_mentions if mention in project_member_ids
]

apiserver/plane/bgtasks/notification_task.py Show resolved Hide resolved
apiserver/plane/bgtasks/notification_task.py Show resolved Hide resolved
@pushya22 pushya22 merged commit 1420b7e into preview Nov 27, 2024
13 of 15 checks passed
@pushya22 pushya22 deleted the chore/restrict-email branch November 27, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants