Skip to content

Commit 2705703

Browse files
authored
Add all repliers as Mentions (#692)
* Add all repliers as Mentions Add all repliers as Mentions so that they receive notifications. See: https://wordpress.org/support/topic/ux-deviation-from-expected-behaviour-in-2-0/ * Fix readme * typo
1 parent dd91156 commit 2705703

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Project maintained on GitHub at [automattic/wordpress-activitypub](https://githu
141141
* Fixed: Remove old/abandoned Crons
142142
* Added: Various endpoints for the "Enable Mastodon Apps" plugin
143143
* Added: Event Objects
144+
* Added: Send notification to all Repliers if a new Comment is added
144145
* Added: Vary-Header support behind feature flag
145146

146147
### 2.0.1 ###

includes/transformer/class-comment.php

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use WP_Comment;
55
use WP_Comment_Query;
66

7+
use Activitypub\Webfinger;
78
use Activitypub\Comment as Comment_Utils;
89
use Activitypub\Model\Blog_User;
910
use Activitypub\Collection\Users;
@@ -178,31 +179,7 @@ protected function get_cc() {
178179
}
179180
}
180181

181-
$comment_query = new WP_Comment_Query(
182-
array(
183-
'post_id' => $this->wp_object->comment_post_ID,
184-
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
185-
'meta_query' => array(
186-
array(
187-
'key' => 'source_id',
188-
'compare' => 'EXISTS',
189-
),
190-
),
191-
)
192-
);
193-
194-
if ( $comment_query->comments ) {
195-
foreach ( $comment_query->comments as $comment ) {
196-
if ( empty( $comment->comment_author_url ) ) {
197-
continue;
198-
}
199-
$cc[] = \esc_url( $comment->comment_author_url );
200-
}
201-
}
202-
203-
$cc = \array_unique( $cc );
204-
205-
return $cc;
182+
return array_unique( $cc );
206183
}
207184

208185
/**
@@ -236,9 +213,64 @@ protected function get_tag() {
236213
* @return array The list of @-Mentions.
237214
*/
238215
protected function get_mentions() {
216+
\add_filter( 'activitypub_extract_mentions', array( $this, 'extract_reply_context' ) );
217+
239218
return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->comment_content, $this->wp_object );
240219
}
241220

221+
/**
222+
* Collect all other Users that participated in this comment-thread
223+
* to send them a notification about the new reply.
224+
*
225+
* @param array $mentions The already mentioned ActivityPub users
226+
*
227+
* @return array The list of all Repliers.
228+
*/
229+
public function extract_reply_context( $mentions ) {
230+
// Check if `$this->wp_object` is a WP_Comment
231+
if ( 'WP_Comment' !== get_class( $this->wp_object ) ) {
232+
return $mentions;
233+
}
234+
235+
$comment_query = new WP_Comment_Query(
236+
array(
237+
'post_id' => $this->wp_object->comment_post_ID,
238+
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
239+
'meta_query' => array(
240+
array(
241+
'key' => 'protocol',
242+
'value' => 'activitypub',
243+
),
244+
),
245+
)
246+
);
247+
248+
if ( $comment_query->comments ) {
249+
foreach ( $comment_query->comments as $comment ) {
250+
$id = null;
251+
$comment_meta = \get_comment_meta( $comment->comment_ID );
252+
253+
if ( ! empty( $comment_meta['source_id'][0] ) ) {
254+
$id = \esc_url( $comment_meta['source_id'][0] );
255+
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) {
256+
$id = \esc_url( $comment_meta['source_url'][0] );
257+
} elseif ( ! empty( $comment->comment_author_url ) ) {
258+
$id = $comment->comment_author_url;
259+
}
260+
261+
if ( $id ) {
262+
$acct = Webfinger::uri_to_acct( $id );
263+
if ( $acct && ! is_wp_error( $acct ) ) {
264+
$acct = str_replace( 'acct:', '@', $acct );
265+
$mentions[ $acct ] = $id;
266+
}
267+
}
268+
}
269+
}
270+
271+
return $mentions;
272+
}
273+
242274
/**
243275
* Returns the locale of the post.
244276
*

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Project maintained on GitHub at [automattic/wordpress-activitypub](https://githu
141141
* Fixed: Remove old/abandoned Crons
142142
* Added: Various endpoints for the "Enable Mastodon Apps" plugin
143143
* Added: Event Objects
144+
* Added: Send notification to all Repliers if a new Comment is added
144145
* Added: Vary-Header support behind feature flag
145146

146147
= 2.0.1 =

0 commit comments

Comments
 (0)