3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
20 * See update_profile.php for documentation
23 namespace Friendica\Module\Conversation;
25 use Friendica\BaseModule;
26 use Friendica\Content\BoundariesPager;
27 use Friendica\Content\Conversation;
28 use Friendica\Content\Feature;
29 use Friendica\Content\Nav;
30 use Friendica\Content\Text\HTML;
31 use Friendica\Content\Widget;
32 use Friendica\Content\Widget\TrendingTags;
33 use Friendica\Core\Renderer;
34 use Friendica\Database\DBA;
36 use Friendica\Model\Item;
37 use Friendica\Model\Post;
38 use Friendica\Model\User;
39 use Friendica\Network\HTTPException;
41 class Community extends BaseModule
44 * Type of the community page
48 const DISABLED_VISITOR = -1;
51 const LOCAL_AND_GLOBAL = 2;
56 protected static $page_style;
57 protected static $content;
58 protected static $accountTypeString;
59 protected static $accountType;
60 protected static $itemsPerPage;
61 protected static $min_id;
62 protected static $max_id;
63 protected static $item_id;
65 protected function content(array $request = []): string
67 $this->parseRequest();
69 $t = Renderer::getMarkupTemplate("community.tpl");
70 $o = Renderer::replaceMacros($t, [
73 '$show_global_community_hint' => (self::$content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
74 '$global_community_hint' => DI::l10n()->t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
77 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
78 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
79 $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
82 if (empty($_GET['mode']) || ($_GET['mode'] != 'raw')) {
85 if ((DI::userSession()->isAuthenticated() || in_array(self::$page_style, [self::LOCAL_AND_GLOBAL, self::LOCAL])) && empty(DI::config()->get('system', 'singleuser'))) {
87 'label' => DI::l10n()->t('Local Community'),
88 'url' => 'community/local',
89 'sel' => self::$content == 'local' ? 'active' : '',
90 'title' => DI::l10n()->t('Posts from local users on this server'),
91 'id' => 'community-local-tab',
96 if (DI::userSession()->isAuthenticated() || in_array(self::$page_style, [self::LOCAL_AND_GLOBAL, self::GLOBAL])) {
98 'label' => DI::l10n()->t('Global Community'),
99 'url' => 'community/global',
100 'sel' => self::$content == 'global' ? 'active' : '',
101 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
102 'id' => 'community-global-tab',
107 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
108 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
110 Nav::setSelected('community');
112 DI::page()['aside'] .= Widget::accountTypes('community/' . self::$content, self::$accountTypeString);
114 if (DI::userSession()->getLocalUserId() && DI::config()->get('system', 'community_no_sharer')) {
115 $path = self::$content;
116 if (!empty($this->parameters['accounttype'])) {
117 $path .= '/' . $this->parameters['accounttype'];
119 $query_parameters = [];
121 if (!empty($_GET['min_id'])) {
122 $query_parameters['min_id'] = $_GET['min_id'];
124 if (!empty($_GET['max_id'])) {
125 $query_parameters['max_id'] = $_GET['max_id'];
127 if (!empty($_GET['last_commented'])) {
128 $query_parameters['max_id'] = $_GET['last_commented'];
131 $path_all = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : '');
132 $path_no_sharer = $path . '?' . http_build_query(array_merge($query_parameters, ['no_sharer' => true]));
133 DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_sharer.tpl'), [
134 '$title' => DI::l10n()->t('Own Contacts'),
135 '$path_all' => $path_all,
136 '$path_no_sharer' => $path_no_sharer,
137 '$no_sharer' => !empty($_REQUEST['no_sharer']),
138 '$all' => DI::l10n()->t('Include'),
139 '$no_sharer_label' => DI::l10n()->t('Hide'),
143 if (Feature::isEnabled(DI::userSession()->getLocalUserId(), 'trending_tags')) {
144 DI::page()['aside'] .= TrendingTags::getHTML(self::$content);
147 // We need the editor here to be able to reshare an item.
148 if (DI::userSession()->isAuthenticated()) {
149 $o .= DI::conversation()->statusEditor([], 0, true);
153 $items = self::getItems();
155 if (!DBA::isResult($items)) {
156 DI::sysmsg()->addNotice(DI::l10n()->t('No results.'));
160 $o .= DI::conversation()->render($items, Conversation::MODE_COMMUNITY, false, false, 'commented', DI::userSession()->getLocalUserId());
162 $pager = new BoundariesPager(
164 DI::args()->getQueryString(),
165 $items[0]['commented'],
166 $items[count($items) - 1]['commented'],
170 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
171 $o .= HTML::scrollLoader();
173 $o .= $pager->renderMinimal(count($items));
180 * Computes module parameters from the request and local configuration
182 * @throws HTTPException\BadRequestException
183 * @throws HTTPException\ForbiddenException
185 protected function parseRequest()
187 if (DI::config()->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) {
188 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
191 self::$page_style = DI::config()->get('system', 'community_page_style');
193 if (self::$page_style == self::DISABLED) {
194 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
197 self::$accountTypeString = $_GET['accounttype'] ?? $this->parameters['accounttype'] ?? '';
198 self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
200 self::$content = $this->parameters['content'] ?? '';
201 if (!self::$content) {
202 if (!empty(DI::config()->get('system', 'singleuser'))) {
203 // On single user systems only the global page does make sense
204 self::$content = 'global';
206 // When only the global community is allowed, we use this as default
207 self::$content = self::$page_style == self::GLOBAL ? 'global' : 'local';
211 if (!in_array(self::$content, ['local', 'global'])) {
212 throw new HTTPException\BadRequestException(DI::l10n()->t('Community option not available.'));
215 // Check if we are allowed to display the content to visitors
216 if (!DI::userSession()->isAuthenticated()) {
217 $available = self::$page_style == self::LOCAL_AND_GLOBAL;
220 $available = (self::$page_style == self::LOCAL) && (self::$content == 'local');
224 $available = (self::$page_style == self::GLOBAL) && (self::$content == 'global');
228 throw new HTTPException\ForbiddenException(DI::l10n()->t('Not available.'));
232 if (DI::mode()->isMobile()) {
233 self::$itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_mobile_network',
234 DI::config()->get('system', 'itemspage_network_mobile'));
236 self::$itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_network',
237 DI::config()->get('system', 'itemspage_network'));
240 if (!empty($_GET['item'])) {
241 $item = Post::selectFirst(['parent'], ['id' => $_GET['item']]);
242 self::$item_id = $item['parent'] ?? 0;
247 self::$min_id = $_GET['min_id'] ?? null;
248 self::$max_id = $_GET['max_id'] ?? null;
249 self::$max_id = $_GET['last_commented'] ?? self::$max_id;
253 * Computes the displayed items.
255 * Community pages have a restriction on how many successive posts by the same author can show on any given page,
256 * so we may have to retrieve more content beyond the first query
261 protected static function getItems()
263 $items = self::selectItems(self::$min_id, self::$max_id, self::$item_id, self::$itemsPerPage);
265 $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
266 if ($maxpostperauthor != 0 && self::$content == 'local') {
268 $previousauthor = '';
270 $selected_items = [];
272 while (count($selected_items) < self::$itemsPerPage && ++$count < 50 && count($items) > 0) {
273 foreach ($items as $item) {
274 if ($previousauthor == $item["author-link"]) {
279 $previousauthor = $item["author-link"];
281 if (($numposts < $maxpostperauthor) && (count($selected_items) < self::$itemsPerPage)) {
282 $selected_items[] = $item;
286 // If we're looking at a "previous page", the lookup continues forward in time because the list is
287 // sorted in chronologically decreasing order
288 if (isset(self::$min_id)) {
289 self::$min_id = $items[0]['commented'];
291 // In any other case, the lookup continues backwards in time
292 self::$max_id = $items[count($items) - 1]['commented'];
295 $items = self::selectItems(self::$min_id, self::$max_id, self::$item_id, self::$itemsPerPage);
298 $selected_items = $items;
301 return $selected_items;
305 * Database query for the community page
312 * @TODO Move to repository/factory
314 private static function selectItems($min_id, $max_id, $item_id, $itemspage)
316 if (self::$content == 'local') {
317 if (!is_null(self::$accountType)) {
318 $condition = ["`wall` AND `origin` AND `private` = ? AND `owner-contact-type` = ?", Item::PUBLIC, self::$accountType];
320 $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
322 } elseif (self::$content == 'global') {
323 if (!is_null(self::$accountType)) {
324 $condition = ["`uid` = ? AND `private` = ? AND `owner-contact-type` = ?", 0, Item::PUBLIC, self::$accountType];
326 $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
332 $params = ['order' => ['commented' => true], 'limit' => $itemspage];
334 if (!empty($item_id)) {
335 $condition[0] .= " AND `id` = ?";
336 $condition[] = $item_id;
338 if (DI::userSession()->getLocalUserId() && !empty($_REQUEST['no_sharer'])) {
339 $condition[0] .= " AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ? AND `post-user`.`uri-id` = `post-thread-user-view`.`uri-id`)";
340 $condition[] = DI::userSession()->getLocalUserId();
343 if (isset($max_id)) {
344 $condition[0] .= " AND `commented` < ?";
345 $condition[] = $max_id;
348 if (isset($min_id)) {
349 $condition[0] .= " AND `commented` > ?";
350 $condition[] = $min_id;
352 // Previous page case: we want the items closest to min_id but for that we need to reverse the query order
353 if (!isset($max_id)) {
354 $params['order']['commented'] = false;
359 $r = Post::selectThreadForUser(0, ['uri-id', 'commented', 'author-link'], $condition, $params);
361 $items = Post::toArray($r);
366 // Previous page case: once we get the relevant items closest to min_id, we need to restore the expected display order
367 if (empty($item_id) && isset($min_id) && !isset($max_id)) {
368 $items = array_reverse($items);