とりあえず、関連記事。
"WordPressのプラグイン「ActivityPub」のアカウントa@Aの投稿にマストドンのアカウントb@Bから返信があり、その返信にa@Aがコメント欄から返信して、その返信にさらにa@Aがコメント欄から返信する際に、b@Bへのメンションを外したいのですが、できません。
さらに、同じスレッドで、別のマストドンのアカウントc@Cからa@Aに返信(メンションはa@A宛だけ)があった後、そのc@Cの返信にa@Aが返信しようとすると、b@Bへのメンションも追加されてしまいます。
"
コメント欄ではメンション先を選べない | いしい@試行錯誤
https://ishii00141.stars.ne.jp/20250216-1635-895/
"今も同じかどうか確認するためのテスト。
"
コメントと通知の関係を確認するテスト | いしい@試行錯誤
https://ishii00141.stars.ne.jp/20250311-0803-1646/

この問題に関するコードは、これではないかと思うものを見つけたのでメモ。
/wp-content/plugins/activitypub/includes/transformer/class-comment.php
/**
* Gets the ancestors of the comment, but only the ones that are ActivityPub comments.
*
* @return array The list of ancestors.
*/
protected function get_comment_ancestors() {
$ancestors = get_comment_ancestors( $this->item );
// Now that we have the full tree of ancestors, only return the ones received from the fediverse.
return array_filter(
$ancestors,
function ( $comment_id ) {
return \get_comment_meta( $comment_id, 'protocol', true ) === 'activitypub';
}
);
}
/**
* Collect all other Users that participated in this comment-thread
* to send them a notification about the new reply.
*
* @param array $mentions Optional. The already mentioned ActivityPub users. Default empty array.
*
* @return array The list of all Repliers.
*/
public function extract_reply_context( $mentions = array() ) {
// Check if `$this->item` is a WP_Comment.
if ( 'WP_Comment' !== get_class( $this->item ) ) {
return $mentions;
}
$ancestors = $this->get_comment_ancestors();
if ( ! $ancestors ) {
return $mentions;
}
foreach ( $ancestors as $comment_id ) {
$comment = \get_comment( $comment_id );
if ( $comment && ! empty( $comment->comment_author_url ) ) {
$acct = Webfinger::uri_to_acct( $comment->comment_author_url );
if ( $acct && ! is_wp_error( $acct ) ) {
$acct = str_replace( 'acct:', '@', $acct );
$mentions[ $acct ] = $comment->comment_author_url;
}
}
}
return $mentions;
}
このコードを #Gemini に分析してもらったら、「このコードの目的」は次の通りだった。
このコードは、WordPressサイトにActivityPub連携機能が実装されている場合に、コメントへの返信があった際に、その返信がどのコメントスレッドに対するものかを把握し、そのスレッドに参加している他のActivityPubユーザーに通知を送るために、必要な情報を収集する役割を果たしていると考えられます。これにより、分散型のソーシャルネットワーク上でのコミュニケーションを円滑に行うことができます。
要するに、スレッドに参加している全員に通知する仕組みらしい。バグではなく仕様。
コメント
foreach のコードの前に return $mentions; を追加して foreach のコードを無視させることで、不要な通知を避けることができた。
https://ishii00141.stars.ne.jp/?c=687
すなわち、通知が必要ならばコメント欄にメンションを記載しなければいけないようにできた。
https://ishii00141.stars.ne.jp/?c=688
その方向で、対処方法を #Gemini に相談してみることにする。
このコードを使って、通知(cc)のためのメンションリストを作っていると思われるコード。
これに関してはいろいろと試したが、通知を防ぐことはできなかった。
\add_filter( 'activitypub_extract_mentions', array( $this, 'extract_reply_context' ) );
をコメントアウトするだけで、ccに通知URLが追加されずに済む。
https://ishii00141.stars.ne.jp/?c=722
スレッド内のアカウントに自動的に通知(ccにアカウントのURLを追加)されるのを防ぐには、次のコードをfunctions.phpに追加することで実現できた。
このコードでは、コメント欄にメンションを記載しても通知(ccにアカウントのURLを追加)されないようなので、残念ながら採用できない。
このコードを使って、送信するコンテンツにメンションを追加していると思われるコード。
Fediverseで表示された場合に自動追加されたメンションが表示されないようにするには、次のコードをfunctions.phpに追加することで実現できた。ただし、通知はされる。