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