Skip to content

Commit

Permalink
Hide sticky posts that are not public (#969)
Browse files Browse the repository at this point in the history
* hide sticky posts that are not public

* remove value

* check if meta does not exist

* also ignore for outbox query!

* Delete `activitypub_content_visibility` when updated to an empty value.

* hide only local posts in outbox

* add unit tests
  • Loading branch information
pfefferle authored Nov 7, 2024
1 parent 1b7a138 commit 1ccf0ba
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
17 changes: 17 additions & 0 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public static function init() {

\add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );

\add_action( 'updated_postmeta', array( self::class, 'updated_postmeta' ), 10, 4 );

// Register several post_types.
self::register_post_types();
}
Expand Down Expand Up @@ -567,4 +569,19 @@ public static function user_register( $user_id ) {
$user->add_cap( 'activitypub' );
}
}

/**
* Delete `activitypub_content_visibility` when updated to an empty value.
*
* @param int $meta_id ID of updated metadata entry.
* @param int $object_id Post ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
* if the value is an array, an object, or itself a PHP-serialized string.
*/
public static function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( 'activitypub_content_visibility' === $meta_key && empty( $meta_value ) ) {
\delete_post_meta( $object_id, 'activitypub_content_visibility' );
}
}
}
8 changes: 8 additions & 0 deletions includes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ public static function migrate_to_4_1_0() {
if ( ! $object_type ) {
\update_option( 'activitypub_object_type', 'note' );
}

// Clean up empty visibility meta.
global $wpdb;
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
"DELETE FROM $wpdb->postmeta
WHERE meta_key = 'activitypub_content_visibility'
AND (meta_value IS NULL OR meta_value = '')"
);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions includes/rest/class-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,19 @@ public static function featured_get( $request ) {
if ( ! is_single_user() && User_Collection::BLOG_USER_ID === $user->get__id() ) {
$posts = array();
} elseif ( $sticky_posts && is_array( $sticky_posts ) ) {
// only show public posts.
$args = array(
'post__in' => $sticky_posts,
'ignore_sticky_posts' => 1,
'orderby' => 'date',
'order' => 'DESC',
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
array(
'key' => 'activitypub_content_visibility',
'compare' => 'NOT EXISTS',
),
),
);

if ( $user->get__id() > 0 ) {
Expand Down
13 changes: 13 additions & 0 deletions includes/rest/class-outbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ public static function user_outbox_get( $request ) {
'author' => $user_id > 0 ? $user_id : null,
'paged' => $page,
'post_type' => $post_types,
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'activitypub_content_visibility',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'activitypub_content_visibility',
'value' => ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL,
'compare' => '!=',
),
),
)
);

Expand Down
58 changes: 58 additions & 0 deletions tests/test-class-activitypub-migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,45 @@ public function test_migrate_actor_mode() {
}

public function test_migrate_to_4_1_0() {
$post1 = \wp_insert_post(
array(
'post_author' => 1,
'post_content' => 'activitypub_content_visibility test',
)
);

$post2 = \wp_insert_post(
array(
'post_author' => 1,
'post_content' => 'activitypub_content_visibility test',
)
);

\update_post_meta( $post1, 'activitypub_content_visibility', '' );
\update_post_meta( $post1, 'activitypub_content_123', '456' );
\update_post_meta( $post2, 'activitypub_content_visibility', 'local' );
\update_post_meta( $post2, 'activitypub_content_123', '' );

$metas1 = \get_post_meta( $post1 );

$this->assertEquals(
array(
'activitypub_content_visibility' => array( '' ),
'activitypub_content_123' => array( '456' ),
),
$metas1
);

$metas2 = \get_post_meta( $post2 );

$this->assertEquals(
array(
'activitypub_content_visibility' => array( 'local' ),
'activitypub_content_123' => array( '' ),
),
$metas2
);

$template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
$object_type = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );

Expand All @@ -66,6 +105,25 @@ public function test_migrate_to_4_1_0() {

\Activitypub\Migration::migrate_to_4_1_0();

\clean_post_cache( $post1 );
$metas1 = \get_post_meta( $post1 );
$this->assertEquals(
array(
'activitypub_content_123' => array( '456' ),
),
$metas1
);

\clean_post_cache( $post2 );
$metas2 = \get_post_meta( $post2 );
$this->assertEquals(
array(
'activitypub_content_visibility' => array( 'local' ),
'activitypub_content_123' => array( '' ),
),
$metas2
);

$template = \get_option( 'activitypub_custom_post_content' );
$content_type = \get_option( 'activitypub_post_content_type' );
$object_type = \get_option( 'activitypub_object_type' );
Expand Down

0 comments on commit 1ccf0ba

Please sign in to comment.