From: Roland Häder Date: Wed, 18 Oct 2023 20:36:08 +0000 (+0200) Subject: Reversed: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=dd8397effd67742ef0c303228bf283add21e2200;p=friendica.git Reversed: - ops? --- diff --git a/src/Module/Conversation/Channel.php b/src/Module/Conversation/Channel.php index 95eb160155..893598f55b 100644 --- a/src/Module/Conversation/Channel.php +++ b/src/Module/Conversation/Channel.php @@ -176,202 +176,7 @@ class Channel extends Timeline throw new HTTPException\BadRequestException($this->l10n->t('Channel not available.')); } - if (!empty($request['item'])) { - $item = Post::selectFirst(['parent-uri-id'], ['id' => $request['item']]); - self::$item_id = $item['parent-uri-id'] ?? 0; - } else { - self::$item_id = 0; - } - - self::$min_id = $request['min_id'] ?? null; - self::$max_id = $request['last_created'] ?? $request['max_id'] ?? null; - } - - /** - * Computes the displayed items. - * - * Community pages have a restriction on how many successive posts by the same author can show on any given page, - * so we may have to retrieve more content beyond the first query - * - * @return array - * @throws \Exception - */ - protected function getItems(array $request) - { - if (self::$content == ChannelEntity::WHATSHOT) { - if (!is_null(self::$accountType)) { - $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` = ?", $this->getMedianComments(4), $this->getMedianActivities(4), self::$accountType]; - } else { - $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` != ?", $this->getMedianComments(4), $this->getMedianActivities(4), Contact::TYPE_COMMUNITY]; - } - } elseif (self::$content == ChannelEntity::FORYOU) { - $cid = Contact::getPublicIdByUserId($this->session->getLocalUserId()); - - $condition = [ - "(`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `relation-thread-score` > ?) OR - ((`comments` >= ? OR `activities` >= ?) AND `owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ?)) OR - (`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` IN (?, ?) AND `notify_new_posts`)))", - $cid, $this->getMedianRelationThreadScore($cid, 4), $this->getMedianComments(4), $this->getMedianActivities(4), $cid, - $this->session->getLocalUserId(), Contact::FRIEND, Contact::SHARING - ]; - } elseif (self::$content == self::FOLLOWERS) { - $condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", $this->session->getLocalUserId(), Contact::FOLLOWER]; - } elseif (self::$content == self::SHARERSOFSHARERS) { - $cid = Contact::getPublicIdByUserId($this->session->getLocalUserId()); - - // @todo Suggest posts from contacts that are followed most by our followers - $condition = [ - "`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `last-interaction` > ? - AND `relation-cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ? AND `relation-thread-score` >= ?) - AND NOT `cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ?))", - DateTimeFormat::utc('now - ' . $this->config->get('channel', 'sharer_interaction_days') .' day'), $cid, $this->getMedianRelationThreadScore($cid, 4), $cid - ]; - } elseif (self::$content == self::IMAGE) { - $condition = ["`media-type` & ?", 1]; - } elseif (self::$content == ChannelEntity::VIDEO) { - $condition = ["`media-type` & ?", 2]; - } elseif (self::$content == ChannelEntity::AUDIO) { - $condition = ["`media-type` & ?", 4]; - } elseif (self::$content == ChannelEntity::LANGUAGE) { - $condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", $this->l10n->convertCodeForLanguageDetection(User::getLanguageCode($this->session->getLocalUserId()))]; - } - - if (self::$content != ChannelEntity::LANGUAGE) { - $condition = $this->addLanguageCondition($condition); - } - - $condition[0] .= " AND NOT EXISTS(SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `post-engagement`.`owner-id` AND (`ignored` OR `blocked` OR `collapsed`))"; - $condition[] = $this->session->getLocalUserId(); - - if ((self::$content != ChannelEntity::WHATSHOT) && !is_null(self::$accountType)) { - $condition[0] .= " AND `contact-type` = ?"; - $condition[] = self::$accountType; - } - - $params = ['order' => ['created' => true], 'limit' => self::$itemsPerPage]; - - if (!empty(self::$item_id)) { - $condition[0] .= " AND `uri-id` = ?"; - $condition[] = self::$item_id; - } else { - if (!empty($request['no_sharer'])) { - $condition[0] .= " AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ? AND `post-user`.`uri-id` = `post-engagement`.`uri-id`)"; - $condition[] = $this->session->getLocalUserId(); - } - - if (isset(self::$max_id)) { - $condition[0] .= " AND `created` < ?"; - $condition[] = self::$max_id; - } - - if (isset(self::$min_id)) { - $condition[0] .= " AND `created` > ?"; - $condition[] = self::$min_id; - - // Previous page case: we want the items closest to min_id but for that we need to reverse the query order - if (!isset(self::$max_id)) { - $params['order']['created'] = false; - } - } - } - - $items = $this->database->selectToArray('post-engagement', ['uri-id', 'created'], $condition, $params); - if (empty($items)) { - return []; - } - - // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order - if (empty(self::$item_id) && isset(self::$min_id) && !isset(self::$max_id)) { - $items = array_reverse($items); - } - - Item::update(['unseen' => false], ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'uri-id' => array_column($items, 'uri-id')]); - - return $items; - } - - private function addLanguageCondition(array $condition): array - { - $conditions = []; - $languages = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId())]); - $languages = $this->l10n->convertForLanguageDetection($languages); - foreach ($languages as $language) { - $conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?"; - $condition[] = $language; - } - if (!empty($conditions)) { - $condition[0] .= " AND (`language` IS NULL OR " . implode(' OR ', $conditions) . ")"; - } - return $condition; - } - - private function getMedianComments(int $divider): int - { - $languages = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId())]); - $cache_key = 'Channel:getMedianComments:' . $divider . ':' . implode(':', $languages); - $comments = $this->cache->get($cache_key); - if (!empty($comments)) { - return $comments; - } - - $condition = ["`contact-type` != ? AND `comments` > ?", Contact::TYPE_COMMUNITY, 0]; - $condition = $this->addLanguageCondition($condition); - - $limit = $this->database->count('post-engagement', $condition) / $divider; - $post = $this->database->selectToArray('post-engagement', ['comments'], $condition, ['order' => ['comments' => true], 'limit' => [$limit, 1]]); - $comments = $post[0]['comments'] ?? 0; - if (empty($comments)) { - return 0; - } - - $this->cache->set($cache_key, $comments, Duration::HALF_HOUR); - $this->logger->debug('Calculated median comments', ['divider' => $divider, 'languages' => $languages, 'median' => $comments]); - return $comments; - } - - private function getMedianActivities(int $divider): int - { - $languages = $this->pConfig->get($this->session->getLocalUserId(), 'channel', 'languages', [User::getLanguageCode($this->session->getLocalUserId())]); - $cache_key = 'Channel:getMedianActivities:' . $divider . ':' . implode(':', $languages); - $activities = $this->cache->get($cache_key); - if (!empty($activities)) { - return $activities; - } - - $condition = ["`contact-type` != ? AND `activities` > ?", Contact::TYPE_COMMUNITY, 0]; - $condition = $this->addLanguageCondition($condition); - - $limit = $this->database->count('post-engagement', $condition) / $divider; - $post = $this->database->selectToArray('post-engagement', ['activities'], $condition, ['order' => ['activities' => true], 'limit' => [$limit, 1]]); - $activities = $post[0]['activities'] ?? 0; - if (empty($activities)) { - return 0; - } - - $this->cache->set($cache_key, $activities, Duration::HALF_HOUR); - $this->logger->debug('Calculated median activities', ['divider' => $divider, 'languages' => $languages, 'median' => $activities]); - return $activities; - } - - private function getMedianRelationThreadScore(int $cid, int $divider): int - { - $cache_key = 'Channel:getThreadScore:' . $cid . ':' . $divider; - $score = $this->cache->get($cache_key); - if (!empty($score)) { - return $score; - } - - $condition = ["`relation-cid` = ? AND `relation-thread-score` > ?", $cid, 0]; - - $limit = $this->database->count('contact-relation', $condition) / $divider; - $relation = $this->database->selectToArray('contact-relation', ['relation-thread-score'], $condition, ['order' => ['relation-thread-score' => true], 'limit' => [$limit, 1]]); - $score = $relation[0]['relation-thread-score'] ?? 0; - if (empty($score)) { - return 0; - } - - $this->cache->set($cache_key, $score, Duration::HALF_HOUR); - $this->logger->debug('Calculated median score', ['cid' => $cid, 'divider' => $divider, 'median' => $score]); - return $score; + $this->maxId = $request['last_created'] ?? $this->maxId; + $this->minId = $request['first_created'] ?? $this->minId; } }