8000 Log Activitypub actions and add the publish date to Announcements by akirk · Pull Request #364 · akirk/friends · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Log Activitypub actions and add the publish date to Announcements #364

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

Merged
merged 8 commits into from
Oct 15, 2024
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
172 changes: 150 additions & 22 deletions feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1797,10 +1797,6 @@ public function queue_like_post( \WP_Post $post, $author_url ) {
*/
public function activitypub_like_post( $url, $external_post_id, $user_id ) {
$type = 'Like';
$inbox = self::get_inbox_by_actor( $url, $type );
if ( is_wp_error( $inbox ) ) {
return $inbox;
}
$actor = \get_author_posts_url( $user_id );

$activity = new \Activitypub\Activity\Activity();
Expand All @@ -1810,8 +1806,34 @@ public function activitypub_like_post( $url, $external_post_id, $user_id ) {
$activity->set_actor( $actor );
$activity->set_object( $external_post_id );
$activity->set_id( $actor . '#like-' . \preg_replace( '~^https?://~', '', $external_post_id ) );
$activity = $activity->to_json();
$response = \Activitypub\safe_remote_post( $inbox, $activity, $user_id );
$activity->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', time() ) );

$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );

if ( empty( $inboxes ) ) {
$message = sprintf(
// translators: %s is the URL of the post.
__( 'Like failed for %s', 'friends' ),
'<a href="' . esc_url( $url ) . '">' . $url . '</a>'
);

$details = array(
'url' => $url,
'error' => __( 'No inboxes to send to.', 'friends' ),
);

Logging::log( 'like-failed', $message, $details, self::SLUG, $user_id );
return;
}

$json = $activity->to_json();

$report = array();
foreach ( $inboxes as $inbox ) {
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
$report[ $inbox ] = wp_remote_retrieve_response_message( $response );
}

$user_feed = User_Feed::get_by_url( $url );
if ( $user_feed instanceof User_Feed ) {
Expand All @@ -1823,6 +1845,19 @@ public function activitypub_like_post( $url, $external_post_id, $user_id ) {
)
);
}
$type = 'like';
$message = sprintf(
// translators: %s is the URL of the post.
__( 'Liked %s', 'friends' ),
'<a href="' . esc_url( $external_post_id ) . '">' . $external_post_id . '</a>'
);
$details = array(
'actor' => $actor,
'url' => $external_post_id,
'inboxes' => $report,
);

Logging::log( 'like', $message, $details, self::SLUG, $user_id );
}

/**
Expand Down Expand Up @@ -1882,10 +1917,6 @@ public function queue_unlike_post( \WP_Post $post, $author_url ) {
*/
public function activitypub_unlike_post( $url, $external_post_id, $user_id ) {
$type = 'Like';
$inbox = self::get_inbox_by_actor( $url, $type );
if ( is_wp_error( $inbox ) ) {
return $inbox;
}
$actor = \get_author_posts_url( $user_id );

$activity = new \Activitypub\Activity\Activity();
Expand All @@ -1902,8 +1933,33 @@ public function activitypub_unlike_post( $url, $external_post_id, $user_id ) {
)
);
$activity->set_id( $actor . '#unlike-' . \preg_replace( '~^https?://~', '', $external_post_id ) );
$activity = $activity->to_json();
$response = \Activitypub\safe_remote_post( $inbox, $activity, $user_id );

$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );

if ( empty( $inboxes ) ) {
$message = sprintf(
// translators: %s is the URL of the post.
__( 'Unlike failed for %s', 'friends' ),
'<a href="' . esc_url( $url ) . '">' . $url . '</a>'
);

$details = array(
'url' => $url,
'error' => __( 'No inboxes to send to.', 'friends' ),
);

Logging::log( 'unlike-failed', $message, $details, self::SLUG, $user_id );
return;
}

$json = $activity->to_json();

$report = array();
foreach ( $inboxes as $inbox ) {
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
$report[ $inbox ] = wp_remote_retrieve_response_message( $response );
}

$user_feed = User_Feed::get_by_url( $url );
if ( $user_feed instanceof User_Feed ) {
Expand All @@ -1915,6 +1971,19 @@ public function activitypub_unlike_post( $url, $external_post_id, $user_id ) {
)
);
}
$type = 'unlike';
$message = sprintf(
// translators: %s is the URL of the post.
__( 'Unliked %s', 'friends' ),
'<a href="' . esc_url( $external_post_id ) . '">' . $external_post_id . '</a>'
);
$details = array(
'actor' => $actor,
'url' => $external_post_id,
'inboxes' => $report,
);

Logging::log( 'unlike', $message, $details, self::SLUG, $user_id );
}

public function boost_button() {
Expand Down Expand Up @@ -2096,18 +2165,48 @@ public function activitypub_announce( $url, $user_id ) {
$activity->set_actor( $actor );
$activity->set_object( $url );
$activity->set_id( $actor . '#activitypub_announce-' . \preg_replace( '~^https?://~', '', $url ) );
$activity->set_to( 'https://www.w3.org/ns/activitystreams#Public' );
$activity->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', time() ) );

$follower_inboxes = \Activitypub\Collection\Followers::get_inboxes( $user_id );
$mentioned_inboxes = \Activitypub\Mention::get_inboxes( $activity->get_cc() );

$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );

if ( empty( $inboxes ) ) {
$message = sprintf(
// translators: %s is the URL of the post.
__( 'Announce failed for %s', 'friends' ),
'<a href="' . esc_url( $url ) . '">' . $url . '</a>'
);

$details = array(
'url' => $url,
'error' => __( 'No inboxes to send to.', 'friends' ),
);

Logging::log( 'announce-failed', $message, $details, self::SLUG, $user_id );
return;
}

$json = $activity->to_json();

$report = array();
foreach ( $inboxes as $inbox ) {
\Activitypub\safe_remote_post( $inbox, $json, $user_id );
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
$report[ $inbox ] = wp_remote_retrieve_response_message( $response );
}

$message = sprintf(
// translators: %s is the URL of the post.
__( 'Announced %s', 'friends' ),
'<a href="' . esc_url( $url ) . '">' . $url . '</a>'
);

$details = array(
'url' => $url,
'inboxes' => $report,
);

Logging::log( 'announce', $message, $details, self::SLUG, $user_id );
}

