X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FConversation%2FChannel.php;h=42ca508be52787d019d6c70495504d008fe82f7b;hb=ca32134830a468c4e0a1de1e013e1deb5d4cbae6;hp=3503a96d46d680361c65e847b8c2592d991fa8d4;hpb=59a822ee872647b3680220ead282438c857d5f5b;p=friendica.git diff --git a/src/Module/Conversation/Channel.php b/src/Module/Conversation/Channel.php index 3503a96d46..42ca508be5 100644 --- a/src/Module/Conversation/Channel.php +++ b/src/Module/Conversation/Channel.php @@ -30,7 +30,7 @@ use Friendica\Content\Nav; use Friendica\Content\Text\HTML; use Friendica\Content\Widget; use Friendica\Content\Widget\TrendingTags; -use Friendica\Core\Logger; +use Friendica\Core\Cache\Enum\Duration; use Friendica\Core\Renderer; use Friendica\Database\DBA; use Friendica\DI; @@ -68,7 +68,7 @@ class Channel extends BaseModule $t = Renderer::getMarkupTemplate("community.tpl"); $o = Renderer::replaceMacros($t, [ '$content' => '', - '$header' => '', + '$header' => '', ]); if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) { @@ -80,30 +80,30 @@ class Channel extends BaseModule $tabs = []; $tabs[] = [ - 'label' => DI::l10n()->t('Whats Hot'), - 'url' => 'channel/' . self::WHATSHOT, - 'sel' => self::$content == self::WHATSHOT ? 'active' : '', - 'title' => DI::l10n()->t('Posts with a lot of interactions'), - 'id' => 'channel-whatshot-tab', - 'accesskey' => 'h' + 'label' => DI::l10n()->t('For you'), + 'url' => 'channel/' . self::FORYOU, + 'sel' => self::$content == self::FORYOU ? 'active' : '', + 'title' => DI::l10n()->t('Posts from contacts you interact with and who interact with you'), + 'id' => 'channel-foryou-tab', + 'accesskey' => 'y' ]; $tabs[] = [ - 'label' => DI::l10n()->t('For you'), - 'url' => 'channel/' . self::FORYOU, - 'sel' => self::$content == self::FORYOU ? 'active' : '', - 'title' => DI::l10n()->t('Posts from contacts you interact with and who interact with you'), - 'id' => 'channel-foryou-tab', - 'accesskey' => 'y' + 'label' => DI::l10n()->t('Followers'), + 'url' => 'channel/' . self::FOLLOWERS, + 'sel' => self::$content == self::FOLLOWERS ? 'active' : '', + 'title' => DI::l10n()->t('Posts from your followers that you don\'t follow'), + 'id' => 'channel-followers-tab', + 'accesskey' => 'f' ]; $tabs[] = [ - 'label' => DI::l10n()->t('Followers'), - 'url' => 'channel/' . self::FOLLOWERS, - 'sel' => self::$content == self::FOLLOWERS ? 'active' : '', - 'title' => DI::l10n()->t('Posts from your followers that you don\'t follow'), - 'id' => 'channel-followers-tab', - 'accesskey' => 'f' + 'label' => DI::l10n()->t('Whats Hot'), + 'url' => 'channel/' . self::WHATSHOT, + 'sel' => self::$content == self::WHATSHOT ? 'active' : '', + 'title' => DI::l10n()->t('Posts with a lot of interactions'), + 'id' => 'channel-whatshot-tab', + 'accesskey' => 'h' ]; $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl'); @@ -130,7 +130,7 @@ class Channel extends BaseModule $query_parameters['max_id'] = $_GET['last_created']; } - $path_all = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : ''); + $path_all = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : ''); $path_no_sharer = $path . '?' . http_build_query(array_merge($query_parameters, ['no_sharer' => true])); DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_sharer.tpl'), [ '$title' => DI::l10n()->t('Own Contacts'), @@ -186,11 +186,11 @@ class Channel extends BaseModule protected function parseRequest() { self::$accountTypeString = $_GET['accounttype'] ?? $this->parameters['accounttype'] ?? ''; - self::$accountType = User::getAccountTypeByString(self::$accountTypeString); + self::$accountType = User::getAccountTypeByString(self::$accountTypeString); self::$content = $this->parameters['content'] ?? ''; if (!self::$content) { - self::$content = self::WHATSHOT; + self::$content = self::FORYOU; } if (!in_array(self::$content, [self::WHATSHOT, self::FORYOU, self::FOLLOWERS])) { @@ -214,15 +214,14 @@ class Channel extends BaseModule } if (!empty($_GET['item'])) { - $item = Post::selectFirst(['parent-uri-id'], ['id' => $_GET['item']]); + $item = Post::selectFirst(['parent-uri-id'], ['id' => $_GET['item']]); self::$item_id = $item['parent-uri-id'] ?? 0; } else { self::$item_id = 0; } - Logger::debug('Blubb', ['get' => $_GET]); - self::$min_id = $_GET['min_id'] ?? null; - self::$max_id = $_GET['max_id'] ?? null; + self::$min_id = $_GET['min_id'] ?? null; + self::$max_id = $_GET['max_id'] ?? null; self::$max_id = $_GET['last_created'] ?? self::$max_id; } @@ -238,32 +237,31 @@ class Channel extends BaseModule protected static function getItems() { if (self::$content == self::WHATSHOT) { - $post = DBA::selectToArray('post-engagement', ['comments'], [], ['order' => ['comments' => true], 'limit' => [DI::config()->get('channel', 'hot_posts_item_limit'), 1]]); - $comments = $post[0]['comments'] ?? 0; if (!is_null(self::$accountType)) { - $condition = ["`comments` >= ? AND `contact-type` = ?", $comments, self::$accountType]; + $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` = ?", self::getMedianComments(4), self::getMedianActivities(4), self::$accountType]; } else { - $condition = ["`comments` >= ?", $comments]; + $condition = ["(`comments` >= ? OR `activities` >= ?) AND `contact-type` != ?", self::getMedianComments(4), self::getMedianActivities(4), Contact::TYPE_COMMUNITY]; } } elseif (self::$content == self::FORYOU) { $cid = Contact::getPublicIdByUserId(DI::userSession()->getLocalUserId()); - if (!is_null(self::$accountType)) { - $condition = ["`author-id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `thread-score` > ? AND `relation-thread-score` > ?) AND `contact-type` = ?", $cid, 0, 0, self::$accountType]; - } else { - $condition = ["`author-id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `thread-score` > ? AND `relation-thread-score` > ?)", $cid, 0, 0]; - } + + $condition = ["(`author-id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `thread-score` > ?) OR + ((`comments` >= ? OR `activities` >= ?) AND `author-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` IN (?, ?))) OR + ( `author-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` IN (?, ?) AND `notify_new_posts`)))", + $cid, self::getMedianThreadScore($cid, 4), self::getMedianComments(4), self::getMedianActivities(4), DI::userSession()->getLocalUserId(), Contact::FRIEND, Contact::SHARING, + DI::userSession()->getLocalUserId(), Contact::FRIEND, Contact::SHARING]; } elseif (self::$content == self::FOLLOWERS) { - if (!is_null(self::$accountType)) { - $condition = ["`author-id` IN (SELECT `pid` FROM `account-user-view` WHERE uid` = ? AND `rel` = ?) AND `contact-type` = ?", DI::userSession()->getLocalUserId(), Contact::FOLLOWER, self::$accountType]; - } else { - $condition = ["`author-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", DI::userSession()->getLocalUserId(), Contact::FOLLOWER]; - } + $condition = ["`author-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", DI::userSession()->getLocalUserId(), Contact::FOLLOWER]; + } + + if ((self::$content != self::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)) { - // @todo $condition[0] .= " AND `uri-id` = ?"; $condition[] = self::$item_id; } else { @@ -301,4 +299,61 @@ class Channel extends BaseModule return $items; } + + private static function getMedianComments(int $divider): int + { + $cache_key = 'Channel:getMedianComments:' . $divider; + $comments = DI::cache()->get($cache_key); + if (!empty($comments)) { + return $comments; + } + + $limit = DBA::count('post-engagement', ["`contact-type` != ? AND `comments` > ?", Contact::TYPE_COMMUNITY, 0]) / $divider; + $post = DBA::selectToArray('post-engagement', ['comments'], ["`contact-type` != ?", Contact::TYPE_COMMUNITY, 0], ['order' => ['comments' => true], 'limit' => [$limit, 1]]); + $comments = $post[0]['comments'] ?? 0; + if (empty($comments)) { + return 0; + } + + DI::cache()->set($cache_key, $comments, Duration::HOUR); + return $comments; + } + + private static function getMedianActivities(int $divider): int + { + $cache_key = 'Channel:getMedianActivities:' . $divider; + $activities = DI::cache()->get($cache_key); + if (!empty($activities)) { + return $activities; + } + + $limit = DBA::count('post-engagement', ["`contact-type` != ? AND `activities` > ?", Contact::TYPE_COMMUNITY, 0]) / $divider; + $post = DBA::selectToArray('post-engagement', ['activities'], ["`contact-type` != ?", Contact::TYPE_COMMUNITY, 0], ['order' => ['activities' => true], 'limit' => [$limit, 1]]); + $activities = $post[0]['activities'] ?? 0; + if (empty($activities)) { + return 0; + } + + DI::cache()->set($cache_key, $activities, Duration::HOUR); + return $activities; + } + + private static function getMedianThreadScore(int $cid, int $divider): int + { + $cache_key = 'Channel:getThreadScore:' . $cid . ':' . $divider; + $score = DI::cache()->get($cache_key); + if (!empty($score)) { + return $score; + } + + $limit = DBA::count('contact-relation', ["`cid` = ? AND `thread-score` > ?", $cid, 0]) / $divider; + $relation = DBA::selectToArray('contact-relation', ['thread-score'], ['cid' => $cid], ['order' => ['thread-score' => true], 'limit' => [$limit, 1]]); + $score = $relation[0]['thread-score'] ?? 0; + if (empty($score)) { + return 0; + } + + DI::cache()->set($cache_key, $score, Duration::HOUR); + return $score; + } }