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