]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Add common relationship methods to Model\ContactRelation
[friendica.git] / src / Model / Item.php
index 5cce6096fce86f5fc8b23e781742a183a47b7197..b6f8ffa3d1665e5950aa3b69e9dbb05a868fea50 100644 (file)
@@ -1554,9 +1554,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);
-                       }
+                       ContactRelation::store($parent['author-id'], $item['author-id'], $item['created']);
                }
 
                return $item;
@@ -1703,6 +1701,10 @@ class Item
                        'photo' => $item['owner-avatar'], 'network' => $item['network']];
                $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
 
+               // 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);
 
@@ -1965,7 +1967,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']));
 
@@ -1990,9 +1992,8 @@ class Item
         * 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 +2001,7 @@ class Item
 
                $uids = Tag::getUIDListByURIId($item['uri-id']);
                foreach ($uids as $uid) {
-                       $original['uri-id'] = $item['uri-id'];
-                       $original['gravity'] = $item['gravity'];
-                       $stored = self::storeForUser($original, $uid);
+                       $stored = self::storeForUserByUriId($item['uri-id'], $uid);
                        Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
                }
        }
@@ -2167,14 +2166,38 @@ 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
+        * @return integer stored item id
+        */
+       public static function storeForUserByUriId(int $uri_id, int $uid)
+       {
+               $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;
+               }
+
+               $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
         * @return integer stored item id
         * @throws \Exception
         */
-       public static function storeForUser(array $item, int $uid)
+       private static function storeForUser(array $item, int $uid)
        {
                if (self::exists(['uri-id' => $item['uri-id'], 'uid' => $uid])) {
                        Logger::info('Item already exists', ['uri-id' => $item['uri-id'], 'uid' => $uid]);
@@ -2185,6 +2208,8 @@ class Item
                unset($item['parent']);
                unset($item['mention']);
                unset($item['starred']);
+               unset($item['unseen']);
+               unset($item['psid']);
 
                $item['uid'] = $uid;
                $item['origin'] = 0;
@@ -3016,11 +3041,7 @@ class Item
                }
 
                if (!Item::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $uid])) {
-                       $parent_item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $item['parent-uri-id'], 'uid' => 0]);
-                       if (!empty($parent_item) && ($parent_item['private'] != self::PRIVATE)) {
-                               $stored = self::storeForUser($parent_item, $uid);
-                               Logger::info('Public item stored for user', ['uri-id' => $parent_item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
-                       }
+                       $stored = self::storeForUserByUriId($item['parent-uri-id'], $uid);
                }
 
                // Retrieves the local post owner
@@ -3682,10 +3703,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;
                }
 
@@ -3696,9 +3719,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;
        }
 
@@ -3735,7 +3760,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)) {
@@ -3757,9 +3782,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;
                        }