Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log the newly supported ActivityPub events to the Friends Log #423

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
81 changes: 77 additions & 4 deletions feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,37 @@ protected function process_incoming_activity( $type, $activity, $user_id, $user_
if ( isset( $activity['object']['type'] ) && 'Person' === $activity['object']['type'] ) {
return $this->handle_incoming_update_person( $activity['object'], $user_feed );
}
return $this->handle_incoming_create( $activity['object'] );
$item = $this->handle_incoming_create( $activity['object'] );
if ( isset( $activity['object']['type'] ) && 'Note' === $activity['object']['type'] ) {
$friend_user = $user_feed->get_friend_user();
$post_id = Feed::url_to_postid( $item->permalink );
$message = sprintf(
// translators: %1$s is the post URL, %2$s is the linked user display name.
__( 'Received <a href="%1$s">post update</a> for %2$s', 'friends' ),
$friend_user->get_local_friends_page_url( $post_id ),
'<a href="' . esc_url( $friend_user->get_local_friends_page_url() ) . '">' . esc_html( $friend_user->display_name ) . '</a>'
);
$details = array();
if ( $post_id ) {
$_post = get_post( $post_id );
if ( ! class_exists( 'WP_Text_Diff_Renderer_inline', false ) ) {
require ABSPATH . WPINC . '/wp-diff.php';
}
$diff = new \Text_Diff( explode( 'PHP_EOL', wp_strip_all_tags( $item->content ) ), explode( 'PHP_EOL', wp_strip_all_tags( $_post->post_content ) ) );
$renderer = new \WP_Text_Diff_Renderer_inline();
$details['content'] = $renderer->render( $diff );
if ( empty( $details['content'] ) ) {
unset( $details['content'] );
}
}

if ( ! empty( $details ) ) {
$details['post_id'] = $post_id;
$details['activity'] = $activity;
Logging::log( 'post-update', $message, $details, self::SLUG, 0, $friend_user->ID );
}
}
return $item;
case 'delete':
return $this->handle_incoming_delete( $activity['object'] );
case 'announce':
Expand Down Expand Up @@ -1065,13 +1095,43 @@ private function handle_incoming_update_person( $activity, User_Feed $user_feed
$friend_user = $user_feed->get_friend_user();
$this->log( 'Received person update for ' . $friend_user->user_login, compact( 'activity' ) );

if ( ! empty( $activity['summary'] ) ) {
$friend_user->description = $activity['summary'];
$message = sprintf(
// translators: %s is the user login.
__( 'Received person update for %s', 'friends' ),
'<a href="' . esc_url( $friend_user->get_local_friends_page_url() ) . '">' . esc_html( $friend_user->display_name ) . '</a>'
);

$details = array();

if ( ! empty( $activity['summary'] ) && $friend_user->description !== $activity['summary'] ) {
if ( ! class_exists( 'WP_Text_Diff_Renderer_inline', false ) ) {
require ABSPATH . WPINC . '/wp-diff.php';
}
$summary = wp_encode_emoji( $activity['summary'] );
$diff = new \Text_Diff( explode( PHP_EOL, $friend_user->description ), explode( PHP_EOL, $summary ) );
$renderer = new \WP_Text_Diff_Renderer_inline();
$details['summary'] = $renderer->render( $diff );
if ( empty( $details['summary'] ) ) {
unset( $details['summary'] );
} else {
$message .= ' ' . __( 'Updated description.', 'friends' );
}

$friend_user->description = $summary;
}
if ( ! empty( $activity['icon']['url'] ) ) {
if ( ! empty( $activity['icon']['url'] ) && $friend_user->get_avatar_url() !== $activity['icon']['url'] ) {
$details['old-icon'] = '<img src="' . esc_url( $friend_user->get_avatar_url() ) . '" style="max-height: 32px; max-width: 32px" />';
$details['new-icon'] = '<img src="' . esc_url( $activity['icon']['url'] ) . '" style="max-height: 32px; max-width: 32px" />';
$friend_user->update_user_icon_url( $activity['icon']['url'] );
$message .= ' ' . __( 'Updated icon.', 'friends' );
}
$friend_user->save();

if ( ! empty( $details ) ) {
$details['object'] = $activity;
Logging::log( 'user-update', $message, $details, self::SLUG, 0, $friend_user->ID );
}

return null; // No feed item to submit.
}
/**
Expand Down Expand Up @@ -1261,6 +1321,7 @@ public function handle_incoming_move( $activity, User_Feed $user_feed ) {
'post-format' => $user_feed->get_post_format(),
'active' => $user_feed->is_active(),
);
$this->log( 'Received Move from ' . $old_url . ' to ' . $feed['url'] );

// Similar as in process_admin_edit_friend_feeds.
if ( $user_feed->get_url() !== $feed['url'] ) {
Expand Down Expand Up @@ -1296,7 +1357,19 @@ public function handle_incoming_move( $activity, User_Feed $user_feed ) {
$user_feed->get_url()
)
);

$message = sprintf(
// translators: %s is the new URL.
__( '%1$s moved to a new URL: %2$s', 'friends' ),
'<a href="' . esc_url( $old_url ) . '">' . esc_html( $feed['title'] ) . '</a>',
'<a href="' . esc_url( $new_feed->get_url() ) . '">' . esc_html( $new_feed->get_title() ) . '</a>'
);

Logging::log( 'feed-move', $message, $activity, self::SLUG, 0, $friend->ID );

return $new_feed;
} else {
$this->log( 'Move URL didn\'t change, old: ' . $old_url . ', new: ' . $feed['url'] );
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ public function process_admin_add_friend( $vars ) {
$avatar = $feed_details['avatar'];
}
if ( ! $description && ! empty( $feed_details['description'] ) ) {
$description = $feed_details['description'];
$description = wp_encode_emoji( $feed_details['description'] );
}
}

Expand Down
2 changes: 0 additions & 2 deletions includes/class-friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Friends;

use stdClass;

/**
* This is the class for the Friends Plugin.
*
Expand Down
4 changes: 4 additions & 0 deletions includes/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,10 @@ public function template_override( $template ) {
$wp_query->is_404 = false;

status_header( 200 );
if ( 'frontend/index' === $this->template ) {
$args['frontend_default_view'] = get_user_option( 'friends_frontend_default_view', get_current_user_id() );

}
return Friends::template_loader()->get_template_part( $this->template, null, $args, false );
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static function log( $type, $message, $details, $module, $user_id ) {
array(
'post_type' => self::CPT,
'post_title' => $message,
'post_content' => wp_json_encode( $details, JSON_PRETTY_PRINT ),
'post_content' => wp_json_encode( $details, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
'post_author' => $user_id,
'post_status' => 'publish',
)
Expand Down
8 changes: 4 additions & 4 deletions includes/class-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function __construct( \WP_Term $term ) {
$this->data = (object) array(
'ID' => $this->ID,
'user_login' => $term->name,
'display_name' => get_metadata( 'term', $term->term_id, 'display_name', true ),
'user_url' => get_metadata( 'term', $term->term_id, 'user_url', true ),
'description' => get_metadata( 'term', $term->term_id, 'description', true ),
'user_registered' => get_metadata( 'term', $term->term_id, 'created', true ),
'display_name' => $this->get_user_option( 'display_name' ),
'user_url' => $this->get_user_option( 'user_url' ),
'description' => $this->get_user_option( 'description' ),
'user_registered' => $this->get_user_option( 'created' ),
);
}

Expand Down
23 changes: 22 additions & 1 deletion templates/admin/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
*/

?>
<style>
del {
background-color: #ffabaf;
text-decoration: none;
}

ins {
background-color: #68de7c;
text-decoration: none;
}
</style>
<table class="widefat">
<thead>
<tr>
Expand All @@ -23,7 +34,17 @@
<td>
<details>
<summary><?php echo wp_kses_post( $log->post_title ); ?></summary>
<pre><?php echo wp_kses_post( $log->post_content ); ?></pre>
<pre>
<?php
echo wp_kses(
str_replace( array( '&lt;del&gt;', '&lt;/del&gt;', '&lt;ins&gt;', '&lt;/ins&gt;' ), array( '<del>', '</del>', '<ins>', '</ins>' ), esc_html( $log->post_content ) ),
array(
'ins' => array(),
'del' => array(),
)
);
?>
</pre>
</details>
</td>
<td><?php echo esc_html( get_the_author_meta( 'display_name', $log->post_author ) ); ?></td>
Expand Down
7 changes: 6 additions & 1 deletion templates/frontend/author-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
)
);
} else {
if ( $args['friend_user']->get_avatar_url() ) {
?>
<img src="<?php echo esc_attr( $args['friend_user']->get_avatar_url() ); ?>" alt="<?php echo esc_attr( $args['friend_user']->display_name ); ?>" class="avatar" width="36" height="36" style="vertical-align: middle;" />
<?php
}
echo esc_html( $args['friend_user']->display_name );
}
?>
Expand All @@ -52,7 +57,7 @@
<p>
<?php
echo wp_kses(
str_replace( '</p>', '<br/>', $args['friend_user']->description ),
make_clickable( str_replace( '</p>', '<br/>', $args['friend_user']->description ) ),
array(
'a' => array( 'href' => array() ),
'span' => array( 'class' => array() ),
Expand Down
Loading