Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d1348fd
Add comprehensive ActivityPub moderation system
obenland Jul 28, 2025
d640adf
Add comprehensive PHPUnit tests for Moderation API
obenland Jul 28, 2025
202047e
Apply PHPCBF code formatting fixes
obenland Jul 28, 2025
2892d71
Improve moderation JavaScript UX and fix terminology
obenland Jul 29, 2025
cc9cf01
Add code coverage annotations and refactor blocking logic
obenland Jul 29, 2025
db625c1
Add changelog entry for blocking and moderation system
obenland Jul 29, 2025
72f919e
Fix AJAX nonce verification order for better security
obenland Jul 29, 2025
3c8d6f0
Consolidate moderation admin functionality and fix AJAX parameter con…
obenland Jul 29, 2025
ecaacac
Add moderation meta and settings for ActivityPub
obenland Jul 29, 2025
2084082
Align variable assignments in ajax_moderation_settings
obenland Jul 29, 2025
c4b48e6
Fix test_add_user_block_valid_types by mocking remote actor response
obenland Jul 29, 2025
b4f9c68
Remove explicit cap check
obenland Jul 29, 2025
42bb423
Backslashit
obenland Jul 29, 2025
52b0423
Remove actor block functionality from moderation settings
obenland Jul 29, 2025
11dd52a
Merge branch 'trunk' into add/block-lists
obenland Jul 29, 2025
d225429
Moderation: Refine UI with singular class names and table-only handling
obenland Jul 29, 2025
1d94cf3
Use namespaced functions and improve moderation labels
obenland Jul 29, 2025
15679ca
Refactor moderation to remove actor-level blocking
obenland Jul 29, 2025
889aa91
Merge branch 'trunk' into add/block-lists
obenland Jul 29, 2025
dcf1cc1
Fix feedback
obenland Jul 30, 2025
9308e9f
Merge branch 'trunk' into add/block-lists
obenland Jul 30, 2025
51038f8
Refactor moderation to use Activity objects
obenland Jul 30, 2025
2d264e2
Improve ActivityPub moderation JavaScript with duplicate prevention a…
obenland Jul 30, 2025
92cc5be
add summary and name
pfefferle Aug 1, 2025
f1b9224
Improve object URI handling and keyword checks in moderation
pfefferle Aug 1, 2025
8150dd9
Merge branch 'trunk' into add/block-lists
pfefferle Aug 1, 2025
605c73c
Add 'id' field to Note objects in moderation tests
pfefferle Aug 1, 2025
439c17f
Add tests for blocked activity attributes in moderation
pfefferle Aug 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/changelog/2024-blocking-moderation-system
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add comprehensive blocking and moderation system for ActivityPub with user-specific and site-wide controls for actors, domains, and keywords.
305 changes: 305 additions & 0 deletions assets/js/activitypub-moderation-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
/**
* ActivityPub Moderation Admin JavaScript
*/

