]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge pull request #7570 from nupplaphil/bug/friendica-7298
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index ed792db16e75a85952bf5a1eb4bcc2da80ad2241..e44ae1cf66c2dfa4ff9413cc2b457d10fbb39279 100644 (file)
@@ -21,6 +21,7 @@ use Friendica\Model\User;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\Plaintext;
+use Friendica\Util\XML;
 use Friendica\Util\JsonLD;
 use Friendica\Util\LDSignature;
 use Friendica\Model\Profile;
@@ -228,11 +229,6 @@ class Transmitter
                        return [];
                }
 
-               // On old installations and never changed contacts this might not be filled
-               if (empty($contact['avatar'])) {
-                       $contact['avatar'] = $contact['photo'];
-               }
-
                $data = ['@context' => ActivityPub::CONTEXT];
                $data['id'] = $contact['url'];
                $data['diaspora:guid'] = $user['guid'];
@@ -253,7 +249,7 @@ class Transmitter
                        'publicKeyPem' => $user['pubkey']];
                $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
                $data['icon'] = ['type' => 'Image',
-                       'url' => $contact['avatar']];
+                       'url' => $contact['photo']];
 
                $data['generator'] = self::getService();
 
@@ -362,7 +358,7 @@ class Transmitter
 
                if (Config::get('debug', 'total_ap_delivery')) {
                        // Will be activated in a later step
-                       $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
+                       $networks = Protocol::FEDERATED;
                } else {
                        // For now only send to these contacts:
                        $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
@@ -390,7 +386,7 @@ class Transmitter
                                }
                        }
                } else {
-                       $receiver_list = Item::enumeratePermissions($item);
+                       $receiver_list = Item::enumeratePermissions($item, true);
 
                        foreach ($terms as $term) {
                                $cid = Contact::getIdForURL($term['url'], $item['uid']);
@@ -435,7 +431,7 @@ class Transmitter
                                                                $data['to'][] = $profile['url'];
                                                        } else {
                                                                $data['cc'][] = $profile['url'];
-                                                               if (!$item['private']) {
+                                                               if (!$item['private'] && !empty($actor_profile['followers'])) {
                                                                        $data['cc'][] = $actor_profile['followers'];
                                                                }
                                                        }
@@ -534,7 +530,7 @@ class Transmitter
 
                if (Config::get('debug', 'total_ap_delivery')) {
                        // Will be activated in a later step
-                       $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
+                       $networks = Protocol::FEDERATED;
                } else {
                        // For now only send to these contacts:
                        $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
@@ -772,6 +768,8 @@ class Transmitter
                        $type = 'TentativeAccept';
                } elseif ($item['verb'] == ACTIVITY_FOLLOW) {
                        $type = 'Follow';
+               } elseif ($item['verb'] == ACTIVITY_TAG) {
+                       $type = 'Add';
                } else {
                        $type = '';
                }
@@ -875,6 +873,8 @@ class Transmitter
 
                if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
                        $data['object'] = self::createNote($item);
+               } elseif ($data['type'] == 'Add') {
+                       $data = self::createAddTag($item, $data);
                } elseif ($data['type'] == 'Announce') {
                        $data = self::createAnnounce($item, $data);
                } elseif ($data['type'] == 'Follow') {
@@ -1034,7 +1034,7 @@ class Transmitter
                // Simplify image codes
                $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $item['body']);
 
-               // Grab all pictures and create attachments out of them
+               // Grab all pictures without alternative descriptions and create attachments out of them
                if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures)) {
                        foreach ($pictures[1] as $picture) {
                                $imgdata = Image::getInfoFromURL($picture);
@@ -1047,6 +1047,19 @@ class Transmitter
                        }
                }
 
+               // Grab all pictures with alternative description and create attachments out of them
+               if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
+                       foreach ($pictures as $picture) {
+                               $imgdata = Image::getInfoFromURL($picture[1]);
+                               if ($imgdata) {
+                                       $attachments[] = ['type' => 'Document',
+                                               'mediaType' => $imgdata['mime'],
+                                               'url' => $picture[1],
+                                               'name' => $picture[2]];
+                               }
+                       }
+               }
+
                return $attachments;
        }
 
