Skip to content

Commit 96feed2

Browse files
akirkpfefferle
andauthored
Don't send direct message notification for received public activities (#1092)
* Don't send direct message notification for public actitivies * Changelog * readme * Use filter instead of action for clarity * Better test texts, add cc * added some other little tweaks --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent ad8d77a commit 96feed2

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Improved
1111
* Reactions block: Remove the `wp-block-editor` dependency for frontend views
1212

13+
### Fixed
14+
* Direct Messages: Don't send notification for received public activities
15+
1316
## [4.5.0] - 2024-12-17
1417

1518
### Added

includes/class-mailer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ public static function new_follower( $notification ) {
149149
*/
150150
public static function direct_message( $activity, $user_id ) {
151151
// Check if Activity is public or not.
152-
if (
153-
is_activity_public( $activity ) &&
154-
is_activity_reply( $activity )
155-
) {
152+
if ( is_activity_public( $activity ) ) {
156153
return;
157154
}
158155

includes/handler/class-create.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public static function init() {
4545
*/
4646
public static function handle_create( $activity, $user_id, $activity_object = null ) {
4747
// Check if Activity is public or not.
48-
if ( ! is_activity_public( $activity ) ) {
49-
// @todo maybe send email.
48+
if (
49+
! is_activity_public( $activity ) ||
50+
! is_activity_reply( $activity )
51+
) {
5052
return;
5153
}
5254

@@ -67,7 +69,6 @@ public static function handle_create( $activity, $user_id, $activity_object = nu
6769
}
6870

6971
if ( is_self_ping( $activity['object']['id'] ) ) {
70-
// @todo maybe send email.
7172
return;
7273
}
7374

@@ -120,7 +121,6 @@ public static function validate_object( $valid, $param, $request ) {
120121

121122
$required = array(
122123
'id',
123-
'inReplyTo',
124124
'content',
125125
);
126126

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ For reasons of data protection, it is not possible to see the followers of other
135135
= Unreleased =
136136

137137
* Improved: Reactions block: Remove the `wp-block-editor` dependency for frontend views
138+
* Fixed: Direct Messages: Don't send notification for received public activities
138139

139140
= 4.5.0 =
140141

tests/includes/class-test-mailer.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public function test_init() {
217217
*/
218218
public function test_direct_message() {
219219
$user_id = self::$user_id;
220+
$mock = new \MockAction();
220221

221222
$activity = array(
222223
'actor' => 'https://example.com/author',
@@ -235,6 +236,7 @@ function () {
235236
);
236237
}
237238
);
239+
add_filter( 'wp_mail', array( $mock, 'filter' ), 1 );
238240

239241
// Capture email.
240242
add_filter(
@@ -255,14 +257,15 @@ function ( $args ) use ( $user_id ) {
255257
$public_activity = array(
256258
'actor' => 'https://example.com/author',
257259
'object' => array(
258-
'content' => 'Test public message',
260+
'content' => 'Test public reply',
259261
'inReplyTo' => 'https://example.com/post/1',
260262
),
261263
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
262264
);
263265

264266
// Reset email capture.
265267
remove_all_filters( 'wp_mail' );
268+
add_filter( 'wp_mail', array( $mock, 'filter' ), 1 );
266269
add_filter(
267270
'wp_mail',
268271
function ( $args ) {
@@ -273,6 +276,32 @@ function ( $args ) {
273276

274277
Mailer::direct_message( $public_activity, $user_id );
275278

279+
// Test public activity (should not send email).
280+
$public_activity = array(
281+
'actor' => 'https://example.com/author',
282+
'object' => array(
283+
'content' => 'Test public activity',
284+
'inReplyTo' => null,
285+
),
286+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
287+
'cc' => array( 'https://example.com/followers' ),
288+
);
289+
290+
// Reset email capture.
291+
remove_all_filters( 'wp_mail' );
292+
add_filter( 'wp_mail', array( $mock, 'filter' ), 1 );
293+
add_filter(
294+
'wp_mail',
295+
function ( $args ) {
296+
$this->fail( 'Email should not be sent for public activity' );
297+
return $args;
298+
}
299+
);
300+
301+
Mailer::direct_message( $public_activity, $user_id );
302+
303+
$this->assertEquals( 1, $mock->get_call_count() );
304+
276305
// Clean up.
277306
remove_all_filters( 'pre_get_remote_metadata_by_actor' );
278307
remove_all_filters( 'wp_mail' );

0 commit comments

Comments
 (0)