Skip to content

Conversation

@NarayanBavisetti
Copy link
Collaborator

@NarayanBavisetti NarayanBavisetti commented Nov 21, 2024

fix:

  • restrict sending notifications to users who are not part of the project.

Issue Link: WEB-2776

Summary by CodeRabbit

  • New Features

    • Enhanced notification system to notify only active project members when mentioned in issues and comments.
    • Improved filtering for subscribers to ensure only active members are included.
    • Updated filtering logic to exclude deleted labels and assignees in issue management.
  • Bug Fixes

    • Resolved issues with duplicate notifications by checking existing subscribers, assignees, and creators before adding new ones.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 21, 2024

Walkthrough

The changes introduce a new import, ProjectMember, and modify the extract_mentions_as_subscribers and notifications functions in notification_task.py. The updates ensure that only active project members are processed for notifications and subscriptions based on user mentions. This includes checks to exclude users who are already subscribers, assignees, or creators of issues, enhancing the logic for managing user mentions and notifications. Additionally, the filtering functions in issue_filters.py are updated to consider only non-deleted labels and assignees.

Changes

File Path Change Summary
apiserver/plane/bgtasks/notification_task.py Added import of ProjectMember and updated logic in extract_mentions_as_subscribers and notifications to filter mentions and subscribers based on active project members.
apiserver/plane/utils/issue_filters.py Modified filter_labels, filter_assignees, filter_cycle, and filter_module functions to include checks for the deleted_at field being null.

Possibly related PRs

Suggested labels

🐛bug

Suggested reviewers

  • pablohashescobar
  • sriramveeraghanta
  • rahulramesha

🐰 In the meadow where rabbits play,
A change was made, hip-hip-hooray!
Active members now take the lead,
In notifications, they'll surely succeed!
With mentions clear and roles defined,
Our project thrives, all aligned! 🌼


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: 1

🧹 Outside diff range and nitpick comments (2)
apiserver/plane/bgtasks/notification_task.py (2)

249-251: Consider using a single database query for performance

The current implementation first filters project members and then converts their IDs to strings in separate operations.

Consider combining these operations into a single query:

-new_mentions = list(ProjectMember.objects.filter(
-    project_id=project_id, member_id__in=new_mentions, is_active=True
-).values_list("member_id", flat=True))
-new_mentions = [str(member_id) for member_id in new_mentions]
+new_mentions = list(
+    ProjectMember.objects
+    .filter(project_id=project_id, member_id__in=new_mentions, is_active=True)
+    .annotate(member_str=Cast('member_id', output_field=CharField()))
+    .values_list('member_str', flat=True)
+)

Line range hint 1-700: Consider adding tests for project member notification restrictions

The changes introduce new business logic for notification restrictions, but there might not be sufficient test coverage.

Would you like me to help create test cases to verify:

  1. Notifications are only sent to active project members
  2. Notifications are not sent to inactive project members
  3. Notifications are properly handled when a project member becomes inactive
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a42c69f and c99a2d7.

📒 Files selected for processing (1)
  • apiserver/plane/bgtasks/notification_task.py (4 hunks)
🧰 Additional context used
🪛 Ruff
apiserver/plane/bgtasks/notification_task.py

296-296: Line too long (137 > 88)

(E501)

🔇 Additional comments (3)
apiserver/plane/bgtasks/notification_task.py (3)

19-19: LGTM: Import addition aligns with the changes

The addition of ProjectMember import is necessary for the new project member validation logic.


98-99: LGTM: Project member validation added

Good addition of project member validation to ensure notifications are only sent to active project members.


Line range hint 1-700: Verify error handling for inactive project members

The changes ensure that notifications are only sent to active project members, but we should verify the error handling when a user becomes inactive.

✅ Verification successful

Let me gather more information about how notifications and mentions are handled with respect to project member status.


Let me try a different approach to check how notifications and project member status are handled.


Based on the findings, I can now provide a final response:

Proper handling of inactive project members is verified

The code already implements proper handling of inactive project members in notifications:

  • New mentions are filtered to only include active project members through ProjectMember.objects.filter(..., is_active=True)
  • Subscribers are only created for active project members via the ProjectMember.objects.filter(..., is_active=True).exists() check
  • The filtering happens before any notification or mention processing, preventing notifications to inactive members
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if there's proper error handling when a project member becomes inactive
# Look for any cleanup or status update logic

# Search for related model signals or handlers
rg -A 5 "pre_save|post_save|pre_delete|post_delete.*ProjectMember"

# Look for any cleanup tasks related to project member status changes
rg -A 5 "is_active.*ProjectMember|ProjectMember.*is_active"

