Merge remote-tracking branch 'upstream/develop' into size
[friendica.git/.git] / src / Module / Conversation / Timeline.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Conversation;
23
24 use Friendica\App;
25 use Friendica\App\Mode;
26 use Friendica\BaseModule;
27 use Friendica\Content\Conversation\Collection\Timelines;
28 use Friendica\Content\Conversation\Entity\Channel as ChannelEntity;
29 use Friendica\Content\Conversation\Repository\UserDefinedChannel;
30 use Friendica\Core\Cache\Capability\ICanCache;
31 use Friendica\Core\Cache\Enum\Duration;
32 use Friendica\Core\Config\Capability\IManageConfigValues;
33 use Friendica\Core\L10n;
34 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
35 use Friendica\Core\Renderer;
36 use Friendica\Core\Session\Capability\IHandleUserSessions;
37 use Friendica\Model\Contact;
38 use Friendica\Model\User;
39 use Friendica\Database\Database;
40 use Friendica\Database\DBA;
41 use Friendica\Model\Item;
42 use Friendica\Model\Post;
43 use Friendica\Model\Post\Engagement;
44 use Friendica\Model\Verb;
45 use Friendica\Module\Response;
46 use Friendica\Protocol\Activity;
47 use Friendica\Util\DateTimeFormat;
48 use Friendica\Util\Profiler;
49 use Psr\Log\LoggerInterface;
50
51 class Timeline extends BaseModule
52 {
53         /** @var string */
54         protected $selectedTab;
55         /** @var mixed */
56         protected $minId;
57         /** @var mixed */
58         protected $maxId;
59         /** @var string */
60         protected $accountTypeString;
61         /** @var int */
62         protected $accountType;
63         /** @var int */
64         protected $itemUriId;
65         /** @var int */
66         protected $itemsPerPage;
67         /** @var bool */
68         protected $noSharer;
69         /** @var bool */
70         protected $force;
71         /** @var bool */
72         protected $update;
73         /** @var bool */
74         protected $raw;
75
76         /** @var App\Mode $mode */
77         protected $mode;
78         /** @var IHandleUserSessions */
79         protected $session;
80         /** @var Database */
81         protected $database;
82         /** @var IManagePersonalConfigValues */
83         protected $pConfig;
84         /** @var IManageConfigValues The config */
85         protected $config;
86         /** @var ICanCache */
87         protected $cache;
88         /** @var UserDefinedChannel */
89         protected $channelRepository;
90
91         public function __construct(UserDefinedChannel $channel, Mode $mode, IHandleUserSessions $session, Database $database, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, ICanCache $cache, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
92         {
93                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
94
95                 $this->channelRepository = $channel;
96                 $this->mode              = $mode;
97                 $this->session           = $session;
98                 $this->database          = $database;
99                 $this->pConfig           = $pConfig;
100                 $this->config            = $config;
101                 $this->cache             = $cache;
102         }
103
104         /**
105          * Computes module parameters from the request and local configuration
106          *
107          * @throws HTTPException\BadRequestException
108          * @throws HTTPException\ForbiddenException
109          */
110         protected function parseRequest(array $request)
111         {
112                 $this->logger->debug('Got request', $request);
113                 $this->selectedTab = $this->parameters['content'] ?? $request['channel'] ?? '';
114
115                 $this->accountTypeString = $request['accounttype'] ?? $this->parameters['accounttype'] ?? '';
116                 $this->accountType       = User::getAccountTypeByString($this->accountTypeString);
117
118                 if ($this->mode->isMobile()) {
119                         $this->itemsPerPage = $this->pConfig->get(
120                                 $this->session->getLocalUserId(),
121                                 'system',
122                                 'itemspage_mobile_network',
123                                 $this->config->get('system', 'itemspage_network_mobile')
124                         );
125                 } else {
126                         $this->itemsPerPage = $this->pConfig->get(
127                                 $this->session->getLocalUserId(),
128                                 'system',
129                                 'itemspage_network',
130                                 $this->config->get('system', 'itemspage_network')
131                         );
132                 }
133
134                 if (!empty($request['item'])) {
135                         $item            = Post::selectFirst(['parent', 'parent-uri-id'], ['id' => $request['item']]);
136                         $this->itemUriId = $item['parent-uri-id'] ?? 0;
137                 } else {
138                         $this->itemUriId = 0;
139                 }
140
141                 $this->minId = $request['min_id'] ?? null;
142                 $this->maxId = $request['max_id'] ?? null;
143
144                 $this->noSharer = !empty($request['no_sharer']);
145                 $this->force    = !empty($request['force']) && !empty($request['item']);
146                 $this->update   = !empty($request['force']) && !empty($request['first_received']) && !empty($request['first_created']) && !empty($request['first_uriid']) && !empty($request['first_commented']);
147                 $this->raw      = !empty($request['mode']) && ($request['mode'] == 'raw');
148         }
149
150         protected function getNoSharerWidget(string $base): string
151         {
152                 $path = $this->selectedTab;
153                 if (!empty($this->accountTypeString)) {
154                         $path .= '/' . $this->accountTypeString;
155                 }
156                 $query_parameters = [];
157
158                 if (!empty($this->minId)) {
159                         $query_parameters['min_id'] = $this->minId;
160                 }
161                 if (!empty($this->maxId)) {
162                         $query_parameters['max_id'] = $this->maxId;
163                 }
164
165                 $path_all       = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : '');
166                 $path_no_sharer = $path . '?' . http_build_query(array_merge($query_parameters, ['no_sharer' => true]));
167                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_sharer.tpl'), [
168                         '$title'           => $this->l10n->t('Own Contacts'),
169                         '$path_all'        => $path_all,
170                         '$path_no_sharer'  => $path_no_sharer,
171                         '$no_sharer'       => $this->noSharer,
172                         '$all'             => $this->l10n->t('Include'),
173                         '$no_sharer_label' => $this->l10n->t('Hide'),
174                         '$base'            => $base,
175                 ]);
176         }
177
178         protected function getTabArray(Timelines $timelines, string $prefix, string $parameter = ''): array
179         {
180                 $tabs = [];
181
182                 foreach ($timelines as $tab) {
183                         if (is_null($tab->path) && !empty($parameter)) {
184                                 $path = $prefix . '?' . http_build_query([$parameter => $tab->code]);
185                         } else {
186                                 $path = $tab->path ?? $prefix . '/' . $tab->code;
187                         }
188                         $tabs[$tab->code] = [
189                                 'code'      => $tab->code,
190                                 'label'     => $tab->label,
191                                 'url'       => $path,
192                                 'sel'       => $this->selectedTab == $tab->code ? 'active' : '',
193                                 'title'     => $tab->description,
194                                 'id'        => $prefix . '-' . $tab->code . '-tab',
195                                 'accesskey' => $tab->accessKey,
196                         ];
197                 }
198                 return $tabs;
199         }
200
201         /**
202          * Database query for the channel page
203          *
204          * @return array
205          * @throws \Exception
206          */
207         protected function getChannelItems()
208         {
209                 $items = $this->getRawChannelItems();
210
211                 $contacts = $this->database->selectToArray('user-contact', ['cid'], ['channel-frequency' => Contact\User::FREQUENCY_REDUCED, 'cid' => array_column($items, 'owner-id')]);
212                 $reduced  = array_column($contacts, 'cid');
213
214                 $maxpostperauthor = $this->config->get('channel', 'max_posts_per_author');
215
216                 if ($maxpostperauthor != 0) {
217                         $count          = 1;
218                         $owner_posts    = [];
219                         $selected_items = [];
220
221                         while (count($selected_items) < $this->itemsPerPage && ++$count < 50 && count($items) > 0) {
222                                 $maxposts = round((count($items) / $this->itemsPerPage) * $maxpostperauthor);
223                                 $minId = $items[array_key_first($items)]['created'];
224                                 $maxId = $items[array_key_last($items)]['created'];
225
226                                 foreach ($items as $item) {
227                                         if (!in_array($item['owner-id'], $reduced)) {
228                                                 continue;
229                                         }
230                                         $owner_posts[$item['owner-id']][$item['uri-id']] = (($item['comments'] * 100) + $item['activities']);
231                                 }
232                                 foreach ($owner_posts as $posts) {
233                                         if (count($posts) <= $maxposts) {
234                                                 continue;
235                                         }
236                                         asort($posts);
237                                         while (count($posts) > $maxposts) {
238                                                 $uri_id = array_key_first($posts);
239                                                 unset($posts[$uri_id]);
240                                                 unset($items[$uri_id]);
241                                         }
242                                 }
243                                 $selected_items = array_merge($selected_items, $items);
244
245                                 // If we're looking at a "previous page", the lookup continues forward in time because the list is
246                                 // sorted in chronologically decreasing order
247                                 if (!empty($this->minId)) {
248                                         $this->minId = $minId;
249                                 } else {
250                                         // In any other case, the lookup continues backwards in time
251                                         $this->maxId = $maxId;
252                                 }
253
254                                 if (count($selected_items) < $this->itemsPerPage) {
255                                         $items = $this->getRawChannelItems();
256                                 }
257                         }
258                 } else {
259                         $selected_items = $items;
260                 }
261
262                 $condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => array_column($selected_items, 'uri-id')];
263                 $this->setItemsSeenByCondition($condition);
264
265                 return $selected_items;
266         }
267
268         /**
269          * Database query for the channel page
270          *
271          * @return array
272          * @throws \Exception
273          */
274         private function getRawChannelItems()
275         {
276                 $uid = $this->session->getLocalUserId();
277
278                 if ($this->selectedTab == ChannelEntity::WHATSHOT) {
279                         if (!is_null($this->accountType)) {
280                                 $condition = ["(`comments` > ? OR `activities` > ?) AND `contact-type` = ?", $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $this->accountType];
281                         } else {
282                                 $condition = ["(`comments` > ? OR `activities` > ?) AND `contact-type` != ?", $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), Contact::TYPE_COMMUNITY];
283                         }
284                 } elseif ($this->selectedTab == ChannelEntity::FORYOU) {
285                         $cid = Contact::getPublicIdByUserId($uid);
286
287                         $condition = [
288                                 "(`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `relation-thread-score` > ?) OR
289                                 ((`comments` >= ? OR `activities` >= ?) AND `owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ?)) OR
290                                 (`owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`notify_new_posts` OR `channel-frequency` = ?))))",
291                                 $cid, $this->getMedianRelationThreadScore($cid, 4), $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $cid,
292                                 $uid, Contact\User::FREQUENCY_ALWAYS
293                         ];
294                 } elseif ($this->selectedTab == ChannelEntity::DISCOVER) {
295                         $cid = Contact::getPublicIdByUserId($uid);
296
297                         $condition = [
298                                 "`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND NOT `follows`) AND
299                                 (`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND NOT `follows` AND `relation-thread-score` > ?) OR
300                                 `owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `cid` = ? AND `relation-thread-score` > ?) OR
301                                 ((`comments` >= ? OR `activities` >= ?) AND 
302                                 (`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `cid` = ? AND `relation-thread-score` > ?)) OR 
303                                 (`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `relation-thread-score` > ?))))",
304                                 $cid, $cid, $this->getMedianRelationThreadScore($cid, 4), $cid, $this->getMedianRelationThreadScore($cid, 4),
305                                 $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $cid, 0, $cid, 0 
306                         ];
307
308                 } elseif ($this->selectedTab == ChannelEntity::FOLLOWERS) {
309                         $condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", $uid, Contact::FOLLOWER];
310                 } elseif ($this->selectedTab == ChannelEntity::SHARERSOFSHARERS) {
311                         $cid = Contact::getPublicIdByUserId($uid);
312
313                         // @todo Suggest posts from contacts that are followed most by our followers
314                         $condition = [
315                                 "`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `last-interaction` > ?
316                                 AND `relation-cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ? AND `relation-thread-score` >= ?)
317                                 AND NOT `cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ?))",
318                                 DateTimeFormat::utc('now - ' . $this->config->get('channel', 'sharer_interaction_days') . ' day'), $cid, $this->getMedianRelationThreadScore($cid, 4), $cid
319                         ];
320                 } elseif ($this->selectedTab == ChannelEntity::IMAGE) {
321                         $condition = ["`media-type` & ?", 1];
322                 } elseif ($this->selectedTab == ChannelEntity::VIDEO) {
323                         $condition = ["`media-type` & ?", 2];
324                 } elseif ($this->selectedTab == ChannelEntity::AUDIO) {
325                         $condition = ["`media-type` & ?", 4];
326                 } elseif ($this->selectedTab == ChannelEntity::LANGUAGE) {
327                         $condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", User::getLanguageCode($uid)];
328                 } elseif (is_numeric($this->selectedTab)) {
329                         $condition = $this->getUserChannelConditions($this->selectedTab, $uid);
330                 }
331
332                 if (($this->selectedTab != ChannelEntity::LANGUAGE) && !is_numeric($this->selectedTab)) {
333                         $condition = $this->addLanguageCondition($uid, $condition);
334                 }
335
336                 $condition = DBA::mergeConditions($condition, ["(NOT `restricted` OR EXISTS(SELECT `id` FROM `post-user` WHERE `uid` = ? AND `uri-id` = `post-engagement`.`uri-id`))", $uid]);
337
338                 $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]);
339
340                 if (($this->selectedTab != ChannelEntity::WHATSHOT) && !is_null($this->accountType)) {
341                         $condition = DBA::mergeConditions($condition, ['contact-type' => $this->accountType]);
342                 }
343
344                 $params = ['order' => ['created' => true], 'limit' => $this->itemsPerPage];
345
346                 if (!empty($this->itemUriId)) {
347                         $condition = DBA::mergeConditions($condition, ['uri-id' => $this->itemUriId]);
348                 } else {
349                         if ($this->noSharer) {
350                                 $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()]);
351                         }
352
353                         if (isset($this->maxId)) {
354                                 $condition = DBA::mergeConditions($condition, ["`created` < ?", $this->maxId]);
355                         }
356
357                         if (isset($this->minId)) {
358                                 $condition = DBA::mergeConditions($condition, ["`created` > ?", $this->minId]);
359
360                                 // Previous page case: we want the items closest to min_id but for that we need to reverse the query order
361                                 if (!isset($this->maxId)) {
362                                         $params['order']['created'] = false;
363                                 }
364                         }
365                 }
366
367                 $items = [];
368                 $result = $this->database->select('post-engagement', ['uri-id', 'created', 'owner-id', 'comments', 'activities'], $condition, $params);
369                 if ($this->database->errorNo()) {
370                         throw new \Exception($this->database->errorMessage(), $this->database->errorNo());
371                 }
372
373                 while ($item = $this->database->fetch($result)) {
374                         $items[$item['uri-id']] = $item;
375                 }
376                 $this->database->close($result);
377
378                 if (empty($items)) {
379                         return [];
380                 }
381
382                 // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order
383                 if (empty($this->itemUriId) && isset($this->minId) && !isset($this->maxId)) {
384                         $items = array_reverse($items, true);
385                 }
386
387                 $condition = ['unseen' => true, 'uid' => $uid, 'parent-uri-id' => array_column($items, 'uri-id')];
388                 $this->setItemsSeenByCondition($condition);
389
390                 return $items;
391         }
392
393         private function getUserChannelConditions(int $id, int $uid): array
394         {
395                 $channel = $this->channelRepository->selectById($id, $uid);
396                 if (empty($channel)) {
397                         return [];
398                 }
399
400                 $condition = [];
401
402                 if (!empty($channel->circle)) {
403                         if ($channel->circle == -1) {
404                                 $condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` IN (?, ?))", $uid, Contact::SHARING, Contact::FRIEND];
405                         } elseif ($channel->circle == -2) {
406                                 $condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", $uid, Contact::FOLLOWER];
407                         } elseif ($channel->circle == -3) {
408                                 $condition = ["EXISTS(SELECT `uri-id` FROM `post-thread-user` WHERE `uid` = ? AND `post-thread-user`.`uri-id` = `post-engagement`.`uri-id`)", $uid];
409                         } elseif ($channel->circle > 0) {
410                                 $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]);
411                         }
412                 }
413
414                 if (!empty($channel->fullTextSearch)) {
415                         $condition = DBA::mergeConditions($condition, ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", Engagement::escapeKeywords($channel->fullTextSearch)]);
416                 }
417
418                 if (!empty($channel->includeTags)) {
419                         $search       = explode(',', mb_strtolower($channel->includeTags));
420                         $placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
421                         $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));
422                 }
423
424                 if (!empty($channel->excludeTags)) {
425                         $search       = explode(',', mb_strtolower($channel->excludeTags));
426                         $placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
427                         $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));
428                 }
429
430                 if (!is_null($channel->minSize)) {
431                         $condition = DBA::mergeConditions($condition, ["`size` >= ?", $channel->minSize]);
432                 }
433
434                 if (!is_null($channel->maxSize)) {
435                         $condition = DBA::mergeConditions($condition, ["`size` <= ?", $channel->maxSize]);
436                 }
437
438                 if (!empty($channel->mediaType)) {
439                         $condition = DBA::mergeConditions($condition, ["`media-type` & ?", $channel->mediaType]);
440                 }
441
442                 // For "addLanguageCondition" to work, the condition must not be empty
443                 $condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages);
444
445                 return $condition;
446         }
447
448         private function addLanguageCondition(int $uid, array $condition, $languages = null): array
449         {
450                 $conditions = [];
451                 $languages  = $languages ?: User::getWantedLanguages($uid);
452                 foreach ($languages as $language) {
453                         $conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?";
454                         $condition[]  = $language;
455                 }
456                 if (!empty($conditions)) {
457                         $condition[0] .= " AND (" . implode(' OR ', $conditions) . ")";
458                 }
459                 return $condition;
460         }
461
462         private function getMedianComments(int $uid, int $divider): int
463         {
464                 $languages = User::getWantedLanguages($uid);
465                 $cache_key = 'Channel:getMedianComments:' . $divider . ':' . implode(':', $languages);
466                 $comments  = $this->cache->get($cache_key);
467                 if (!empty($comments)) {
468                         return $comments;
469                 }
470
471                 $condition = ["`contact-type` != ? AND `comments` > ? AND NOT `restricted`", Contact::TYPE_COMMUNITY, 0];
472                 $condition = $this->addLanguageCondition($uid, $condition);
473
474                 $limit    = $this->database->count('post-engagement', $condition) / $divider;
475                 $post     = $this->database->selectToArray('post-engagement', ['comments'], $condition, ['order' => ['comments' => true], 'limit' => [$limit, 1]]);
476                 $comments = $post[0]['comments'] ?? 0;
477                 if (empty($comments)) {
478                         return 0;
479                 }
480
481                 $this->cache->set($cache_key, $comments, Duration::HALF_HOUR);
482                 $this->logger->debug('Calculated median comments', ['divider' => $divider, 'languages' => $languages, 'median' => $comments]);
483                 return $comments;
484         }
485
486         private function getMedianActivities(int $uid, int $divider): int
487         {
488                 $languages  = User::getWantedLanguages($uid);
489                 $cache_key  = 'Channel:getMedianActivities:' . $divider . ':' . implode(':', $languages);
490                 $activities = $this->cache->get($cache_key);
491                 if (!empty($activities)) {
492                         return $activities;
493                 }
494
495                 $condition = ["`contact-type` != ? AND `activities` > ? AND NOT `restricted`", Contact::TYPE_COMMUNITY, 0];
496                 $condition = $this->addLanguageCondition($uid, $condition);
497
498                 $limit      = $this->database->count('post-engagement', $condition) / $divider;
499                 $post       = $this->database->selectToArray('post-engagement', ['activities'], $condition, ['order' => ['activities' => true], 'limit' => [$limit, 1]]);
500                 $activities = $post[0]['activities'] ?? 0;
501                 if (empty($activities)) {
502                         return 0;
503                 }
504
505                 $this->cache->set($cache_key, $activities, Duration::HALF_HOUR);
506                 $this->logger->debug('Calculated median activities', ['divider' => $divider, 'languages' => $languages, 'median' => $activities]);
507                 return $activities;
508         }
509
510         private function getMedianRelationThreadScore(int $cid, int $divider): int
511         {
512                 $cache_key = 'Channel:getThreadScore:' . $cid . ':' . $divider;
513                 $score     = $this->cache->get($cache_key);
514                 if (!empty($score)) {
515                         return $score;
516                 }
517
518                 $condition = ["`relation-cid` = ? AND `relation-thread-score` > ?", $cid, 0];
519
520                 $limit    = $this->database->count('contact-relation', $condition) / $divider;
521                 $relation = $this->database->selectToArray('contact-relation', ['relation-thread-score'], $condition, ['order' => ['relation-thread-score' => true], 'limit' => [$limit, 1]]);
522                 $score    = $relation[0]['relation-thread-score'] ?? 0;
523                 if (empty($score)) {
524                         return 0;
525                 }
526
527                 $this->cache->set($cache_key, $score, Duration::HALF_HOUR);
528                 $this->logger->debug('Calculated median score', ['cid' => $cid, 'divider' => $divider, 'median' => $score]);
529                 return $score;
530         }
531
532         /**
533          * Computes the displayed items.
534          *
535          * Community pages have a restriction on how many successive posts by the same author can show on any given page,
536          * so we may have to retrieve more content beyond the first query
537          *
538          * @return array
539          * @throws \Exception
540          */
541         protected function getCommunityItems()
542         {
543                 $items = $this->selectItems();
544
545                 if ($this->selectedTab == 'local') {
546                         $maxpostperauthor = (int)$this->config->get('system', 'max_author_posts_community_page');
547                         $key = 'author-id';
548                 } elseif ($this->selectedTab == 'global') {
549                         $maxpostperauthor = (int)$this->config->get('system', 'max_server_posts_community_page');
550                         $key = 'author-gsid';
551                 } else {
552                         $maxpostperauthor = 0;
553                 }
554                 if ($maxpostperauthor != 0) {
555                         $count          = 1;
556                         $author_posts   = [];
557                         $selected_items = [];
558
559                         while (count($selected_items) < $this->itemsPerPage && ++$count < 50 && count($items) > 0) {
560                                 $maxposts = round((count($items) / $this->itemsPerPage) * $maxpostperauthor);
561                                 $minId = $items[array_key_first($items)]['received'];
562                                 $maxId = $items[array_key_last($items)]['received'];
563
564                                 foreach ($items as $item) {
565                                         $author_posts[$item[$key]][$item['uri-id']] = $item['received'];
566                                 }
567                                 foreach ($author_posts as $posts) {
568                                         if (count($posts) <= $maxposts) {
569                                                 continue;
570                                         }
571                                         asort($posts);
572                                         while (count($posts) > $maxposts) {
573                                                 $uri_id = array_key_first($posts);
574                                                 unset($posts[$uri_id]);
575                                                 unset($items[$uri_id]);
576                                         }
577                                 }
578                                 $selected_items = array_merge($selected_items, $items);
579
580                                 // If we're looking at a "previous page", the lookup continues forward in time because the list is
581                                 // sorted in chronologically decreasing order
582                                 if (!empty($this->minId)) {
583                                         $this->minId = $minId;
584                                 } else {
585                                         // In any other case, the lookup continues backwards in time
586                                         $this->maxId = $maxId;
587                                 }
588
589                                 if (count($selected_items) < $this->itemsPerPage) {
590                                         $items = $this->selectItems();
591                                 }
592                         }
593                 } else {
594                         $selected_items = $items;
595                 }
596
597                 $condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => array_column($selected_items, 'uri-id')];
598                 $this->setItemsSeenByCondition($condition);
599
600                 return $selected_items;
601         }
602
603         /**
604          * Database query for the community page
605          *
606          * @return array
607          * @throws \Exception
608          * @TODO Move to repository/factory
609          */
610         private function selectItems()
611         {
612                 if ($this->selectedTab == 'local') {
613                         $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
614                 } elseif ($this->selectedTab == 'global') {
615                         $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
616                 } else {
617                         return [];
618                 }
619
620                 if (!is_null($this->accountType)) {
621                         $condition = DBA::mergeConditions($condition, ['owner-contact-type' => $this->accountType]);
622                 }
623
624                 $params = ['order' => ['received' => true], 'limit' => $this->itemsPerPage];
625
626                 if (!empty($this->itemUriId)) {
627                         $condition = DBA::mergeConditions($condition, ['uri-id' => $this->itemUriId]);
628                 } else {
629                         if ($this->session->getLocalUserId() && $this->noSharer) {
630                                 $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ? AND `post-user`.`uri-id` = `post-thread-user-view`.`uri-id`)", $this->session->getLocalUserId()]);
631                         }
632
633                         if (isset($this->maxId)) {
634                                 $condition = DBA::mergeConditions($condition, ["`received` < ?", $this->maxId]);
635                         }
636
637                         if (isset($this->minId)) {
638                                 $condition = DBA::mergeConditions($condition, ["`received` > ?", $this->minId]);
639
640                                 // Previous page case: we want the items closest to min_id but for that we need to reverse the query order
641                                 if (!isset($this->maxId)) {
642                                         $params['order']['received'] = false;
643                                 }
644                         }
645                 }
646
647                 $items = [];
648                 $result = Post::selectThreadForUser($this->session->getLocalUserId() ?: 0, ['uri-id', 'received', 'author-id', 'author-gsid'], $condition, $params);
649
650                 while ($item = $this->database->fetch($result)) {
651                         $item['comments'] = 0;
652
653                         $items[$item['uri-id']] = $item;
654                 }
655                 $this->database->close($result);
656
657                 if (empty($items)) {
658                         return [];
659                 }
660
661                 $uriids = array_keys($items);
662                 
663                 foreach (Post\Counts::get(['parent-uri-id' => $uriids, 'verb' => Activity::POST]) as $count) {
664                         $items[$count['parent-uri-id']]['comments'] += $count['count'];
665                 }
666
667                 // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order
668                 if (empty($this->itemUriId) && isset($this->minId) && !isset($this->maxId)) {
669                         $items = array_reverse($items);
670                 }
671
672                 return $items;
673         }
674
675         /**
676          * Sets items as seen
677          *
678          * @param array $condition The array with the SQL condition
679          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
680          */
681         protected function setItemsSeenByCondition(array $condition)
682         {
683                 if (empty($condition)) {
684                         return;
685                 }
686
687                 $unseen = Post::exists($condition);
688
689                 if ($unseen) {
690                         /// @todo handle huge "unseen" updates in the background to avoid timeout errors
691                         Item::update(['unseen' => false], $condition);
692                 }
693         }
694 }