From 61abc6377de478fdf4f7a71251ea90bbcff8d7b1 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 4 Apr 2022 23:07:44 +0000 Subject: [PATCH] Preparation for "Featured" collection added --- database.sql | 4 +- doc/database/db_apcontact.md | 2 + src/Model/APContact.php | 3 ++ src/Protocol/ActivityPub/Processor.php | 75 ++++++++++++++++++++++++++ src/Protocol/ActivityPub/Receiver.php | 6 +-- static/dbstructure.config.php | 4 +- 6 files changed, 88 insertions(+), 6 deletions(-) diff --git a/database.sql b/database.sql index ffe0202033..b986cc409f 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2022.05-dev (Siberian Iris) --- DB_UPDATE_VERSION 1453 +-- DB_UPDATE_VERSION 1454 -- ------------------------------------------ @@ -332,6 +332,8 @@ CREATE TABLE IF NOT EXISTS `apcontact` ( `inbox` varchar(255) NOT NULL COMMENT '', `outbox` varchar(255) COMMENT '', `sharedinbox` varchar(255) COMMENT '', + `featured` varchar(255) COMMENT 'Address for the collection of featured posts', + `featured-tags` varchar(255) COMMENT 'Address for the collection of featured tags', `manually-approve` boolean COMMENT '', `discoverable` boolean COMMENT 'Mastodon extension: true if profile is published in their directory', `nick` varchar(255) NOT NULL DEFAULT '' COMMENT '', diff --git a/doc/database/db_apcontact.md b/doc/database/db_apcontact.md index ee95d994f9..34cf4ec18b 100644 --- a/doc/database/db_apcontact.md +++ b/doc/database/db_apcontact.md @@ -17,6 +17,8 @@ Fields | inbox | | varchar(255) | NO | | NULL | | | outbox | | varchar(255) | YES | | NULL | | | sharedinbox | | varchar(255) | YES | | NULL | | +| featured | Address for the collection of featured posts | varchar(255) | YES | | NULL | | +| featured-tags | Address for the collection of featured tags | varchar(255) | YES | | NULL | | | manually-approve | | boolean | YES | | NULL | | | discoverable | Mastodon extension: true if profile is published in their directory | boolean | YES | | NULL | | | nick | | varchar(255) | NO | | | | diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 9759876fab..deed6299de 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -232,6 +232,9 @@ class APContact self::unarchiveInbox($apcontact['sharedinbox'], true); } + $apcontact['featured'] = JsonLD::fetchElement($compacted, 'toot:featured', '@id'); + $apcontact['featured-tags'] = JsonLD::fetchElement($compacted, 'toot:featuredTags', '@id'); + $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername', '@value') ?? ''; $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name', '@value'); diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 533d3a29b1..647357811e 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -21,6 +21,7 @@ namespace Friendica\Protocol\ActivityPub; +use Exception; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; use Friendica\Content\Text\Markdown; @@ -40,6 +41,7 @@ use Friendica\Model\Mail; use Friendica\Model\Tag; use Friendica\Model\User; use Friendica\Model\Post; +use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use Friendica\Protocol\Relay; @@ -439,6 +441,79 @@ class Processor self::postItem($activity, $item); } + /** + * Fetch the Uri-Id of a post for the "featured" collection + * + * @param array $activity + * @return null|int + */ + private static function getUriIdForFeaturedCollection(array $activity) + { + $actor = APContact::getByURL($activity['actor']); + if (empty($actor)) { + return null; + } + + // Refetch the account when the "featured" collection is missing. + // This can be removed in a future version (end of 2022 should be good). + if (empty($actor['featured'])) { + $actor = APContact::getByURL($activity['actor'], true); + if (empty($actor)) { + return null; + } + } + + if ($activity['target_id'] != $actor['featured']) { + return null; + } + + $id = Contact::getIdForURL($activity['actor']); + if (empty($id)) { + return null; + } + + $parent = Post::selectFirst(['uri-id'], ['uri' => $activity['object_id'], 'author-id' => $id]); + if (!empty($parent['uri-id'])) { + return $parent['uri-id']; + } + + return null; + } + + /** + * Add a post to the "Featured" collection + * + * @param array $activity + */ + public static function addToFeaturedCollection(array $activity) + { + $uriid = self::getUriIdForFeaturedCollection($activity); + if (empty($uriid)) { + return; + } + + Logger::debug('Add post to featured collection', ['uri-id' => $uriid]); + + // @todo Add functionality + } + + /** + * Remove a post to the "Featured" collection + * + * @param array $activity + */ + public static function removeFromFeaturedCollection(array $activity) + { + $uriid = self::getUriIdForFeaturedCollection($activity); + if (empty($uriid)) { + return; + } + + Logger::debug('Remove post from featured collection', ['uri-id' => $uriid]); + + // @todo Add functionality + } + /** * Create an event * diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index b14780b196..0894ffc453 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -578,8 +578,7 @@ class Receiver if ($object_data['object_type'] == 'as:tag') { ActivityPub\Processor::addTag($object_data); } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) { - // Seems to be used by Mastodon to announce that a post is pinned - self::storeUnhandledActivity(false, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer); + ActivityPub\Processor::addToFeaturedCollection($object_data); } elseif ($object_data['object_type'] == '') { // The object type couldn't be determined. We don't have it and we can't fetch it. We ignore this activity. } else { @@ -680,8 +679,7 @@ class Receiver case 'as:Remove': if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { - // Seems to be used by Mastodon to remove the pinned status of a post - self::storeUnhandledActivity(false, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer); + ActivityPub\Processor::removeFromFeaturedCollection($object_data); } elseif ($object_data['object_type'] == '') { // The object type couldn't be determined. We don't have it and we can't fetch it. We ignore this activity. } else { diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index fd1e39cc2e..36c9ca3e0a 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1453); + define('DB_UPDATE_VERSION', 1454); } return [ @@ -394,6 +394,8 @@ return [ "inbox" => ["type" => "varchar(255)", "not null" => "1", "comment" => ""], "outbox" => ["type" => "varchar(255)", "comment" => ""], "sharedinbox" => ["type" => "varchar(255)", "comment" => ""], + "featured" => ["type" => "varchar(255)", "comment" => "Address for the collection of featured posts"], + "featured-tags" => ["type" => "varchar(255)", "comment" => "Address for the collection of featured tags"], "manually-approve" => ["type" => "boolean", "comment" => ""], "discoverable" => ["type" => "boolean", "comment" => "Mastodon extension: true if profile is published in their directory"], "nick" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], -- 2.39.5