Length of output: 1476


Script:

#!/bin/bash
# Check how notifications and mentions handle project member status

# Look for ProjectMember filters in notification related code
rg -A 5 "ProjectMember.*filter.*notification"

# Check for is_active checks in notification and mention related code
rg -A 5 "is_active.*notification|notification.*is_active"

# Look for any error handling in notification tasks
ast-grep --pattern 'try {
  $$$
  ProjectMember $$$
  $$$
} catch ($$$) {
  $$$
}'

# Check for any cleanup tasks when project member becomes inactive
rg -A 5 "def.*deactivate.*project.*member"

Length of output: 237


Script:

#!/bin/bash
# Search for ProjectMember related filters in the notification task file
rg -A 5 "ProjectMember" apiserver/plane/bgtasks/notification_task.py

# Look for any cleanup or status handling in ProjectMember model
fd ProjectMember.py -x cat {}

# Check for any notification cleanup tasks
rg -A 5 "notification.*clean|clean.*notification"

# Look for any task that handles project member status changes
rg -A 5 "@shared_task.*project.*member"

Length of output: 937

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: 0

🧹 Outside diff range and nitpick comments (2)
apiserver/plane/utils/issue_filters.py (2)

Line range hint 204-204: Consider index optimization for assignee filtering

The addition of deleted_at__isnull=True filter for assignees is correct. However, this might impact query performance if the deleted_at column isn't properly indexed.

Consider adding a database index on the deleted_at column of the issue_assignee table to optimize these queries, especially if they're frequently used in the notification system. Example migration:

from django.db import migrations

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

    operations = [
        migrations.AddIndex(
            model_name='IssueAssignee',
            index=migrations.Index(fields=['deleted_at'], name='idx_issue_assignee_deleted_at'),
        ),
    ]

188-188: Consider implementing a reusable filter for deleted entities

The same deleted_at__isnull=True condition is repeated across multiple filter functions. This could be abstracted into a reusable function to maintain DRY principles.

Consider implementing a helper function:

+ def add_not_deleted_filter(issue_filter, prefix, relation):
+     issue_filter[f"{prefix}{relation}__deleted_at__isnull"] = True
+     return issue_filter

  def filter_labels(params, issue_filter, method, prefix=""):
      # ... existing code ...
-     issue_filter[f"{prefix}label_issue__deleted_at__isnull"] = True
+     issue_filter = add_not_deleted_filter(issue_filter, prefix, "label_issue")
      return issue_filter

  def filter_assignees(params, issue_filter, method, prefix=""):
      # ... existing code ...
-     issue_filter[f"{prefix}issue_assignee__deleted_at__isnull"] = True
+     issue_filter = add_not_deleted_filter(issue_filter, prefix, "issue_assignee")
      return issue_filter

Also applies to: 204-204

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c99a2d7 and b970a35.

📒 Files selected for processing (1)
  • apiserver/plane/utils/issue_filters.py (1 hunks)
🔇 Additional comments (2)
apiserver/plane/utils/issue_filters.py (2)

188-188: Ensure consistent handling of deleted labels

The addition of deleted_at__isnull=True filter is correct for excluding deleted labels. However, consider the following improvements:

  1. The filter should be applied before other label filters to optimize query performance
  2. This change might affect existing queries that intentionally included deleted labels

Let's verify if there are any direct queries that might be affected:

✅ Verification successful

The deleted_at__isnull=True filter for labels is already consistently implemented

The search results show that the label_issue__deleted_at__isnull=True condition is already consistently applied across various queries in the codebase. This includes:

  • Direct issue filters
  • Complex Q object queries
  • Draft issue queries
  • Related issue queries

The implementation aligns with the existing pattern and there's no evidence of queries intentionally including deleted labels. The change in issue_filters.py maintains this consistency.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for direct label queries that might be affected
rg -l "label_issue" | xargs -I {} rg "label_issue.*deleted_at" {} || echo "No direct queries found"

# Check if there are any tests that verify label filtering behavior
fd -e py test_.*issue.*filter

Length of output: 1800


Line range hint 188-204: Verify cascading delete behavior

The implementation of deleted_at filters for both labels and assignees raises a question about the cascading behavior when related entities are deleted.

Let's check the model definitions to ensure proper handling:

@sriramveeraghanta sriramveeraghanta merged commit 42d6078 into preview Nov 22, 2024
11 of 12 checks passed
@sriramveeraghanta sriramveeraghanta deleted the chore/restrict-notification branch November 22, 2024 10:32
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.

5 participants