]> git.mxchange.org Git - friendica.git/blob - src/Module/Conversation/Channel.php
Avoid jumps in the timeline after "received" has been selected
[friendica.git] / src / Module / Conversation / Channel.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\Content\BoundariesPager;
27 use Friendica\Content\Conversation;
28 use Friendica\Content\Conversation\Entity\Timeline as TimelineEntity;
29 use Friendica\Content\Conversation\Factory\Timeline as TimelineFactory;
30 use Friendica\Content\Feature;
31 use Friendica\Content\Nav;
32 use Friendica\Content\Text\HTML;
33 use Friendica\Content\Widget;
34 use Friendica\Content\Widget\TrendingTags;
35 use Friendica\Core\Cache\Capability\ICanCache;
36 use Friendica\Core\Config\Capability\IManageConfigValues;
37 use Friendica\Core\L10n;
38 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
39 use Friendica\Core\Renderer;
40 use Friendica\Core\Session\Capability\IHandleUserSessions;
41 use Friendica\Module\Security\Login;
42 use Friendica\Network\HTTPException;
43 use Friendica\Database\Database;
44 use Friendica\Module\Response;
45 use Friendica\Navigation\SystemMessages;
46 use Friendica\Util\Profiler;
47 use Psr\Log\LoggerInterface;
48
49 class Channel extends Timeline
50 {
51         /** @var TimelineFactory */
52         protected $timeline;
53         /** @var Conversation */
54         protected $conversation;
55         /** @var App\Page */
56         protected $page;
57         /** @var SystemMessages */
58         protected $systemMessages;
59
60         public function __construct(TimelineFactory $timeline, Conversation $conversation, App\Page $page, SystemMessages $systemMessages, 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 = [])
61         {
62                 parent::__construct($mode, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
63
64                 $this->timeline       = $timeline;
65                 $this->conversation   = $conversation;
66                 $this->page           = $page;
67                 $this->systemMessages = $systemMessages;
68         }
69
70         protected function content(array $request = []): string
71         {
72                 if (!$this->session->getLocalUserId()) {
73                         return Login::form();
74                 }
75
76                 $this->parseRequest($request);
77
78                 $t = Renderer::getMarkupTemplate("community.tpl");
79                 $o = Renderer::replaceMacros($t, [
80                         '$content' => '',
81                         '$header'  => '',
82                 ]);
83
84                 if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'infinite_scroll')) {
85                         $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
86                         $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => $this->args->getQueryString()]);
87                 }
88
89                 if (empty($request['mode']) || ($request['mode'] != 'raw')) {
90                         $tabs = $this->getTabArray($this->timeline->getChannelsForUser($this->session->getLocalUserId()), 'channel');
91                         $tabs = array_merge($tabs, $this->getTabArray($this->timeline->getCommunities(true), 'channel'));
92
93                         $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
94                         $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
95
96                         Nav::setSelected('channel');
97
98                         $this->page['aside'] .= Widget::accountTypes('channel/' . $this->selectedTab, $this->accountTypeString);
99
100                         if (!in_array($this->selectedTab, [TimelineEntity::FOLLOWERS, TimelineEntity::FORYOU]) && $this->config->get('system', 'community_no_sharer')) {
101                                 $this->page['aside'] .= $this->getNoSharerWidget('channel');
102                         }
103
104                         if (Feature::isEnabled($this->session->getLocalUserId(), 'trending_tags')) {
105                                 $this->page['aside'] .= TrendingTags::getHTML($this->selectedTab);
106                         }
107
108                         // We need the editor here to be able to reshare an item.
109                         $o .= $this->conversation->statusEditor([], 0, true);
110                 }
111
112                 if ($this->timeline->isChannel($this->selectedTab)) {
113                         $items = $this->getChannelItems();
114                         $order = 'created';
115                 } else {
116                         $items = $this->getCommunityItems();
117                         $order = 'commented';
118                 }
119
120                 if (!$this->database->isResult($items)) {
121                         $this->systemMessages->addNotice($this->l10n->t('No results.'));
122                         return $o;
123                 }
124
125                 $o .= $this->conversation->render($items, Conversation::MODE_CHANNEL, false, false, $order, $this->session->getLocalUserId());
126
127                 $pager = new BoundariesPager(
128                         $this->l10n,
129                         $this->args->getQueryString(),
130                         $items[0][$order],
131                         $items[count($items) - 1][$order],
132                         $this->itemsPerPage
133                 );
134
135                 if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'infinite_scroll')) {
136                         $o .= HTML::scrollLoader();
137                 } else {
138                         $o .= $pager->renderMinimal(count($items));
139                 }
140
141                 return $o;
142         }
143
144         /**
145          * Computes module parameters from the request and local configuration
146          *
147          * @throws HTTPException\BadRequestException
148          * @throws HTTPException\ForbiddenException
149          */
150         protected function parseRequest(array $request)
151         {
152                 parent::parseRequest($request);
153
154                 if (!$this->selectedTab) {
155                         $this->selectedTab = TimelineEntity::FORYOU;
156                 }
157
158                 if (!$this->timeline->isChannel($this->selectedTab) && !$this->timeline->isCommunity($this->selectedTab)) {
159                         throw new HTTPException\BadRequestException($this->l10n->t('Channel not available.'));
160                 }
161
162                 $this->maxId = $request['last_created'] ?? $this->maxId;
163                 $this->minId = $request['first_created'] ?? $this->minId;
164         }
165 }