]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Mode depending control for the behaviour with blocked contacts
[friendica.git] / src / Model / Item.php
index bb4400a860e98f4552f606dada56646d15d95e3e..05ff10273cb4894aaef3de85c9974df616cd4a8a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -623,10 +623,6 @@ class Item
                        return false;
                }
 
-               if (!empty($item['uid']) && !self::isAllowedByUser($item, $item['uid'])) {
-                       return false;
-               }
-
                if ($item['verb'] == Activity::FOLLOW) {
                        if (!$item['origin'] && ($item['author-id'] == Contact::getPublicIdByUserId($item['uid']))) {
                                // Our own follow request can be relayed to us. We don't store it to avoid notification chaos.
@@ -819,43 +815,8 @@ class Item
 
        private static function prepareOriginPost(array $item): array
        {
-               $item['wall'] = 1;
-               $item['origin'] = 1;
-               $item['network'] = Protocol::DFRN;
-               $item['protocol'] = Conversation::PARCEL_DIRECT;
-               $item['direction'] = Conversation::PUSH;
-
-               $owner = User::getOwnerDataById($item['uid']);
-
-               if (empty($item['contact-id'])) {
-                       $item['contact-id'] = $owner['id'];
-               }
-
-               if (empty($item['author-link']) && empty($item['author-id'])) {
-                       $item['author-link']   = $owner['url'];
-                       $item['author-name']   = $owner['name'];
-                       $item['author-avatar'] = $owner['thumb'];
-               }
-
-               if (empty($item['owner-link']) && empty($item['owner-id'])) {
-                       $item['owner-link']   = $item['author-link'];
-                       $item['owner-name']   = $item['author-name'];
-                       $item['owner-avatar'] = $item['author-avatar'];
-               }
-
-               // Setting the object type if not defined before
-               if (empty($item['object-type'])) {
-                       $item['object-type'] = Activity\ObjectType::NOTE; // Default value
-                       $objectdata = BBCode::getAttachedData($item['body']);
-
-                       if ($objectdata['type'] == 'link') {
-                               $item['object-type'] = Activity\ObjectType::BOOKMARK;
-                       } elseif ($objectdata['type'] == 'video') {
-                               $item['object-type'] = Activity\ObjectType::VIDEO;
-                       } elseif ($objectdata['type'] == 'photo') {
-                               $item['object-type'] = Activity\ObjectType::IMAGE;
-                       }
-               }
+               $item = DI::contentItem()->initializePost($item);
+               $item = DI::contentItem()->finalizePost($item);
 
                return $item;
        }
@@ -1023,13 +984,6 @@ class Item
                                return 0;
                        }
 
-                       // If the thread originated from this node, we check the permission against the thread starter
-                       $condition = ['uri-id' => $toplevel_parent['uri-id'], 'wall' => true];
-                       $localTopLevelParent = Post::selectFirst(['uid'], $condition);
-                       if (!empty($localTopLevelParent['uid']) && !self::isAllowedByUser($item, $localTopLevelParent['uid'])) {
-                               return 0;
-                       }
-
                        $parent_id             = $toplevel_parent['id'];
                        $item['parent-uri']    = $toplevel_parent['uri'];
                        $item['parent-uri-id'] = $toplevel_parent['uri-id'];
@@ -1081,6 +1035,14 @@ class Item
                        }
                }
 
+               if ($notify) {
+                       if (Photo::setPermissionFromBody($item['body'], $item['uid'], $item['contact-id'], $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid'])) {
+                               $item['object-type'] = Activity\ObjectType::IMAGE;
+                       }
+
+                       $item = DI::contentItem()->moveAttachmentsFromBodyToAttach($item);
+               }
+
                $item['parent-uri-id'] = ItemURI::getIdByURI($item['parent-uri']);
                $item['thr-parent-id'] = ItemURI::getIdByURI($item['thr-parent']);
 
@@ -1253,6 +1215,8 @@ class Item
                        Post\Content::insert($item['uri-id'], $item);
                }
 
