Skip to content

Commit 76fe523

Browse files
authored
Make notification settings user-specific (#1586)
1 parent 19d0ea1 commit 76fe523

File tree

14 files changed

+561
-226
lines changed

14 files changed

+561
-226
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Updated notification settings to be user-specific for more personalization.

assets/css/activitypub-embed.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
gap: 5px;
9696
}
9797
@media only screen and (max-width: 399px) {
98-
.activitypub-embed-meta .ap-stat {
98+
.activitypub-embed-meta span.ap-stat {
9999
display: none !important;
100100
}
101101
}

includes/class-activitypub.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ public static function uninstall() {
9898
\delete_option( 'activitypub_application_user_private_key' );
9999
\delete_option( 'activitypub_application_user_public_key' );
100100
\delete_option( 'activitypub_blog_user_also_known_as' );
101+
\delete_option( 'activitypub_blog_user_mailer_new_dm' );
102+
\delete_option( 'activitypub_blog_user_mailer_new_follower' );
103+
\delete_option( 'activitypub_blog_user_mailer_new_mention' );
101104
\delete_option( 'activitypub_blog_user_moved_to' );
102105
\delete_option( 'activitypub_blog_user_private_key' );
103106
\delete_option( 'activitypub_blog_user_public_key' );
@@ -110,9 +113,6 @@ public static function uninstall() {
110113
\delete_option( 'activitypub_enable_users' );
111114
\delete_option( 'activitypub_header_image' );
112115
\delete_option( 'activitypub_last_post_with_permalink_as_id' );
113-
\delete_option( 'activitypub_mailer_new_dm' );
114-
\delete_option( 'activitypub_mailer_new_follower' );
115-
\delete_option( 'activitypub_mailer_new_mention' );
116116
\delete_option( 'activitypub_max_image_attachments' );
117117
\delete_option( 'activitypub_migration_lock' );
118118
\delete_option( 'activitypub_object_type' );
@@ -821,6 +821,42 @@ public static function register_user_meta() {
821821
)
822822
);
823823

824+
\register_meta(
825+
'user',
826+
$blog_prefix . 'activitypub_mailer_new_dm',
827+
array(
828+
'type' => 'integer',
829+
'description' => 'Send a notification when someone sends this user a direct message.',
830+
'single' => true,
831+
'sanitize_callback' => 'absint',
832+
)
833+
);
834+
\add_filter( 'get_user_option_activitypub_mailer_new_dm', array( self::class, 'user_options_default' ) );
835+
836+
\register_meta(
837+
'user',
838+
$blog_prefix . 'activitypub_mailer_new_follower',
839+
array(
840+
'type' => 'integer',
841+
'description' => 'Send a notification when someone starts to follow this user.',
842+
'single' => true,
843+
'sanitize_callback' => 'absint',
844+
)
845+
);
846+
\add_filter( 'get_user_option_activitypub_mailer_new_follower', array( self::class, 'user_options_default' ) );
847+
848+
\register_meta(
849+
'user',
850+
$blog_prefix . 'activitypub_mailer_new_mention',
851+
array(
852+
'type' => 'integer',
853+
'description' => 'Send a notification when someone mentions this user.',
854+
'single' => true,
855+
'sanitize_callback' => 'absint',
856+
)
857+
);
858+
\add_filter( 'get_user_option_activitypub_mailer_new_mention', array( self::class, 'user_options_default' ) );
859+
824860
\register_meta(
825861
'user',
826862
'activitypub_show_welcome_tab',
@@ -845,4 +881,18 @@ public static function register_user_meta() {
845881
)
846882
);
847883
}
884+
885+
/**
886+
* Set default values for user options.
887+
*
888+
* @param bool|string $value Option value.
889+
* @return bool|string
890+
*/
891+
public static function user_options_default( $value ) {
892+
if ( false === $value ) {
893+
return '1';
894+
}
895+
896+
return $value;
897+
}
848898
}