@@ -1136,7 +1149,7 @@ class Transmitter
        {
                $event = [];
                $event['name'] = $item['event-summary'];
-               $event['content'] = BBCode::convert($item['event-desc'], false, 7);
+               $event['content'] = BBCode::convert($item['event-desc'], false, 9);
                $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
 
                if (!$item['event-nofinish']) {
@@ -1226,7 +1239,7 @@ class Transmitter
                        $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
                        $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
 
-                       $data['content'] = BBCode::convert($body, false, 7);
+                       $data['content'] = BBCode::convert($body, false, 9);
                }
 
                $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
@@ -1251,6 +1264,30 @@ class Transmitter
                return $data;
        }
 
+       /**
+        * Creates an an "add tag" entry
+        *
+        * @param array $item
+        * @param array $data activity data
+        *
+        * @return array with activity data for adding tags
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function createAddTag($item, $data)
+       {
+               $object = XML::parseString($item['object'], false);
+               $target = XML::parseString($item["target"], false);
+
+               $data['diaspora:guid'] = $item['guid'];
+               $data['actor'] = $item['author-link'];
+               $data['target'] = (string)$target->id;
+               $data['summary'] = BBCode::toPlaintext($item['body']);
+               $data['object'] = ['id' => (string)$object->id, 'type' => 'tag', 'name' => (string)$object->title, 'content' => (string)$object->content];
+
+               return $data;
+       }
+
        /**
         * Creates an announce object entry
         *
@@ -1263,6 +1300,7 @@ class Transmitter
         */
        private static function createAnnounce($item, $data)
        {
+               $orig_body = $item['body'];
                $announce = api_share_as_retweet($item);
                if (empty($announce['plink'])) {
                        $data['type'] = 'Create';
@@ -1275,12 +1313,29 @@ class Transmitter
                if (!empty($activity)) {
                        $ldactivity = JsonLD::compact($activity);
                        $id = JsonLD::fetchElement($ldactivity, '@id');
+                       $type = str_replace('as:', '', JsonLD::fetchElement($ldactivity, '@type'));
                        if (!empty($id)) {
-                               $data['object'] = $id;
+                               if (empty($announce['share-pre-body'])) {
+                                       // Pure announce, without a quote
+                                       $data['type'] = 'Announce';
+                                       $data['object'] = $id;
+                                       return $data;
+                               }
+
+                               // Quote
+                               $data['type'] = 'Create';
+                               $item['body'] = trim($announce['share-pre-body']) . "\n" . $id;
+                               $data['object'] = self::createNote($item);
+
+                               /// @todo Finally descide how to implement this in AP. This is a possible way:
+                               $data['object']['attachment'][] = ['type' => $type, 'id' => $id];
+
+                               $data['object']['source']['content'] = $orig_body;
                                return $data;
                        }
                }
 
+               $item['body'] = $orig_body;
                $data['type'] = 'Create';
                $data['object'] = self::createNote($item);
                return $data;
@@ -1450,6 +1505,10 @@ class Transmitter
        public static function sendActivity($activity, $target, $uid, $id = '')
        {
                $profile = APContact::getByURL($target);
+               if (empty($profile['inbox'])) {
+                       Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
+                       return;
+               }
 
                $owner = User::getOwnerDataById($uid);
 
@@ -1486,6 +1545,10 @@ class Transmitter
        public static function sendFollowObject($object, $target, $uid = 0)
        {
                $profile = APContact::getByURL($target);
+               if (empty($profile['inbox'])) {
+                       Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
+                       return;
+               }
 
                if (empty($uid)) {
                        // Fetch the list of administrators
@@ -1532,6 +1595,10 @@ class Transmitter
        public static function sendContactAccept($target, $id, $uid)
        {
                $profile = APContact::getByURL($target);
+               if (empty($profile['inbox'])) {
+                       Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
+                       return;
+               }
 
                $owner = User::getOwnerDataById($uid);
                $data = ['@context' => ActivityPub::CONTEXT,
@@ -1565,6 +1632,10 @@ class Transmitter
        public static function sendContactReject($target, $id, $uid)
        {
                $profile = APContact::getByURL($target);
+               if (empty($profile['inbox'])) {
+                       Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
+                       return;
+               }
 
                $owner = User::getOwnerDataById($uid);
                $data = ['@context' => ActivityPub::CONTEXT,
@@ -1598,6 +1669,10 @@ class Transmitter
        public static function sendContactUndo($target, $cid, $uid)
        {
                $profile = APContact::getByURL($target);
+               if (empty($profile['inbox'])) {
+                       Logger::warning('No inbox found for target', ['target' => $target, 'profile' => $profile]);
+                       return;
+               }
 
                $object_id = self::activityIDFromContact($cid);
                if (empty($object_id)) {