/**
Expand Down Expand Up @@ -2150,18 +2249,47 @@ public function activitypub_unannounce( $url, $user_id ) {
'id' => $actor . '#activitypub_announce-' . \preg_replace( '~^https?://~', '', $url ),
)
);
$activity->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', time() ) );

$follower_inboxes = \Activitypub\Collection\Followers::get_inboxes( $user_id );
$mentioned_inboxes = \Activitypub\Mention::get_inboxes( $activity->get_cc() );

$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $user_id, $activity );
$inboxes = array_unique( $inboxes );

if ( empty( $inboxes ) ) {
$message = sprintf(
// translators: %s is the URL of the post.
__( 'Unannounce failed for %s', 'friends' ),
'<a href="' . esc_url( $url ) . '">' . $url . '</a>'
);

$details = array(
'url' => $url,
'error' => __( 'No inboxes to send to.', 'friends' ),
);

Logging::log( 'unannounce-failed', $message, $details, self::SLUG, $user_id );
return;
}

$json = $activity->to_json();

$report = array();
foreach ( $inboxes as $inbox ) {
\Activitypub\safe_remote_post( $inbox, $json, $user_id );
$response = \Activitypub\safe_remote_post( $inbox, $json, $user_id );
$report[ $inbox ] = wp_remote_retrieve_response_message( $response );
}

$message = sprintf(
// translators: %s is the URL of the post.
__( 'Unannounced %s', 'friends' ),
'<a href="' . esc_url( $url ) . '">' . $url . '</a>'
);

$details = array(
'url' => $url,
'inboxes' => $report,
);

Logging::log( 'unannounce', $message, $details, self::SLUG, $user_id );
}

/**
Expand Down
32 changes: 32 additions & 0 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ public function admin_menu() {
add_action( 'load-' . $page_type . '_page_edit-friend-rules', array( $this, 'process_admin_edit_friend_rules' ) );
}

if ( isset( $_GET['page'] ) && 'friends-logs' === $_GET['page'] ) {
// translators: as in log file.
$title = __( 'Log', 'friends' );
add_submenu_page( 'friends', $title, $title, $required_role, 'friends-logs', array( $this, 'render_friends_logs' ) );
}

if ( isset( $_GET['page'] ) && 'unfriend' === $_GET['page'] ) {
$user = new User( intval( $_GET['user'] ) );
if ( $user ) {
Expand Down Expand Up @@ -2491,6 +2497,32 @@ public function render_admin_import_export() {
public function process_admin_import_export() {
}

public function render_friends_logs() {
Friends::template_loader()->get_template_part(
'admin/settings-header',
null,
array(
'active' => 'friends-logs',
'title' => __( 'Friends', 'friends' ),
)
);
$this->check_admin_settings();

?>
<h1><?php esc_html_e( 'Logs', 'friends' ); ?></h1>
<?php

Friends::template_loader()->get_template_part(
'admin/logs',
null,
array(
'logs' => Logging::get_logs(),
)
);

Friends::template_loader()->get_template_part( 'admin/settings-footer' );
}

/**
* Gets the friend roles.
*
Expand Down
80 changes: 80 additions & 0 deletions includes/class-logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
* @author Alex Kirk
*/
class Logging {
/**
* The custom post type for logging.
*
* @var string
*/
const CPT = 'friends_log';

/**
* Contains a reference to the Friends class.
*
Expand All @@ -39,8 +46,44 @@ public function __construct( Friends $friends ) {
* Register the WordPress hooks
*/
private function register_hooks() {
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'friends_retrieved_new_posts', array( $this, 'log_feed_successfully_fetched' ), 10, 3 );
add_action( 'friends_retrieve_friends_error', array( $this, 'log_feed_error' ), 10, 2 );
add_action( 'friends_log', array( $this, 'log_entry' ), 10, 2 );
}

/**
* Register the custom post type for logging.
*/
public function register_post_type() {
$args = array(
'labels' => array(
'name' => __( 'Friends Logs', 'friends' ),
'singular_name' => __( 'Friends Log', 'friends' ),
),
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'supports' => array( 'title', 'editor' ),
);
register_post_type( self::CPT, $args );

register_post_meta(
self::CPT,
'type',
array(
'type' => 'string',
'single' => true,
)
);
register_post_meta(
self::CPT,
'module',
array(
'type' => 'string',
'single' => true,
)
);
}

/**
Expand Down Expand Up @@ -72,4 +115,41 @@ public function log_feed_successfully_fetched( User_Feed $user_feed, $new_posts,
public function log_feed_error( User_Feed $user_feed, $error ) {
$user_feed->update_last_log( $error->get_error_message() );
}

/**
* Save a log message.
*
* @param string $type The type of log message.
* @param string $message The message.
* @param array $details The details of the log message.
* @param string $module The module that generated the log message.
* @param int $user_id The ID of the user that generated the log message.
* @return int The ID of the log post.
*/
public static function log( $type, $message, $details, $module, $user_id ) {
8368 $post_id = wp_insert_post(
array(
'post_type' => self::CPT,
'post_title' => $message,
'post_content' => wp_json_encode( $details, JSON_PRETTY_PRINT ),
'post_author' => $user_id,
'post_status' => 'publish',
)
);

add_post_meta( $post_id, 'type', $type );
add_post_meta( $post_id, 'module', $module );

return $post_id;
}

public static function get_logs() {
$logs = get_posts(
array(
'post_type' => self::CPT,
'posts_per_page' => 100,
)
);
return $logs;
}
}
Loading
Loading
0