]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #9146 from tobiasd/2020.09-CHANGELOG
[friendica.git] / src / Model / Item.php
index 4526be3e9226d0fe5c09f3b168a794ff1e1fc98b..6e41cd46c01b78b12e260256210f3d1ccbee1b0a 100644 (file)
@@ -40,7 +40,6 @@ use Friendica\Protocol\Diaspora;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
 use Friendica\Util\Network;
-use Friendica\Util\Security;
 use Friendica\Util\Strings;
 use Friendica\Worker\Delivery;
 use Text_LanguageDetect;
@@ -57,6 +56,16 @@ class Item
        const PT_VIDEO = 18;
        const PT_DOCUMENT = 19;
        const PT_EVENT = 32;
+       const PT_TAG = 64;
+       const PT_TO = 65;
+       const PT_CC = 66;
+       const PT_BTO = 67;
+       const PT_BCC = 68;
+       const PT_FOLLOWER = 69;
+       const PT_ANNOUNCEMENT = 70;
+       const PT_COMMENT = 71;
+       const PT_STORED = 72;
+       const PT_GLOBAL = 73;
        const PT_PERSONAL_NOTE = 128;
 
        // Field list that is used to display the items
@@ -201,19 +210,7 @@ class Item
                        return [];
                }
 
-               if (empty($condition) || !is_array($condition)) {
-                       $condition = ['iid' => $pinned];
-               } else {
-                       reset($condition);
-                       $first_key = key($condition);
-                       if (!is_int($first_key)) {
-                               $condition['iid'] = $pinned;
-                       } else {
-                               $values_string = substr(str_repeat("?, ", count($pinned)), 0, -2);
-                               $condition[0] = '(' . $condition[0] . ") AND `iid` IN (" . $values_string . ")";
-                               $condition = array_merge($condition, $pinned);
-                       }
-               }
+               $condition = DBA::mergeConditions(['iid' => $pinned], $condition);
 
                return self::selectThreadForUser($uid, $selected, $condition, $params);
        }
@@ -685,7 +682,7 @@ class Item
                        'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid',
                        'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id'];
 
-               $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network'];
+               $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network', 'author-id' => 'parent-author-id'];
 
                $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name',
                        'network' => 'parent-author-network'];
@@ -1355,7 +1352,7 @@ class Item
         * @param array $item
         * @return boolean item is valid
         */
-       private static function isValid(array $item)
+       public static function isValid(array $item)
        {
                // When there is no content then we don't post it
                if ($item['body'].$item['title'] == '') {
@@ -1384,7 +1381,7 @@ class Item
                        }
                }
 
-               if (Contact::isBlocked($item['author-id'])) {
+               if (!empty($item['author-id']) && Contact::isBlocked($item['author-id'])) {
                        Logger::notice('Author is blocked node-wide', ['author-link' => $item['author-link'], 'item-uri' => $item['uri']]);
                        return false;
                }
@@ -1394,12 +1391,12 @@ class Item
                        return false;
                }
 
