use Friendica\BaseModule;
use Friendica\Content\Conversation\Collection\Timelines;
use Friendica\Content\Conversation\Entity\Channel as ChannelEntity;
+use Friendica\Content\Conversation\Entity\Community;
+use Friendica\Content\Conversation\Entity\UserDefinedChannel as UserDefinedChannelEntity;
use Friendica\Content\Conversation\Repository\UserDefinedChannel;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Model\Post\Engagement;
-use Friendica\Model\Verb;
use Friendica\Module\Response;
use Friendica\Protocol\Activity;
use Friendica\Util\DateTimeFormat;
/** @var bool */
protected $update;
/** @var bool */
+ protected $ping;
+ /** @var bool */
protected $raw;
+ /** @var string */
+ protected $order;
+ /** @var string */
+ protected $network;
/** @var App\Mode $mode */
protected $mode;
$this->itemUriId = 0;
}
+ $this->order = 'created';
+
$this->minId = $request['min_id'] ?? null;
$this->maxId = $request['max_id'] ?? null;
$this->raw = !empty($request['mode']) && ($request['mode'] == 'raw');
}
+ protected function setMaxMinByOrder(array $request)
+ {
+ switch ($this->order) {
+ case 'received':
+ $this->maxId = $request['last_received'] ?? $this->maxId;
+ $this->minId = $request['first_received'] ?? $this->minId;
+ break;
+ case 'created':
+ $this->maxId = $request['last_created'] ?? $this->maxId;
+ $this->minId = $request['first_created'] ?? $this->minId;
+ break;
+ case 'uriid':
+ $this->maxId = $request['last_uriid'] ?? $this->maxId;
+ $this->minId = $request['first_uriid'] ?? $this->minId;
+ break;
+ default:
+ $this->order = 'commented';
+ $this->maxId = $request['last_commented'] ?? $this->maxId;
+ $this->minId = $request['first_commented'] ?? $this->minId;
+ }
+ }
+
protected function getNoSharerWidget(string $base): string
{
$path = $this->selectedTab;
+
+ $query_parameters = [];
if (!empty($this->accountTypeString)) {
- $path .= '/' . $this->accountTypeString;
+ $query_parameters['accounttype'] = $this->accountTypeString;
}
- $query_parameters = [];
-
if (!empty($this->minId)) {
$query_parameters['min_id'] = $this->minId;
}
* @return array
* @throws \Exception
*/
- protected function getChannelItems()
+ protected function getChannelItems(array $request)
{
- $items = $this->getRawChannelItems();
+ $items = $this->getRawChannelItems($request);
+ $total = min(count($items), $this->itemsPerPage);
$contacts = $this->database->selectToArray('user-contact', ['cid'], ['channel-frequency' => Contact\User::FREQUENCY_REDUCED, 'cid' => array_column($items, 'owner-id')]);
$reduced = array_column($contacts, 'cid');
$owner_posts = [];
$selected_items = [];
- while (count($selected_items) < $this->itemsPerPage && ++$count < 50 && count($items) > 0) {
- $maxposts = round((count($items) / $this->itemsPerPage) * $maxpostperauthor);
- $minId = $items[array_key_first($items)]['created'];
- $maxId = $items[array_key_last($items)]['created'];
+ while (count($selected_items) < $total && ++$count < 50 && count($items) > 0) {
+ $maxposts = round((count($items) / $total) * $maxpostperauthor);
+ $minId = $items[array_key_first($items)][$this->order];
+ $maxId = $items[array_key_last($items)][$this->order];
foreach ($items as $item) {
if (!in_array($item['owner-id'], $reduced)) {
$this->maxId = $maxId;
}
- if (count($selected_items) < $this->itemsPerPage) {
- $items = $this->getRawChannelItems();
+ if (count($selected_items) < $total) {
+ $items = $this->getRawChannelItems($request);
}
}
} else {
* @return array
* @throws \Exception
*/
- private function getRawChannelItems()
+ private function getRawChannelItems(array $request)
{
$uid = $this->session->getLocalUserId();
+ $table = 'post-engagement';
+
if ($this->selectedTab == ChannelEntity::WHATSHOT) {
if (!is_null($this->accountType)) {
$condition = ["(`comments` > ? OR `activities` > ?) AND `contact-type` = ?", $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $this->accountType];
$cid, $this->getMedianRelationThreadScore($cid, 4), $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $cid,
$uid, Contact\User::FREQUENCY_ALWAYS
];
+ } elseif ($this->selectedTab == ChannelEntity::DISCOVER) {
+ $cid = Contact::getPublicIdByUserId($uid);
+
+ $condition = [
+ "`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND NOT `follows`) AND
+ (`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND NOT `follows` AND `relation-thread-score` > ?) OR
+ `owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `cid` = ? AND `relation-thread-score` > ?) OR
+ ((`comments` >= ? OR `activities` >= ?) AND
+ (`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `cid` = ? AND `relation-thread-score` > ?)) OR
+ (`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `relation-thread-score` > ?))))",
+ $cid, $cid, $this->getMedianRelationThreadScore($cid, 4), $cid, $this->getMedianRelationThreadScore($cid, 4),
+ $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $cid, 0, $cid, 0
+ ];
+
} elseif ($this->selectedTab == ChannelEntity::FOLLOWERS) {
$condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", $uid, Contact::FOLLOWER];
} elseif ($this->selectedTab == ChannelEntity::SHARERSOFSHARERS) {
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 ($this->selectedTab == ChannelEntity::QUIETSHARERS) {
+ $cid = Contact::getPublicIdByUserId($uid);
+
+ $condition = [
+ "`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ? AND `post-score` <= ?)",
+ $cid, $this->getMedianPostScore($cid, 2)
+ ];
} elseif ($this->selectedTab == ChannelEntity::IMAGE) {
$condition = ["`media-type` & ?", 1];
} elseif ($this->selectedTab == ChannelEntity::VIDEO) {
} elseif ($this->selectedTab == ChannelEntity::AUDIO) {
$condition = ["`media-type` & ?", 4];
} elseif ($this->selectedTab == ChannelEntity::LANGUAGE) {
- $condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", User::getLanguageCode($uid)];
- } elseif (is_numeric($this->selectedTab)) {
- $condition = $this->getUserChannelConditions($this->selectedTab, $uid);
+ $condition = ["`language` = ?", User::getLanguageCode($uid)];
+ } elseif (is_numeric($this->selectedTab) && !empty($channel = $this->channelRepository->selectById($this->selectedTab, $uid))) {
+ $condition = $this->getUserChannelConditions($channel, $uid);
+ if (in_array($channel->circle, [-3, -4, -5])) {
+ $table = 'post-searchindex-user-view';
+ $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
+ $orders = ['-3' => 'created', '-4' => 'received', '-5' => 'commented'];
+ $this->order = $orders[$channel->circle];
+ }
+ }
+
+ $this->setMaxMinByOrder($request);
+
+ if (!empty($this->network)) {
+ $condition = DBA::mergeConditions($condition, ['network' => $this->network]);
}
if (($this->selectedTab != ChannelEntity::LANGUAGE) && !is_numeric($this->selectedTab)) {
$condition = $this->addLanguageCondition($uid, $condition);
}
- $condition = DBA::mergeConditions($condition, ["(NOT `restricted` OR EXISTS(SELECT `id` FROM `post-user` WHERE `uid` = ? AND `uri-id` = `post-engagement`.`uri-id`))", $uid]);
+ $condition = DBA::mergeConditions($condition, ["(NOT `restricted` OR EXISTS(SELECT `id` FROM `post-user` WHERE `uid` = ? AND `uri-id` = `$table`.`uri-id`))", $uid]);
- $condition = DBA::mergeConditions($condition, ["NOT EXISTS(SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `post-engagement`.`owner-id` AND (`ignored` OR `blocked` OR `collapsed` OR `is-blocked` OR `channel-frequency` = ?))", $uid, Contact\User::FREQUENCY_NEVER]);
+ $condition = DBA::mergeConditions($condition, ["NOT EXISTS(SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `$table`.`owner-id` AND (`ignored` OR `blocked` OR `collapsed` OR `is-blocked` OR `channel-frequency` = ?))", $uid, Contact\User::FREQUENCY_NEVER]);
if (($this->selectedTab != ChannelEntity::WHATSHOT) && !is_null($this->accountType)) {
$condition = DBA::mergeConditions($condition, ['contact-type' => $this->accountType]);
}
- $params = ['order' => ['created' => true], 'limit' => $this->itemsPerPage];
+ $params = ['order' => [$this->order => true], 'limit' => $this->itemsPerPage];
if (!empty($this->itemUriId)) {
$condition = DBA::mergeConditions($condition, ['uri-id' => $this->itemUriId]);
} else {
if ($this->noSharer) {
- $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ? AND `post-user`.`uri-id` = `post-engagement`.`uri-id`)", $this->session->getLocalUserId()]);
+ $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ? AND `post-user`.`uri-id` = `$table`.`uri-id`)", $this->session->getLocalUserId()]);
}
if (isset($this->maxId)) {
- $condition = DBA::mergeConditions($condition, ["`created` < ?", $this->maxId]);
+ $condition = DBA::mergeConditions($condition, ["`$this->order` < ?", $this->maxId]);
}
if (isset($this->minId)) {
- $condition = DBA::mergeConditions($condition, ["`created` > ?", $this->minId]);
+ $condition = DBA::mergeConditions($condition, ["`$this->order` > ?", $this->minId]);
// Previous page case: we want the items closest to min_id but for that we need to reverse the query order
if (!isset($this->maxId)) {
- $params['order']['created'] = false;
+ $params['order'][$this->order] = false;
}
}
}
$items = [];
- $result = $this->database->select('post-engagement', ['uri-id', 'created', 'owner-id', 'comments', 'activities'], $condition, $params);
+ $fields = ['uri-id', 'owner-id', 'comments', 'activities'];
+ $fields[] = $this->order;
+ $result = $this->database->select($table, $fields, $condition, $params);
if ($this->database->errorNo()) {
throw new \Exception($this->database->errorMessage(), $this->database->errorNo());
}
return $items;
}
- private function getUserChannelConditions(int $id, int $uid): array
+ private function getUserChannelConditions(UserDefinedChannelEntity $channel, int $uid): array
{
- $channel = $this->channelRepository->selectById($id, $uid);
- if (empty($channel)) {
- return [];
- }
-
$condition = [];
if (!empty($channel->circle)) {
$condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` IN (?, ?))", $uid, Contact::SHARING, Contact::FRIEND];
} elseif ($channel->circle == -2) {
$condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", $uid, Contact::FOLLOWER];
- } elseif ($channel->circle == -3) {
- $condition = ["EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `uid` = ? AND `post-thread-user`.`uri-id` = `post-engagement`.`uri-id`)", $uid];
} elseif ($channel->circle > 0) {
$condition = DBA::mergeConditions($condition, ["`owner-id` IN (SELECT `pid` FROM `group_member` INNER JOIN `account-user-view` ON `group_member`.`contact-id` = `account-user-view`.`id` WHERE `gid` = ? AND `account-user-view`.`uid` = ?)", $channel->circle, $uid]);
}
}
if (!empty($channel->fullTextSearch)) {
- $search = $channel->fullTextSearch;
- foreach (Engagement::KEYWORDS as $keyword) {
- $search = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $search);
+ if (!empty($channel->includeTags)) {
+ $additional = $this->addIncludeTags($channel->includeTags);
+ } else {
+ $additional = '';
+ }
+
+ if (!empty($channel->excludeTags)) {
+ foreach (explode(',', mb_strtolower($channel->excludeTags)) as $tag) {
+ $additional .= ' -tag:' . $tag;
+ }
+ }
+
+ if (!empty($channel->mediaType)) {
+ $additional .= $this->addMediaTerms($channel->mediaType);
+ }
+
+ $additional .= $this->addLanguageSearchTerms($uid, $channel->languages);
+
+ if ($additional) {
+ $searchterms = '+(' . trim($channel->fullTextSearch) . ')' . $additional;
+ } else {
+ $searchterms = $channel->fullTextSearch;
+ }
+
+ $condition = DBA::mergeConditions($condition, ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", Engagement::escapeKeywords($searchterms)]);
+ } else {
+ if (!empty($channel->includeTags)) {
+ $search = explode(',', mb_strtolower($channel->includeTags));
+ $placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
+ $condition = DBA::mergeConditions($condition, array_merge(["`uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search));
+ }
+
+ if (!empty($channel->excludeTags)) {
+ $search = explode(',', mb_strtolower($channel->excludeTags));
+ $placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
+ $condition = DBA::mergeConditions($condition, array_merge(["NOT `uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search));
+ }
+
+ if (!empty($channel->mediaType)) {
+ $condition = DBA::mergeConditions($condition, ["`media-type` & ?", $channel->mediaType]);
}
- $condition = DBA::mergeConditions($condition, ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", $search]);
+
+ // For "addLanguageCondition" to work, the condition must not be empty
+ $condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages);
+ }
+
+ if (!is_null($channel->minSize)) {
+ $condition = DBA::mergeConditions($condition, ["`size` >= ?", $channel->minSize]);
}
- if (!empty($channel->includeTags)) {
- $search = explode(',', mb_strtolower($channel->includeTags));
- $placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
- $condition = DBA::mergeConditions($condition, array_merge(["`uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search));
+ if (!is_null($channel->maxSize)) {
+ $condition = DBA::mergeConditions($condition, ["`size` <= ?", $channel->maxSize]);
}
- if (!empty($channel->excludeTags)) {
- $search = explode(',', mb_strtolower($channel->excludeTags));
- $placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
- $condition = DBA::mergeConditions($condition, array_merge(["NOT `uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search));
+ return $condition;
+ }
+
+ private function addIncludeTags(string $includeTags): string
+ {
+ $tagterms = '';
+ foreach (explode(',', mb_strtolower($includeTags)) as $tag) {
+ $tagterms .= ' tag:' . $tag;
}
- if (!empty($channel->mediaType)) {
- $condition = DBA::mergeConditions($condition, ["`media-type` & ?", $channel->mediaType]);
+ if ($tagterms) {
+ return ' +(' . trim($tagterms) . ')';
+ } else {
+ return '';
}
+ }
- // For "addLanguageCondition" to work, the condition must not be empty
- $condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages);
+ private function addMediaTerms(int $mediaType): string
+ {
+ $mediaterms = '';
+ if ($mediaType & 1) {
+ $mediaterms .= ' media:image';
+ }
- return $condition;
+ if ($mediaType & 2) {
+ $mediaterms .= ' media:video';
+ }
+
+ if ($mediaType & 4) {
+ $mediaterms .= ' media:audio';
+ }
+
+ if ($mediaterms) {
+ return ' +(' . trim($mediaterms) . ')';
+ } else {
+ return '';
+ }
+ }
+
+ private function addLanguageSearchTerms(int $uid, $languages = null): string
+ {
+ $langterms = '';
+ foreach ($languages ?: User::getWantedLanguages($uid) as $language) {
+ $langterms .= ' language:' . $language;
+ }
+
+ if ($langterms) {
+ return ' +(' . trim($langterms) . ')';
+ } else {
+ return '';
+ }
}
private function addLanguageCondition(int $uid, array $condition, $languages = null): array
{
$conditions = [];
- $languages = $languages ?: User::getWantedLanguages($uid);
- foreach ($languages as $language) {
- $conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?";
+ foreach ($languages ?: User::getWantedLanguages($uid) as $language) {
+ $conditions[] = "`language` = ?";
$condition[] = $language;
}
+
if (!empty($conditions)) {
$condition[0] .= " AND (" . implode(' OR ', $conditions) . ")";
}
return $score;
}
+ private function getMedianPostScore(int $cid, int $divider): int
+ {
+ $cache_key = 'Channel:getPostScore:' . $cid . ':' . $divider;
+ $score = $this->cache->get($cache_key);
+ if (!empty($score)) {
+ return $score;
+ }
+
+ $condition = ["`relation-cid` = ? AND `post-score` > ?", $cid, 0];
+
+ $limit = $this->database->count('contact-relation', $condition) / $divider;
+ $relation = $this->database->selectToArray('contact-relation', ['post-score'], $condition, ['order' => ['post-score' => true], 'limit' => [$limit, 1]]);
+ $score = $relation[0]['post-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;
+ }
+
/**
* Computes the displayed items.
*
{
$items = $this->selectItems();
- if ($this->selectedTab == 'local') {
+ if ($this->selectedTab == Community::LOCAL) {
$maxpostperauthor = (int)$this->config->get('system', 'max_author_posts_community_page');
$key = 'author-id';
- } elseif ($this->selectedTab == 'global') {
+ } elseif ($this->selectedTab == Community::GLOBAL) {
$maxpostperauthor = (int)$this->config->get('system', 'max_server_posts_community_page');
$key = 'author-gsid';
} else {
*/
private function selectItems()
{
- if ($this->selectedTab == 'local') {
+ $this->order = 'received';
+
+ if ($this->selectedTab == Community::LOCAL) {
$condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
} elseif ($this->selectedTab == 'global') {
$condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
}
$items = [];
- $result = Post::selectThreadForUser($this->session->getLocalUserId() ?: 0, ['uri-id', 'received', 'author-id', 'author-gsid'], $condition, $params);
+ if ($this->selectedTab == Community::LOCAL) {
+ $result = Post::selectOriginThread(['uri-id', 'received', 'author-id', 'author-gsid'], $condition, $params);
+ } else {
+ $result = Post::selectThreadForUser($this->session->getLocalUserId() ?: 0, ['uri-id', 'received', 'author-id', 'author-gsid'], $condition, $params);
+ }
while ($item = $this->database->fetch($result)) {
$item['comments'] = 0;
*/
protected function setItemsSeenByCondition(array $condition)
{
- if (empty($condition)) {
+ if (empty($condition) || $this->ping) {
return;
}