+               $item['parent'] = $parent_id;
+
                // Create Diaspora signature
                if ($item['origin'] && empty($item['diaspora_signed_text']) && ($item['gravity'] != self::GRAVITY_PARENT)) {
                        $signed = Diaspora::createCommentSignature($item);
@@ -1336,10 +1300,7 @@ class Item
                }
 
                if ($notify) {
-                       if (!\Friendica\Content\Feature::isEnabled($posted_item['uid'], 'explicit_mentions') && ($posted_item['gravity'] == self::GRAVITY_COMMENT)) {
-                               Tag::createImplicitMentions($posted_item['uri-id'], $posted_item['thr-parent-id']);
-                       }
-                       Hook::callAll('post_local_end', $posted_item);
+                       DI::contentItem()->postProcessPost($posted_item);
                } else {
                        Hook::callAll('post_remote_end', $posted_item);
                }
@@ -1363,6 +1324,19 @@ class Item
                $transmit = $notify || ($posted_item['visible'] && ($parent_origin || $posted_item['origin']));
 
                if ($transmit) {
+                       if ($posted_item['uid'] && Contact\User::isBlocked($posted_item['author-id'], $posted_item['uid'])) {
+                               Logger::info('Message from blocked author will not be relayed', ['item' => $posted_item['id'], 'uri' => $posted_item['uri'], 'cid' => $posted_item['author-id']]);
+                               $transmit = false;
+                       }
+                       if ($transmit && $posted_item['uid'] && Contact\User::isBlocked($posted_item['owner-id'], $posted_item['uid'])) {
+                               Logger::info('Message from blocked owner will not be relayed', ['item' => $posted_item['id'], 'uri' => $posted_item['uri'], 'cid' => $posted_item['owner-id']]);
+                               $transmit = false;
+                       }
+                       if ($transmit && !empty($posted_item['causer-id']) && $posted_item['uid'] && Contact\User::isBlocked($posted_item['causer-id'], $posted_item['uid'])) {
+                               Logger::info('Message from blocked causer will not be relayed', ['item' => $posted_item['id'], 'uri' => $posted_item['uri'], 'cid' => $posted_item['causer-id']]);
+                               $transmit = false;
+                       }
+
                        // Don't relay participation messages
                        if (($posted_item['verb'] == Activity::FOLLOW) &&
                                (!$posted_item['origin'] || ($posted_item['author-id'] != Contact::getPublicIdByUserId($uid)))) {
@@ -1773,16 +1747,16 @@ class Item
                $item['origin'] = 0;
                $item['wall'] = 0;
 
-               $item['contact-id'] = self::contactId($item);
-
                $notify = false;
                if ($item['gravity'] == self::GRAVITY_PARENT) {
                        $contact = DBA::selectFirst('contact', [], ['id' => $item['contact-id'], 'self' => false]);
                        if (DBA::isResult($contact)) {
                                $notify = self::isRemoteSelf($contact, $item);
+                               $item['wall'] = (bool)$notify;
                        }
                }
 
+               $item['contact-id'] = self::contactId($item);
                $distributed = self::insert($item, $notify);
 
                if (!$distributed) {
@@ -3017,10 +2991,14 @@ class Item
                $item['mentions'] = $tags['mentions'];
 
                if (!$is_preview) {
+                       $item['body'] = preg_replace("#\s*\[attachment .*?].*?\[/attachment]\s*#ism", "\n", $item['body']);
                        $item['body'] = Post\Media::removeFromEndOfBody($item['body'] ?? '');
                }
 
                $body = $item['body'];
+               if ($is_preview) {
+                       $item['body'] = preg_replace("#\s*\[attachment .*?].*?\[/attachment]\s*#ism", "\n", $item['body']);
+               }
 
                $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media', 'quote-uri-id', 'post-type'];
 
@@ -3065,8 +3043,6 @@ class Item
                $attachments = Post\Media::splitAttachments($item['uri-id'], $shared_links, $item['has-media'] ?? false);
                $item['body'] = self::replaceVisualAttachments($attachments, $item['body'] ?? '');
 
-               $item['body'] = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", "\n", $item['body']);
-
                self::putInCache($item);
                $item['body'] = $body;
                $s = $item["rendered-html"];
@@ -3078,6 +3054,14 @@ class Item
                // Compile eventual content filter reasons
                $filter_reasons = [];
                if (!$is_preview && DI::userSession()->getPublicContactId() != $item['author-id']) {
+                       if (!empty($item['user-blocked-author']) || !empty($item['user-blocked-owner'])) {
+                               $filter_reasons[] = DI::l10n()->t('%s is blocked', $item['author-name']);
+                       } elseif (!empty($item['user-ignored-author']) || !empty($item['user-ignored-owner'])) {
+                               $filter_reasons[] = DI::l10n()->t('%s is ignored', $item['author-name']);
+                       } elseif (!empty($item['user-collapsed-author']) || !empty($item['user-collapsed-owner'])) {
+                               $filter_reasons[] = DI::l10n()->t('Content from %s is collapsed', $item['author-name']);
+                       }
+
                        if (!empty($item['content-warning']) && (!DI::userSession()->getLocalUserId() || !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'disable_cw', false))) {
                                $filter_reasons[] = DI::l10n()->t('Content warning: %s', $item['content-warning']);
                        }
@@ -3166,7 +3150,7 @@ class Item
                        ],
                ]);
        }
-       
+
 
        /**
         * Check if the body contains a link
@@ -3449,13 +3433,15 @@ class Item
                                }
 
                                // @todo Use a template
-                               $preview_mode =  DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'preview_mode', BBCode::PREVIEW_LARGE);
+                               $preview_mode = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'preview_mode', BBCode::PREVIEW_LARGE);
                                if ($preview_mode != BBCode::PREVIEW_NONE) {
                                        $rendered = BBCode::convertAttachment('', BBCode::INTERNAL, false, $data, $uriid, $preview_mode);
+                               } else {
+                                       $rendered = '';
                                }
                        } elseif (!self::containsLink($content, $data['url'], Post\Media::HTML)) {
                                $rendered = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/link.tpl'), [
-                                       '$url'  => $data['url'],
+                                       '$url'   => $data['url'],
                                        '$title' => $data['title'],
                                ]);
                        } else {
@@ -3713,43 +3699,6 @@ class Item
                return 0;
        }
 
-       /**
-        * Check a prospective item array against user-level permissions
-        *
-        * @param array $item Expected keys: uri, gravity, and
-        *                    author-link if is author-id is set,
-        *                    owner-link if is owner-id is set,
-        *                    causer-link if is causer-id is set.
-        * @param int   $user_id Local user ID
-        * @return bool
-        * @throws \Exception
-        */
-       protected static function isAllowedByUser(array $item, int $user_id): bool
-       {
-               if (!empty($item['author-id']) && Contact\User::isBlocked($item['author-id'], $user_id)) {
-                       Logger::notice('Author is blocked by user', ['author-link' => $item['author-link'], 'uid' => $user_id, 'item-uri' => $item['uri']]);
-                       return false;
-               }
-
-               if (!empty($item['owner-id']) && Contact\User::isBlocked($item['owner-id'], $user_id)) {
-                       Logger::notice('Owner is blocked by user', ['owner-link' => $item['owner-link'], 'uid' => $user_id, 'item-uri' => $item['uri']]);
-                       return false;
-               }
-
-               // The causer is set during a thread completion, for example because of a reshare. It countains the responsible actor.
-               if (!empty($item['causer-id']) && Contact\User::isBlocked($item['causer-id'], $user_id)) {
-                       Logger::notice('Causer is blocked by user', ['causer-link' => $item['causer-link'] ?? $item['causer-id'], 'uid' => $user_id, 'item-uri' => $item['uri']]);
-                       return false;
-               }
-
-               if (!empty($item['causer-id']) && ($item['gravity'] === self::GRAVITY_PARENT) && Contact\User::isIgnored($item['causer-id'], $user_id)) {
-                       Logger::notice('Causer is ignored by user', ['causer-link' => $item['causer-link'] ?? $item['causer-id'], 'uid' => $user_id, 'item-uri' => $item['uri']]);
-                       return false;
-               }
-
-               return true;
-       }
-
        /**
         * Fetch the uri-id of a quote
         *