]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Merge remote-tracking branch 'upstream/2021.03-rc' into issue-10126
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index e6f3e156690042bd1701c20c9d662b181238c048..66aedf65b06d39f69a8e2e567026870ff3bc4fc8 100644 (file)
@@ -162,6 +162,9 @@ class Processor
                        return $item;
                }
 
+               $leading = '';
+               $trailing = '';
+
                foreach ($activity['attachments'] as $attach) {
                        switch ($attach['type']) {
                                case 'link':
@@ -193,39 +196,65 @@ class Processor
                                                        }
                                                }
 
-                                               $item['body'] .= "\n";
-
                                                // image is the preview/thumbnail URL
                                                if (!empty($attach['image'])) {
-                                                       $item['body'] .= '[url=' . $attach['url'] . ']';
+                                                       $media = '[url=' . $attach['url'] . ']';
                                                        $attach['url'] = $attach['image'];
+                                               } else {
+                                                       $media = '';
                                                }
 
                                                if (empty($attach['name'])) {
-                                                       $item['body'] .= '[img]' . $attach['url'] . '[/img]';
+                                                       $media .= '[img]' . $attach['url'] . '[/img]';
                                                } else {
-                                                       $item['body'] .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
+                                                       $media .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
                                                }
 
                                                if (!empty($attach['image'])) {
-                                                       $item['body'] .= '[/url]';
+                                                       $media .= '[/url]';
                                                }
+
+                                               if ($item['post-type'] == Item::PT_IMAGE) {
+                                                       $leading .= $media;
+                                               } else {
+                                                       $trailing .= $media;
+                                               }               
                                        } elseif ($filetype == 'audio') {
                                                if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
                                                        continue 2;
                                                }
 
-                                               $item['body'] = '[audio]' . $attach['url'] . "[/audio]\n" . $item['body'];
+                                               if ($item['post-type'] == Item::PT_AUDIO) {
+                                                       $leading .= '[audio]' . $attach['url'] . "[/audio]\n";
+                                               } else {
+                                                       $trailing .= '[audio]' . $attach['url'] . "[/audio]\n";
+                                               }
                                        } elseif ($filetype == 'video') {
                                                if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
                                                        continue 2;
                                                }
 
-                                               $item['body'] = '[video]' . $attach['url'] . "[/video]\n" . $item['body'];
+                                               if ($item['post-type'] == Item::PT_VIDEO) {
+                                                       $leading .= '[video]' . $attach['url'] . "[/video]\n";
+                                               } else {
+                                                       $trailing .= '[video]' . $attach['url'] . "[/video]\n";
+                                               }
                                        }
                        }
                }
 
+               if (!empty($leading) && !empty(trim($item['body']))) {
+                       $item['body'] = $leading . "[hr]\n" . $item['body'];
+               } elseif (!empty($leading)) {
+                       $item['body'] = $leading;
+               }
+
+               if (!empty($trailing) && !empty(trim($item['body']))) {
+                       $item['body'] = $item['body'] . "\n[hr]" . $trailing;
+               } elseif (!empty($trailing)) {
+                       $item['body'] = $trailing;
+               }
+
                return $item;
        }
 
@@ -237,7 +266,7 @@ class Processor
         */
        public static function updateItem($activity)
        {
-               $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity'], ['uri' => $activity['id']]);
+               $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity', 'post-type'], ['uri' => $activity['id']]);
                if (!DBA::isResult($item)) {
                        Logger::warning('No existing item, item will be created', ['uri' => $activity['id']]);
                        $item = self::createItem($activity);
@@ -323,6 +352,24 @@ class Processor
                        $item['direction'] = Conversation::RELAY;
                }
 
+               if ($activity['object_type'] == 'as:Article') {
+                       $item['post-type'] = Item::PT_ARTICLE;
+               } elseif ($activity['object_type'] == 'as:Audio') {
+                       $item['post-type'] = Item::PT_AUDIO;
+               } elseif ($activity['object_type'] == 'as:Document') {
+                       $item['post-type'] = Item::PT_DOCUMENT;
+               } elseif ($activity['object_type'] == 'as:Event') {
+                       $item['post-type'] = Item::PT_EVENT;
+               } elseif ($activity['object_type'] == 'as:Image') {
+                       $item['post-type'] = Item::PT_IMAGE;
+               } elseif ($activity['object_type'] == 'as:Page') {
+                       $item['post-type'] = Item::PT_PAGE;
+               } elseif ($activity['object_type'] == 'as:Video') {
+                       $item['post-type'] = Item::PT_VIDEO;
+               } else {
+                       $item['post-type'] = Item::PT_NOTE;
+               }
+
                $item['isForum'] = false;
 
                if (!empty($activity['thread-completion'])) {
@@ -443,6 +490,7 @@ class Processor
                $item['verb'] = $verb;
                $item['thr-parent'] = $activity['object_id'];
                $item['gravity'] = GRAVITY_ACTIVITY;
+               unset($item['post-type']);
                $item['object-type'] = Activity\ObjectType::NOTE;
 
                $item['diaspora_signed_text'] = $activity['diaspora:like'] ?? '';
@@ -465,7 +513,7 @@ class Processor
                $event['finish']    = $activity['end-time'];
                $event['nofinish']  = empty($event['finish']);
                $event['location']  = $activity['location'];
-               $event['adjust']    = true;
+               $event['adjust']    = $activity['adjust'] ?? true;
                $event['cid']       = $item['contact-id'];
                $event['uid']       = $item['uid'];
                $event['uri']       = $item['uri'];
@@ -616,34 +664,34 @@ class Processor
                        $type = $activity['reception_type'][$receiver] ?? Receiver::TARGET_UNKNOWN;
                        switch($type) {
                                case Receiver::TARGET_TO:
-                                       $item['post-type'] = Item::PT_TO;
+                                       $item['post-reason'] = Item::PR_TO;
                                        break;
                                case Receiver::TARGET_CC:
-                                       $item['post-type'] = Item::PT_CC;
+                                       $item['post-reason'] = Item::PR_CC;
                                        break;
                                case Receiver::TARGET_BTO:
-                                       $item['post-type'] = Item::PT_BTO;
+                                       $item['post-reason'] = Item::PR_BTO;
                                        break;
                                case Receiver::TARGET_BCC:
-                                       $item['post-type'] = Item::PT_BCC;
+                                       $item['post-reason'] = Item::PR_BCC;
                                        break;
                                case Receiver::TARGET_FOLLOWER:
-                                       $item['post-type'] = Item::PT_FOLLOWER;
+                                       $item['post-reason'] = Item::PR_FOLLOWER;
                                        break;
                                case Receiver::TARGET_ANSWER:
-                                       $item['post-type'] = Item::PT_COMMENT;
+                                       $item['post-reason'] = Item::PR_COMMENT;
                                        break;
                                case Receiver::TARGET_GLOBAL:
-                                       $item['post-type'] = Item::PT_GLOBAL;
+                                       $item['post-reason'] = Item::PR_GLOBAL;
                                        break;
                                default:
-                                       $item['post-type'] = Item::PT_ARTICLE;
+                                       $item['post-reason'] = Item::PR_NONE;
                        }
 
                        if (!empty($activity['from-relay'])) {
-                               $item['post-type'] = Item::PT_RELAY;
+                               $item['post-reason'] = Item::PR_RELAY;
                        } elseif (!empty($activity['thread-completion'])) {
-                               $item['post-type'] = Item::PT_FETCHED;
+                               $item['post-reason'] = Item::PR_FETCHED;
                        }
 
                        if ($item['isForum'] ?? false) {