User interface for (un)following & (un)blocking an external user.
rodzic
07da99fcde
commit
2454dbdda4
131
index.php
131
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 = "<a href=\"/follow?account=$actorUsername\">➕</a> ";
|
||||
$interactHTML = "<a href=\"/users?account=$actorUsername\">➕</a> ";
|
||||
} else {
|
||||
$interactHTML = "";
|
||||
}
|
||||
|
@ -750,7 +750,7 @@ echo <<< HTML
|
|||
<body>
|
||||
<fieldset>
|
||||
<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">
|
||||
<label for="content">Your message:</label><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
|
||||
function follow() {
|
||||
function users() {
|
||||
if ( isset( $_GET["account"] ) ) {
|
||||
$accountURl = htmlspecialchars( $_GET["account"] );
|
||||
} else {
|
||||
$announceURl = "";
|
||||
$accountURl = "";
|
||||
}
|
||||
echo <<< HTML
|
||||
<!DOCTYPE html>
|
||||
|
@ -1252,12 +1252,20 @@ HTML;
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<form action="/follow_user" method="post" enctype="multipart/form-data">
|
||||
<label for="user">User to follow</label>
|
||||
<form action="/action/users" method="post" enctype="multipart/form-data">
|
||||
<label for="user">User:</label>
|
||||
<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>
|
||||
<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>
|
||||
</body>
|
||||
</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();
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue