]> git.mxchange.org Git - friendica.git/blob - src/Module/Conversation/Community.php
Avoid jumps in the timeline after "received" has been selected
[friendica.git] / src / Module / Conversation / Community.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  * See update_profile.php for documentation
21  */
22
23 namespace Friendica\Module\Conversation;
24
25 use Friendica\App;
26 use Friendica\App\Mode;
27 use Friendica\Content\BoundariesPager;
28 use Friendica\Content\Conversation;
29 use Friendica\Content\Conversation\Entity\Timeline as TimelineEntity;
30 use Friendica\Content\Conversation\Factory\Timeline as TimelineFactory;
31 use Friendica\Content\Feature;
32 use Friendica\Content\Nav;
33 use Friendica\Content\Text\HTML;
34 use Friendica\Content\Widget;
35 use Friendica\Content\Widget\TrendingTags;
36 use Friendica\Core\Cache\Capability\ICanCache;
37 use Friendica\Core\Config\Capability\IManageConfigValues;
38 use Friendica\Core\L10n;
39 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
40 use Friendica\Core\Renderer;
41 use Friendica\Core\Session\Capability\IHandleUserSessions;
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 Community extends Timeline
50 {
51         /**
52          * Type of the community page
53          * @{
54          */
55         const DISABLED         = -2;
56         const DISABLED_VISITOR = -1;
57         const LOCAL            = 0;
58         const GLOBAL           = 1;
59         const LOCAL_AND_GLOBAL = 2;
60
61         protected $pageStyle;
62
63         /** @var TimelineFactory */
64         protected $timeline;
65         /** @var Conversation */
66         protected $conversation;
67         /** @var App\Page */
68         protected $page;
69         /** @var SystemMessages */
70         protected $systemMessages;
71
72         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 = [])
73         {
74                 parent::__construct($mode, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
75
76                 $this->timeline       = $timeline;
77                 $this->conversation   = $conversation;
78                 $this->page           = $page;
79                 $this->systemMessages = $systemMessages;
80         }
81
82         protected function content(array $request = []): string
83         {
84                 $this->parseRequest($request);
85
86                 $t = Renderer::getMarkupTemplate("community.tpl");
87                 $o = Renderer::replaceMacros($t, [
88                         '$content' => '',
89                         '$header' => '',
90                         '$show_global_community_hint' => ($this->selectedTab == TimelineEntity::GLOBAL) && $this->config->get('system', 'show_global_community_hint'),
91                         '$global_community_hint' => $this->l10n->t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
92                 ]);
93
94                 if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'infinite_scroll')) {
95                         $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
96                         $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => $this->args->getQueryString()]);
97                 }
98
99                 if (empty($request['mode']) || ($request['mode'] != 'raw')) {
100                         $tabs    = $this->getTabArray($this->timeline->getCommunities($this->session->isAuthenticated()), 'community');
101                         $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
102                         $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
103
104                         Nav::setSelected('community');
105
106                         $this->page['aside'] .= Widget::accountTypes('community/' . $this->selectedTab, $this->accountTypeString);
107
108                         if ($this->session->getLocalUserId() && $this->config->get('system', 'community_no_sharer')) {
109                                 $this->page['aside'] .= $this->getNoSharerWidget('community');
110                         }
111
112                         if (Feature::isEnabled($this->session->getLocalUserId(), 'trending_tags')) {
113                                 $this->page['aside'] .= TrendingTags::getHTML($this->selectedTab);
114                         }
115
116                         // We need the editor here to be able to reshare an item.
117                         if ($this->session->isAuthenticated()) {
118                                 $o .= $this->conversation->statusEditor([], 0, true);
119                         }
120                 }
121
122                 $items = $this->getCommunityItems();
123
124                 if (!$this->database->isResult($items)) {
125                         $this->systemMessages->addNotice($this->l10n->t('No results.'));
126                         return $o;
127                 }
128
129                 $o .= $this->conversation->render($items, Conversation::MODE_COMMUNITY, false, false, 'received', $this->session->getLocalUserId());
130
131                 $pager = new BoundariesPager(
132                         $this->l10n,
133                         $this->args->getQueryString(),
134                         $items[0]['received'],
135                         $items[count($items) - 1]['received'],
136                         $this->itemsPerPage
137                 );
138
139                 if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'infinite_scroll')) {
140                         $o .= HTML::scrollLoader();
141                 } else {
142                         $o .= $pager->renderMinimal(count($items));
143                 }
144
145                 return $o;
146         }
147
148         /**
149          * Computes module parameters from the request and local configuration
150          *
151          * @throws HTTPException\BadRequestException
152          * @throws HTTPException\ForbiddenException
153          */
154         protected function parseRequest($request)
155         {
156                 parent::parseRequest($request);
157
158                 if ($this->config->get('system', 'block_public') && !$this->session->isAuthenticated()) {
159                         throw new HTTPException\ForbiddenException($this->l10n->t('Public access denied.'));
160                 }
161
162                 $this->pageStyle = $this->config->get('system', 'community_page_style');
163
164                 if ($this->pageStyle == self::DISABLED) {
165                         throw new HTTPException\ForbiddenException($this->l10n->t('Access denied.'));
166                 }
167
168                 if (!$this->selectedTab) {
169                         if (!empty($this->config->get('system', 'singleuser'))) {
170                                 // On single user systems only the global page does make sense
171                                 $this->selectedTab = TimelineEntity::GLOBAL;
172                         } else {
173                                 // When only the global community is allowed, we use this as default
174                                 $this->selectedTab = $this->pageStyle == self::GLOBAL ? TimelineEntity::GLOBAL : TimelineEntity::LOCAL;
175                         }
176                 }
177
178                 if (!$this->timeline->isCommunity($this->selectedTab)) {
179                         throw new HTTPException\BadRequestException($this->l10n->t('Community option not available.'));
180                 }
181
182                 // Check if we are allowed to display the content to visitors
183                 if (!$this->session->isAuthenticated()) {
184                         $available = $this->pageStyle == self::LOCAL_AND_GLOBAL;
185
186                         if (!$available) {
187                                 $available = ($this->pageStyle == self::LOCAL) && ($this->selectedTab == TimelineEntity::LOCAL);
188                         }
189
190                         if (!$available) {
191                                 $available = ($this->pageStyle == self::GLOBAL) && ($this->selectedTab == TimelineEntity::GLOBAL);
192                         }
193
194                         if (!$available) {
195                                 throw new HTTPException\ForbiddenException($this->l10n->t('Not available.'));
196                         }
197                 }
198
199                 $this->maxId = $request['last_received'] ?? $this->maxId;
200                 $this->minId = $request['first_received'] ?? $this->minId;
201         }
202 }