From ca9f8e7420d925fb316f8841f338a4583d94fe51 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 23 Jul 2020 03:26:54 +0000 Subject: [PATCH 1/2] New function "Item::storeForUserByUriId" --- mod/item.php | 2 +- src/Model/Item.php | 40 +++++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/mod/item.php b/mod/item.php index c4d7231c2..f6157f3db 100644 --- a/mod/item.php +++ b/mod/item.php @@ -139,7 +139,7 @@ function item_post(App $a) { // When commenting on a public post then store the post for the current user // This enables interaction like starring and saving into folders if ($toplevel_item['uid'] == 0) { - $stored = Item::storeForUser($toplevel_item, local_user()); + $stored = Item::storeForUserByUriId($toplevel_item['uri-id'], local_user()); Logger::info('Public item stored for user', ['uri-id' => $toplevel_item['uri-id'], 'uid' => $uid, 'stored' => $stored]); if ($stored) { $toplevel_item = Item::selectFirst([], ['id' => $stored]); diff --git a/src/Model/Item.php b/src/Model/Item.php index 5cce6096f..98e4cdf30 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1965,7 +1965,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 +1990,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 +1999,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 +2164,33 @@ 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 (empty($item) || ($item['private'] != self::PRIVATE)) { + 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 +2201,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 +3034,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 From d72d59bf9e59bf97e5cf77947c37900e7b56c3e6 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 28 Jul 2020 06:42:12 +0000 Subject: [PATCH 2/2] Logging and check for network added --- src/Model/Item.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index a4ba41a69..6567e5e26 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2175,7 +2175,12 @@ class Item public static function storeForUserByUriId(int $uri_id, int $uid) { $item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => 0]); - if (empty($item) || ($item['private'] != self::PRIVATE)) { + 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; }