3 * @copyright Copyright (C) 2020, Friendica
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\Feature;
28 use Friendica\Content\Nav;
29 use Friendica\Content\Text\HTML;
30 use Friendica\Content\Widget\TrendingTags;
31 use Friendica\Core\ACL;
32 use Friendica\Core\Renderer;
33 use Friendica\Core\Session;
34 use Friendica\Database\DBA;
36 use Friendica\Model\Item;
37 use Friendica\Model\User;
38 use Friendica\Network\HTTPException;
40 class Community extends BaseModule
42 protected static $page_style;
43 protected static $content;
44 protected static $accounttype;
45 protected static $itemsPerPage;
46 protected static $since_id;
47 protected static $max_id;
49 public static function content(array $parameters = [])
51 self::parseRequest($parameters);
53 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
54 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
55 $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
60 if (empty($_GET['mode']) || ($_GET['mode'] != 'raw')) {
63 if ((Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
65 'label' => DI::l10n()->t('Local Community'),
66 'url' => 'community/local',
67 'sel' => self::$content == 'local' ? 'active' : '',
68 'title' => DI::l10n()->t('Posts from local users on this server'),
69 'id' => 'community-local-tab',
74 if (Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
76 'label' => DI::l10n()->t('Global Community'),
77 'url' => 'community/global',
78 'sel' => self::$content == 'global' ? 'active' : '',
79 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
80 'id' => 'community-global-tab',
85 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
86 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
88 Nav::setSelected('community');
90 DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_accounts.tpl'), [
91 '$title' => DI::l10n()->t('Accounts'),
92 '$content' => self::$content,
93 '$accounttype' => ($parameters['accounttype'] ?? ''),
94 '$all' => DI::l10n()->t('All'),
95 '$person' => DI::l10n()->t('Persons'),
96 '$organisation' => DI::l10n()->t('Organisations'),
97 '$news' => DI::l10n()->t('News'),
98 '$community' => DI::l10n()->t('Forums'),
101 if (local_user() && DI::config()->get('system', 'community_no_sharer')) {
102 $path = self::$content;
103 if (!empty($parameters['accounttype'])) {
104 $path .= '/' . $parameters['accounttype'];
106 $query_parameters = [];
108 if (!empty($_GET['since_id'])) {
109 $query_parameters['since_id'] = $_GET['since_id'];
111 if (!empty($_GET['max_id'])) {
112 $query_parameters['max_id'] = $_GET['max_id'];
114 if (!empty($_GET['last_commented'])) {
115 $query_parameters['max_id'] = $_GET['last_commented'];
118 $path_all = $path . (!empty($query_parameters) ? '?' . http_build_query($query_parameters) : '');
119 $path_no_sharer = $path . '?' . http_build_query(array_merge($query_parameters, ['no_sharer' => true]));
120 DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_sharer.tpl'), [
121 '$title' => DI::l10n()->t('Own Contacts'),
122 '$path_all' => $path_all,
123 '$path_no_sharer' => $path_no_sharer,
124 '$no_sharer' => !empty($_REQUEST['no_sharer']),
125 '$all' => DI::l10n()->t('Include'),
126 '$no_sharer_label' => DI::l10n()->t('Hide'),
130 if (Feature::isEnabled(local_user(), 'trending_tags')) {
131 DI::page()['aside'] .= TrendingTags::getHTML(self::$content);
134 // We need the editor here to be able to reshare an item.
135 if (Session::isAuthenticated()) {
138 'allow_location' => DI::app()->user['allow_location'],
139 'default_location' => DI::app()->user['default-location'],
140 'nickname' => DI::app()->user['nickname'],
141 'lockstate' => (is_array(DI::app()->user) && (strlen(DI::app()->user['allow_cid']) || strlen(DI::app()->user['allow_gid']) || strlen(DI::app()->user['deny_cid']) || strlen(DI::app()->user['deny_gid'])) ? 'lock' : 'unlock'),
142 'acl' => ACL::getFullSelectorHTML(DI::page(), DI::app()->user, true),
144 'visitor' => 'block',
145 'profile_uid' => local_user(),
147 $o .= status_editor(DI::app(), $x, 0, true);
151 $items = self::getItems();
153 if (!DBA::isResult($items)) {
154 notice(DI::l10n()->t('No results.'));
158 $o .= conversation(DI::app(), $items, 'community', false, false, 'commented', local_user());
160 $pager = new BoundariesPager(
162 DI::args()->getQueryString(),
163 $items[0]['commented'],
164 $items[count($items) - 1]['commented'],
168 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
169 $o .= HTML::scrollLoader();
171 $o .= $pager->renderMinimal(count($items));
174 $t = Renderer::getMarkupTemplate("community.tpl");
175 return Renderer::replaceMacros($t, [
178 '$show_global_community_hint' => (self::$content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
179 '$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.")
184 * Computes module parameters from the request and local configuration
186 * @param array $parameters
187 * @throws HTTPException\BadRequestException
188 * @throws HTTPException\ForbiddenException
190 protected static function parseRequest(array $parameters)
192 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
193 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
196 self::$page_style = DI::config()->get('system', 'community_page_style');
198 if (self::$page_style == CP_NO_INTERNAL_COMMUNITY) {
199 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
202 switch ($parameters['accounttype'] ?? '') {
204 self::$accounttype = User::ACCOUNT_TYPE_PERSON;
207 self::$accounttype = User::ACCOUNT_TYPE_ORGANISATION;
210 self::$accounttype = User::ACCOUNT_TYPE_NEWS;
213 self::$accounttype = User::ACCOUNT_TYPE_COMMUNITY;
216 self::$accounttype = null;
220 self::$content = $parameters['content'] ?? '';
221 if (!self::$content) {
222 if (!empty(DI::config()->get('system', 'singleuser'))) {
223 // On single user systems only the global page does make sense
224 self::$content = 'global';
226 // When only the global community is allowed, we use this as default
227 self::$content = self::$page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
231 if (!in_array(self::$content, ['local', 'global'])) {
232 throw new HTTPException\BadRequestException(DI::l10n()->t('Community option not available.'));
235 // Check if we are allowed to display the content to visitors
236 if (!Session::isAuthenticated()) {
237 $available = self::$page_style == CP_USERS_AND_GLOBAL;
240 $available = (self::$page_style == CP_USERS_ON_SERVER) && (self::$content == 'local');
244 $available = (self::$page_style == CP_GLOBAL_COMMUNITY) && (self::$content == 'global');
248 throw new HTTPException\ForbiddenException(DI::l10n()->t('Not available.'));
252 if (DI::mode()->isMobile()) {
253 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
254 DI::config()->get('system', 'itemspage_network_mobile'));
256 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
257 DI::config()->get('system', 'itemspage_network'));
260 // now that we have the user settings, see if the theme forces
261 // a maximum item number which is lower then the user choice
262 if ((DI::app()->force_max_items > 0) && (DI::app()->force_max_items < self::$itemsPerPage)) {
263 self::$itemsPerPage = DI::app()->force_max_items;
266 self::$since_id = $_GET['since_id'] ?? null;
267 self::$max_id = $_GET['max_id'] ?? null;
268 self::$max_id = $_GET['last_commented'] ?? self::$max_id;
272 * Computes the displayed items.
274 * Community pages have a restriction on how many successive posts by the same author can show on any given page,
275 * so we may have to retrieve more content beyond the first query
280 protected static function getItems()
282 $items = self::selectItems(self::$since_id, self::$max_id, self::$itemsPerPage);
284 $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
285 if ($maxpostperauthor != 0 && self::$content == 'local') {
287 $previousauthor = '';
289 $selected_items = [];
291 while (count($selected_items) < self::$itemsPerPage && ++$count < 50 && count($items) > 0) {
292 foreach ($items as $item) {
293 if ($previousauthor == $item["author-link"]) {
298 $previousauthor = $item["author-link"];
300 if (($numposts < $maxpostperauthor) && (count($selected_items) < self::$itemsPerPage)) {
301 $selected_items[] = $item;
305 // If we're looking at a "previous page", the lookup continues forward in time because the list is
306 // sorted in chronologically decreasing order
307 if (isset(self::$since_id)) {
308 self::$since_id = $items[0]['commented'];
310 // In any other case, the lookup continues backwards in time
311 self::$max_id = $items[count($items) - 1]['commented'];
314 $items = self::selectItems(self::$since_id, self::$max_id, self::$itemsPerPage);
317 $selected_items = $items;
320 return $selected_items;
324 * Database query for the comunity page
331 * @TODO Move to repository/factory
333 private static function selectItems($since_id, $max_id, $itemspage)
337 if (self::$content == 'local') {
338 if (!is_null(self::$accounttype)) {
339 $condition = ["`wall` AND `origin` AND `private` = ? AND `owner`.`contact-type` = ?", Item::PUBLIC, self::$accounttype];
341 $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
343 } elseif (self::$content == 'global') {
344 if (!is_null(self::$accounttype)) {
345 $condition = ["`uid` = ? AND `private` = ? AND `owner`.`contact-type` = ?", 0, Item::PUBLIC, self::$accounttype];
347 $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
353 if (local_user() && !empty($_REQUEST['no_sharer'])) {
354 $condition[0] .= " AND NOT EXISTS (SELECT `uri-id` FROM `thread` AS t1 WHERE `t1`.`uri-id` = `thread`.`uri-id` AND `t1`.`uid` = ?)";
355 $condition[] = local_user();
358 if (isset($max_id)) {
359 $condition[0] .= " AND `commented` < ?";
360 $condition[] = $max_id;
363 if (isset($since_id)) {
364 $condition[0] .= " AND `commented` > ?";
365 $condition[] = $since_id;
368 $r = Item::selectThreadForUser(0, ['uri', 'commented', 'author-link'], $condition, ['order' => ['commented' => true], 'limit' => $itemspage]);
370 return DBA::toArray($r);