Skip to content

Commit

Permalink
Settings to enable/disable notifications (#1109)
Browse files Browse the repository at this point in the history
* Settings to enable/disable notifications

This PR adds a settings section to enable/disable notifications.

Fix #1107

* enable by default for new users

* autoload

* use add_option

* call add_notification_defaults

* fix tests
  • Loading branch information
pfefferle authored Dec 20, 2024
1 parent c7026f8 commit 144e632
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Add a filter to allow modifying the ActivityPub preview template
* `@mentions` in the JSON representation of the reply
* Add settings to enable/disable e-mail notifications for new followers and direct messages

### Improved

Expand Down
19 changes: 19 additions & 0 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,25 @@ public static function register_settings() {
)
);

\register_setting(
'activitypub',
'activitypub_mailer_new_follower',
array(
'type' => 'boolean',
'description' => \__( 'Send notifications via e-mail when a new follower is added.', 'activitypub' ),
'default' => '0',
)
);
\register_setting(
'activitypub',
'activitypub_mailer_new_dm',
array(
'type' => 'boolean',
'description' => \__( 'Send notifications via e-mail when a direct message is received.', 'activitypub' ),
'default' => '0',
)
);

// Blog-User Settings.
\register_setting(
'activitypub_blog',
Expand Down
8 changes: 6 additions & 2 deletions includes/class-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ public static function init() {
\add_filter( 'comment_notification_text', array( self::class, 'comment_notification_text' ), 10, 2 );

// New follower notification.
\add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );
if ( '1' === \get_option( 'activitypub_mailer_new_follower', '0' ) ) {
\add_action( 'activitypub_notification_follow', array( self::class, 'new_follower' ) );
}

// Direct message notification.
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 );
if ( '1' === \get_option( 'activitypub_mailer_new_dm', '0' ) ) {
\add_action( 'activitypub_inbox_create', array( self::class, 'direct_message' ), 10, 2 );
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions includes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ public static function update_comment_counts( $batch_size = 100, $offset = 0 ) {
*/
public static function add_default_settings() {
self::add_activitypub_capability();
self::add_notification_defaults();
}

/**
Expand All @@ -471,6 +472,14 @@ private static function add_activitypub_capability() {
}
}

/**
* Add default notification settings.
*/
private static function add_notification_defaults() {
\add_option( 'activitypub_mailer_new_follower', '1' );
\add_option( 'activitypub_mailer_new_dm', '1' );
}

/**
* Rename meta keys.
*
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ For reasons of data protection, it is not possible to see the followers of other

* Added: A filter to allow modifying the ActivityPub preview template
* Added: `@mentions` in the JSON representation of the reply
* Added: Settings to enable/disable e-mail notifications for new followers and direct messages
* Improved: HTML to e-mail text conversion
* Improved: Direct Messages: Test for the user being in the to field
* Improved: Better support for FSE color schemes
Expand Down
76 changes: 56 additions & 20 deletions templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,43 @@
</tbody>
</table>
</div>

<div class="box">
<h3><?php \esc_html_e( 'Notifications', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'Choose which notifications you want to receive. The plugin currently only supports e-mail notifications, but we will add more options in the future.', 'activitypub' ); ?></p>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<?php \esc_html_e( 'Type', 'activitypub' ); ?>
</th>
<th>
<?php \esc_html_e( 'E-Mail', 'activitypub' ); ?>
</th>
</tr>
<tr>
<td scope="row">
<?php \esc_html_e( 'New followers', 'activitypub' ); ?>
</td>
<td>
<label>
<input type="checkbox" name="activitypub_mailer_new_follower" id="activitypub_mailer_new_follower" value="1" <?php \checked( '1', \get_option( 'activitypub_mailer_new_follower', '0' ) ); ?> />
</label>
</td>
</tr>
<tr>
<td scope="row">
<?php \esc_html_e( 'Direct Messages', 'activitypub' ); ?>
</td>
<td>
<label>
<input type="checkbox" name="activitypub_mailer_new_dm" id="activitypub_mailer_new_dm" value="1" <?php \checked( '1', \get_option( 'activitypub_mailer_new_dm', '0' ) ); ?> />
</label>
</td>
</tr>
<?php \do_settings_fields( 'activitypub', 'security' ); ?>
</tbody>
</table>
</div>
<div class="box">
<h3><?php \esc_html_e( 'General', 'activitypub' ); ?></h3>
<table class="form-table">
Expand All @@ -211,25 +247,6 @@
</p>
</td>
</tr>
<tr>
<th scope="row">
<?php \esc_html_e( 'Blocklist', 'activitypub' ); ?>
</th>
<td>
<p>
<?php
echo \wp_kses(
\sprintf(
// translators: %s is a URL.
\__( 'To block servers, add the host of the server to the "<a href="%s">Disallowed Comment Keys</a>" list.', 'activitypub' ),
\esc_url( \admin_url( 'options-discussion.php#disallowed_keys' ) )
),
'default'
);
?>
</p>
</td>
</tr>
<?php \do_settings_fields( 'activitypub', 'general' ); ?>
<?php \do_settings_fields( 'activitypub', 'server' ); ?>
</tbody>
Expand Down Expand Up @@ -260,6 +277,25 @@
</td>
</tr>
<?php endif; ?>
<tr>
<th scope="row">
<?php \esc_html_e( 'Blocklist', 'activitypub' ); ?>
</th>
<td>
<p>
<?php
echo \wp_kses(
\sprintf(
// translators: %s is a URL.
\__( 'To block servers, add the host of the server to the "<a href="%s">Disallowed Comment Keys</a>" list.', 'activitypub' ),
\esc_url( \admin_url( 'options-discussion.php#disallowed_keys' ) )
),
'default'
);
?>
</p>
</td>
</tr>
<?php \do_settings_fields( 'activitypub', 'security' ); ?>
</tbody>
</table>
Expand Down
21 changes: 18 additions & 3 deletions tests/includes/class-test-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,26 @@ function ( $args ) {
* @covers ::init
*/
public function test_init() {
\delete_option( 'activitypub_mailer_new_follower' );
\delete_option( 'activitypub_mailer_new_dm' );

Mailer::init();

$this->assertEquals( 10, \has_filter( 'comment_notification_subject', array( Mailer::class, 'comment_notification_subject' ) ) );
$this->assertEquals( 10, \has_filter( 'comment_notification_text', array( Mailer::class, 'comment_notification_text' ) ) );
$this->assertEquals( 10, \has_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) ) );
$this->assertEquals( 10, \has_action( 'activitypub_inbox_create', array( Mailer::class, 'direct_message' ) ) );

\remove_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) );
\remove_action( 'activitypub_inbox_create', array( Mailer::class, 'direct_message' ) );

\update_option( 'activitypub_mailer_new_follower', '0' );
\update_option( 'activitypub_mailer_new_dm', '0' );

Mailer::init();

$this->assertEquals( 10, has_filter( 'comment_notification_subject', array( Mailer::class, 'comment_notification_subject' ) ) );
$this->assertEquals( 10, has_filter( 'comment_notification_text', array( Mailer::class, 'comment_notification_text' ) ) );
$this->assertEquals( 10, has_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) ) );
$this->assertEquals( false, \has_action( 'activitypub_notification_follow', array( Mailer::class, 'new_follower' ) ) );
$this->assertEquals( false, \has_action( 'activitypub_inbox_create', array( Mailer::class, 'direct_message' ) ) );
}

/**
Expand Down

0 comments on commit 144e632

Please sign in to comment.