From 2454dbdda4fce35da8bf97c96a1cf604812f74bf Mon Sep 17 00:00:00 2001 From: Terence Eden Date: Tue, 5 Mar 2024 20:41:32 +0000 Subject: [PATCH] User interface for (un)following & (un)blocking an external user. --- index.php | 131 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 100 insertions(+), 31 deletions(-) diff --git a/index.php b/index.php index d0fc7b1..153e8e6 100644 --- a/index.php +++ b/index.php @@ -88,12 +88,12 @@ outbox(); // Optional. Dynamic. case "write": write(); // User interface for writing posts. - case "send": + case "action/send": send(); // API for posting content to the Fediverse. - case "follow": - follow(); // User interface for following an external user. - case "follow_user": - follow_user(); // API for following a user. + case "users": + users(); // User interface for (un)following & (un)blocking an external user. + case "action/users": + action_users(); // API for following a user. case "read": view( "read" );// User interface for reading posts. case ".well-known/nodeinfo": @@ -583,7 +583,7 @@ HTML; // Buttons to interact with a message. // By default, just shows a "Follow User" button. if ( "read" == $style ) { - $interactHTML = " "; + $interactHTML = " "; } else { $interactHTML = ""; } @@ -750,7 +750,7 @@ echo <<< HTML
Send a message -
+

@@ -1235,11 +1235,11 @@ HTML; } // This creates a UI for the user to follow another user - function follow() { + function users() { if ( isset( $_GET["account"] ) ) { $accountURl = htmlspecialchars( $_GET["account"] ); } else { - $announceURl = ""; + $accountURl = ""; } echo <<< HTML @@ -1252,12 +1252,20 @@ HTML; - - + +
+
+


- +
@@ -1269,14 +1277,25 @@ HTML; // It looks up the external user's details // Then it sends a follow request // If the request is accepted, it saves the details in `data/following/` as a JSON file - function follow_user() { + function action_users() { global $password, $server, $username, $key_private, $directories; // Does the posted password match the stored password? if( $password != $_POST["password"] ) { echo "Wrong Password!"; die(); } // Get the posted content - $user = $_POST["user"]; + $user = $_POST["user"]; + $action = $_POST["action"]; + + // Is this a valid action? + if ( match( $action ) { + "Follow", "Unfollow", "Block", "Unblock" => false, + default => true, + } ) { + // Discard it, no further processing. + echo "{$action} not supported"; + die(); + } // Split the user (@user@example.com) into username and server list( , $follow_name, $follow_server ) = explode( "@", $user ); @@ -1299,27 +1318,77 @@ HTML; // Get the user's inbox $profileInbox = $profileData["inbox"]; - - // Create a follow request - $guid = uuid(); - $message = [ - "@context" => "https://www.w3.org/ns/activitystreams", - "id" => "https://{$server}/{$guid}", - "type" => "Follow", - "actor" => "https://{$server}/{$username}", - "object" => $profileURl - ]; - // Sign a request to follow - // The Accept is POSTed to the inbox on the server of the user who requested the follow + // Create a user request + $guid = uuid(); + + // Different user actions have subtly different messages to send. + if ( "Follow" == $action ) { + $message = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://{$server}/{$guid}", + "type" => "Follow", + "actor" => "https://{$server}/{$username}", + "object" => $profileURl + ]; + } else if ( "Unfollow" == $action ) { + $message = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://{$server}/{$guid}", + "type" => "Undo", + "actor" => "https://{$server}/{$username}", + "object" => array( + //"id" => null, // Should be the original ID if possible, but not necessary https://www.w3.org/wiki/ActivityPub/Primer/Referring_to_activities + "type" => "Follow", + "actor" => "https://{$server}/{$username}", + "object" => $profileURl + ) + ]; + } else if ( "Block" == $action ) { + $message = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://{$server}/{$guid}", + "type" => "Block", + "actor" => "https://{$server}/{$username}", + "object" => $profileURl, + "to" => $profileURl + ]; + } else if ( "Unblock" == $action ) { + $message = [ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => "https://{$server}/{$guid}", + "type" => "Undo", + "actor" => "https://{$server}/{$username}", + "object" => array( + //"id" => null, // Should be the original ID if possible, but not necessary https://www.w3.org/wiki/ActivityPub/Primer/Referring_to_activities + "type" => "Block", + "actor" => "https://{$server}/{$username}", + "object" => $profileURl + ) + ]; + } + + // Sign & send the request sentMessageToSingle( $profileInbox, $message ); - // Save the user's details - $following_filename = urlencode( $profileURl ); - file_put_contents( $directories["following"] . "/{$following_filename}.json", json_encode( $profileData ) ); + if ( "Follow" == $action ) { + // Save the user's details + $following_filename = urlencode( $profileURl ); + file_put_contents( $directories["following"] . "/{$following_filename}.json", json_encode( $profileData ) ); - // Render the JSON so the user can see the POST has worked - header( "Location: https://{$server}/data/following/" . urlencode( $following_filename ) . ".json" ); + // Render the JSON so the user can see the POST has worked + header( "Location: https://{$server}/data/following/" . urlencode( $following_filename ) . ".json" ); + } else if ( "Block" == $action || "Unfollow" == $action ) { + // Delete the user if they exist in the following directory. + $following_filename = urlencode( $profileURl ); + unlink( $directories["following"] . "/{$following_filename}.json" ); + + // Let the user know it worked + echo "{$user} {$action}ed!"; + } else if ( "Unblock" == $action ) { + // Let the user know it worked + echo "{$user} {$action}ed!"; + } die(); }