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