(function( $ ) {
'use strict';

/**
* Helper function to validate domain format
*/
function isValidDomain( domain ) {
// Basic domain validation - must contain at least one dot and valid characters
var domainRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return domainRegex.test( domain ) && domain.includes( '.' ) && domain.length > 3;
}

/**
* Helper function to check if a term already exists in the UI
*/
function isTermAlreadyBlocked( type, value, context, userId ) {
var selector;
if ( context === 'user' ) {
selector = '.activitypub-user-block-list[data-user-id="' + userId + '"] .remove-user-block-btn[data-type="' + type + '"][data-value="' + value + '"]';
} else if ( context === 'site' ) {
selector = '.remove-site-block-btn[data-type="' + type + '"][data-value="' + value + '"]';
}
return $( selector ).length > 0;
}

/**
* Helper function to add a blocked term to the UI
*/
function addBlockedTermToUI( type, value, context, userId ) {
if ( context === 'user' ) {
// For user moderation, add to the appropriate table
var container = $( '.activitypub-user-block-list[data-user-id="' + userId + '"]' );

var table = container.find( '.activitypub-blocked-' + type );
if ( table.length === 0 ) {
table = $( '<table class="widefat striped activitypub-blocked-' + type + '" role="presentation" style="max-width: 500px; margin: 15px 0;"><tbody></tbody></table>' );
container.find( '#new_user_' + type ).closest( '.add-user-block-form' ).before( table );
}
table.append( '<tr><td>' + value + '</td><td style="width: 80px;"><button type="button" class="button button-small remove-user-block-btn" data-type="' + type + '" data-value="' + value + '">Remove</button></td></tr>' );
} else if ( context === 'site' ) {
// For site moderation, add to the appropriate table
var container = $( '#new_site_' + type ).closest( '.activitypub-site-block-list' );
var table = container.find( '.activitypub-site-blocked-' + type );
if ( table.length === 0 ) {
table = $( '<table class="widefat striped activitypub-site-blocked-' + type + '" role="presentation" style="max-width: 500px; margin: 15px 0;"><tbody></tbody></table>' );
container.find( '.add-site-block-form' ).before( table );
}
table.append( '<tr><td>' + value + '</td><td style="width: 80px;"><button type="button" class="button button-small remove-site-block-btn" data-type="' + type + '" data-value="' + value + '">Remove</button></td></tr>' );
}
}

/**
* Helper function to remove a blocked term from the UI
*/
function removeBlockedTermFromUI( type, value, context ) {
// Find and remove the specific blocked term element
var selector = '.remove-' + context + '-block-btn[data-type="' + type + '"][data-value="' + value + '"]';
var button = $( selector );

if ( button.length > 0 ) {
// Remove the parent table row
var parent = button.closest( 'tr' );
var container = parent.closest( 'table' );
parent.remove();

// If the container is now empty, remove it
if ( container.find( 'tr' ).length === 0 ) {
container.remove();
}
}
}

/**
* Initialize moderation functionality
*/
function init() {
// User moderation management.
initUserModeration();

// Site moderation management.
initSiteModeration();
}

/**
* Initialize user moderation management
*/
function initUserModeration() {
// Function to add user blocked term.
function addUserBlockedTerm( type, userId ) {
var input = $( '#new_user_' + type );
var value = input.val().trim();

if ( ! value ) {
// Use wp.a11y.speak for better accessibility.
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( activitypubModerationL10n.enterValue, 'assertive' );
} else {
alert( activitypubModerationL10n.enterValue );
}
return;
}

// Validate domain format if this is a domain block
if ( type === 'domain' && ! isValidDomain( value ) ) {
var message = activitypubModerationL10n.invalidDomain || 'Please enter a valid domain (e.g., example.com).';
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
}
alert( message );
return;
}

// Check if the term is already blocked
if ( isTermAlreadyBlocked( type, value, 'user', userId ) ) {
var message = activitypubModerationL10n.alreadyBlocked || 'This term is already blocked.';
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
}
alert( message );
return;
}

wp.ajax.post( 'activitypub_moderation_settings', {
context: 'user',
operation: 'add',
user_id: userId,
type: type,
value: value,
_wpnonce: activitypubModerationL10n.nonce
}).done( function() {
// Clear input and add item to the UI.
input.val( '' );
addBlockedTermToUI( type, value, 'user', userId );
}).fail( function( response ) {
var message = response && response.message ? response.message : activitypubModerationL10n.addBlockFailed;
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
} else {
alert( message );
}
});
}

// Function to remove user blocked term.
function removeUserBlockedTerm( type, value, userId ) {
wp.ajax.post( 'activitypub_moderation_settings', {
context: 'user',
operation: 'remove',
user_id: userId,
type: type,
value: value,
_wpnonce: activitypubModerationL10n.nonce
}).done( function() {
removeBlockedTermFromUI( type, value, 'user' );
}).fail( function( response ) {
var message = response && response.message ? response.message : activitypubModerationL10n.removeBlockFailed;
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
} else {
alert( message );
}
});
}

// Add user block functionality (button click).
$( document ).on( 'click', '.add-user-block-btn', function( e ) {
e.preventDefault();
var type = $( this ).data( 'type' );
var userId = $( this ).closest( '.activitypub-user-block-list' ).data( 'user-id' );
addUserBlockedTerm( type, userId );
});

// Add user block functionality (Enter key).
$( document ).on( 'keypress', '#new_user_domain, #new_user_keyword', function( e ) {
if ( e.which === 13 ) { // Enter key.
e.preventDefault();
var inputId = $( this ).attr( 'id' );
var type = inputId.replace( 'new_user_', '' );
var userId = $( this ).closest( '.activitypub-user-block-list' ).data( 'user-id' );
addUserBlockedTerm( type, userId );
}
});

