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;
$t = Renderer::getMarkupTemplate("community.tpl");
$o = Renderer::replaceMacros($t, [
'$content' => '',
- '$header' => '',
+ '$header' => '',
]);
if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
$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');
$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'),
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])) {
}
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;
}
*/
protected static function getItems()
{
- $post = DBA::selectToArray('post-engagement', ['comments'], [], ['order' => ['comments' => true], 'limit' => [100, 1]]);
-
if (self::$content == self::WHATSHOT) {
- $comments = $post[0]['comments'] ?? 0;
if (!is_null(self::$accountType)) {
- $condition = ["`comments` >= ? AND `contact-type` = ?", $comments, self::$accountType];
+ $condition = ["`comments` >= ? AND `contact-type` = ?", self::getMedianComments(4), self::$accountType];
} else {
- $condition = ["`comments` >= ?", $comments];
+ $condition = ["`comments` >= ? AND `contact-type` != ?", self::getMedianComments(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` >= ? AND `author-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` IN (?, ?))))",
+ $cid, self::getMedianThreadScore($cid, 4), self::getMedianComments(3), 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 {
return $items;
}
+
+ private static function getMedianComments(int $divider): int
+ {
+ $cache_key = 'Channel:getHotPostsItemLimit:' . $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 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;
+ }
}