From 838c071cee7536c6509cbffd2baa0cf12c5179f4 Mon Sep 17 00:00:00 2001 From: Terence Eden Date: Sat, 17 Feb 2024 20:48:03 +0000 Subject: [PATCH] Add mentions --- README.md | 1 + index.php | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e6f7cae..3f5ff67 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Optionally, upload an `icon.png` to make the user look nice. * The `index.php` file performs a specific action depending on the path requested. * Log files are saved as .txt in the `/logs` directory. * Post files are saved as .json in the `/posts` directory. +* This has sloppy support for linking #hashtags, https:// URls, and @ mentions. ## Requirements diff --git a/index.php b/index.php index dcd4b06..d17de4d 100644 --- a/index.php +++ b/index.php @@ -485,8 +485,8 @@ HTML; // Get any hashtags $hashtags = []; $hashtag_pattern = '/(?:^|\s)\#(\w+)/'; // Beginning of string, or whitespace, followed by # - preg_match_all( $hashtag_pattern, $content, $matches ); - foreach ($matches[1] as $match) { + preg_match_all( $hashtag_pattern, $content, $hashtag_matches ); + foreach ($hashtag_matches[1] as $match) { $hashtags[] = $match; } @@ -501,15 +501,43 @@ HTML; // Add HTML links for hashtags into the text $content = preg_replace( - '/(?#$1", $content ); + // Detect user mentions + $usernames = []; + $usernames_pattern = '/@(\S+)@(\S+)/'; // This is a *very* sloppy regex + preg_match_all( $usernames_pattern, $content, $usernames_matches ); + foreach ( $usernames_matches[0] as $match ) { + $usernames[] = $match; + } + + // Construct the mentions value for the note object + // This goes in the generic "tag" property + // TODO: Add this to the CC field + foreach ( $usernames as $username ) { + list( $null, $user, $domain ) = explode( "@", $username ); + $tags[] = array( + "type" => "Mention", + "href" => "https://{$domain}/@{$user}", + "name" => "{$username}" + ); + + // Add HTML links to usernames + $username_link = "$username"; + $content = str_replace( $username, $username_link, $content ); + + } + // Construct the content $content = "

{$content}

"; - return ["HTML" => $content, "TagArray" => $tags]; + return [ + "HTML" => $content, + "TagArray" => $tags + ]; } // "One to stun, two to kill, three to make sure"