// Remove user block functionality.
$( document ).on( 'click', '.remove-user-block-btn', function( e ) {
e.preventDefault();
var type = $( this ).data( 'type' );
var value = $( this ).data( 'value' );
var userId = $( this ).closest( '.activitypub-user-block-list' ).data( 'user-id' );
removeUserBlockedTerm( type, value, userId );
});
}

/**
* Initialize site moderation management
*/
function initSiteModeration() {
// Function to add site blocked term.
function addSiteBlockedTerm( type ) {
var input = $( '#new_site_' + type );
var value = input.val().trim();

if ( ! value ) {
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( activitypubModerationL10n.enterValue, 'assertive' );
} else {
alert( activitypubModerationL10n.enterValue );
}
return;
}

// Validate domain format if this is a domain block
if ( type === 'domain' && ! isValidDomain( value ) ) {
var message = activitypubModerationL10n.invalidDomain || 'Please enter a valid domain (e.g., example.com).';
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
}
alert( message );
return;
}

// Check if the term is already blocked
if ( isTermAlreadyBlocked( type, value, 'site' ) ) {
var message = activitypubModerationL10n.alreadyBlocked || 'This term is already blocked.';
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
}
alert( message );
return;
}

wp.ajax.post( 'activitypub_moderation_settings', {
context: 'site',
operation: 'add',
type: type,
value: value,
_wpnonce: activitypubModerationL10n.nonce
}).done( function() {
// Clear input and add item to the UI.
input.val( '' );
addBlockedTermToUI( type, value, 'site' );
}).fail( function( response ) {
var message = response && response.message ? response.message : activitypubModerationL10n.addBlockFailed;
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
} else {
alert( message );
}
});
}

// Function to remove site blocked term.
function removeSiteBlockedTerm( type, value ) {
wp.ajax.post( 'activitypub_moderation_settings', {
context: 'site',
operation: 'remove',
type: type,
value: value,
_wpnonce: activitypubModerationL10n.nonce
}).done( function() {
removeBlockedTermFromUI( type, value, 'site' );
}).fail( function( response ) {
var message = response && response.message ? response.message : activitypubModerationL10n.removeBlockFailed;
if ( wp.a11y && wp.a11y.speak ) {
wp.a11y.speak( message, 'assertive' );
} else {
alert( message );
}
});
}

// Add site block functionality (button click).
$( document ).on( 'click', '.add-site-block-btn', function( e ) {
e.preventDefault();
var type = $( this ).data( 'type' );
addSiteBlockedTerm( type );
});

// Add site block functionality (Enter key).
$( document ).on( 'keypress', '#new_site_domain, #new_site_keyword', function( e ) {
if ( e.which === 13 ) { // Enter key.
e.preventDefault();
var inputId = $( this ).attr( 'id' );
var type = inputId.replace( 'new_site_', '' );
addSiteBlockedTerm( type );
}
});

// Remove site block functionality.
$( document ).on( 'click', '.remove-site-block-btn', function( e ) {
e.preventDefault();
var type = $( this ).data( 'type' );
var value = $( this ).data( 'value' );
removeSiteBlockedTerm( type, value );
});
}

// Initialize when document is ready.
$( document ).ready( init );

})( jQuery );
41 changes: 41 additions & 0 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,47 @@ public static function register_user_meta() {
'sanitize_callback' => 'absint',
)
);

// Moderation user meta.
\register_meta(
'user',
'activitypub_blocked_actors',
array(
'type' => 'array',
'description' => 'User-specific blocked ActivityPub actors.',
'single' => true,
'default' => array(),
'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
)
);

\register_meta(
'user',
'activitypub_blocked_domains',
array(
'type' => 'array',
'description' => 'User-specific blocked ActivityPub domains.',
'single' => true,
'default' => array(),
'sanitize_callback' => function ( $value ) {
return \array_unique( \array_map( array( Sanitize::class, 'host_list' ), $value ) );
},
)
);

\register_meta(
'user',
'activitypub_blocked_keywords',
array(
'type' => 'array',
'description' => 'User-specific blocked ActivityPub keywords.',
'single' => true,
'default' => array(),
'sanitize_callback' => function ( $value ) {
return \array_map( 'sanitize_text_field', $value );
},
)
);
}

/**
Expand Down
Loading
Loading