From 3b535e12ca7381b5e5a9c2fbdfc89fc4be56caa0 Mon Sep 17 00:00:00 2001 From: Terence Eden Date: Sun, 3 Mar 2024 15:00:01 +0000 Subject: [PATCH] De-duplicate updated messages received --- index.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index a0ff7b2..ae72335 100644 --- a/index.php +++ b/index.php @@ -501,8 +501,8 @@ $totalFollowers = count( $follower_files ); $following_files = glob( $directories["following"] . "/*.json"); $totalFollowing = count( $following_files ); - + // Show the HTML page echo <<< HTML @@ -561,13 +561,32 @@ HTML; // Keep the most recent 200 $message_files = array_slice( $message_files, 0, 200 ); - // Sometimes messages are received out of order. - // This sorts them by their published time or, if there is none, the received time. + // Loop through the messages: + // Remove any which have been updated. + // Ensure messages are in the right order. $messages_ordered = []; + $messages_ids = []; foreach ( $message_files as $message_file ) { // Get the contents of the JSON $message = json_decode( file_get_contents( $message_file ), true ); - + + // Get the ID + if ( isset( $message["object"]["id"] ) ) { + $id = $message["object"]["id"]; + } else if ( isset( $message["id"] ) ) { + $id = $message["id"]; + } + + // Has this message ID already been seen? + // If so, it's an older update. Skip displaying it + if ( !in_array( $id, $messages_ids ) ) { + $messages_ids[] = $id; + } else { + continue; + } + + // Sometimes messages are received out of order. + // This sorts them by their published time or, if there is none, the received time. // Use the timestamp of the message. If there is none, use the date in the filename if ( isset( $message["published"] ) ) { $published = $message["published"];