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