"self::" should be "$this->" on non static functions
[friendica.git/.git] / src / Module / Conversation / Timeline.php
index d0f8f09..9088854 100644 (file)
@@ -26,6 +26,8 @@ use Friendica\App\Mode;
 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;
@@ -41,7 +43,6 @@ use Friendica\Database\DBA;
 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;
@@ -71,7 +72,13 @@ class Timeline extends BaseModule
        /** @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;
@@ -138,6 +145,8 @@ class Timeline extends BaseModule
                        $this->itemUriId = 0;
                }
 
+               $this->order = 'created';
+
                $this->minId = $request['min_id'] ?? null;
                $this->maxId = $request['max_id'] ?? null;
 
@@ -147,14 +156,36 @@ class Timeline extends BaseModule
                $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;
                }
@@ -204,9 +235,10 @@ class Timeline extends BaseModule
         * @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');
@@ -218,10 +250,10 @@ class Timeline extends BaseModule
                        $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)) {
@@ -251,8 +283,8 @@ class Timeline extends BaseModule
                                        $this->maxId = $maxId;
                                }
 
-                               if (count($selected_items) < $this->itemsPerPage) {
-                                       $items = $this->getRawChannelItems();
+                               if (count($selected_items) < $total) {
+                                       $items = $this->getRawChannelItems($request);
                                }
                        }
                } else {
@@ -271,10 +303,12 @@ class Timeline extends BaseModule
         * @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];
@@ -317,6 +351,13 @@ class Timeline extends BaseModule
                                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) {
@@ -324,48 +365,62 @@ class Timeline extends BaseModule
                } 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());
                }
@@ -390,13 +445,8 @@ class Timeline extends BaseModule
                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)) {
@@ -404,27 +454,56 @@ class Timeline extends BaseModule
                                $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)) {
-                       $condition = DBA::mergeConditions($condition, ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", Engagement::escapeKeywords($channel->fullTextSearch)]);
-               }
+                       if (!empty($channel->includeTags)) {
+                               $additional = $this->addIncludeTags($channel->includeTags);
+                       } else {
+                               $additional = '';
+                       }
 
-               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)) {
+                               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;
+                       }
 
-               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));
+                       $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]);
+                       }
+       
+                       // For "addLanguageCondition" to work, the condition must not be empty
+                       $condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages);
                }
 
                if (!is_null($channel->minSize)) {
@@ -435,24 +514,67 @@ class Timeline extends BaseModule
                        $condition = DBA::mergeConditions($condition, ["`size` <= ?", $channel->maxSize]);
                }
 
-               if (!empty($channel->mediaType)) {
-                       $condition = DBA::mergeConditions($condition, ["`media-type` & ?", $channel->mediaType]);
+               return $condition;
+       }
+
+       private function addIncludeTags(string $includeTags): string
+       {
+               $tagterms = '';
+               foreach (explode(',', mb_strtolower($includeTags)) as $tag) {
+                       $tagterms .= ' tag:' . $tag;
+               }
+
+               if ($tagterms) {
+                       return ' +(' . trim($tagterms) . ')';
+               } else {
+                       return '';
+               }
+       }
+
+       private function addMediaTerms(int $mediaType): string
+       {
+               $mediaterms = '';
+               if ($mediaType & 1) {
+                       $mediaterms .= ' media:image';
                }
 
-               // For "addLanguageCondition" to work, the condition must not be empty
-               $condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages);
+               if ($mediaType & 2) {
+                       $mediaterms .= ' media:video';
+               }
 
-               return $condition;
+               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) . ")";
                }
@@ -529,6 +651,28 @@ class Timeline extends BaseModule
                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.
         *
@@ -542,10 +686,10 @@ class Timeline extends BaseModule
        {
                $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 {
@@ -609,7 +753,9 @@ class Timeline extends BaseModule
         */
        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];
@@ -645,7 +791,11 @@ class Timeline extends BaseModule
                }
 
                $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;
@@ -680,7 +830,7 @@ class Timeline extends BaseModule
         */
        protected function setItemsSeenByCondition(array $condition)
        {
-               if (empty($condition)) {
+               if (empty($condition) || $this->ping) {
                        return;
                }