includes/class-mailer.php

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,9 @@ public static function init() {
2020
\add_filter( 'comment_notification_subject', array( self::class, 'comment_notification_subject' ), 10, 2 );
2121
\add_filter( 'comment_notification_text', array( self::class, 'comment_notification_text' ), 10, 2 );
2222

23-
// New follower notification.
24-
if ( '1' === \get_option( 'activitypub_mailer_new_follower', '0' ) ) {
25-
\add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );
26-
}
27-
28-
// Direct message notification.
29-
if ( '1' === \get_option( 'activitypub_mailer_new_dm', '0' ) ) {
30-
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 );
31-
}
32-
33-
// Direct message notification.
34-
if ( '1' === \get_option( 'activitypub_mailer_new_mention', '1' ) ) {
35-
\add_action( 'activitypub_inbox_create', array( self::class, 'mention' ), 10, 2 );
36-
}
23+
\add_action( 'activitypub_inbox_follow', array( self::class, 'new_follower' ), 10, 2 );
24+
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 );
25+
\add_action( 'activitypub_inbox_create', array( self::class, 'mention' ), 10, 2 );
3726
}
3827

3928
/**
@@ -115,38 +104,40 @@ public static function comment_notification_text( $message, $comment_id ) {
115104
/**
116105
* Send a notification email for every new follower.
117106
*
118-
* @param Notification $notification The notification object.
107+
* @param array $activity The activity object.
108+
* @param int $user_id The id of the local blog-user.
119109
*/
120-
public static function new_follower( $notification ) {
121-
$actor = get_remote_metadata_by_actor( $notification->actor );
110+
public static function new_follower( $activity, $user_id ) {
111+
if ( $user_id > Actors::BLOG_USER_ID ) {
112+
if ( ! \get_user_option( 'activitypub_mailer_new_follower', $user_id ) ) {
113+
return;
114+
}
122115

116+
$email = \get_userdata( $user_id )->user_email;
117+
$admin_url = '/users.php?page=activitypub-followers-list';
118+
} else {
119+
if ( '1' !== \get_option( 'activitypub_blog_user_mailer_new_follower', '1' ) ) {
120+
return;
121+
}
122+
123+
$email = \get_option( 'admin_email' );
124+
$admin_url = '/options-general.php?page=activitypub&tab=followers';
125+
}
126+
127+
$actor = get_remote_metadata_by_actor( $activity['actor'] );
123128
if ( ! $actor || \is_wp_error( $actor ) ) {
124129
return;
125130
}
126131

127132
if ( empty( $actor['webfinger'] ) ) {
128-
$actor['webfinger'] = '@' . ( $actor['preferredUsername'] ?? $actor['name'] ) . '@' . wp_parse_url( $actor['url'], PHP_URL_HOST );
129-
}
130-
131-
$email = \get_option( 'admin_email' );
132-
$admin_url = '/options-general.php?page=activitypub&tab=followers';
133-
134-
if ( $notification->target > Actors::BLOG_USER_ID ) {
135-
$user = \get_user_by( 'id', $notification->target );
136-
137-
if ( ! $user ) {
138-
return;
139-
}
140-
141-
$email = $user->user_email;
142-
$admin_url = '/users.php?page=activitypub-followers-list';
133+
$actor['webfinger'] = '@' . ( $actor['preferredUsername'] ?? $actor['name'] ) . '@' . \wp_parse_url( $actor['url'], PHP_URL_HOST );
143134
}
144135

145136
$template_args = array_merge(
146137
$actor,
147138
array(
148139
'admin_url' => $admin_url,
149-
'target' => $notification->target,
140+
'user_id' => $user_id,
150141
'stats' => array(
151142
'outbox' => null,
152143
'followers' => null,
@@ -161,16 +152,16 @@ public static function new_follower( $notification ) {
161152
}
162153

163154
$result = Http::get( $actor[ $field ], true );
164-
if ( 200 === wp_remote_retrieve_response_code( $result ) ) {
165-
$body = \json_decode( wp_remote_retrieve_body( $result ), true );
155+
if ( 200 === \wp_remote_retrieve_response_code( $result ) ) {
156+
$body = \json_decode( \wp_remote_retrieve_body( $result ), true );
166157
if ( isset( $body['totalItems'] ) ) {
167158
$template_args['stats'][ $field ] = $body['totalItems'];
168159
}
169160
}
170161
}
171162

172163
/* translators: 1: Blog name, 2: Follower name */
173-
$subject = \sprintf( \__( '[%1$s] New Follower: %2$s', 'activitypub' ), get_option( 'blogname' ), $actor['name'] );
164+
$subject = \sprintf( \__( '[%1$s] New Follower: %2$s', 'activitypub' ), \get_option( 'blogname' ), $actor['name'] );
174165

175166
\ob_start();
176167
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-follower.php', false, $template_args );
@@ -208,26 +199,28 @@ public static function direct_message( $activity, $user_id ) {
208199
return;
209200
}
210201

202+
if ( $user_id > Actors::BLOG_USER_ID ) {
203+
if ( ! \get_user_option( 'activitypub_mailer_new_dm', $user_id ) ) {
204+
return;
205+
}
206+
207+
$email = \get_userdata( $user_id )->user_email;
208+
} else {
209+
if ( '1' !== \get_option( 'activitypub_blog_user_mailer_new_dm', '1' ) ) {
210+
return;
211+
}
212+
213+
$email = \get_option( 'admin_email' );
214+
}
215+
211216
$actor = get_remote_metadata_by_actor( $activity['actor'] );
212217

213218
if ( ! $actor || \is_wp_error( $actor ) || empty( $activity['object']['content'] ) ) {
214219
return;
215220
}
216221

217222
if ( empty( $actor['webfinger'] ) ) {
218-
$actor['webfinger'] = '@' . ( $actor['preferredUsername'] ?? $actor['name'] ) . '@' . wp_parse_url( $actor['url'], PHP_URL_HOST );
219-
}
220-
221-
$email = \get_option( 'admin_email' );
222-
223-
if ( (int) $user_id > Actors::BLOG_USER_ID ) {
224-
$user = \get_user_by( 'id', $user_id );
225-
226-
if ( ! $user ) {
227-
return;
228-
}
229-
230-
$email = $user->user_email;
223+
$actor['webfinger'] = '@' . ( $actor['preferredUsername'] ?? $actor['name'] ) . '@' . \wp_parse_url( $actor['url'], PHP_URL_HOST );
231224
}
232225

233226
$template_args = array(
@@ -237,7 +230,7 @@ public static function direct_message( $activity, $user_id ) {
237230
);
238231

239232
/* translators: 1: Blog name, 2 Actor name */
240-
$subject = \sprintf( \esc_html__( '[%1$s] Direct Message from: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $actor['name'] ) );
233+
$subject = \sprintf( \esc_html__( '[%1$s] Direct Message from: %2$s', 'activitypub' ), \esc_html( \get_option( 'blogname' ) ), \esc_html( $actor['name'] ) );
241234

242235
\ob_start();
243236
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-dm.php', false, $template_args );
@@ -282,25 +275,27 @@ public static function mention( $activity, $user_id ) {
282275
return;
283276
}
284277

278+
if ( $user_id > Actors::BLOG_USER_ID ) {
279+
if ( ! \get_user_option( 'activitypub_mailer_new_mention', $user_id ) ) {
280+
return;
281+
}
282+
283+
$email = \get_userdata( $user_id )->user_email;
284+
} else {
285+
if ( '1' !== \get_option( 'activitypub_blog_user_mailer_new_mention', '1' ) ) {
286+
return;
287+
}
288+
289+
$email = \get_option( 'admin_email' );
290+
}
291+
285292
$actor = get_remote_metadata_by_actor( $activity['actor'] );
286293
if ( \is_wp_error( $actor ) ) {
287294
return;
288295
}
289296

290297
if ( empty( $actor['webfinger'] ) ) {
291-
$actor['webfinger'] = '@' . ( $actor['preferredUsername'] ?? $actor['name'] ) . '@' . wp_parse_url( $actor['url'], PHP_URL_HOST );
292-
}
293-
294-
$email = \get_option( 'admin_email' );
295-
296-
if ( (int) $user_id > Actors::BLOG_USER_ID ) {
297-
$user = \get_user_by( 'id', $user_id );
298-
299-
if ( ! $user ) {
300-
return;
301-
}
302-
303-
$email = $user->user_email;
298+
$actor['webfinger'] = '@' . ( $actor['preferredUsername'] ?? $actor['name'] ) . '@' . \wp_parse_url( $actor['url'], PHP_URL_HOST );
304299
}
305300

306301
$template_args = array(
@@ -310,7 +305,7 @@ public static function mention( $activity, $user_id ) {
310305
);
311306

312307
/* translators: 1: Blog name, 2 Actor name */
313-
$subject = \sprintf( \esc_html__( '[%1$s] Mention from: %2$s', 'activitypub' ), \esc_html( get_option( 'blogname' ) ), \esc_html( $actor['name'] ) );
308+
$subject = \sprintf( \esc_html__( '[%1$s] Mention from: %2$s', 'activitypub' ), \esc_html( \get_option( 'blogname' ) ), \esc_html( $actor['name'] ) );
314309

315310
\ob_start();
316311
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/emails/new-mention.php', false, $template_args );

includes/class-migration.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public static function maybe_migrate() {
192192
self::delete_mastodon_api_orphaned_extra_fields();
193193
}
194194
if ( \version_compare( $version_from_db, 'unreleased', '<' ) ) {
195-
self::add_mention_notification_option();
195+
self::update_notification_options();
196196
}
197197

198198
/*
@@ -743,7 +743,6 @@ public static function update_comment_author_emails( $batch_size = 50, $offset =
743743
*/
744744
public static function add_default_settings() {
745745
self::add_activitypub_capability();
746-
self::add_notification_defaults();
747746
self::add_default_extra_field();
748747
}
749748

@@ -798,15 +797,6 @@ private static function add_activitypub_capability() {
798797
}
799798
}
800799

801-
/**
802-
* Add default notification settings.
803-
*/
804-
private static function add_notification_defaults() {
805-
\add_option( 'activitypub_mailer_new_follower', '1' );
806-
\add_option( 'activitypub_mailer_new_dm', '1' );
807-
\add_option( 'activitypub_mailer_new_mention', '1' );
808-
}
809-
810800
/**
811801
* Add a default extra field for the user.
812802
*/
@@ -929,9 +919,26 @@ public static function delete_mastodon_api_orphaned_extra_fields() {
929919
}
930920

931921
/**
932-
* Add the new mention notification option.
922+
* Update notification options.
933923
*/
934-
public static function add_mention_notification_option() {
935-
\add_option( 'activitypub_mailer_new_mention', '1' );
924+
public static function update_notification_options() {
925+
$new_dm = \get_option( 'activitypub_mailer_new_dm', '1' );
926+
$new_follower = \get_option( 'activitypub_mailer_new_follower', '1' );
927+
928+
// Add the blog user notification options.
929+
\add_option( 'activitypub_blog_user_mailer_new_dm', $new_dm );
930+
\add_option( 'activitypub_blog_user_mailer_new_follower', $new_follower );
931+
\add_option( 'activitypub_blog_user_mailer_new_mention', '1' );
932+
933+
// Add the actor notification options.
934+
foreach ( Actors::get_collection() as $actor ) {
935+
\update_user_option( $actor->get__id(), 'activitypub_mailer_new_dm', $new_dm );
936+
\update_user_option( $actor->get__id(), 'activitypub_mailer_new_follower', $new_follower );
937+
\update_user_option( $actor->get__id(), 'activitypub_mailer_new_mention', '1' );
938+
}
939+
940+
// Delete the old notification options.
941+
\delete_option( 'activitypub_mailer_new_dm' );
942+
\delete_option( 'activitypub_mailer_new_follower' );
936943
}
937944
}

0 commit comments

Comments
 (0)