User interface for (un)following & (un)blocking an external user.

merge-requests/5/head
Terence Eden 2024-03-05 20:41:32 +00:00
rodzic 07da99fcde
commit 2454dbdda4
1 zmienionych plików z 100 dodań i 31 usunięć

127
index.php
Wyświetl plik

@ -88,12 +88,12 @@
outbox(); // Optional. Dynamic. outbox(); // Optional. Dynamic.
case "write": case "write":
write(); // User interface for writing posts. write(); // User interface for writing posts.
case "send": case "action/send":
send(); // API for posting content to the Fediverse. send(); // API for posting content to the Fediverse.
case "follow": case "users":
follow(); // User interface for following an external user. users(); // User interface for (un)following & (un)blocking an external user.
case "follow_user": case "action/users":
follow_user(); // API for following a user. action_users(); // API for following a user.
case "read": case "read":
view( "read" );// User interface for reading posts. view( "read" );// User interface for reading posts.
case ".well-known/nodeinfo": case ".well-known/nodeinfo":
@ -583,7 +583,7 @@ HTML;
// Buttons to interact with a message. // Buttons to interact with a message.
// By default, just shows a "Follow User" button. // By default, just shows a "Follow User" button.
if ( "read" == $style ) { if ( "read" == $style ) {
$interactHTML = "<a href=\"/follow?account=$actorUsername\"></a> "; $interactHTML = "<a href=\"/users?account=$actorUsername\"></a> ";
} else { } else {
$interactHTML = ""; $interactHTML = "";
} }
@ -750,7 +750,7 @@ echo <<< HTML
<body> <body>
<fieldset> <fieldset>
<legend>Send a message</legend> <legend>Send a message</legend>
<form action="/send" method="post" enctype="multipart/form-data"> <form action="/action/send" method="post" enctype="multipart/form-data">
<input type="hidden" id="type" name="type" value="Create"> <input type="hidden" id="type" name="type" value="Create">
<label for="content">Your message:</label><br> <label for="content">Your message:</label><br>
<textarea id="content" name="content" rows="5" cols="32"></textarea><br> <textarea id="content" name="content" rows="5" cols="32"></textarea><br>
@ -1235,11 +1235,11 @@ HTML;
} }
// This creates a UI for the user to follow another user // This creates a UI for the user to follow another user
function follow() { function users() {
if ( isset( $_GET["account"] ) ) { if ( isset( $_GET["account"] ) ) {
$accountURl = htmlspecialchars( $_GET["account"] ); $accountURl = htmlspecialchars( $_GET["account"] );
} else { } else {
$announceURl = ""; $accountURl = "";
} }
echo <<< HTML echo <<< HTML
<!DOCTYPE html> <!DOCTYPE html>
@ -1252,12 +1252,20 @@ HTML;
</style> </style>
</head> </head>
<body> <body>
<form action="/follow_user" method="post" enctype="multipart/form-data"> <form action="/action/users" method="post" enctype="multipart/form-data">
<label for="user">User to follow</label> <label for="user">User:</label>
<input name="user" id="user" type="text" size="32" placeholder="@user@example.com" value="{$accountURl}"><br> <input name="user" id="user" type="text" size="32" placeholder="@user@example.com" value="{$accountURl}"><br>
<label for="action">action</label><br>
<select name="action" id="action">
<option value="">--Please choose an option--</option>
<option value="Follow">Follow</option>
<option value="Unfollow">Unfollow</option>
<option value="Block">Block</option>
<option value="Unblock">Unblock</option>
</select><br>
<label for="password">Password</label><br> <label for="password">Password</label><br>
<input name="password" id="password" type="password" size="32"><br> <input name="password" id="password" type="password" size="32"><br>
<input type="submit" value="Send Follow Request"> <input type="submit" value="Send User Request">
</form> </form>
</body> </body>
</html> </html>
@ -1269,14 +1277,25 @@ HTML;
// It looks up the external user's details // It looks up the external user's details
// Then it sends a follow request // Then it sends a follow request
// If the request is accepted, it saves the details in `data/following/` as a JSON file // 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; global $password, $server, $username, $key_private, $directories;
// Does the posted password match the stored password? // Does the posted password match the stored password?
if( $password != $_POST["password"] ) { echo "Wrong Password!"; die(); } if( $password != $_POST["password"] ) { echo "Wrong Password!"; die(); }
// Get the posted content // 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 // Split the user (@user@example.com) into username and server
list( , $follow_name, $follow_server ) = explode( "@", $user ); list( , $follow_name, $follow_server ) = explode( "@", $user );
@ -1300,26 +1319,76 @@ HTML;
// Get the user's inbox // Get the user's inbox
$profileInbox = $profileData["inbox"]; $profileInbox = $profileData["inbox"];
// Create a follow request // Create a user request
$guid = uuid(); $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 // Different user actions have subtly different messages to send.
// The Accept is POSTed to the inbox on the server of the user who requested the follow 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 ); sentMessageToSingle( $profileInbox, $message );
// Save the user's details if ( "Follow" == $action ) {
$following_filename = urlencode( $profileURl ); // Save the user's details
file_put_contents( $directories["following"] . "/{$following_filename}.json", json_encode( $profileData ) ); $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 // Render the JSON so the user can see the POST has worked
header( "Location: https://{$server}/data/following/" . urlencode( $following_filename ) . ".json" ); 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(); die();
} }