]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Receiver.php
Merge pull request #8447 from annando/issue-7771-funkwhale
[friendica.git] / src / Protocol / ActivityPub / Receiver.php
index 7d25d94f1788abcfa9e53333b6f9a78552092bce..30728ff11afedadaff91508bde266bd193045305 100644 (file)
@@ -28,7 +28,6 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Model\Contact;
 use Friendica\Model\APContact;
-use Friendica\Model\Conversation;
 use Friendica\Model\Item;
 use Friendica\Model\User;
 use Friendica\Protocol\Activity;
@@ -56,7 +55,7 @@ class Receiver
 {
        const PUBLIC_COLLECTION = 'as:Public';
        const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
-       const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event'];
+       const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event', 'as:Audio'];
        const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept'];
 
        /**
@@ -127,7 +126,7 @@ class Receiver
                        $trust_source = false;
                }
 
-               self::processActivity($ldactivity, $body, $uid, $trust_source);
+               self::processActivity($ldactivity, $body, $uid, $trust_source, true);
        }
 
        /**
@@ -175,15 +174,16 @@ class Receiver
        /**
         * Prepare the object array
         *
-        * @param array   $activity
-        * @param integer $uid User ID
-        * @param         $trust_source
+        * @param array   $activity     Array with activity data
+        * @param integer $uid          User ID
+        * @param boolean $push         Message had been pushed to our system
+        * @param boolean $trust_source Do we trust the source?
         *
         * @return array with object data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function prepareObjectData($activity, $uid, &$trust_source)
+       private static function prepareObjectData($activity, $uid, $push, &$trust_source)
        {
                $actor = JsonLD::fetchElement($activity, 'as:actor', '@id');
                if (empty($actor)) {
@@ -231,8 +231,15 @@ class Receiver
                                Logger::log("Object data couldn't be processed", Logger::DEBUG);
                                return [];
                        }
+
                        $object_data['object_id'] = $object_id;
 
+                       if ($type == 'as:Announce') {
+                               $object_data['push'] = false;
+                       } else {
+                               $object_data['push'] = $push;
+                       }
+
                        // Test if it is an answer to a mail
                        if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) {
                                $object_data['directmessage'] = true;
@@ -303,33 +310,6 @@ class Receiver
                return 0;
        }
 
-       /**
-        * Store the unprocessed data into the conversation table
-        * This has to be done outside the regular function,
-        * since we store everything - not only item posts.
-        *
-        * @param array  $activity Array with activity data
-        * @param string $body     The raw message
-        * @throws \Exception
-        */
-       private static function storeConversation($activity, $body)
-       {
-               if (empty($body) || empty($activity['id'])) {
-                       return;
-               }
-
-               $conversation = [
-                       'protocol' => Conversation::PARCEL_ACTIVITYPUB,
-                       'item-uri' => $activity['id'],
-                       'reply-to-uri' => $activity['reply-to-id'] ?? '',
-                       'conversation-href' => $activity['context'] ?? '',
-                       'conversation-uri' => $activity['conversation'] ?? '',
-                       'source' => $body,
-                       'received' => DateTimeFormat::utcNow()];
-
-               DBA::insert('conversation', $conversation, true);
-       }
-
        /**
         * Processes the activity object
         *
@@ -337,9 +317,10 @@ class Receiver
         * @param string  $body
         * @param integer $uid          User ID
         * @param boolean $trust_source Do we trust the source?
+        * @param boolean $push         Message had been pushed to our system
         * @throws \Exception
         */
-       public static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
+       public static function processActivity($activity, $body = '', $uid = null, $trust_source = false, $push = false)
        {
                $type = JsonLD::fetchElement($activity, '@type');
                if (!$type) {
@@ -369,7 +350,7 @@ class Receiver
                }
 
                // $trust_source is called by reference and is set to true if the content was retrieved successfully
-               $object_data = self::prepareObjectData($activity, $uid, $trust_source);
+               $object_data = self::prepareObjectData($activity, $uid, $push, $trust_source);
                if (empty($object_data)) {
                        Logger::log('No object data found', Logger::DEBUG);
                        return;
@@ -380,9 +361,8 @@ class Receiver
                        return;
                }
 
-               // Only store content related stuff - and no announces, since they possibly overwrite the original content
-               if (in_array($object_data['object_type'], self::CONTENT_TYPES) && ($type != 'as:Announce')) {
-                       self::storeConversation($object_data, $body);
+               if (!empty($body) && empty($object_data['raw'])) {
+                       $object_data['raw'] = $body;
                }
 
                // Internal flag for thread completion. See Processor.php
@@ -419,6 +399,11 @@ class Receiver
                                                $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id');
                                                $announce_object_data['object_id'] = $object_data['object_id'];
                                                $announce_object_data['object_type'] = $object_data['object_type'];
+                                               $announce_object_data['push'] = $push;
+
+                                               if (!empty($body)) {
+                                                       $announce_object_data['raw'] = $body;
+                                               }
 
                                                ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
                                        }
@@ -506,14 +491,15 @@ class Receiver
        /**
         * Fetch the receiver list from an activity array
         *
-        * @param array  $activity
-        * @param string $actor
-        * @param array  $tags
+        * @param array   $activity
+        * @param string  $actor
+        * @param array   $tags
+        * @param boolean $fetch_unlisted 
         *
         * @return array with receivers (user id)
         * @throws \Exception
         */
-       private static function getReceivers($activity, $actor, $tags = [])
+       private static function getReceivers($activity, $actor, $tags = [], $fetch_unlisted = false)
        {
                $receivers = [];
 
@@ -551,6 +537,11 @@ class Receiver
                                        $receivers['uid:0'] = 0;
                                }
 
+                               // Add receiver "-1" for unlisted posts 
+                               if ($fetch_unlisted && ($receiver == self::PUBLIC_COLLECTION) && ($element == 'as:cc')) {
+                                       $receivers['uid:-1'] = -1;
+                               }
+
                                if (($receiver == self::PUBLIC_COLLECTION) && !empty($actor)) {
                                        // This will most likely catch all OStatus connections to Mastodon
                                        $condition = ['alias' => [$actor, Strings::normaliseLink($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]
@@ -802,7 +793,12 @@ class Receiver
                }
 
                if (in_array($type, self::CONTENT_TYPES)) {
-                       return self::processObject($object);
+                       $object_data = self::processObject($object);
+
+                       if (!empty($data)) {
+                               $object_data['raw'] = json_encode($data);
+                       }
+                       return $object_data;
                }
 
                if ($type == 'as:Announce') {
@@ -940,6 +936,75 @@ class Receiver
                return $object_data;
        }
 
+       /**
+        * Check if the "as:url" element is an array with multiple links
+        * This is the case with audio and video posts.
+        * Then the links are added as attachments
+        *
+        * @param array $object      The raw object
+        * @param array $object_data The parsed object data for later processing
+        * @return array the object data
+        */
+       private static function processAttachmentUrls(array $object, array $object_data) {
+               // Check if this is some url with multiple links
+               if (empty($object['as:url'])) {
+                       return $object_data;
+               }
+               
+               $urls = $object['as:url'];
+               $keys = array_keys($urls);
+               if (!is_numeric(array_pop($keys))) {
+                       return $object_data;
+               }
+
+               $attachments = [];
+
+               foreach ($urls as $url) {
+                       if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
+                               continue;
+                       }
+
+                       $href = JsonLD::fetchElement($url, 'as:href', '@id');
+                       if (empty($href)) {
+                               continue;
+                       }
+
+                       $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
+                       if (empty($mediatype)) {
+                               continue;
+                       }
+
+                       if ($mediatype == 'text/html') {
+                               $object_data['alternate-url'] = $href;
+                       }
+
+                       $filetype = strtolower(substr($mediatype, 0, strpos($mediatype, '/')));
+
+                       if ($filetype == 'audio') {
+                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href];
+                       } elseif ($filetype == 'video') {
+                               $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
+
+                               // We save bandwidth by using a moderate height
+                               // Peertube normally uses these heights: 240, 360, 480, 720, 1080
+                               if (!empty($attachments[$filetype]['height']) &&
+                                       (($height > 480) || $height < $attachments[$filetype]['height'])) {
+                                       continue;
+                               }
+
+                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href, 'height' => $height];
+                       }
+               }
+
+               foreach ($attachments as $type => $attachment) {
+                       $object_data['attachments'][] = ['type' => $type,
+                               'mediaType' => $attachment['type'],
+                               'name' => '',
+                               'url' => $attachment['url']];
+               }
+               return $object_data;
+       }
+
        /**
         * Fetches data from the object part of an activity
         *
@@ -987,6 +1052,7 @@ class Receiver
                        $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
                }
 
+               $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');
                $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
                $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
                $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
@@ -1022,7 +1088,13 @@ class Receiver
                        }
                }
 
-               $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags']);
+               if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
+                       $object_data = self::processAttachmentUrls($object, $object_data);
+               }
+
+               $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags'], true);
+               $object_data['unlisted'] = in_array(-1, $object_data['receiver']);
+               unset($object_data['receiver']['uid:-1']);
 
                // Common object data: