]> 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 39fb3cb54c1b5aac8690afcef40edbfba19b7c8c..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::isBlockedByUser($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,18 +1406,18 @@ class Item
                        return false;
                }
 
-               if (!empty($item['uid']) && Contact::isBlockedByUser($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;
                }
 
                // The causer is set during a thread completion, for example because of a reshare. It countains the responsible actor.
-               if (!empty($item['uid']) && !empty($item['causer-id']) && Contact::isBlockedByUser($item['causer-id'], $item['uid'])) {
+               if (!empty($item['uid']) && !empty($item['causer-id']) && Contact\User::isBlocked($item['causer-id'], $item['uid'])) {
                        Logger::notice('Causer is blocked by user', ['causer-link' => $item['causer-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]);
                        return false;
                }
 
-               if (!empty($item['uid']) && !empty($item['causer-id']) && ($item['parent-uri'] == $item['uri']) && Contact::isIgnoredByUser($item['causer-id'], $item['uid'])) {
+               if (!empty($item['uid']) && !empty($item['causer-id']) && ($item['parent-uri'] == $item['uri']) && Contact\User::isIgnored($item['causer-id'], $item['uid'])) {
                        Logger::notice('Causer is ignored by user', ['causer-link' => $item['causer-link'], 'uid' => $item['uid'], 'item-uri' => $item['uri']]);
                        return false;
                }
@@ -1554,9 +1551,7 @@ class Item
                        }
 
                        // Update the contact relations
-                       if ($item['author-id'] != $parent['author-id']) {
-                               DBA::update('contact-relation', ['last-interaction' => $item['created']], ['cid' => $parent['author-id'], 'relation-cid' => $item['author-id']], true);
-                       }
+                       Contact\Relation::store($parent['author-id'], $item['author-id'], $item['created']);
                }
 
                return $item;
@@ -1580,6 +1575,8 @@ class Item
                        return GRAVITY_COMMENT;
                } elseif ($activity->match($item['verb'], Activity::FOLLOW)) {
                        return GRAVITY_ACTIVITY;
+               } elseif ($activity->match($item['verb'], Activity::ANNOUNCE)) {
+                       return GRAVITY_ACTIVITY;
                }
                Logger::info('Unknown gravity for verb', ['verb' => $item['verb']]);
                return GRAVITY_UNKNOWN;   // Should not happen
@@ -1703,6 +1700,15 @@ 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']);
+
                // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
                $item["contact-id"] = self::contactId($item);
 
@@ -1796,6 +1802,9 @@ class Item
                // It is mainly used in the "post_local" hook.
                unset($item['api_source']);
 
+               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']);
@@ -1965,7 +1974,7 @@ class Item
                check_user_notification($current_post);
 
                // Distribute items to users who subscribed to their tags
-               self::distributeByTags($item, $orig_item);
+               self::distributeByTags($item);
 
                $transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin']));
 
@@ -1986,13 +1995,57 @@ class Item
                return $current_post;
        }
 
+       /**
+        * 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.
+        * Changing the owner helps in grouping forum posts.
+        *
+        * @param array $item
+        * @return void
+        */
+       private static function setOwnerforResharedItem(array $item)
+       {
+               $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;
+               }
+
+               $author = Contact::selectFirst(['url', 'contact-type'], ['id' => $item['author-id']]);
+               if (!DBA::isResult($author)) {
+                       Logger::error('Author not found', ['id' => $item['author-id']]);
+                       return;
+               }
+
+               $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;
+               }
+
+               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;
+                       }
+                       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]);
+       }
+
        /**
         * Distribute the given item to users who subscribed to their tags
         *
         * @param array $item     Processed item
-        * @param array $original Original item
         */
-       private static function distributeByTags(array $item, array $original)
+       private static function distributeByTags(array $item)
        {
                if (($item['uid'] != 0) || ($item['gravity'] != GRAVITY_PARENT) || !in_array($item['network'], Protocol::FEDERATED)) {
                        return;
@@ -2000,9 +2053,14 @@ class Item
 
                $uids = Tag::getUIDListByURIId($item['uri-id']);
                foreach ($uids as $uid) {
-                       $original['uri-id'] = $item['uri-id'];
-                       $stored = self::storeForUser($original, $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]);
                }
        }
 
@@ -2166,7 +2224,36 @@ class Item
        }
 
        /**
-        * Store public items for the receivers
+        * Store a public item defined by their URI-ID for the given users
+        *
+        * @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, array $fields = [])
+       {
+               $item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => 0]);
+               if (!DBA::isResult($item)) {
+                       return 0;
+               }
+
+               if (($item['private'] == self::PRIVATE) || !in_array($item['network'], Protocol::FEDERATED)) {
+                       Logger::notice('Item is private or not from a federated network. It will not be stored for the user.', ['uri-id' => $uri_id, 'uid' => $uid, 'private' => $item['private'], 'network' => $item['network']]);
+                       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;
+       }
+
+       /**
+        * Store a public item array for the given users
         *
         * @param array   $item   The item entry that will be stored
         * @param integer $uid    The user that will receive the item entry
@@ -2184,24 +2271,28 @@ class Item
                unset($item['parent']);
                unset($item['mention']);
                unset($item['starred']);
+               unset($item['unseen']);
+               unset($item['psid']);
 
                $item['uid'] = $uid;
                $item['origin'] = 0;
                $item['wall'] = 0;
 
-               if ($item['uri'] == $item['parent-uri']) {
+               if ($item['gravity'] == GRAVITY_PARENT) {
                        $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']);
                } else {
                        $contact = Contact::getByURLForUser($item['author-link'], $uid, false, ['id']);
                }
 
-               if (!empty($item['contact-id'])) {
+               if (!empty($contact['id'])) {
                        $item['contact-id'] = $contact['id'];
                } else {
                        // Shouldn't happen at all
+                       Logger::warning('contact-id could not be fetched', ['uid' => $uid, 'item' => $item]);
                        $self = DBA::selectFirst('contact', ['id'], ['self' => true, 'uid' => $uid]);
                        if (!DBA::isResult($self)) {
                                // Shouldn't happen even less
+                               Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]);
                                return 0;
                        }
                        $item['contact-id'] = $self['id'];
@@ -2210,7 +2301,7 @@ class Item
                /// @todo Handling of "event-id"
 
                $notify = false;
-               if ($item['uri'] == $item['parent-uri']) {
+               if ($item['gravity'] == GRAVITY_PARENT) {
                        $contact = DBA::selectFirst('contact', [], ['id' => $item['contact-id'], 'self' => false]);
                        if (DBA::isResult($contact)) {
                                $notify = self::isRemoteSelf($contact, $item);
@@ -2641,10 +2732,10 @@ 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);
 
+               Item::performActivity($item_id, 'announce', $uid);
+
                return false;
        }
 
@@ -2974,11 +3065,12 @@ class Item
         *
         * Toggle activities as like,dislike,attend of an item
         *
-        * @param string $item_id
+        * @param int $item_id
         * @param string $verb
         *            Activity verb. One of
         *            like, unlike, dislike, undislike, attendyes, unattendyes,
-        *            attendno, unattendno, attendmaybe, unattendmaybe
+        *            attendno, unattendno, attendmaybe, unattendmaybe,
+        *            announce, unannouce
         * @return bool
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
@@ -2986,15 +3078,15 @@ class Item
         *            array $arr
         *            'post_id' => ID of posted item
         */
-       public static function performActivity($item_id, $verb)
+       public static function performActivity(int $item_id, string $verb, int $uid)
        {
-               if (!Session::isAuthenticated()) {
+               if (empty($uid)) {
                        return false;
                }
 
-               Logger::log('like: verb ' . $verb . ' item ' . $item_id);
+               Logger::notice('Start create activity', ['verb' => $verb, 'item' => $item_id, 'user' => $uid]);
 
-               $item = self::selectFirst(self::ITEM_FIELDLIST, ['`id` = ? OR `uri` = ?', $item_id, $item_id]);
+               $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
                        Logger::log('like: unknown item ' . $item_id);
                        return false;
@@ -3002,44 +3094,35 @@ class Item
 
                $item_uri = $item['uri'];
 
-               $uid = $item['uid'];
-               if (($uid == 0) && local_user()) {
-                       $uid = local_user();
+               if (!in_array($item['uid'], [0, $uid])) {
+                       return false;
                }
 
-               if (!Security::canWriteToUserWall($uid)) {
-                       Logger::log('like: unable to write on wall ' . $uid);
-                       return false;
+               if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
+                       $stored = self::storeForUserByUriId($item['parent-uri-id'], $uid);
+                       if (($item['parent-uri-id'] == $item['uri-id']) && !empty($stored)) {
+                               $item = self::selectFirst(self::ITEM_FIELDLIST, ['id' => $stored]);
+                               if (!DBA::isResult($item)) {
+                                       Logger::info('Could not fetch just created item - should not happen', ['stored' => $stored, 'uid' => $uid, 'item-uri' => $item_uri]);
+                                       return false;
+                               }
+                       }
                }
 
                // Retrieves the local post owner
-               $owner_self_contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'self' => true]);
-               if (!DBA::isResult($owner_self_contact)) {
-                       Logger::log('like: unknown owner ' . $uid);
+               $owner = User::getOwnerDataById($uid);
+               if (empty($owner)) {
+                       Logger::info('Empty owner for user', ['uid' => $uid]);
                        return false;
                }
 
                // Retrieve the current logged in user's public contact
-               $author_id = public_contact();
-
-               $author_contact = DBA::selectFirst('contact', ['url'], ['id' => $author_id]);
-               if (!DBA::isResult($author_contact)) {
-                       Logger::log('like: unknown author ' . $author_id);
+               $author_id = Contact::getIdForURL($owner['url']);
+               if (empty($author_id)) {
+                       Logger::info('Empty public contact');
                        return false;
                }
 
-               // Contact-id is the uid-dependant author contact
-               if (local_user() == $uid) {
-                       $item_contact_id = $owner_self_contact['id'];
-               } else {
-                       $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid, false);
-                       $item_contact = DBA::selectFirst('contact', [], ['id' => $item_contact_id]);
-                       if (!DBA::isResult($item_contact)) {
-                               Logger::log('like: unknown item contact ' . $item_contact_id);
-                               return false;
-                       }
-               }
-
                $activity = null;
                switch ($verb) {
                        case 'like':
@@ -3066,8 +3149,12 @@ class Item
                        case 'unfollow':
                                $activity = Activity::FOLLOW;
                                break;
+                       case 'announce':
+                       case 'unannounce':
+                               $activity = Activity::ANNOUNCE;
+                               break;
                        default:
-                               Logger::log('like: unknown verb ' . $verb . ' for item ' . $item_id);
+                               Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
                                return false;
                }
 
@@ -3138,7 +3225,7 @@ class Item
                        'guid'          => System::createUUID(),
                        'uri'           => self::newURI($item['uid']),
                        'uid'           => $item['uid'],
-                       'contact-id'    => $item_contact_id,
+                       'contact-id'    => $owner['id'],
                        'wall'          => $item['wall'],
                        'origin'        => 1,
                        'network'       => Protocol::DFRN,
@@ -3204,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;
                }
@@ -3671,10 +3757,12 @@ class Item
         *
         * @return integer item id
         */
-       public static function fetchByLink($uri, $uid = 0)
+       public static function fetchByLink(string $uri, int $uid = 0)
        {
+               Logger::info('Trying to fetch link', ['uid' => $uid, 'uri' => $uri]);
                $item_id = self::searchByLink($uri, $uid);
                if (!empty($item_id)) {
+                       Logger::info('Link found', ['uid' => $uid, 'uri' => $uri, 'id' => $item_id]);
                        return $item_id;
                }
 
@@ -3685,9 +3773,11 @@ class Item
                }
 
                if (!empty($item_id)) {
+                       Logger::info('Link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $item_id]);
                        return $item_id;
                }
 
+               Logger::info('Link not found', ['uid' => $uid, 'uri' => $uri]);
                return 0;
        }
 
@@ -3724,7 +3814,7 @@ class Item
         *
         * @return array item array with data from the original item
         */
-       public static function addShareDataFromOriginal($item)
+       public static function addShareDataFromOriginal(array $item)
        {
                $shared = self::getShareArray($item);
                if (empty($shared)) {
@@ -3746,9 +3836,9 @@ class Item
                        }
 
                        // Otherwhise try to find (and possibly fetch) the item via the link. This should work for Diaspora and ActivityPub posts
-                       $id = self::fetchByLink($shared['link'], $uid);
+                       $id = self::fetchByLink($shared['link'] ?? '', $uid);
                        if (empty($id)) {
-                               Logger::info('Original item not found', ['url' => $shared['link'], 'callstack' => System::callstack()]);
+                               Logger::info('Original item not found', ['url' => $shared['link'] ?? '', 'callstack' => System::callstack()]);
                                return $item;
                        }