]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge pull request #7092 from MrPetovan/task/7047-theme-error-page
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 7d33fb2dc54713cdafdcd7a1bbe6713a811764e6..a06fbdcf19b532a0cad73877deddc8e05fd4672f 100644 (file)
@@ -20,6 +20,7 @@ use Friendica\Model\Term;
 use Friendica\Model\User;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\Plaintext;
 use Friendica\Util\JsonLD;
 use Friendica\Util\LDSignature;
 use Friendica\Model\Profile;
@@ -152,7 +153,7 @@ class Transmitter
 
                $condition = ['uid' => 0, 'contact-id' => $public_contact, 'author-id' => $public_contact,
                        'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
-                       'deleted' => false, 'visible' => true];
+                       'deleted' => false, 'visible' => true, 'moderated' => false];
                $count = DBA::count('item', $condition);
 
                $data = ['@context' => ActivityPub::CONTEXT];
@@ -186,6 +187,18 @@ class Transmitter
                return $data;
        }
 
+       /**
+        * Return the service array containing information the used software and it's url
+        *
+        * @return array with service data
+        */
+       private static function getService()
+       {
+               return ['type' => 'Service',
+                       'name' =>  FRIENDICA_PLATFORM . " '" . FRIENDICA_CODENAME . "' " . FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
+                       'url' => BaseObject::getApp()->getBaseURL()];
+       }
+
        /**
         * Return the ActivityPub profile of the given user
         *
@@ -242,10 +255,29 @@ class Transmitter
                $data['icon'] = ['type' => 'Image',
                        'url' => $contact['avatar']];
 
+               $data['generator'] = self::getService();
+
                // tags: https://kitty.town/@inmysocks/100656097926961126.json
                return $data;
        }
 
+       /**
+        * @param string $username
+        * @return array
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public static function getDeletedUser($username)
+       {
+               return [
+                       '@context' => ActivityPub::CONTEXT,
+                       'id' => System::baseUrl() . '/profile/' . $username,
+                       'type' => 'Tombstone',
+                       'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+                       'updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+                       'deleted' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+               ];
+       }
+
        /**
         * Returns an array with permissions of a given item array
         *
@@ -303,15 +335,16 @@ class Transmitter
        /**
         * Creates an array of permissions from an item thread
         *
-        * @param array   $item
-        * @param boolean $blindcopy
-        * @param boolean $last_id
+        * @param array   $item       Item array
+        * @param boolean $blindcopy  addressing via "bcc" or "cc"?
+        * @param integer $last_id    Last item id for adding receivers
+        * @param boolean $forum_mode "true" means that we are sending content to a forum
         *
         * @return array with permission data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0)
+       private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0, $forum_mode = false)
        {
                if ($last_id == 0) {
                        $last_id = $item['id'];
@@ -399,7 +432,7 @@ class Transmitter
                                                }
                                        } else {
                                                // Public thread parent post always are directed to the followes
-                                               if (!$item['private']) {
+                                               if (!$item['private'] && !$forum_mode) {
                                                        $data['cc'][] = $actor_profile['followers'];
                                                }
                                        }
@@ -463,6 +496,18 @@ class Transmitter
                return $receivers;
        }
 
+       /**
+        * Check if an inbox is archived
+        *
+        * @param string $url Inbox url
+        *
+        * @return boolean "true" if inbox is archived
+        */
+       private static function archivedInbox($url)
+       {
+               return DBA::exists('inbox-status', ['url' => $url, 'archive' => true]);
+       }
+
        /**
         * Fetches a list of inboxes of followers of a given user
         *
@@ -504,7 +549,9 @@ class Transmitter
                                } else {
                                        $target = $profile['sharedinbox'];
                                }
-                               $inboxes[$target] = $target;
+                               if (!self::archivedInbox($target)) {
+                                       $inboxes[$target] = $target;
+                               }
                        }
                }
                DBA::close($contacts);
@@ -515,18 +562,18 @@ class Transmitter
        /**
         * Fetches an array of inboxes for the given item and user
         *
-        * @param array   $item
-        * @param integer $uid      User ID
-        * @param boolean $personal fetch personal inboxes
-        * @param integer $last_id Last item id for adding receivers
-        *
+        * @param array   $item       Item array
+        * @param integer $uid        User ID
+        * @param boolean $personal   fetch personal inboxes
+        * @param integer $last_id    Last item id for adding receivers
+        * @param boolean $forum_mode "true" means that we are sending content to a forum
         * @return array with inboxes
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0)
+       public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_mode = false)
        {
-               $permissions = self::createPermissionBlockForItem($item, true, $last_id);
+               $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_mode);
                if (empty($permissions)) {
                        return [];
                }
@@ -561,7 +608,9 @@ class Transmitter
                                                } else {
                                                        $target = $profile['sharedinbox'];
                                                }
-                                               $inboxes[$target] = $target;
+                                               if (!self::archivedInbox($target)) {
+                                                       $inboxes[$target] = $target;
+                                               }
                                        }
                                }
                        }
@@ -659,7 +708,7 @@ class Transmitter
                        return false;
                }
 
-               if ($item['wall']) {
+               if ($item['wall'] && ($item['uri'] == $item['parent-uri'])) {
                        $owner = User::getOwnerDataById($item['uid']);
                        if (($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && ($item['author-link'] != $owner['url'])) {
                                $type = 'Announce';
@@ -674,7 +723,7 @@ class Transmitter
                        $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
                        $conversation = DBA::selectFirst('conversation', ['source'], $condition);
                        if (DBA::isResult($conversation)) {
-                               $data = json_decode($conversation['source']);
+                               $data = json_decode($conversation['source'], true);
                                if (!empty($data)) {
                                        return $data;
                                }
@@ -697,11 +746,16 @@ class Transmitter
 
                $data['id'] = $item['uri'] . '#' . $type;
                $data['type'] = $type;
-               $data['actor'] = $item['owner-link'];
+
+               if (Item::isForumPost($item) && ($type != 'Announce')) {
+                       $data['actor'] = $item['author-link'];
+               } else {
+                       $data['actor'] = $item['owner-link'];
+               }
 
                $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM);
 
-               $data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()];
+               $data['instrument'] = self::getService();
 
                $data = array_merge($data, self::createPermissionBlockForItem($item, false));
 
@@ -1014,7 +1068,7 @@ class Transmitter
                        return $data;
                }
 
-               $data['summary'] = null; // Ignore by now
+               $data['summary'] = BBCode::toPlaintext(BBCode::getAbstract($item['body'], Protocol::ACTIVITYPUB));
 
                if ($item['uri'] != $item['thr-parent']) {
                        $data['inReplyTo'] = $item['thr-parent'];
@@ -1048,6 +1102,8 @@ class Transmitter
 
                if ($type == 'Note') {
                        $body = self::removePictures($body);
+               } elseif (($type == 'Article') && empty($data['summary'])) {
+                       $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
                }
 
                if ($type == 'Event') {
@@ -1157,7 +1213,7 @@ class Transmitter
                        'actor' => $owner['url'],
                        'object' => $suggestion['url'],
                        'content' => $suggestion['note'],
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [ActivityPub::PUBLIC_COLLECTION],
                        'cc' => []];
 
@@ -1186,7 +1242,7 @@ class Transmitter
                        'actor' => $owner['url'],
                        'object' => $owner['url'],
                        'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [ActivityPub::PUBLIC_COLLECTION],
                        'cc' => []];
 
@@ -1209,13 +1265,23 @@ class Transmitter
        {
                $owner = User::getOwnerDataById($uid);
 
+               if (empty($owner)) {
+                       Logger::error('No owner data found, the deletion message cannot be processed.', ['user' => $uid]);
+                       return false;
+               }
+
+               if (empty($owner['uprvkey'])) {
+                       Logger::error('No private key for owner found, the deletion message cannot be processed.', ['user' => $uid]);
+                       return false;
+               }
+
                $data = ['@context' => ActivityPub::CONTEXT,
                        'id' => System::baseUrl() . '/activity/' . System::createGUID(),
                        'type' => 'Delete',
                        'actor' => $owner['url'],
                        'object' => $owner['url'],
                        'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [ActivityPub::PUBLIC_COLLECTION],
                        'cc' => []];
 
@@ -1246,7 +1312,7 @@ class Transmitter
                        'actor' => $owner['url'],
                        'object' => self::getProfile($uid),
                        'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [$profile['followers']],
                        'cc' => []];
 
@@ -1282,7 +1348,7 @@ class Transmitter
                        'type' => $activity,
                        'actor' => $owner['url'],
                        'object' => $profile['url'],
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
                Logger::log('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
@@ -1331,7 +1397,7 @@ class Transmitter
                        'type' => 'Follow',
                        'actor' => $owner['url'],
                        'object' => $object,
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
                Logger::log('Sending follow ' . $object . ' to ' . $target . ' for user ' . $uid, Logger::DEBUG);
@@ -1361,7 +1427,7 @@ class Transmitter
                        'object' => ['id' => $id, 'type' => 'Follow',
                                'actor' => $profile['url'],
                                'object' => $owner['url']],
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
                Logger::log('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
@@ -1391,7 +1457,7 @@ class Transmitter
                        'object' => ['id' => $id, 'type' => 'Follow',
                                'actor' => $profile['url'],
                                'object' => $owner['url']],
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
                Logger::log('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);
@@ -1428,7 +1494,7 @@ class Transmitter
                        'object' => ['id' => $object_id, 'type' => 'Follow',
                                'actor' => $owner['url'],
                                'object' => $profile['url']],
-                       'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()],
+                       'instrument' => self::getService(),
                        'to' => [$profile['url']]];
 
                Logger::log('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, Logger::DEBUG);