From edb1ce04179f4352ef906c28cce375de208c497f Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2023 14:18:03 +0000 Subject: [PATCH 01/10] C2S: Posting is now possible --- src/Module/ActivityPub/Outbox.php | 27 ++++ src/Module/ActivityPub/Whoami.php | 2 +- src/Protocol/ActivityPub/Processor.php | 58 ++++++++ src/Protocol/ActivityPub/Receiver.php | 177 ++++++++++++++++++++++--- static/routes.config.php | 2 +- 5 files changed, 243 insertions(+), 23 deletions(-) diff --git a/src/Module/ActivityPub/Outbox.php b/src/Module/ActivityPub/Outbox.php index fd73028c23..5cd2507777 100644 --- a/src/Module/ActivityPub/Outbox.php +++ b/src/Module/ActivityPub/Outbox.php @@ -26,6 +26,7 @@ use Friendica\Model\User; use Friendica\Module\BaseApi; use Friendica\Protocol\ActivityPub; use Friendica\Util\HTTPSignature; +use Friendica\Util\Network; /** * ActivityPub Outbox @@ -55,4 +56,30 @@ class Outbox extends BaseApi System::jsonExit($outbox, 'application/activity+json'); } + + protected function post(array $request = []) + { + self::checkAllowedScope(self::SCOPE_WRITE); + $uid = self::getCurrentUserID(); + $postdata = Network::postdata(); + + if (empty($postdata) || empty($this->parameters['nickname'])) { + throw new \Friendica\Network\HTTPException\BadRequestException(); + } + + $owner = User::getOwnerDataByNick($this->parameters['nickname']); + if (empty($owner)) { + throw new \Friendica\Network\HTTPException\NotFoundException(); + } + if ($owner['uid'] != $uid) { + throw new \Friendica\Network\HTTPException\ForbiddenException(); + } + + $activity = json_decode($postdata, true); + if (empty($activity)) { + throw new \Friendica\Network\HTTPException\BadRequestException(); + } + + ActivityPub\Receiver::processC2SActivity($activity, $uid, self::getCurrentApplication() ?? []); + } } diff --git a/src/Module/ActivityPub/Whoami.php b/src/Module/ActivityPub/Whoami.php index f9513907cc..cac21744f8 100644 --- a/src/Module/ActivityPub/Whoami.php +++ b/src/Module/ActivityPub/Whoami.php @@ -96,7 +96,7 @@ class Whoami extends BaseApi 'oauthRegistrationEndpoint' => DI::baseUrl() . '/api/v1/apps', 'oauthTokenEndpoint' => DI::baseUrl() . '/oauth/token', 'sharedInbox' => DI::baseUrl() . '/inbox', - 'uploadMedia' => DI::baseUrl() . '/api/upload_media' // @todo Endpoint does not exist at the moment +// 'uploadMedia' => DI::baseUrl() . '/api/upload_media' // @todo Endpoint does not exist at the moment ]; $data['generator'] = ActivityPub\Transmitter::getService(); diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 6836eafe34..5a51d31c41 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -35,6 +35,7 @@ use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Model\Conversation; use Friendica\Model\Event; +use Friendica\Model\Group; use Friendica\Model\GServer; use Friendica\Model\Item; use Friendica\Model\ItemURI; @@ -2143,4 +2144,61 @@ class Processor return $body; } + + public static function processC2SContent(array $object_data, array $application, int $uid): array + { + $owner = User::getOwnerDataById($uid); + + $item = []; + + $item['network'] = Protocol::DFRN; + $item['uid'] = $uid; + $item['verb'] = Activity::POST; + $item['contact-id'] = $owner['id']; + $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid); + $item['title'] = $object_data['name']; + $item['body'] = Markdown::toBBCode($object_data['content']); + $item['app'] = $application['name'] ?? 'API'; + + if (!empty($object_data['target'][Receiver::TARGET_GLOBAL])) { + $item['allow_cid'] = ''; + $item['allow_gid'] = ''; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::PUBLIC; + } elseif (isset($object_data['target'][Receiver::TARGET_GLOBAL])) { + $item['allow_cid'] = ''; + $item['allow_gid'] = ''; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::UNLISTED; + } elseif (!empty($object_data['target'][Receiver::TARGET_FOLLOWER])) { + $item['allow_cid'] = ''; + $item['allow_gid'] = '<' . Group::FOLLOWERS . '>'; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::PRIVATE; + } else { + // @todo Set permissions via the $object_data['target'] array + $item['allow_cid'] = '<' . $owner['id'] . '>'; + $item['allow_gid'] = ''; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::PRIVATE; + } + + if (!empty($object_data['summary'])) { + $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $object_data['summary'] . "[/abstract]\n" . $item['body']; + } + + if ($object_data['reply-to-id']) { + $item['gravity'] = Item::GRAVITY_COMMENT; + } else { + $item['gravity'] = Item::GRAVITY_PARENT; + } + + $item = DI::contentItem()->expandTags($item); + + return $item; + } } diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 6654b18fc6..784d0259d4 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -446,7 +446,7 @@ class Receiver } elseif (in_array($type, array_merge(self::ACTIVITY_TYPES, ['as:Announce', 'as:Follow'])) && in_array($object_type, self::CONTENT_TYPES)) { // Create a mostly empty array out of the activity data (instead of the object). // This way we later don't have to check for the existence of each individual array element. - $object_data = self::processObject($activity); + $object_data = self::processObject($activity, false); $object_data['name'] = $type; $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id'); $object_data['object_id'] = $object_id; @@ -691,8 +691,6 @@ class Receiver */ public static function routeActivities(array $object_data, string $type, bool $push, bool $fetch_parents = true, int $uid = 0): bool { - $activity = $object_data['object_activity'] ?? []; - switch ($type) { case 'as:Create': if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { @@ -1435,12 +1433,12 @@ class Receiver Logger::info('Empty type'); return false; } - $object_data = self::processObject($object); + $object_data = self::processObject($object, false); } // We currently don't handle 'pt:CacheFile', but with this step we avoid logging if (in_array($type, self::CONTENT_TYPES) || ($type == 'pt:CacheFile')) { - $object_data = self::processObject($object); + $object_data = self::processObject($object, false); if (!empty($data)) { $object_data['raw-object'] = json_encode($data); @@ -1849,9 +1847,9 @@ class Receiver * @return array|bool Object data or FALSE if $object does not contain @id element * @throws \Exception */ - private static function processObject(array $object) + private static function processObject(array $object, bool $c2s) { - if (!JsonLD::fetchElement($object, '@id')) { + if (!$c2s && !JsonLD::fetchElement($object, '@id')) { return false; } @@ -1983,21 +1981,25 @@ class Receiver $object_data['question'] = self::processQuestion($object); } - $receiverdata = self::getReceivers($object, $object_data['actor'] ?? '', $object_data['tags'], true, false); - $receivers = $reception_types = []; - foreach ($receiverdata as $key => $data) { - $receivers[$key] = $data['uid']; - $reception_types[$data['uid']] = $data['type'] ?? 0; + if ($c2s) { + $object_data['target'] = self::getTargets($object, $object_data['actor'] ?? ''); + $object_data['receiver'] = []; + } else { + $receiverdata = self::getReceivers($object, $object_data['actor'] ?? '', $object_data['tags'], true, false); + $receivers = $reception_types = []; + foreach ($receiverdata as $key => $data) { + $receivers[$key] = $data['uid']; + $reception_types[$data['uid']] = $data['type'] ?? 0; + } + + $object_data['receiver_urls'] = self::getReceiverURL($object); + $object_data['receiver'] = $receivers; + $object_data['reception_type'] = $reception_types; + + $object_data['unlisted'] = in_array(-1, $object_data['receiver']); + unset($object_data['receiver'][-1]); + unset($object_data['reception_type'][-1]); } - - $object_data['receiver_urls'] = self::getReceiverURL($object); - $object_data['receiver'] = $receivers; - $object_data['reception_type'] = $reception_types; - - $object_data['unlisted'] = in_array(-1, $object_data['receiver']); - unset($object_data['receiver'][-1]); - unset($object_data['reception_type'][-1]); - return $object_data; } @@ -2025,4 +2027,137 @@ class Receiver { return DBA::exists('arrived-activity', ['object-id' => $id]); } + + public static function processC2SActivity(array $activity, int $uid, array $application) + { + $ldactivity = JsonLD::compact($activity); + if (empty($ldactivity)) { + Logger::notice('Invalid activity', ['activity' => $activity, 'uid' => $uid]); + return; + } + + $type = JsonLD::fetchElement($ldactivity, '@type'); + if (!$type) { + Logger::notice('Empty type', ['activity' => $ldactivity, 'uid' => $uid]); + return; + } + + $object_id = JsonLD::fetchElement($ldactivity, 'as:object', '@id') ?? ''; + $object_type = self::fetchObjectType($ldactivity, $object_id, $uid); + if (!$object_type && !$object_id) { + Logger::notice('Empty object type or id', ['activity' => $ldactivity, 'uid' => $uid]); + return; + } + + Logger::debug('Processing activity', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'activity' => $ldactivity]); + self::routeC2SActivities($type, $object_type, $object_id, $uid, $application, $ldactivity); + throw new \Friendica\Network\HTTPException\AcceptedException(); + } + + private static function getTargets(array $object, string $actor): array + { + $profile = APContact::getByURL($actor); + $followers = $profile['followers']; + + $targets = []; + + foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) { + switch ($element) { + case 'as:to': + $type = self::TARGET_TO; + break; + case 'as:cc': + $type = self::TARGET_CC; + break; + case 'as:bto': + $type = self::TARGET_BTO; + break; + case 'as:bcc': + $type = self::TARGET_BCC; + break; + } + $receiver_list = JsonLD::fetchElementArray($object, $element, '@id'); + if (empty($receiver_list)) { + continue; + } + + foreach ($receiver_list as $receiver) { + if ($receiver == self::PUBLIC_COLLECTION) { + $targets[self::TARGET_GLOBAL] = ($element == 'as:to'); + continue; + } + + if ($receiver == $followers) { + $targets[self::TARGET_FOLLOWER] = true; + continue; + } + $targets[$type][] = Contact::getIdForURL($receiver); + } + } + return $targets; + } + + private static function routeC2SActivities(string $type, string $object_type, string $object_id, int $uid, array $application, array $ldactivity) + { + switch ($type) { + case 'as:Create': + if (in_array($object_type, self::CONTENT_TYPES)) { + self::createContent($uid, $application, $ldactivity); + } + break; + case 'as:Update': + if (in_array($object_type, self::CONTENT_TYPES) && !empty($object_id)) { + self::updateContent($uid, $object_id, $application, $ldactivity); + } + break; + case 'as:Follow': + if (in_array($object_type, self::ACCOUNT_TYPES) && !empty($object_id)) { + self::followAccount($uid, $object_id, $ldactivity); + } + break; + } + } + + private static function createContent(int $uid, array $application, array $ldactivity) + { + $object_data = self::processObject($ldactivity['as:object'], true); + $item = Processor::processC2SContent($object_data, $application, $uid); + Logger::debug('Got data', ['item' => $item, 'object' => $object_data]); + + $id = Item::insert($item, true); + if (!empty($id)) { + $item = Post::selectFirst(['uri-id'], ['id' => $id]); + if (!empty($item['uri-id'])) { + System::jsonExit(Transmitter::createActivityFromItem($id)); + } + } + } + + private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity) + { + $id = Item::fetchByLink($object_id, $uid); + $original_post = Post::selectFirst(['uri-id'], ['uid' => $uid, 'origin' => true, 'id' => $id]); + if (empty($original_post)) { + Logger::debug('Item not found or does not belong to the user', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); + return; + } + + $object_data = self::processObject($ldactivity['as:object'], true); + $item = Processor::processC2SContent($object_data, $application, $uid); + if (empty($item['title']) && empty($item['body'])) { + Logger::debug('Empty body and title', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); + return; + } + $post = ['title' => $item['title'], 'body' => $item['body']]; + Logger::debug('Got data', ['id' => $id, 'uid' => $uid, 'item' => $post]); + Item::update($post, ['id' => $id]); + Item::updateDisplayCache($original_post['uri-id']); + + System::jsonExit(Transmitter::createActivityFromItem($id)); + } + + private static function followAccount($uid, $object_id, $ldactivity) + { + + } } diff --git a/static/routes.config.php b/static/routes.config.php index 391a7dc5ee..58fce72777 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -547,7 +547,7 @@ return [ '/h2b' => [Module\Oembed::class, [R::GET]], '/{hash}' => [Module\Oembed::class, [R::GET]], ], - '/outbox/{nickname}' => [Module\ActivityPub\Outbox::class, [R::GET]], + '/outbox/{nickname}' => [Module\ActivityPub\Outbox::class, [R::GET, R::POST]], '/owa' => [Module\Owa::class, [R::GET]], '/openid' => [Module\Security\OpenID::class, [R::GET]], '/opensearch' => [Module\OpenSearch::class, [R::GET]], From 2c41ebbfaa45ea4417180b048cb8b8743f42c405 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 06:27:45 +0000 Subject: [PATCH 02/10] passing the return value --- src/Module/ActivityPub/Outbox.php | 2 +- src/Protocol/ActivityPub/Processor.php | 1 + src/Protocol/ActivityPub/Receiver.php | 90 ++++++++++++++++++++------ 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/src/Module/ActivityPub/Outbox.php b/src/Module/ActivityPub/Outbox.php index 5cd2507777..f2e88cafe6 100644 --- a/src/Module/ActivityPub/Outbox.php +++ b/src/Module/ActivityPub/Outbox.php @@ -80,6 +80,6 @@ class Outbox extends BaseApi throw new \Friendica\Network\HTTPException\BadRequestException(); } - ActivityPub\Receiver::processC2SActivity($activity, $uid, self::getCurrentApplication() ?? []); + System::jsonExit(ActivityPub\Receiver::processC2SActivity($activity, $uid, self::getCurrentApplication() ?? [])); } } diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 5a51d31c41..026aea4716 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -2192,6 +2192,7 @@ class Processor } if ($object_data['reply-to-id']) { + $item['thr-parent'] = $object_data['reply-to-id']; $item['gravity'] = Item::GRAVITY_COMMENT; } else { $item['gravity'] = Item::GRAVITY_PARENT; diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 784d0259d4..d452676b8b 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1843,6 +1843,7 @@ class Receiver * Fetches data from the object part of an activity * * @param array $object + * @param bool $c2s "true" = The object is a "client to server" object * * @return array|bool Object data or FALSE if $object does not contain @id element * @throws \Exception @@ -2028,32 +2029,46 @@ class Receiver return DBA::exists('arrived-activity', ['object-id' => $id]); } - public static function processC2SActivity(array $activity, int $uid, array $application) + /** + * Process client to server activities + * + * @param array $activity + * @param integer $uid + * @param array $application + * @return array + */ + public static function processC2SActivity(array $activity, int $uid, array $application): array { $ldactivity = JsonLD::compact($activity); if (empty($ldactivity)) { Logger::notice('Invalid activity', ['activity' => $activity, 'uid' => $uid]); - return; + return []; } $type = JsonLD::fetchElement($ldactivity, '@type'); if (!$type) { Logger::notice('Empty type', ['activity' => $ldactivity, 'uid' => $uid]); - return; + return []; } $object_id = JsonLD::fetchElement($ldactivity, 'as:object', '@id') ?? ''; $object_type = self::fetchObjectType($ldactivity, $object_id, $uid); if (!$object_type && !$object_id) { Logger::notice('Empty object type or id', ['activity' => $ldactivity, 'uid' => $uid]); - return; + return []; } Logger::debug('Processing activity', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'activity' => $ldactivity]); - self::routeC2SActivities($type, $object_type, $object_id, $uid, $application, $ldactivity); - throw new \Friendica\Network\HTTPException\AcceptedException(); + return self::routeC2SActivities($type, $object_type, $object_id, $uid, $application, $ldactivity); } + /** + * Accumulate the targets and visibility of this post + * + * @param array $object + * @param string $actor + * @return array + */ private static function getTargets(array $object, string $actor): array { $profile = APContact::getByURL($actor); @@ -2097,28 +2112,48 @@ class Receiver return $targets; } - private static function routeC2SActivities(string $type, string $object_type, string $object_id, int $uid, array $application, array $ldactivity) + /** + * Route client to server activities + * + * @param string $type + * @param string $object_type + * @param string $object_id + * @param integer $uid + * @param array $application + * @param array $ldactivity + * @return array + */ + private static function routeC2SActivities(string $type, string $object_type, string $object_id, int $uid, array $application, array $ldactivity): array { switch ($type) { case 'as:Create': if (in_array($object_type, self::CONTENT_TYPES)) { - self::createContent($uid, $application, $ldactivity); + return self::createContent($uid, $application, $ldactivity); } break; case 'as:Update': if (in_array($object_type, self::CONTENT_TYPES) && !empty($object_id)) { - self::updateContent($uid, $object_id, $application, $ldactivity); + return self::updateContent($uid, $object_id, $application, $ldactivity); } break; case 'as:Follow': if (in_array($object_type, self::ACCOUNT_TYPES) && !empty($object_id)) { - self::followAccount($uid, $object_id, $ldactivity); + return self::followAccount($uid, $object_id, $ldactivity); } break; } + return []; } - private static function createContent(int $uid, array $application, array $ldactivity) + /** + * Create a new post or comment + * + * @param integer $uid + * @param array $application + * @param array $ldactivity + * @return array + */ + private static function createContent(int $uid, array $application, array $ldactivity): array { $object_data = self::processObject($ldactivity['as:object'], true); $item = Processor::processC2SContent($object_data, $application, $uid); @@ -2128,36 +2163,55 @@ class Receiver if (!empty($id)) { $item = Post::selectFirst(['uri-id'], ['id' => $id]); if (!empty($item['uri-id'])) { - System::jsonExit(Transmitter::createActivityFromItem($id)); + return Transmitter::createActivityFromItem($id); } } + return []; } - private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity) + /** + * Update an existing post or comment + * + * @param integer $uid + * @param string $object_id + * @param array $application + * @param array $ldactivity + * @return array + */ + private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity):array { $id = Item::fetchByLink($object_id, $uid); $original_post = Post::selectFirst(['uri-id'], ['uid' => $uid, 'origin' => true, 'id' => $id]); if (empty($original_post)) { Logger::debug('Item not found or does not belong to the user', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); - return; + return []; } $object_data = self::processObject($ldactivity['as:object'], true); $item = Processor::processC2SContent($object_data, $application, $uid); if (empty($item['title']) && empty($item['body'])) { Logger::debug('Empty body and title', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); - return; + return []; } $post = ['title' => $item['title'], 'body' => $item['body']]; Logger::debug('Got data', ['id' => $id, 'uid' => $uid, 'item' => $post]); Item::update($post, ['id' => $id]); Item::updateDisplayCache($original_post['uri-id']); - System::jsonExit(Transmitter::createActivityFromItem($id)); + return Transmitter::createActivityFromItem($id); } - private static function followAccount($uid, $object_id, $ldactivity) + /** + * Follow a given account + * @todo Check the expected return value + * + * @param integer $uid + * @param string $object_id + * @param array $ldactivity + * @return array + */ + private static function followAccount(int $uid, string $object_id, array $ldactivity): array { - + return []; } } From a7b3949ca0531328935dcd589f77ba4ab0c21ada Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 11:57:02 +0000 Subject: [PATCH 03/10] Added documentation --- src/Protocol/ActivityPub/Processor.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 026aea4716..dc618370a0 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -2145,6 +2145,14 @@ class Processor return $body; } + /** + * Create an item array from client to server object data + * + * @param array $object_data + * @param array $application + * @param integer $uid + * @return array + */ public static function processC2SContent(array $object_data, array $application, int $uid): array { $owner = User::getOwnerDataById($uid); From b02e48e9c3a7aa6b8452b1d2d557bb64fa9b99e4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 15:32:14 +0000 Subject: [PATCH 04/10] Split C2S activity --- src/Protocol/ActivityPub/Receiver.php | 80 ++++++++++++++++++--------- 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index d452676b8b..42fcb50865 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -446,7 +446,7 @@ class Receiver } elseif (in_array($type, array_merge(self::ACTIVITY_TYPES, ['as:Announce', 'as:Follow'])) && in_array($object_type, self::CONTENT_TYPES)) { // Create a mostly empty array out of the activity data (instead of the object). // This way we later don't have to check for the existence of each individual array element. - $object_data = self::processObject($activity, false); + $object_data = self::processObject($activity); $object_data['name'] = $type; $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id'); $object_data['object_id'] = $object_id; @@ -1433,12 +1433,12 @@ class Receiver Logger::info('Empty type'); return false; } - $object_data = self::processObject($object, false); + $object_data = self::processObject($object); } // We currently don't handle 'pt:CacheFile', but with this step we avoid logging if (in_array($type, self::CONTENT_TYPES) || ($type == 'pt:CacheFile')) { - $object_data = self::processObject($object, false); + $object_data = self::processObject($object); if (!empty($data)) { $object_data['raw-object'] = json_encode($data); @@ -1843,17 +1843,62 @@ class Receiver * Fetches data from the object part of an activity * * @param array $object - * @param bool $c2s "true" = The object is a "client to server" object * * @return array|bool Object data or FALSE if $object does not contain @id element * @throws \Exception */ - private static function processObject(array $object, bool $c2s) + private static function processObject(array $object) { - if (!$c2s && !JsonLD::fetchElement($object, '@id')) { + if (!JsonLD::fetchElement($object, '@id')) { return false; } + $object_data = self::getObjectDataFromActivity($object); + + $receiverdata = self::getReceivers($object, $object_data['actor'] ?? '', $object_data['tags'], true, false); + $receivers = $reception_types = []; + foreach ($receiverdata as $key => $data) { + $receivers[$key] = $data['uid']; + $reception_types[$data['uid']] = $data['type'] ?? 0; + } + + $object_data['receiver_urls'] = self::getReceiverURL($object); + $object_data['receiver'] = $receivers; + $object_data['reception_type'] = $reception_types; + + $object_data['unlisted'] = in_array(-1, $object_data['receiver']); + unset($object_data['receiver'][-1]); + unset($object_data['reception_type'][-1]); + + return $object_data; + } + + /** + * Fetches data from the object part of an client to server activity + * + * @param array $object + * + * @return array Object data + */ + private static function processC2SObject(array $object): array + { + $object_data = self::getObjectDataFromActivity($object); + + $object_data['target'] = self::getTargets($object, $object_data['actor'] ?? ''); + $object_data['receiver'] = []; + + return $object_data; + } + + /** + * Create an object data array from a given activity + * + * @param array $object + * + * @return array Object data + */ + private static function getObjectDataFromActivity(array $object): array + { $object_data = []; $object_data['object_type'] = JsonLD::fetchElement($object, '@type'); $object_data['id'] = JsonLD::fetchElement($object, '@id'); @@ -1982,25 +2027,6 @@ class Receiver $object_data['question'] = self::processQuestion($object); } - if ($c2s) { - $object_data['target'] = self::getTargets($object, $object_data['actor'] ?? ''); - $object_data['receiver'] = []; - } else { - $receiverdata = self::getReceivers($object, $object_data['actor'] ?? '', $object_data['tags'], true, false); - $receivers = $reception_types = []; - foreach ($receiverdata as $key => $data) { - $receivers[$key] = $data['uid']; - $reception_types[$data['uid']] = $data['type'] ?? 0; - } - - $object_data['receiver_urls'] = self::getReceiverURL($object); - $object_data['receiver'] = $receivers; - $object_data['reception_type'] = $reception_types; - - $object_data['unlisted'] = in_array(-1, $object_data['receiver']); - unset($object_data['receiver'][-1]); - unset($object_data['reception_type'][-1]); - } return $object_data; } @@ -2155,7 +2181,7 @@ class Receiver */ private static function createContent(int $uid, array $application, array $ldactivity): array { - $object_data = self::processObject($ldactivity['as:object'], true); + $object_data = self::processC2SObject($ldactivity['as:object']); $item = Processor::processC2SContent($object_data, $application, $uid); Logger::debug('Got data', ['item' => $item, 'object' => $object_data]); @@ -2187,7 +2213,7 @@ class Receiver return []; } - $object_data = self::processObject($ldactivity['as:object'], true); + $object_data = self::processC2SObject($ldactivity['as:object']); $item = Processor::processC2SContent($object_data, $application, $uid); if (empty($item['title']) && empty($item['body'])) { Logger::debug('Empty body and title', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); From 8fe6419d392b2abff5c7ba2552b9fe8d4dad69f2 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 21:27:11 +0000 Subject: [PATCH 05/10] New class for c2s activities --- src/Module/ActivityPub/Outbox.php | 2 +- src/Protocol/ActivityPub/ClientToServer.php | 310 ++++++++++++++++++++ src/Protocol/ActivityPub/Processor.php | 66 ----- src/Protocol/ActivityPub/Receiver.php | 207 +------------ 4 files changed, 313 insertions(+), 272 deletions(-) create mode 100644 src/Protocol/ActivityPub/ClientToServer.php diff --git a/src/Module/ActivityPub/Outbox.php b/src/Module/ActivityPub/Outbox.php index f2e88cafe6..01061d8c67 100644 --- a/src/Module/ActivityPub/Outbox.php +++ b/src/Module/ActivityPub/Outbox.php @@ -80,6 +80,6 @@ class Outbox extends BaseApi throw new \Friendica\Network\HTTPException\BadRequestException(); } - System::jsonExit(ActivityPub\Receiver::processC2SActivity($activity, $uid, self::getCurrentApplication() ?? [])); + System::jsonExit(ActivityPub\ClientToServer::processActivity($activity, $uid, self::getCurrentApplication() ?? [])); } } diff --git a/src/Protocol/ActivityPub/ClientToServer.php b/src/Protocol/ActivityPub/ClientToServer.php new file mode 100644 index 0000000000..c8ddcbfe93 --- /dev/null +++ b/src/Protocol/ActivityPub/ClientToServer.php @@ -0,0 +1,310 @@ +. + * + */ + +namespace Friendica\Protocol\ActivityPub; + +use Friendica\Content\Text\Markdown; +use Friendica\Core\Logger; +use Friendica\Core\Protocol; +use Friendica\DI; +use Friendica\Model\APContact; +use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Model\Item; +use Friendica\Model\Post; +use Friendica\Model\User; +use Friendica\Protocol\Activity; +use Friendica\Util\JsonLD; + +/** + * ActivityPub Client To Server class + */ +class ClientToServer +{ + /** + * Process client to server activities + * + * @param array $activity + * @param integer $uid + * @param array $application + * @return array + */ + public static function processActivity(array $activity, int $uid, array $application): array + { + $ldactivity = JsonLD::compact($activity); + if (empty($ldactivity)) { + Logger::notice('Invalid activity', ['activity' => $activity, 'uid' => $uid]); + return []; + } + + $type = JsonLD::fetchElement($ldactivity, '@type'); + if (!$type) { + Logger::notice('Empty type', ['activity' => $ldactivity, 'uid' => $uid]); + return []; + } + + $object_id = JsonLD::fetchElement($ldactivity, 'as:object', '@id') ?? ''; + $object_type = Receiver::fetchObjectType($ldactivity, $object_id, $uid); + if (!$object_type && !$object_id) { + Logger::notice('Empty object type or id', ['activity' => $ldactivity, 'uid' => $uid]); + return []; + } + + Logger::debug('Processing activity', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'activity' => $ldactivity]); + return self::routeActivities($type, $object_type, $object_id, $uid, $application, $ldactivity); + } + + /** + * Route client to server activities + * + * @param string $type + * @param string $object_type + * @param string $object_id + * @param integer $uid + * @param array $application + * @param array $ldactivity + * @return array + */ + private static function routeActivities(string $type, string $object_type, string $object_id, int $uid, array $application, array $ldactivity): array + { + switch ($type) { + case 'as:Create': + if (in_array($object_type, Receiver::CONTENT_TYPES)) { + return self::createContent($uid, $application, $ldactivity); + } + break; + case 'as:Update': + if (in_array($object_type, Receiver::CONTENT_TYPES) && !empty($object_id)) { + return self::updateContent($uid, $object_id, $application, $ldactivity); + } + break; + case 'as:Follow': + if (in_array($object_type, Receiver::ACCOUNT_TYPES) && !empty($object_id)) { + return self::followAccount($uid, $object_id, $ldactivity); + } + break; + } + return []; + } + + /** + * Create a new post or comment + * + * @param integer $uid + * @param array $application + * @param array $ldactivity + * @return array + */ + private static function createContent(int $uid, array $application, array $ldactivity): array + { + $object_data = self::processObject($ldactivity['as:object']); + $item = ClientToServer::processContent($object_data, $application, $uid); + Logger::debug('Got data', ['item' => $item, 'object' => $object_data]); + + $id = Item::insert($item, true); + if (!empty($id)) { + $item = Post::selectFirst(['uri-id'], ['id' => $id]); + if (!empty($item['uri-id'])) { + return Transmitter::createActivityFromItem($id); + } + } + return []; + } + + /** + * Update an existing post or comment + * + * @param integer $uid + * @param string $object_id + * @param array $application + * @param array $ldactivity + * @return array + */ + private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity):array + { + $id = Item::fetchByLink($object_id, $uid); + $original_post = Post::selectFirst(['uri-id'], ['uid' => $uid, 'origin' => true, 'id' => $id]); + if (empty($original_post)) { + Logger::debug('Item not found or does not belong to the user', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); + return []; + } + + $object_data = self::processObject($ldactivity['as:object']); + $item = ClientToServer::processContent($object_data, $application, $uid); + if (empty($item['title']) && empty($item['body'])) { + Logger::debug('Empty body and title', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); + return []; + } + $post = ['title' => $item['title'], 'body' => $item['body']]; + Logger::debug('Got data', ['id' => $id, 'uid' => $uid, 'item' => $post]); + Item::update($post, ['id' => $id]); + Item::updateDisplayCache($original_post['uri-id']); + + return Transmitter::createActivityFromItem($id); + } + + /** + * Follow a given account + * @todo Check the expected return value + * + * @param integer $uid + * @param string $object_id + * @param array $ldactivity + * @return array + */ + private static function followAccount(int $uid, string $object_id, array $ldactivity): array + { + return []; + } + + /** + * Fetches data from the object part of an client to server activity + * + * @param array $object + * + * @return array Object data + */ + private static function processObject(array $object): array + { + $object_data = Receiver::getObjectDataFromActivity($object); + + $object_data['target'] = self::getTargets($object, $object_data['actor'] ?? ''); + $object_data['receiver'] = []; + + return $object_data; + } + + /** + * Accumulate the targets and visibility of this post + * + * @param array $object + * @param string $actor + * @return array + */ + private static function getTargets(array $object, string $actor): array + { + $profile = APContact::getByURL($actor); + $followers = $profile['followers']; + + $targets = []; + + foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) { + switch ($element) { + case 'as:to': + $type = Receiver::TARGET_TO; + break; + case 'as:cc': + $type = Receiver::TARGET_CC; + break; + case 'as:bto': + $type = Receiver::TARGET_BTO; + break; + case 'as:bcc': + $type = Receiver::TARGET_BCC; + break; + } + $receiver_list = JsonLD::fetchElementArray($object, $element, '@id'); + if (empty($receiver_list)) { + continue; + } + + foreach ($receiver_list as $receiver) { + if ($receiver == Receiver::PUBLIC_COLLECTION) { + $targets[Receiver::TARGET_GLOBAL] = ($element == 'as:to'); + continue; + } + + if ($receiver == $followers) { + $targets[Receiver::TARGET_FOLLOWER] = true; + continue; + } + $targets[$type][] = Contact::getIdForURL($receiver); + } + } + return $targets; + } + + /** + * Create an item array from client to server object data + * + * @param array $object_data + * @param array $application + * @param integer $uid + * @return array + */ + private static function processContent(array $object_data, array $application, int $uid): array + { + $owner = User::getOwnerDataById($uid); + + $item = []; + + $item['network'] = Protocol::DFRN; + $item['uid'] = $uid; + $item['verb'] = Activity::POST; + $item['contact-id'] = $owner['id']; + $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid); + $item['title'] = $object_data['name']; + $item['body'] = Markdown::toBBCode($object_data['content']); + $item['app'] = $application['name'] ?? 'API'; + + if (!empty($object_data['target'][Receiver::TARGET_GLOBAL])) { + $item['allow_cid'] = ''; + $item['allow_gid'] = ''; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::PUBLIC; + } elseif (isset($object_data['target'][Receiver::TARGET_GLOBAL])) { + $item['allow_cid'] = ''; + $item['allow_gid'] = ''; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::UNLISTED; + } elseif (!empty($object_data['target'][Receiver::TARGET_FOLLOWER])) { + $item['allow_cid'] = ''; + $item['allow_gid'] = '<' . Group::FOLLOWERS . '>'; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::PRIVATE; + } else { + // @todo Set permissions via the $object_data['target'] array + $item['allow_cid'] = '<' . $owner['id'] . '>'; + $item['allow_gid'] = ''; + $item['deny_cid'] = ''; + $item['deny_gid'] = ''; + $item['private'] = Item::PRIVATE; + } + + if (!empty($object_data['summary'])) { + $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $object_data['summary'] . "[/abstract]\n" . $item['body']; + } + + if ($object_data['reply-to-id']) { + $item['thr-parent'] = $object_data['reply-to-id']; + $item['gravity'] = Item::GRAVITY_COMMENT; + } else { + $item['gravity'] = Item::GRAVITY_PARENT; + } + + $item = DI::contentItem()->expandTags($item); + + return $item; + } +} diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index dc618370a0..859e7a2451 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -2144,70 +2144,4 @@ class Processor return $body; } - - /** - * Create an item array from client to server object data - * - * @param array $object_data - * @param array $application - * @param integer $uid - * @return array - */ - public static function processC2SContent(array $object_data, array $application, int $uid): array - { - $owner = User::getOwnerDataById($uid); - - $item = []; - - $item['network'] = Protocol::DFRN; - $item['uid'] = $uid; - $item['verb'] = Activity::POST; - $item['contact-id'] = $owner['id']; - $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid); - $item['title'] = $object_data['name']; - $item['body'] = Markdown::toBBCode($object_data['content']); - $item['app'] = $application['name'] ?? 'API'; - - if (!empty($object_data['target'][Receiver::TARGET_GLOBAL])) { - $item['allow_cid'] = ''; - $item['allow_gid'] = ''; - $item['deny_cid'] = ''; - $item['deny_gid'] = ''; - $item['private'] = Item::PUBLIC; - } elseif (isset($object_data['target'][Receiver::TARGET_GLOBAL])) { - $item['allow_cid'] = ''; - $item['allow_gid'] = ''; - $item['deny_cid'] = ''; - $item['deny_gid'] = ''; - $item['private'] = Item::UNLISTED; - } elseif (!empty($object_data['target'][Receiver::TARGET_FOLLOWER])) { - $item['allow_cid'] = ''; - $item['allow_gid'] = '<' . Group::FOLLOWERS . '>'; - $item['deny_cid'] = ''; - $item['deny_gid'] = ''; - $item['private'] = Item::PRIVATE; - } else { - // @todo Set permissions via the $object_data['target'] array - $item['allow_cid'] = '<' . $owner['id'] . '>'; - $item['allow_gid'] = ''; - $item['deny_cid'] = ''; - $item['deny_gid'] = ''; - $item['private'] = Item::PRIVATE; - } - - if (!empty($object_data['summary'])) { - $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $object_data['summary'] . "[/abstract]\n" . $item['body']; - } - - if ($object_data['reply-to-id']) { - $item['thr-parent'] = $object_data['reply-to-id']; - $item['gravity'] = Item::GRAVITY_COMMENT; - } else { - $item['gravity'] = Item::GRAVITY_PARENT; - } - - $item = DI::contentItem()->expandTags($item); - - return $item; - } } diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 42fcb50865..16257c2f95 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -255,7 +255,7 @@ class Receiver * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function fetchObjectType(array $activity, string $object_id, int $uid = 0) + public static function fetchObjectType(array $activity, string $object_id, int $uid = 0) { if (!empty($activity['as:object'])) { $object_type = JsonLD::fetchElement($activity['as:object'], '@type'); @@ -1873,23 +1873,6 @@ class Receiver return $object_data; } - /** - * Fetches data from the object part of an client to server activity - * - * @param array $object - * - * @return array Object data - */ - private static function processC2SObject(array $object): array - { - $object_data = self::getObjectDataFromActivity($object); - - $object_data['target'] = self::getTargets($object, $object_data['actor'] ?? ''); - $object_data['receiver'] = []; - - return $object_data; - } - /** * Create an object data array from a given activity * @@ -1897,7 +1880,7 @@ class Receiver * * @return array Object data */ - private static function getObjectDataFromActivity(array $object): array + public static function getObjectDataFromActivity(array $object): array { $object_data = []; $object_data['object_type'] = JsonLD::fetchElement($object, '@type'); @@ -2054,190 +2037,4 @@ class Receiver { return DBA::exists('arrived-activity', ['object-id' => $id]); } - - /** - * Process client to server activities - * - * @param array $activity - * @param integer $uid - * @param array $application - * @return array - */ - public static function processC2SActivity(array $activity, int $uid, array $application): array - { - $ldactivity = JsonLD::compact($activity); - if (empty($ldactivity)) { - Logger::notice('Invalid activity', ['activity' => $activity, 'uid' => $uid]); - return []; - } - - $type = JsonLD::fetchElement($ldactivity, '@type'); - if (!$type) { - Logger::notice('Empty type', ['activity' => $ldactivity, 'uid' => $uid]); - return []; - } - - $object_id = JsonLD::fetchElement($ldactivity, 'as:object', '@id') ?? ''; - $object_type = self::fetchObjectType($ldactivity, $object_id, $uid); - if (!$object_type && !$object_id) { - Logger::notice('Empty object type or id', ['activity' => $ldactivity, 'uid' => $uid]); - return []; - } - - Logger::debug('Processing activity', ['type' => $type, 'object_type' => $object_type, 'object_id' => $object_id, 'activity' => $ldactivity]); - return self::routeC2SActivities($type, $object_type, $object_id, $uid, $application, $ldactivity); - } - - /** - * Accumulate the targets and visibility of this post - * - * @param array $object - * @param string $actor - * @return array - */ - private static function getTargets(array $object, string $actor): array - { - $profile = APContact::getByURL($actor); - $followers = $profile['followers']; - - $targets = []; - - foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) { - switch ($element) { - case 'as:to': - $type = self::TARGET_TO; - break; - case 'as:cc': - $type = self::TARGET_CC; - break; - case 'as:bto': - $type = self::TARGET_BTO; - break; - case 'as:bcc': - $type = self::TARGET_BCC; - break; - } - $receiver_list = JsonLD::fetchElementArray($object, $element, '@id'); - if (empty($receiver_list)) { - continue; - } - - foreach ($receiver_list as $receiver) { - if ($receiver == self::PUBLIC_COLLECTION) { - $targets[self::TARGET_GLOBAL] = ($element == 'as:to'); - continue; - } - - if ($receiver == $followers) { - $targets[self::TARGET_FOLLOWER] = true; - continue; - } - $targets[$type][] = Contact::getIdForURL($receiver); - } - } - return $targets; - } - - /** - * Route client to server activities - * - * @param string $type - * @param string $object_type - * @param string $object_id - * @param integer $uid - * @param array $application - * @param array $ldactivity - * @return array - */ - private static function routeC2SActivities(string $type, string $object_type, string $object_id, int $uid, array $application, array $ldactivity): array - { - switch ($type) { - case 'as:Create': - if (in_array($object_type, self::CONTENT_TYPES)) { - return self::createContent($uid, $application, $ldactivity); - } - break; - case 'as:Update': - if (in_array($object_type, self::CONTENT_TYPES) && !empty($object_id)) { - return self::updateContent($uid, $object_id, $application, $ldactivity); - } - break; - case 'as:Follow': - if (in_array($object_type, self::ACCOUNT_TYPES) && !empty($object_id)) { - return self::followAccount($uid, $object_id, $ldactivity); - } - break; - } - return []; - } - - /** - * Create a new post or comment - * - * @param integer $uid - * @param array $application - * @param array $ldactivity - * @return array - */ - private static function createContent(int $uid, array $application, array $ldactivity): array - { - $object_data = self::processC2SObject($ldactivity['as:object']); - $item = Processor::processC2SContent($object_data, $application, $uid); - Logger::debug('Got data', ['item' => $item, 'object' => $object_data]); - - $id = Item::insert($item, true); - if (!empty($id)) { - $item = Post::selectFirst(['uri-id'], ['id' => $id]); - if (!empty($item['uri-id'])) { - return Transmitter::createActivityFromItem($id); - } - } - return []; - } - - /** - * Update an existing post or comment - * - * @param integer $uid - * @param string $object_id - * @param array $application - * @param array $ldactivity - * @return array - */ - private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity):array - { - $id = Item::fetchByLink($object_id, $uid); - $original_post = Post::selectFirst(['uri-id'], ['uid' => $uid, 'origin' => true, 'id' => $id]); - if (empty($original_post)) { - Logger::debug('Item not found or does not belong to the user', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); - return []; - } - - $object_data = self::processC2SObject($ldactivity['as:object']); - $item = Processor::processC2SContent($object_data, $application, $uid); - if (empty($item['title']) && empty($item['body'])) { - Logger::debug('Empty body and title', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); - return []; - } - $post = ['title' => $item['title'], 'body' => $item['body']]; - Logger::debug('Got data', ['id' => $id, 'uid' => $uid, 'item' => $post]); - Item::update($post, ['id' => $id]); - Item::updateDisplayCache($original_post['uri-id']); - - return Transmitter::createActivityFromItem($id); - } - - /** - * Follow a given account - * @todo Check the expected return value - * - * @param integer $uid - * @param string $object_id - * @param array $ldactivity - * @return array - */ - private static function followAccount(int $uid, string $object_id, array $ldactivity): array - { - return []; - } } From 5e84fc849bb16f0dc67cd14dda4bf97e289f1719 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 14 Feb 2023 00:34:47 +0100 Subject: [PATCH 06/10] Apply suggestions from code review Co-authored-by: Hypolite Petovan --- src/Protocol/ActivityPub/ClientToServer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Protocol/ActivityPub/ClientToServer.php b/src/Protocol/ActivityPub/ClientToServer.php index c8ddcbfe93..b49f5a8595 100644 --- a/src/Protocol/ActivityPub/ClientToServer.php +++ b/src/Protocol/ActivityPub/ClientToServer.php @@ -116,7 +116,7 @@ class ClientToServer private static function createContent(int $uid, array $application, array $ldactivity): array { $object_data = self::processObject($ldactivity['as:object']); - $item = ClientToServer::processContent($object_data, $application, $uid); + $item = ClientToServer::processContent($object_data, $application, $uid); Logger::debug('Got data', ['item' => $item, 'object' => $object_data]); $id = Item::insert($item, true); @@ -140,7 +140,7 @@ class ClientToServer */ private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity):array { - $id = Item::fetchByLink($object_id, $uid); + $id = Item::fetchByLink($object_id, $uid); $original_post = Post::selectFirst(['uri-id'], ['uid' => $uid, 'origin' => true, 'id' => $id]); if (empty($original_post)) { Logger::debug('Item not found or does not belong to the user', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); @@ -148,7 +148,7 @@ class ClientToServer } $object_data = self::processObject($ldactivity['as:object']); - $item = ClientToServer::processContent($object_data, $application, $uid); + $item = ClientToServer::processContent($object_data, $application, $uid); if (empty($item['title']) && empty($item['body'])) { Logger::debug('Empty body and title', ['id' => $id, 'uid' => $uid, 'object_id' => $object_id, 'activity' => $ldactivity]); return []; @@ -260,7 +260,7 @@ class ClientToServer $item['uid'] = $uid; $item['verb'] = Activity::POST; $item['contact-id'] = $owner['id']; - $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid); + $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid); $item['title'] = $object_data['name']; $item['body'] = Markdown::toBBCode($object_data['content']); $item['app'] = $application['name'] ?? 'API'; @@ -282,14 +282,14 @@ class ClientToServer $item['allow_gid'] = '<' . Group::FOLLOWERS . '>'; $item['deny_cid'] = ''; $item['deny_gid'] = ''; - $item['private'] = Item::PRIVATE; + $item['private'] = Item::PRIVATE; } else { // @todo Set permissions via the $object_data['target'] array $item['allow_cid'] = '<' . $owner['id'] . '>'; $item['allow_gid'] = ''; $item['deny_cid'] = ''; $item['deny_gid'] = ''; - $item['private'] = Item::PRIVATE; + $item['private'] = Item::PRIVATE; } if (!empty($object_data['summary'])) { @@ -298,7 +298,7 @@ class ClientToServer if ($object_data['reply-to-id']) { $item['thr-parent'] = $object_data['reply-to-id']; - $item['gravity'] = Item::GRAVITY_COMMENT; + $item['gravity'] = Item::GRAVITY_COMMENT; } else { $item['gravity'] = Item::GRAVITY_PARENT; } From 2367f54d41ce207c244d0bbb3138c6f76f5c0bab Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 23:49:08 +0000 Subject: [PATCH 07/10] Soem more C2S stuff moved --- src/Module/ActivityPub/Inbox.php | 4 +- src/Module/ActivityPub/Outbox.php | 3 +- src/Protocol/ActivityPub/ClientToServer.php | 133 ++++++++++++++++++++ src/Protocol/ActivityPub/Transmitter.php | 128 ------------------- 4 files changed, 136 insertions(+), 132 deletions(-) diff --git a/src/Module/ActivityPub/Inbox.php b/src/Module/ActivityPub/Inbox.php index 3ca3c7d779..33126676dd 100644 --- a/src/Module/ActivityPub/Inbox.php +++ b/src/Module/ActivityPub/Inbox.php @@ -61,9 +61,9 @@ class Inbox extends BaseApi if ($owner['uid'] != $uid) { throw new \Friendica\Network\HTTPException\ForbiddenException(); } - $outbox = ActivityPub\Transmitter::getInbox($uid, $page, $request['max_id'] ?? null); + $outbox = ActivityPub\ClientToServer::getInbox($uid, $page, $request['max_id'] ?? null); } else { - $outbox = ActivityPub\Transmitter::getPublicInbox($uid, $page, $request['max_id'] ?? null); + $outbox = ActivityPub\ClientToServer::getPublicInbox($uid, $page, $request['max_id'] ?? null); } System::jsonExit($outbox, 'application/activity+json'); diff --git a/src/Module/ActivityPub/Outbox.php b/src/Module/ActivityPub/Outbox.php index 01061d8c67..492971bba0 100644 --- a/src/Module/ActivityPub/Outbox.php +++ b/src/Module/ActivityPub/Outbox.php @@ -51,8 +51,7 @@ class Outbox extends BaseApi $page = 1; } - $requester = HTTPSignature::getSigner('', $_SERVER); - $outbox = ActivityPub\Transmitter::getOutbox($owner, $uid, $page, $request['max_id'] ?? null, $requester); + $outbox = ActivityPub\ClientToServer::getOutbox($owner, $uid, $page, $request['max_id'] ?? null, HTTPSignature::getSigner('', $_SERVER)); System::jsonExit($outbox, 'application/activity+json'); } diff --git a/src/Protocol/ActivityPub/ClientToServer.php b/src/Protocol/ActivityPub/ClientToServer.php index b49f5a8595..55c20accae 100644 --- a/src/Protocol/ActivityPub/ClientToServer.php +++ b/src/Protocol/ActivityPub/ClientToServer.php @@ -24,6 +24,7 @@ namespace Friendica\Protocol\ActivityPub; use Friendica\Content\Text\Markdown; use Friendica\Core\Logger; use Friendica\Core\Protocol; +use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\Contact; @@ -32,6 +33,7 @@ use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\User; use Friendica\Protocol\Activity; +use Friendica\Protocol\ActivityPub; use Friendica\Util\JsonLD; /** @@ -307,4 +309,135 @@ class ClientToServer return $item; } + + /** + * Public posts for the given owner + * + * @param array $owner Owner array + * @param integer $uid User id + * @param integer $page Page number + * @param integer $max_id Maximum ID + * @param string $requester URL of requesting account + * @param boolean $nocache Wether to bypass caching + * @return array of posts + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function getOutbox(array $owner, int $uid, int $page = null, int $max_id = null, string $requester = ''): array + { + $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED]]; + + if (!empty($requester)) { + $requester_id = Contact::getIdForURL($requester, $owner['uid']); + if (!empty($requester_id)) { + $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']); + if (!empty($permissionSets)) { + $condition = ['psid' => array_merge($permissionSets->column('id'), + [DI::permissionSet()->selectPublicForUser($owner['uid'])])]; + } + } + } + + $condition = array_merge($condition, [ + 'uid' => $owner['uid'], + 'author-id' => Contact::getIdForURL($owner['url'], 0, false), + 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], + 'network' => Protocol::FEDERATED, + 'parent-network' => Protocol::FEDERATED, + 'origin' => true, + 'deleted' => false, + 'visible' => true + ]); + + $apcontact = APContact::getByURL($owner['url']); + + return self::getCollection($condition, DI::baseUrl() . '/outbox/' . $owner['nickname'], $page, $max_id, $uid, $apcontact['statuses_count']); + } + + public static function getInbox(int $uid, int $page = null, int $max_id = null) + { + $owner = User::getOwnerDataById($uid); + + $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'uid' => $uid]; + + return self::getCollection($condition, DI::baseUrl() . '/inbox/' . $owner['nickname'], $page, $max_id, $uid, null); + } + + public static function getPublicInbox(int $uid, int $page = null, int $max_id = null) + { + $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => Item::PUBLIC, + 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'author-blocked' => false, 'author-hidden' => false]; + + return self::getCollection($condition, DI::baseUrl() . '/inbox', $page, $max_id, $uid, null); + } + + private static function getCollection(array $condition, string $path, int $page = null, int $max_id = null, int $uid = null, int $total_items = null) + { + $data = ['@context' => ActivityPub::CONTEXT]; + + $data['id'] = $path; + $data['type'] = 'OrderedCollection'; + + if (!is_null($total_items)) { + $data['totalItems'] = $total_items; + } + + if (!empty($page)) { + $data['id'] .= '?' . http_build_query(['page' => $page]); + } + + if (empty($page) && empty($max_id)) { + $data['first'] = $path . '?page=1'; + } else { + $data['type'] = 'OrderedCollectionPage'; + + $list = []; + + if (!empty($max_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + } + + if (!empty($page)) { + $params = ['limit' => [($page - 1) * 20, 20], 'order' => ['uri-id' => true]]; + } else { + $params = ['limit' => 20, 'order' => ['uri-id' => true]]; + } + + if (!is_null($uid)) { + $items = Post::selectForUser($uid, ['id', 'uri-id'], $condition, $params); + } else { + $items = Post::select(['id', 'uri-id'], $condition, $params); + } + + $last_id = 0; + + while ($item = Post::fetch($items)) { + $activity = Transmitter::createActivityFromItem($item['id'], false, !is_null($uid)); + if (!empty($activity)) { + $list[] = $activity; + $last_id = $item['uri-id']; + continue; + } + } + DBA::close($items); + + if (count($list) == 20) { + $data['next'] = $path . '?max_id=' . $last_id; + } + + // Fix the cached total item count when it is lower than the real count + if (!is_null($total_items)) { + $total = (($page - 1) * 20) + $data['totalItems']; + if ($total > $data['totalItems']) { + $data['totalItems'] = $total; + } + } + + $data['partOf'] = $path; + + $data['orderedItems'] = $list; + } + + return $data; + } } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 0338ca72f1..4b63463b48 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -238,134 +238,6 @@ class Transmitter return $data; } - /** - * Public posts for the given owner - * - * @param array $owner Owner array - * @param integer $uid User id - * @param integer $page Page number - * @param integer $max_id Maximum ID - * @param string $requester URL of requesting account - * @param boolean $nocache Wether to bypass caching - * @return array of posts - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - * @throws \ImagickException - */ - public static function getOutbox(array $owner, int $uid, int $page = null, int $max_id = null, string $requester = ''): array - { - $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED]]; - - if (!empty($requester)) { - $requester_id = Contact::getIdForURL($requester, $owner['uid']); - if (!empty($requester_id)) { - $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']); - if (!empty($permissionSets)) { - $condition = ['psid' => array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectPublicForUser($owner['uid'])])]; - } - } - } - - $condition = array_merge($condition, [ - 'uid' => $owner['uid'], - 'author-id' => Contact::getIdForURL($owner['url'], 0, false), - 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], - 'network' => Protocol::FEDERATED, - 'parent-network' => Protocol::FEDERATED, - 'origin' => true, - 'deleted' => false, - 'visible' => true - ]); - - $apcontact = APContact::getByURL($owner['url']); - - return self::getCollection($condition, DI::baseUrl() . '/outbox/' . $owner['nickname'], $page, $max_id, $uid, $apcontact['statuses_count']); - } - - public static function getInbox(int $uid, int $page = null, int $max_id = null) - { - $owner = User::getOwnerDataById($uid); - - $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'uid' => $uid]; - - return self::getCollection($condition, DI::baseUrl() . '/inbox/' . $owner['nickname'], $page, $max_id, $uid, null); - } - - public static function getPublicInbox(int $uid, int $page = null, int $max_id = null) - { - $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => Item::PUBLIC, - 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'author-blocked' => false, 'author-hidden' => false]; - - return self::getCollection($condition, DI::baseUrl() . '/inbox', $page, $max_id, $uid, null); - } - - private static function getCollection(array $condition, string $path, int $page = null, int $max_id = null, int $uid = null, int $total_items = null) - { - $data = ['@context' => ActivityPub::CONTEXT]; - $data['id'] = $path; - $data['type'] = 'OrderedCollection'; - - if (!is_null($total_items)) { - $data['totalItems'] = $total_items; - } - - if (!empty($page)) { - $data['id'] .= '?' . http_build_query(['page' => $page]); - } - - if (empty($page) && empty($max_id)) { - $data['first'] = $path . '?page=1'; - } else { - $data['type'] = 'OrderedCollectionPage'; - $list = []; - - if (!empty($max_id)) { - $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); - } - - if (!empty($page)) { - $params = ['limit' => [($page - 1) * 20, 20], 'order' => ['uri-id' => true]]; - } else { - $params = ['limit' => 20, 'order' => ['uri-id' => true]]; - } - - if (!is_null($uid)) { - $items = Post::selectForUser($uid, ['id', 'uri-id'], $condition, $params); - } else { - $items = Post::select(['id', 'uri-id'], $condition, $params); - } - - $last_id = 0; - while ($item = Post::fetch($items)) { - $activity = self::createActivityFromItem($item['id'], false, !is_null($uid)); - if (!empty($activity)) { - $list[] = $activity; - $last_id = $item['uri-id']; - continue; - } - } - DBA::close($items); - - if (count($list) == 20) { - $data['next'] = $path . '?max_id=' . $last_id; - } - - // Fix the cached total item count when it is lower than the real count - if (!is_null($total_items)) { - $total = (($page - 1) * 20) + $data['totalItems']; - if ($total > $data['totalItems']) { - $data['totalItems'] = $total; - } - } - - $data['partOf'] = $path; - - $data['orderedItems'] = $list; - } - - return $data; - } - /** * Public posts for the given owner * From c643eb8cb2d400295e9ef65b908f2bc9d62de17d Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 23:53:43 +0000 Subject: [PATCH 08/10] Fix more standards --- src/Protocol/ActivityPub/ClientToServer.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Protocol/ActivityPub/ClientToServer.php b/src/Protocol/ActivityPub/ClientToServer.php index 55c20accae..17de28c51a 100644 --- a/src/Protocol/ActivityPub/ClientToServer.php +++ b/src/Protocol/ActivityPub/ClientToServer.php @@ -140,7 +140,7 @@ class ClientToServer * @param array $ldactivity * @return array */ - private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity):array + private static function updateContent(int $uid, string $object_id, array $application, array $ldactivity): array { $id = Item::fetchByLink($object_id, $uid); $original_post = Post::selectFirst(['uri-id'], ['uid' => $uid, 'origin' => true, 'id' => $id]); @@ -325,7 +325,10 @@ class ClientToServer */ public static function getOutbox(array $owner, int $uid, int $page = null, int $max_id = null, string $requester = ''): array { - $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => [Item::PUBLIC, Item::UNLISTED]]; + $condition = [ + 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], + 'private' => [Item::PUBLIC, Item::UNLISTED] + ]; if (!empty($requester)) { $requester_id = Contact::getIdForURL($requester, $owner['uid']); From 41f6e72aa07e27cb3a61d9a28e9af8e72b04d04e Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 13 Feb 2023 23:57:39 +0000 Subject: [PATCH 09/10] Some more standards --- src/Protocol/ActivityPub/ClientToServer.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Protocol/ActivityPub/ClientToServer.php b/src/Protocol/ActivityPub/ClientToServer.php index 17de28c51a..7c0919f636 100644 --- a/src/Protocol/ActivityPub/ClientToServer.php +++ b/src/Protocol/ActivityPub/ClientToServer.php @@ -336,7 +336,7 @@ class ClientToServer $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']); if (!empty($permissionSets)) { $condition = ['psid' => array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectPublicForUser($owner['uid'])])]; + [DI::permissionSet()->selectPublicForUser($owner['uid'])])]; } } } @@ -361,15 +361,24 @@ class ClientToServer { $owner = User::getOwnerDataById($uid); - $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'uid' => $uid]; + $condition = [ + 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], + 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], + 'uid' => $uid + ]; return self::getCollection($condition, DI::baseUrl() . '/inbox/' . $owner['nickname'], $page, $max_id, $uid, null); } public static function getPublicInbox(int $uid, int $page = null, int $max_id = null) { - $condition = ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], 'private' => Item::PUBLIC, - 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], 'author-blocked' => false, 'author-hidden' => false]; + $condition = [ + 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT], + 'private' => Item::PUBLIC, + 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN], + 'author-blocked' => false, + 'author-hidden' => false + ]; return self::getCollection($condition, DI::baseUrl() . '/inbox', $page, $max_id, $uid, null); } From 64a8ad6601139a686089c060e7dd7dbf51e72e13 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 14 Feb 2023 20:43:54 +0000 Subject: [PATCH 10/10] Fix variable name --- src/Module/ActivityPub/Inbox.php | 6 +++--- src/Protocol/ActivityPub/Processor.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Module/ActivityPub/Inbox.php b/src/Module/ActivityPub/Inbox.php index 33126676dd..77085c119a 100644 --- a/src/Module/ActivityPub/Inbox.php +++ b/src/Module/ActivityPub/Inbox.php @@ -61,12 +61,12 @@ class Inbox extends BaseApi if ($owner['uid'] != $uid) { throw new \Friendica\Network\HTTPException\ForbiddenException(); } - $outbox = ActivityPub\ClientToServer::getInbox($uid, $page, $request['max_id'] ?? null); + $inbox = ActivityPub\ClientToServer::getInbox($uid, $page, $request['max_id'] ?? null); } else { - $outbox = ActivityPub\ClientToServer::getPublicInbox($uid, $page, $request['max_id'] ?? null); + $inbox = ActivityPub\ClientToServer::getPublicInbox($uid, $page, $request['max_id'] ?? null); } - System::jsonExit($outbox, 'application/activity+json'); + System::jsonExit($inbox, 'application/activity+json'); } protected function post(array $request = []) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 859e7a2451..6836eafe34 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -35,7 +35,6 @@ use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Model\Conversation; use Friendica\Model\Event; -use Friendica\Model\Group; use Friendica\Model\GServer; use Friendica\Model\Item; use Friendica\Model\ItemURI;