Add support for reading incomming messages

merge-requests/5/head
Terence Eden 2024-03-01 10:38:18 +00:00
rodzic 15f6602559
commit 1219393c29
2 zmienionych plików z 116 dodań i 5 usunięć

Wyświetl plik

@ -19,6 +19,7 @@ There are no tests, no checks, no security features, no formal verifications, no
1. Check social media to see if the message appears. 1. Check social media to see if the message appears.
1. To follow other users, visit `https://test.example.com/follow` type in the name of the user you want to follow and your password. Press the "Send Follow Request" button. 1. To follow other users, visit `https://test.example.com/follow` type in the name of the user you want to follow and your password. Press the "Send Follow Request" button.
1. Check social media to see if the follow request came through. 1. Check social media to see if the follow request came through.
1. To read the messages that your server's inbox has received, visit `https://test.example.com/read`
## How this works ## How this works

120
index.php
Wyświetl plik

@ -118,9 +118,11 @@
case "outbox": case "outbox":
outbox(); // Optional. Dynamic. outbox(); // Optional. Dynamic.
case "follow": case "follow":
follow(); // User interface for following an external user follow(); // User interface for following an external user
case "follow_user": case "follow_user":
follow_user(); // API for following a user follow_user(); // API for following a user
case "read":
read(); // User interface for reading posts
case "/": case "/":
home(); // Optional. Can be dynamic home(); // Optional. Can be dynamic
default: default:
@ -460,7 +462,7 @@
// User Interface for Homepage: // User Interface for Homepage:
// This creates a basic HTML page. This content appears when someone visits the root of your site. // This creates a basic HTML page. This content appears when someone visits the root of your site.
function home() { function home() {
global $username, $server, $realName, $summary; global $username, $server, $realName, $summary, $directories;
echo <<< HTML echo <<< HTML
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en-GB"> <html lang="en-GB">
@ -485,7 +487,7 @@ echo <<< HTML
<ul> <ul>
HTML; HTML;
// Get all posts, most recent first // Get all posts, most recent first
$posts = array_reverse( glob("posts/*.json") ); $posts = array_reverse( glob( $directories["posts"] . "/*.json") );
// Loop through the posts // Loop through the posts
foreach ($posts as $post) { foreach ($posts as $post) {
@ -785,10 +787,10 @@ HTML;
// The Outbox contains a date-ordered list (newest first) of all the user's posts // The Outbox contains a date-ordered list (newest first) of all the user's posts
// This is optional. // This is optional.
function outbox() { function outbox() {
global $server, $username; global $server, $username, $directories;
// Get all posts // Get all posts
$posts = array_reverse( glob("posts/*.json") ); $posts = array_reverse( glob( $directories["posts"] . "/*.json") );
// Number of posts // Number of posts
$totalItems = count( $posts ); $totalItems = count( $posts );
// Create an ordered list // Create an ordered list
@ -1079,6 +1081,114 @@ HTML;
return $verified; return $verified;
} }
// Displays the most recent 200 messages in the inbox
function read() {
global $server, $directories;
// Get all the files in the inbox
$inbox_files = array_reverse( glob( $directories["inbox"] . "/*.json") );
// Keep the most recent 200
$inbox_files = array_slice( $inbox_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.
$inbox_ordered = [];
foreach ( $inbox_files as $inbox_file ) {
// Get the contents of the JSON
$inbox_message = json_decode( file_get_contents( $inbox_file ), true );
// Use the timestamp of the message. If there is none, use the date in the filename
if ( isset( $inbox_message["published"] ) ) {
$published = $inbox_message["published"];
} else {
$published = end( explode( "/", explode( ".", $inbox_file)[0] ) ) ;
}
// Place in an array where the key is the timestamp
$inbox_ordered[$published] = $inbox_message;
}
// Sort with newest on top
krsort( $inbox_ordered );
// Show a basic HTML interface
echo <<< HTML
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="UTF-8">
<title>Reader</title>
<style>
body { text-align: center; font-family:sans-serif; font-size:1.1em; }
ul { text-align: left; }
img { max-width: 50%; }
</style>
</head>
<body>
<ul>
HTML;
// Print the items in a list
foreach ( $inbox_ordered as $published=>$inbox_message ) {
// Set up the common components
$object = $inbox_message["object"];
$actor = $inbox_message["actor"];
$actorName = end( explode("/", $actor ) );
$actorHTML = "<a href=\"$actor\">@{$actorName}</a>";
$timeHTML = "<time datetime=\"{$published}\">{$published}</time>";
// HTML is *probably* sanitised by the sender. But let's not risk it, eh?
// Using the allow-list from https://docs.joinmastodon.org/spec/activitypub/#sanitization
$allowed_elements = ["p", "span", "br", "a", "del", "pre", "code", "em", "strong", "b", "i", "u", "ul", "ol", "li", "blockquote"];
// What type of message is this?
$type = $inbox_message["type"];
// Render the message according to type
if ( "Create" == $type ) {
// Get the HTML content and sanitise it.
$content = $object["content"];
$content = strip_tags($content, $allowed_elements);
// Add any images
if ( isset( $object["attachment"] ) ) {
foreach ( $object["attachment"] as $attachment ) {
// Only use things which have a MIME Type set
if ( isset( $attachment["mediaType"] ) ) {
$mediaType = explode( "/", $attachment["mediaType"])[0];
if ( "image" == $mediaType ) {
// Get the alt text
isset( $attachment["name"] ) ? $alt = $attachment["name"] : $alt = "";
$content .= "<img src='" . $attachment["url"] . "' alt='{$alt}'>";
} else if ( "video" == $mediaType ) {
$content .= "<video controls><source src='" . $attachment["url"] . "' type='" . $attachment["mediaType"] . "' /></video>";
}else if ( "audio" == $mediaType ) {
$content .= "<audio controls src='" . $attachment["url"] . "' type='" . $attachment["mediaType"] . "'></audio>";
}
}
}
}
echo "<li>{$timeHTML} {$actorHTML} said: <blockquote>{$content}</blockquote></li>";
} else if ( "Like" == $type ) {
$objectHTML = "<a href=\"$object\">{$object}</a>";
echo "<li>{$timeHTML} {$actorHTML} liked {$objectHTML}<br></li>";
} else if ( "Follow" == $type ) {
echo "<li>{$timeHTML} {$actorHTML} followed you<br></li>";
} else if ( "Announce" == $type ) {
$objectHTML = "<a href=\"$object\">{$object}</a>";
echo "<li>{$timeHTML} {$actorHTML} boosted {$objectHTML}<br></li>";
}
}
echo <<< HTML
</ul>
</body>
</html>
HTML;
die();
}
// "One to stun, two to kill, three to make sure" // "One to stun, two to kill, three to make sure"
die(); die();
die(); die();