-
Notifications
You must be signed in to change notification settings - Fork 83
Fix false mention notifications for users in CC without mention tags #2532
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae3ce13
Fix false mention notifications for users in CC without mention tags
obenland 186d72e
Add changelog
matticbot f2f08b2
Refactor mention handling in Mailer class
obenland 432ae19
Merge branch 'trunk' into fix/mention-notification-cc-only
obenland 5adf897
Improve readability
obenland 3c0cf07
Merge branch 'fix/mention-notification-cc-only' of https://github.com…
obenland f809386
Merge branch 'trunk' into fix/mention-notification-cc-only
pfefferle c81672b
Merge branch 'trunk' into fix/mention-notification-cc-only
obenland 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
This file contains hidden or 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 @@ | ||
| Significance: patch | ||
| Type: fixed | ||
|
|
||
| False mention email notifications for users in CC field without actual mention tags. |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -814,6 +814,13 @@ public function test_mention_with_array() { | |
| 'object' => array( | ||
| 'id' => 'https://example.com/post/1', | ||
| 'content' => 'Test mention', | ||
| 'tag' => array( | ||
| array( | ||
| 'type' => 'Mention', | ||
| 'href' => Actors::get_by_id( $user_id )->get_id(), | ||
| 'name' => '@test', | ||
| ), | ||
| ), | ||
| ), | ||
| 'cc' => array( Actors::get_by_id( $user_id )->get_id() ), | ||
| ); | ||
|
|
@@ -935,6 +942,13 @@ public function test_mention_filters_recipients() { | |
| 'object' => array( | ||
| 'id' => 'https://example.com/post/1', | ||
| 'content' => 'Test mention', | ||
| 'tag' => array( | ||
| array( | ||
| 'type' => 'Mention', | ||
| 'href' => Actors::get_by_id( $user_id )->get_id(), | ||
| 'name' => '@test', | ||
| ), | ||
| ), | ||
| ), | ||
| // Only user_id is in CC, not other_user_id. | ||
| 'cc' => array( Actors::get_by_id( $user_id )->get_id() ), | ||
|
|
@@ -1115,4 +1129,122 @@ public function test_respect_existing_notification_settings() { | |
| // Clean up. | ||
| \delete_option( 'activitypub_create_posts' ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that users in CC without actual mention tags do not receive mention notifications. | ||
| * | ||
| * This tests the bug fix where users added to CC (e.g., because they follow the actor) | ||
| * were incorrectly receiving mention notifications even when not actually mentioned. | ||
| * | ||
| * @covers ::mention | ||
| */ | ||
| public function test_mention_requires_tag_not_just_cc() { | ||
| $user_id = self::$user_id; | ||
|
|
||
| // Activity with user in CC but NOT mentioned in tags. | ||
| $activity = array( | ||
| 'actor' => 'https://example.com/sports-account', | ||
| 'object' => array( | ||
| 'id' => 'https://example.com/sports-account/posts/123', | ||
| 'type' => 'Note', | ||
| 'content' => '<p>Join @user1 and @user2 on our stream...</p>', | ||
| 'tag' => array( | ||
| // Other users mentioned, but NOT the local user. | ||
| array( | ||
| 'type' => 'Mention', | ||
| 'href' => 'https://example.com/user1', | ||
| 'name' => '[email protected]', | ||
| ), | ||
| array( | ||
| 'type' => 'Mention', | ||
| 'href' => 'https://example.com/user2', | ||
| 'name' => '[email protected]', | ||
| ), | ||
| ), | ||
| ), | ||
| // User is in CC (e.g., because they follow the actor). | ||
| 'cc' => array( Actors::get_by_id( $user_id )->get_id() ), | ||
| ); | ||
|
|
||
| // Mock remote metadata. | ||
| $metadata_filter = function () { | ||
| return array( | ||
| 'name' => 'Sports Account', | ||
| 'url' => 'https://example.com/sports-account', | ||
| ); | ||
| }; | ||
| \add_filter( 'pre_get_remote_metadata_by_actor', $metadata_filter ); | ||
|
|
||
| $mock = new \MockAction(); | ||
| \add_filter( 'wp_mail', array( $mock, 'filter' ), 1 ); | ||
|
|
||
| // Trigger mention notification. | ||
| Mailer::mention( $activity, $user_id ); | ||
|
|
||
| // Should NOT send any email because user is not actually mentioned in tags. | ||
| $this->assertEquals( 0, $mock->get_call_count(), 'User in CC without mention tag should not receive notification' ); | ||
|
|
||
| // Clean up. | ||
| \remove_filter( 'pre_get_remote_metadata_by_actor', $metadata_filter ); | ||
| \remove_filter( 'wp_mail', array( $mock, 'filter' ), 1 ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that users with actual mention tags DO receive mention notifications. | ||
| * | ||
| * @covers ::mention | ||
| */ | ||
| public function test_mention_with_tag_sends_notification() { | ||
| $user_id = self::$user_id; | ||
|
|
||
| // Activity with user properly mentioned in both CC and tags. | ||
| $activity = array( | ||
| 'actor' => 'https://example.com/author', | ||
| 'object' => array( | ||
| 'id' => 'https://example.com/post/1', | ||
| 'type' => 'Note', | ||
| 'content' => '<p>Hello @testuser, how are you?</p>', | ||
| 'tag' => array( | ||
| array( | ||
| 'type' => 'Mention', | ||
| 'href' => Actors::get_by_id( $user_id )->get_id(), | ||
| 'name' => '@testuser', | ||
| ), | ||
| ), | ||
| ), | ||
| 'cc' => array( Actors::get_by_id( $user_id )->get_id() ), | ||
| ); | ||
|
|
||
| // Mock remote metadata. | ||
| $metadata_filter = function () { | ||
| return array( | ||
| 'name' => 'Test Author', | ||
| 'url' => 'https://example.com/author', | ||
| ); | ||
| }; | ||
| \add_filter( 'pre_get_remote_metadata_by_actor', $metadata_filter ); | ||
|
|
||
| $mock = new \MockAction(); | ||
| \add_filter( 'wp_mail', array( $mock, 'filter' ), 1 ); | ||
|
|
||
| // Capture email. | ||
| $mail_filter = function ( $args ) use ( $user_id ) { | ||
| $this->assertStringContainsString( 'Mention', $args['subject'] ); | ||
| $this->assertStringContainsString( 'Test Author', $args['subject'] ); | ||
| $this->assertEquals( \get_user_by( 'id', $user_id )->user_email, $args['to'] ); | ||
| return $args; | ||
| }; | ||
| \add_filter( 'wp_mail', $mail_filter ); | ||
|
|
||
| // Trigger mention notification. | ||
| Mailer::mention( $activity, $user_id ); | ||
|
|
||
| // Should send 1 email because user is properly mentioned. | ||
| $this->assertEquals( 1, $mock->get_call_count(), 'User properly mentioned in tags should receive notification' ); | ||
|
|
||
| // Clean up. | ||
| \remove_filter( 'pre_get_remote_metadata_by_actor', $metadata_filter ); | ||
| \remove_filter( 'wp_mail', array( $mock, 'filter' ), 1 ); | ||
| \remove_filter( 'wp_mail', $mail_filter ); | ||
| } | ||
| } | ||
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.