-               if (!empty($item['uid']) && Contact\User::isBlocked($item['author-id'], $item['uid'])) {
+               if (!empty($item['uid']) && !empty($item['author-id']) && Contact\User::isBlocked($item['author-id'], $item['uid'])) {
                        Logger::notice('Author is blocked by user', ['author-link' => $item['author-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]);
                        return false;
                }
 
-               if (Contact::isBlocked($item['owner-id'])) {
+               if (!empty($item['owner-id']) && Contact::isBlocked($item['owner-id'])) {
                        Logger::notice('Owner is blocked node-wide', ['owner-link' => $item['owner-link'], 'item-uri' => $item['uri']]);
                        return false;
                }
@@ -1409,7 +1406,7 @@ class Item
                        return false;
                }
 
-               if (!empty($item['uid']) && Contact\User::isBlocked($item['owner-id'], $item['uid'])) {
+               if (!empty($item['uid']) && !empty($item['owner-id']) && Contact\User::isBlocked($item['owner-id'], $item['uid'])) {
                        Logger::notice('Owner is blocked by user', ['owner-link' => $item['owner-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]);
                        return false;
                }
@@ -1703,6 +1700,11 @@ class Item
                        'photo' => $item['owner-avatar'], 'network' => $item['network']];
                $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
 
+               $actor = ($item['gravity'] == GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
+               if (!$item['origin'] && in_array($item['post-type'], [self::PT_ARTICLE, self::PT_COMMENT, self::PT_GLOBAL]) && Contact::isSharing($actor, $item['uid'])) {
+                       $item['post-type'] = self::PT_FOLLOWER;
+               }
+
                // Ensure that there is an avatar cache
                Contact::checkAvatarCache($item['author-id']);
                Contact::checkAvatarCache($item['owner-id']);
@@ -1800,7 +1802,9 @@ class Item
                // It is mainly used in the "post_local" hook.
                unset($item['api_source']);
 
-               self::transformToForumPost($item);
+               if ($item['verb'] == Activity::ANNOUNCE) {
+                       self::setOwnerforResharedItem($item);
+               }
 
                // Check for hashtags in the body and repair or add hashtag links
                $item['body'] = self::setHashtags($item['body']);
@@ -1992,41 +1996,48 @@ class Item
        }
 
        /**
-        * Convert items to forum posts
+        * Change the owner of a parent item if it had been shared by a forum
         *
         * (public) forum posts in the new format consist of the regular post by the author
         * followed by an announce message sent from the forum account.
-        * This means we have to look out for an announce message send by a forum account.
+        * Changing the owner helps in grouping forum posts.
         *
         * @param array $item
         * @return void
         */
-       private static function transformToForumPost(array $item)
+       private static function setOwnerforResharedItem(array $item)
        {
-               if ($item["verb"] != Activity::ANNOUNCE) {
-                       // No announce message, so don't do anything
+               $parent = self::selectFirst(['id', 'owner-id', 'author-id', 'author-link', 'origin', 'post-type'],
+                       ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
+               if (!DBA::isResult($parent)) {
+                       Logger::error('Parent not found', ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
                        return;
                }
 
-               $pcontact = Contact::selectFirst(['nurl'], ['id' => $item['author-id'], 'contact-type' => Contact::TYPE_COMMUNITY]);
-               if (empty($pcontact['nurl'])) {
-                       // The announce message wasn't created by a forum account, so we don't need to continue
+               $author = Contact::selectFirst(['url', 'contact-type'], ['id' => $item['author-id']]);
+               if (!DBA::isResult($author)) {
+                       Logger::error('Author not found', ['id' => $item['author-id']]);
                        return;
                }
 
-               $contact = Contact::selectFirst(['id'], ['nurl' => $pcontact['nurl'], 'uid' => $item['uid']]);
-               if (!empty($contact['id'])) {
-                       $condition = ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']];
-                       Item::update(['owner-id' => $item['author-id'], 'contact-id' => $contact['id']], $condition);
-                       $forum_item = Item::selectFirst(['id'], $condition);
-                       if (!empty($forum_item['id'])) {
-                               // This will trigger notifications like "X shared a new post"
-                               UserItem::setNotification($forum_item['id']);
+               $cid = Contact::getIdForURL($author['url'], $item['uid']);
+               if (empty($cid) || !Contact::isSharing($cid, $item['uid'])) {
+                       Logger::info('The resharer is not a following contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid']]);
+                       return;
+               }
 
-                               check_user_notification($forum_item['id']);
+               if ($author['contact-type'] != Contact::TYPE_COMMUNITY) {
+                       if (!in_array($parent['post-type'], [self::PT_ARTICLE, self::PT_COMMENT]) || Contact::isSharing($parent['owner-id'], $item['uid'])) {
+                               Logger::info('The resharer is no forum: quit', ['resharer' => $item['author-id'], 'owner' => $parent['owner-id'], 'author' => $parent['author-id'], 'uid' => $item['uid']]);
+                               return;
                        }
-                       LOgger::info('Convert message into a forum message', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $contact['id']]);
+                       self::update(['post-type' => self::PT_ANNOUNCEMENT], ['id' => $parent['id']]);
+                       Logger::info('Set announcement post-type', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
+                       return;
                }
+
+               self::update(['owner-id' => $item['author-id'], 'contact-id' => $cid], ['id' => $parent['id']]);
+               Logger::info('Change owner of the parent', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $cid]);
        }
 
        /**
@@ -2042,8 +2053,14 @@ class Item
 
                $uids = Tag::getUIDListByURIId($item['uri-id']);
                foreach ($uids as $uid) {
-                       $stored = self::storeForUserByUriId($item['uri-id'], $uid);
-                       Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
+                       if (Contact::isSharing($item['author-id'], $uid)) {
+                               $fields = [];
+                       } else {
+                               $fields = ['post-type' => self::PT_TAG];
+                       }
+
+                       $stored = self::storeForUserByUriId($item['uri-id'], $uid, $fields);
+                       Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'fields' => $fields, 'stored' => $stored]);
                }
        }
 
@@ -2211,9 +2228,10 @@ class Item
         *
         * @param integer $uri_id URI-ID of the given item
         * @param integer $uid    The user that will receive the item entry
+        * @param array   $fields Additional fields to be stored
         * @return integer stored item id
         */
-       public static function storeForUserByUriId(int $uri_id, int $uid)
+       public static function storeForUserByUriId(int $uri_id, int $uid, array $fields = [])
        {
                $item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => 0]);
                if (!DBA::isResult($item)) {
@@ -2225,6 +2243,10 @@ class Item
                        return 0;
                }
 
+               $item['post-type'] = self::PT_STORED;
+
+               $item = array_merge($item, $fields);
+
                $stored = self::storeForUser($item, $uid);
                Logger::info('Public item stored for user', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
                return $stored;
@@ -2710,13 +2732,9 @@ class Item
                        'owner-id' => $owner_id, 'private' => $private, 'psid' => $psid];
                self::update($fields, ['id' => $item_id]);
 
-               self::updateThread($item_id);
-
                Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id);
 
-               /// @todo This code should be activated by the end of the year 2020
-               // See also "createActivityFromItem"
-               //Item::performActivity($item_id, 'announce', $uid);
+               Item::performActivity($item_id, 'announce', $uid);
 
                return false;
        }
@@ -3273,9 +3291,8 @@ class Item
                $fields = ['uid', 'guid', 'created', 'edited', 'commented', 'received', 'changed', 'post-type',
                        'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id', 'uri-id',
                        'deleted', 'origin', 'forum_mode', 'network', 'author-id', 'owner-id'];
-               $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
 
-               $item = self::selectFirst($fields, $condition);
+               $item = self::selectFirst($fields, ['id' => $itemid, 'gravity' => GRAVITY_PARENT]);
                if (!DBA::isResult($item)) {
                        return;
                }