diff --git a/mod/network.php b/mod/network.php index 3311a796a..f847e6757 100644 --- a/mod/network.php +++ b/mod/network.php @@ -271,7 +271,7 @@ function networkConversation(App $a, $items, Pager $pager, $mode, $update, $orde $a->page_contact = $a->contact; if (!is_array($items)) { - Logger::log("Expecting items to be an array. Got " . print_r($items, true)); + Logger::info('Expecting items to be an array.', ['items' => $items]); $items = []; } @@ -541,7 +541,6 @@ function networkThreadedView(App $a, $update, $parent) } $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : ''); - $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : ''); if ($gid) { $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]); @@ -739,78 +738,6 @@ function networkThreadedView(App $a, $update, $parent) ); } - // Only show it when unfiltered (no groups, no networks, ...) - if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) { - if (DBA::isResult($r)) { - $top_limit = current($r)['order_date']; - $bottom_limit = end($r)['order_date']; - if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) { - $_SESSION['network_last_top_limit'] = $top_limit; - } - } else { - $top_limit = $bottom_limit = DateTimeFormat::utcNow(); - } - - // When checking for updates we need to fetch from the newest date to the newest date before - // Only do this, when the last stored date isn't too long ago (10 times the update interval) - $browser_update = DI::pConfig()->get(local_user(), 'system', 'update_interval', 40000) / 1000; - - if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) && - (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) && - ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) { - $bottom_limit = $_SESSION['network_last_date']; - } - $_SESSION['network_last_date'] = Session::get('network_last_top_limit', $top_limit); - $_SESSION['network_last_date_timestamp'] = time(); - - if ($last_date > $top_limit) { - $top_limit = $last_date; - } elseif ($pager->getPage() == 1) { - // Highest possible top limit when we are on the first page - $top_limit = DateTimeFormat::utcNow(); - } - - // Handle bad performance situations when the distance between top and bottom is too high - // See issue https://github.com/friendica/friendica/issues/8619 - if (strtotime($top_limit) - strtotime($bottom_limit) > 86400) { - // Set the bottom limit to one day in the past at maximum - $bottom_limit = DateTimeFormat::utc(date('c', strtotime($top_limit) - 86400)); - } - - $items = DBA::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date`, `author`.`url` AS `author-link` FROM `item` - STRAIGHT_JOIN (SELECT `uri-id` FROM `tag-search-view` WHERE `name` IN - (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `uid` = 0) AS `tag-search` - ON `item`.`uri-id` = `tag-search`.`uri-id` - STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id` - WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ? AND `item`.`gravity` = ? - AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets, - local_user(), $top_limit, $bottom_limit, GRAVITY_PARENT); - - $data = DBA::toArray($items); - - if (count($data) > 0) { - $tag_top_limit = current($data)['order_date']; - if ($_SESSION['network_last_date'] < $tag_top_limit) { - $_SESSION['network_last_date'] = $tag_top_limit; - } - - Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update); - $s = []; - foreach ($r as $item) { - $s[$item['uri']] = $item; - } - foreach ($data as $item) { - // Don't show hash tag posts from blocked or ignored contacts - $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)", - Strings::normaliseLink($item['author-link']), local_user()]; - if (!DBA::exists('contact', $condition)) { - $s[$item['uri']] = $item; - } - } - $r = $s; - } - } - $parents_str = ''; $date_offset = ''; diff --git a/src/Model/Item.php b/src/Model/Item.php index e31097f53..f7e7ae873 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1964,6 +1964,9 @@ class Item check_user_notification($current_post); + // Distribute items to users who subscribed to their tags + self::distributeByTags($item, $orig_item); + $transmit = $notify || ($item['visible'] && ($parent_origin || $item['origin'])); if ($transmit) { @@ -1983,6 +1986,26 @@ class Item return $current_post; } + /** + * 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) + { + if (($item['uid'] != 0) || ($item['gravity'] != GRAVITY_PARENT) || !in_array($item['network'], Protocol::FEDERATED)) { + return; + } + + $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]); + } + } + /** * Insert a new item content entry * @@ -2079,13 +2102,6 @@ class Item $origin = $item['origin']; - unset($item['id']); - unset($item['parent']); - unset($item['mention']); - unset($item['wall']); - unset($item['origin']); - unset($item['starred']); - $users = []; /// @todo add a field "pcid" in the contact table that referrs to the public contact id. @@ -2145,33 +2161,50 @@ class Item if ($origin_uid == $uid) { $item['diaspora_signed_text'] = $signed_text; } - self::storeForUser($itemid, $item, $uid); + self::storeForUser($item, $uid); } } /** * Store public items for the receivers * - * @param integer $itemid Item ID that should be added * @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 */ - private static function storeForUser($itemid, $item, $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]); + return 0; + } + + unset($item['id']); + unset($item['parent']); + unset($item['mention']); + unset($item['starred']); + $item['uid'] = $uid; $item['origin'] = 0; $item['wall'] = 0; + if ($item['uri'] == $item['parent-uri']) { - $item['contact-id'] = Contact::getIdForURL($item['owner-link'], $uid); + $contact = Contact::getByURLForUser($item['owner-link'], $uid, false, ['id']); } else { - $item['contact-id'] = Contact::getIdForURL($item['author-link'], $uid); + $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)) { - return; + // Shouldn't happen even less + Logger::warning('self contact could not be fetched', ['uid' => $uid, 'item' => $item]); + return 0; } $item['contact-id'] = $self['id']; } @@ -2189,10 +2222,11 @@ class Item $distributed = self::insert($item, $notify, true); if (!$distributed) { - Logger::info("Distributed public item wasn't stored", ['id' => $itemid, 'user' => $uid]); + Logger::info("Distributed public item wasn't stored", ['uri-id' => $item['uri-id'], 'user' => $uid]); } else { - Logger::info('Distributed public item was stored', ['id' => $itemid, 'user' => $uid, 'stored' => $distributed]); + Logger::info('Distributed public item was stored', ['uri-id' => $item['uri-id'], 'user' => $uid, 'stored' => $distributed]); } + return $distributed; } /** diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 3424a2377..a48f2cb92 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -536,5 +536,41 @@ class Tag } return Strings::startsWithChars($tag, $tag_chars); - } + } + + /** + * Fetch user who subscribed to the given tag + * + * @param string $tag + * @return array User list + */ + private static function getUIDListByTag(string $tag) + { + $uids = []; + $searches = DBA::select('search', ['uid'], ['term' => $tag]); + while ($search = DBA::fetch($searches)) { + $uids[] = $search['uid']; + } + DBA::close($searches); + + return $uids; + } + + /** + * Fetch user who subscribed to the tags of the given item + * + * @param integer $uri_id + * @return array User list + */ + public static function getUIDListByURIId(int $uri_id) + { + $uids = []; + $tags = self::getByURIId($uri_id, [self::HASHTAG]); + + foreach ($tags as $tag) { + $uids = array_merge($uids, self::getUIDListByTag(self::TAG_CHARACTER[self::HASHTAG] . $tag['name'])); + } + + return array_unique($uids); + } }