]> git.mxchange.org Git - friendica.git/blob - src/Module/Conversation/Timeline.php
Merge pull request #13446 from annando/restricted
[friendica.git] / src / Module / Conversation / Timeline.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Timeline as TimelineEntity;
29 use Friendica\Core\Cache\Capability\ICanCache;
30 use Friendica\Core\Cache\Enum\Duration;
31 use Friendica\Core\Config\Capability\IManageConfigValues;
32 use Friendica\Core\L10n;
33 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
34 use Friendica\Core\Renderer;
35 use Friendica\Core\Session\Capability\IHandleUserSessions;
36 use Friendica\Model\Contact;
37 use Friendica\Model\User;
38 use Friendica\Database\Database;
39 use Friendica\Database\DBA;
40 use Friendica\Model\Item;
41 use Friendica\Model\Post;
42 use Friendica\Module\Response;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Profiler;
45 use Psr\Log\LoggerInterface;
46
47 class Timeline extends BaseModule
48 {
49         /** @var string */
50         protected $selectedTab;
51         /** @var mixed */
52         protected $minId;
53         /** @var mixed */
54         protected $maxId;
55         /** @var string */
56         protected $accountTypeString;
57         /** @var int */
58         protected $accountType;
59         /** @var int */
60         protected $itemUriId;
61         /** @var int */
62         protected $itemsPerPage;
63         /** @var bool */
64         protected $noSharer;
65         /** @var bool */
66         protected $force;
67         /** @var bool */
68         protected $update;
69
70         /** @var App\Mode $mode */
71         protected $mode;
72         /** @var IHandleUserSessions */
73         protected $session;
74         /** @var Database */
75         protected $database;
76         /** @var IManagePersonalConfigValues */
77         protected $pConfig;
78         /** @var IManageConfigValues The config */
79         protected $config;
80         /** @var ICanCache */
81         protected $cache;
82
83         public function __construct(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 = [])
84         {
85                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
86
87                 $this->mode     = $mode;
88                 $this->session  = $session;
89                 $this->database = $database;
90                 $this->pConfig  = $pConfig;
91                 $this->config   = $config;
92                 $this->cache    = $cache;
93         }
94
95         /**
96          * Computes module parameters from the request and local configuration
97          *
98          * @throws HTTPException\BadRequestException
99          * @throws HTTPException\ForbiddenException
100          */
101         protected function parseRequest(array $request)
102         {
103                 $this->logger->debug('Got request', $request);
104                 $this->selectedTab = $this->parameters['content'] ?? $request['channel'] ?? '';
105
106                 $this->accountTypeString = $request['accounttype'] ?? $this->parameters['accounttype'] ?? '';
107                 $this->accountType       = User::getAccountTypeByString($this->accountTypeString);
108
109                 if ($this->mode->isMobile()) {
110                         $this->itemsPerPage = $this->pConfig->get(
111                                 $this->session->getLocalUserId(),
112                                 'system',
113                                 'itemspage_mobile_network',
114                                 $this->config->get('system', 'itemspage_network_mobile')
115                         );
116                 } else {
117                         $this->itemsPerPage = $this->pConfig->get(
118                                 $this->session->getLocalUserId(),
119                                 'system',
120                                 'itemspage_network',
121                                 $this->config->get('system', 'itemspage_network')
122                         );
123                 }
124
125                 if (!empty($request['item'])) {
126                         $item            = Post::selectFirst(['parent', 'parent-uri-id'], ['id' => $request['item']]);
127                         $this->itemUriId = $item['parent-uri-id'] ?? 0;
128                 } else {
129                         $this->itemUriId = 0;
130                 }
131
132                 $this->minId = $request['min_id'] ?? null;
133                 $this->maxId = $request['max_id'] ?? null;
134
135                 $this->noSharer = !empty($request['no_sharer']);
136                 $this->force    = !empty($request['force']) && !empty($request['item']);
137                 $this->update   = !empty($request['force']) && !empty($request['first_received']) && !empty($request['first_created']) && !empty($request['first_uriid']) && !empty($request['first_commented']);
138         }
139
140         protected function getNoSharerWidget(string $base): string
141         {
142                 $path = $this->selectedTab;
143                 if (!empty($this->accountTypeString)) {
144                         $path .= '/' . $this->accountTypeString;
145                 }
146                 $query_parameters = [];
147
148                 if (!empty($this->minId)) {
149                         $query_parameters['min_id'] = $this->minId;
150                 }
151                 if (!empty($this->maxId)) {
152                         $query_parameters['max_id'] = $this->maxId;
153                 }
154
155                 $path_all       = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : '');
156                 $path_no_sharer = $path . '?' . http_build_query(array_merge($query_parameters, ['no_sharer' => true]));
157                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_sharer.tpl'), [
158                         '$title'           => $this->l10n->t('Own Contacts'),
159                         '$path_all'        => $path_all,
160                         '$path_no_sharer'  => $path_no_sharer,
161                         '$no_sharer'       => $this->noSharer,
162                         '$all'             => $this->l10n->t('Include'),
163                         '$no_sharer_label' => $this->l10n->t('Hide'),
164                         '$base'            => $base,
165                 ]);
166         }
167
168         protected function getTabArray(Timelines $timelines, string $prefix, string $parameter = ''): array
169         {
170                 $tabs = [];
171
172                 foreach ($timelines as $tab) {
173                         if (is_null($tab->path) && !empty($parameter)) {
174                                 $path = $prefix . '?' . http_build_query([$parameter => $tab->code]);
175                         } else {
176                                 $path = $tab->path ?? $prefix . '/' . $tab->code;
177                         }
178                         $tabs[$tab->code] = [
179                                 'label'     => $tab->label,
180                                 'url'       => $path,
181                                 'sel'       => $this->selectedTab == $tab->code ? 'active' : '',
182                                 'title'     => $tab->description,
183                                 'id'        => $prefix . '-' . $tab->code . '-tab',
184                                 'accesskey' => $tab->accessKey,
185                         ];
186                 }
187                 return $tabs;
188         }
189
190         /**
191          * Database query for the channel page
192          *
193          * @return array
194          * @throws \Exception
195          */
196         protected function getChannelItems()
197         {
198                 $items = $this->getRawChannelItems();
199
200                 $contacts = $this->database->selectToArray('user-contact', ['cid'], ['channel-frequency' => Contact\User::FREQUENCY_REDUCED, 'cid' => array_column($items, 'owner-id')]);
201                 $reduced  = array_column($contacts, 'cid');
202
203                 $maxpostperauthor = $this->config->get('channel', 'max_posts_per_author');
204
205                 if ($maxpostperauthor != 0) {
206                         $count          = 1;
207                         $owner_posts    = [];
208                         $selected_items = [];
209
210                         while (count($selected_items) < $this->itemsPerPage && ++$count < 50 && count($items) > 0) {
211                                 $maxposts = round((count($items) / $this->itemsPerPage) * $maxpostperauthor);
212                                 $minId = $items[array_key_first($items)]['created'];
213                                 $maxId = $items[array_key_last($items)]['created'];
214
215                                 foreach ($items as $item) {
216                                         if (!in_array($item['owner-id'], $reduced)) {
217                                                 continue;
218                                         }
219                                         $owner_posts[$item['owner-id']][$item['uri-id']] = (($item['comments'] * 100) + $item['activities']);
220                                 }
221                                 foreach ($owner_posts as $posts) {
222                                         if (count($posts) <= $maxposts) {
223                                                 continue;
224                                         }
225                                         asort($posts);
226                                         while (count($posts) > $maxposts) {
227                                                 $uri_id = array_key_first($posts);
228                                                 unset($posts[$uri_id]);
229                                                 unset($items[$uri_id]);
230                                         }
231                                 }
232                                 $selected_items = array_merge($selected_items, $items);
233
234                                 // If we're looking at a "previous page", the lookup continues forward in time because the list is
235                                 // sorted in chronologically decreasing order
236                                 if (!empty($this->minId)) {
237                                         $this->minId = $minId;
238                                 } else {
239                                         // In any other case, the lookup continues backwards in time
240                                         $this->maxId = $maxId;
241                                 }
242
243                                 if (count($selected_items) < $this->itemsPerPage) {
244                                         $items = $this->getRawChannelItems();
245                                 }
246                         }
247                 } else {
248                         $selected_items = $items;
249                 }
250
251                 $condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => array_column($selected_items, 'uri-id')];
252                 $this->setItemsSeenByCondition($condition);
253
254                 return $selected_items;
255         }
256
257         /**
258          * Database query for the channel page
259          *
260          * @return array
261          * @throws \Exception
262          */
263         private function getRawChannelItems()
264         {
265                 $uid = $this->session->getLocalUserId();
266
267                 if ($this->selectedTab == TimelineEntity::WHATSHOT) {
268                         if (!is_null($this->accountType)) {
269                                 $condition = ["(`comments` > ? OR `activities` > ?) AND `contact-type` = ?", $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $this->accountType];
270                         } else {
271                                 $condition = ["(`comments` > ? OR `activities` > ?) AND `contact-type` != ?", $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), Contact::TYPE_COMMUNITY];
272                         }
273                 } elseif ($this->selectedTab == TimelineEntity::FORYOU) {
274                         $cid = Contact::getPublicIdByUserId($uid);
275
276                         $condition = [
277                                 "(`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `relation-thread-score` > ?) OR
278                                 ((`comments` >= ? OR `activities` >= ?) AND `owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ?)) OR
279                                 (`owner-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`notify_new_posts` OR `channel-frequency` = ?))))",
280                                 $cid, $this->getMedianRelationThreadScore($cid, 4), $this->getMedianComments($uid, 4), $this->getMedianActivities($uid, 4), $cid,
281                                 $uid, Contact\User::FREQUENCY_ALWAYS
282                         ];
283                 } elseif ($this->selectedTab == TimelineEntity::FOLLOWERS) {
284                         $condition = ["`owner-id` IN (SELECT `pid` FROM `account-user-view` WHERE `uid` = ? AND `rel` = ?)", $uid, Contact::FOLLOWER];
285                 } elseif ($this->selectedTab == TimelineEntity::SHARERSOFSHARERS) {
286                         $cid = Contact::getPublicIdByUserId($uid);
287
288                         // @todo Suggest posts from contacts that are followed most by our followers
289                         $condition = [
290                                 "`owner-id` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `last-interaction` > ?
291                                 AND `relation-cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ? AND `relation-thread-score` >= ?)
292                                 AND NOT `cid` IN (SELECT `cid` FROM `contact-relation` WHERE `follows` AND `relation-cid` = ?))",
293                                 DateTimeFormat::utc('now - ' . $this->config->get('channel', 'sharer_interaction_days') . ' day'), $cid, $this->getMedianRelationThreadScore($cid, 4), $cid
294                         ];
295                 } elseif ($this->selectedTab == TimelineEntity::IMAGE) {
296                         $condition = ["`media-type` & ?", 1];
297                 } elseif ($this->selectedTab == TimelineEntity::VIDEO) {
298                         $condition = ["`media-type` & ?", 2];
299                 } elseif ($this->selectedTab == TimelineEntity::AUDIO) {
300                         $condition = ["`media-type` & ?", 4];
301                 } elseif ($this->selectedTab == TimelineEntity::LANGUAGE) {
302                         $condition = ["JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?", $this->l10n->convertCodeForLanguageDetection(User::getLanguageCode($uid))];
303                 }
304
305                 if ($this->selectedTab != TimelineEntity::LANGUAGE) {
306                         $condition = $this->addLanguageCondition($uid, $condition);
307                 }
308
309                 $condition = DBA::mergeConditions($condition, ["(NOT `restricted` OR EXISTS(SELECT `id` FROM `post-user` WHERE `uid` = ? AND `uri-id` = `post-engagement`.`uri-id`))", $uid]);
310
311                 $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]);
312
313                 if (($this->selectedTab != TimelineEntity::WHATSHOT) && !is_null($this->accountType)) {
314                         $condition = DBA::mergeConditions($condition, ['contact-type' => $this->accountType]);
315                 }
316
317                 $params = ['order' => ['created' => true], 'limit' => $this->itemsPerPage];
318
319                 if (!empty($this->itemUriId)) {
320                         $condition = DBA::mergeConditions($condition, ['uri-id' => $this->itemUriId]);
321                 } else {
322                         if ($this->noSharer) {
323                                 $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()]);
324                         }
325
326                         if (isset($this->maxId)) {
327                                 $condition = DBA::mergeConditions($condition, ["`created` < ?", $this->maxId]);
328                         }
329
330                         if (isset($this->minId)) {
331                                 $condition = DBA::mergeConditions($condition, ["`created` > ?", $this->minId]);
332
333                                 // Previous page case: we want the items closest to min_id but for that we need to reverse the query order
334                                 if (!isset($this->maxId)) {
335                                         $params['order']['created'] = false;
336                                 }
337                         }
338                 }
339
340                 $items = [];
341                 $result = $this->database->select('post-engagement', ['uri-id', 'created', 'owner-id', 'comments', 'activities'], $condition, $params);
342                 while ($item = $this->database->fetch($result)) {
343                         $items[$item['uri-id']] = $item;
344                 }
345                 $this->database->close($result);
346
347                 if (empty($items)) {
348                         return [];
349                 }
350
351                 // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order
352                 if (empty($this->itemUriId) && isset($this->minId) && !isset($this->maxId)) {
353                         $items = array_reverse($items, true);
354                 }
355
356                 $condition = ['unseen' => true, 'uid' => $uid, 'parent-uri-id' => array_column($items, 'uri-id')];
357                 $this->setItemsSeenByCondition($condition);
358
359                 return $items;
360         }
361
362         private function addLanguageCondition(int $uid, array $condition): array
363         {
364                 $conditions = [];
365                 $languages  = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid)]);
366                 $languages  = $this->l10n->convertForLanguageDetection($languages);
367                 foreach ($languages as $language) {
368                         $conditions[] = "JSON_EXTRACT(JSON_KEYS(language), '$[0]') = ?";
369                         $condition[]  = $language;
370                 }
371                 if (!empty($conditions)) {
372                         $condition[0] .= " AND (`language` IS NULL OR " . implode(' OR ', $conditions) . ")";
373                 }
374                 return $condition;
375         }
376
377         private function getMedianComments(int $uid, int $divider): int
378         {
379                 $languages = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid)]);
380                 $cache_key = 'Channel:getMedianComments:' . $divider . ':' . implode(':', $languages);
381                 $comments  = $this->cache->get($cache_key);
382                 if (!empty($comments)) {
383                         return $comments;
384                 }
385
386                 $condition = ["`contact-type` != ? AND `comments` > ? AND NOT `restricted`", Contact::TYPE_COMMUNITY, 0];
387                 $condition = $this->addLanguageCondition($uid, $condition);
388
389                 $limit    = $this->database->count('post-engagement', $condition) / $divider;
390                 $post     = $this->database->selectToArray('post-engagement', ['comments'], $condition, ['order' => ['comments' => true], 'limit' => [$limit, 1]]);
391                 $comments = $post[0]['comments'] ?? 0;
392                 if (empty($comments)) {
393                         return 0;
394                 }
395
396                 $this->cache->set($cache_key, $comments, Duration::HALF_HOUR);
397                 $this->logger->debug('Calculated median comments', ['divider' => $divider, 'languages' => $languages, 'median' => $comments]);
398                 return $comments;
399         }
400
401         private function getMedianActivities(int $uid, int $divider): int
402         {
403                 $languages  = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid)]);
404                 $cache_key  = 'Channel:getMedianActivities:' . $divider . ':' . implode(':', $languages);
405                 $activities = $this->cache->get($cache_key);
406                 if (!empty($activities)) {
407                         return $activities;
408                 }
409
410                 $condition = ["`contact-type` != ? AND `activities` > ? AND NOT `restricted`", Contact::TYPE_COMMUNITY, 0];
411                 $condition = $this->addLanguageCondition($uid, $condition);
412
413                 $limit      = $this->database->count('post-engagement', $condition) / $divider;
414                 $post       = $this->database->selectToArray('post-engagement', ['activities'], $condition, ['order' => ['activities' => true], 'limit' => [$limit, 1]]);
415                 $activities = $post[0]['activities'] ?? 0;
416                 if (empty($activities)) {
417                         return 0;
418                 }
419
420                 $this->cache->set($cache_key, $activities, Duration::HALF_HOUR);
421                 $this->logger->debug('Calculated median activities', ['divider' => $divider, 'languages' => $languages, 'median' => $activities]);
422                 return $activities;
423         }
424
425         private function getMedianRelationThreadScore(int $cid, int $divider): int
426         {
427                 $cache_key = 'Channel:getThreadScore:' . $cid . ':' . $divider;
428                 $score     = $this->cache->get($cache_key);
429                 if (!empty($score)) {
430                         return $score;
431                 }
432
433                 $condition = ["`relation-cid` = ? AND `relation-thread-score` > ?", $cid, 0];
434
435                 $limit    = $this->database->count('contact-relation', $condition) / $divider;
436                 $relation = $this->database->selectToArray('contact-relation', ['relation-thread-score'], $condition, ['order' => ['relation-thread-score' => true], 'limit' => [$limit, 1]]);
437                 $score    = $relation[0]['relation-thread-score'] ?? 0;
438                 if (empty($score)) {
439                         return 0;
440                 }
441
442                 $this->cache->set($cache_key, $score, Duration::HALF_HOUR);
443                 $this->logger->debug('Calculated median score', ['cid' => $cid, 'divider' => $divider, 'median' => $score]);
444                 return $score;
445         }
446
447         /**
448          * Computes the displayed items.
449          *
450          * Community pages have a restriction on how many successive posts by the same author can show on any given page,
451          * so we may have to retrieve more content beyond the first query
452          *
453          * @return array
454          * @throws \Exception
455          */
456         protected function getCommunityItems()
457         {
458                 $items = $this->selectItems();
459
460                 $maxpostperauthor = (int) $this->config->get('system', 'max_author_posts_community_page');
461                 if ($maxpostperauthor != 0 && $this->selectedTab == 'local') {
462                         $count          = 1;
463                         $previousauthor = '';
464                         $numposts       = 0;
465                         $selected_items = [];
466
467                         while (count($selected_items) < $this->itemsPerPage && ++$count < 50 && count($items) > 0) {
468                                 foreach ($items as $item) {
469                                         if ($previousauthor == $item["author-link"]) {
470                                                 ++$numposts;
471                                         } else {
472                                                 $numposts = 0;
473                                         }
474                                         $previousauthor = $item["author-link"];
475
476                                         if (($numposts < $maxpostperauthor) && (count($selected_items) < $this->itemsPerPage)) {
477                                                 $selected_items[] = $item;
478                                         }
479                                 }
480
481                                 // If we're looking at a "previous page", the lookup continues forward in time because the list is
482                                 // sorted in chronologically decreasing order
483                                 if (isset($this->minId)) {
484                                         $this->minId = $items[0]['received'];
485                                 } else {
486                                         // In any other case, the lookup continues backwards in time
487                                         $this->maxId = $items[count($items) - 1]['received'];
488                                 }
489
490                                 $items = $this->selectItems();
491                         }
492                 } else {
493                         $selected_items = $items;
494                 }
495
496                 $condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => array_column($selected_items, 'uri-id')];
497                 $this->setItemsSeenByCondition($condition);
498
499                 return $selected_items;
500         }
501
502         /**
503          * Database query for the community page
504          *
505          * @return array
506          * @throws \Exception
507          * @TODO Move to repository/factory
508          */
509         private function selectItems()
510         {
511                 if ($this->selectedTab == 'local') {
512                         $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
513                 } elseif ($this->selectedTab == 'global') {
514                         $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
515                 } else {
516                         return [];
517                 }
518
519                 if (!is_null($this->accountType)) {
520                         $condition = DBA::mergeConditions($condition, ['owner-contact-type' => $this->accountType]);
521                 }
522
523                 $params = ['order' => ['received' => true], 'limit' => $this->itemsPerPage];
524
525                 if (!empty($this->itemUriId)) {
526                         $condition = DBA::mergeConditions($condition, ['uri-id' => $this->itemUriId]);
527                 } else {
528                         if ($this->session->getLocalUserId() && $this->noSharer) {
529                                 $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()]);
530                         }
531
532                         if (isset($this->maxId)) {
533                                 $condition = DBA::mergeConditions($condition, ["`received` < ?", $this->maxId]);
534                         }
535
536                         if (isset($this->minId)) {
537                                 $condition = DBA::mergeConditions($condition, ["`received` > ?", $this->minId]);
538
539                                 // Previous page case: we want the items closest to min_id but for that we need to reverse the query order
540                                 if (!isset($this->maxId)) {
541                                         $params['order']['received'] = false;
542                                 }
543                         }
544                 }
545
546                 $r = Post::selectThreadForUser($this->session->getLocalUserId() ?: 0, ['uri-id', 'received', 'author-link'], $condition, $params);
547
548                 $items = Post::toArray($r);
549                 if (empty($items)) {
550                         return [];
551                 }
552
553                 // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order
554                 if (empty($this->itemUriId) && isset($this->minId) && !isset($this->maxId)) {
555                         $items = array_reverse($items);
556                 }
557
558                 return $items;
559         }
560
561         /**
562          * Sets items as seen
563          *
564          * @param array $condition The array with the SQL condition
565          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
566          */
567         protected function setItemsSeenByCondition(array $condition)
568         {
569                 if (empty($condition)) {
570                         return;
571                 }
572
573                 $unseen = Post::exists($condition);
574
575                 if ($unseen) {
576                         /// @todo handle huge "unseen" updates in the background to avoid timeout errors
577                         Item::update(['unseen' => false], $condition);
578                 }
579         }
580 }