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\Widget\TrendingTags;
30 use Friendica\Core\ACL;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session;
33 use Friendica\Database\DBA;
35 use Friendica\Model\Item;
36 use Friendica\Model\User;
37 use Friendica\Network\HTTPException;
39 class Community extends BaseModule
41 protected static $page_style;
42 protected static $content;
43 protected static $accounttype;
44 protected static $itemsPerPage;
45 protected static $since_id;
46 protected static $max_id;
48 public static function content(array $parameters = [])
50 self::parseRequest($parameters);
54 if ((Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
56 'label' => DI::l10n()->t('Local Community'),
57 'url' => 'community/local',
58 'sel' => self::$content == 'local' ? 'active' : '',
59 'title' => DI::l10n()->t('Posts from local users on this server'),
60 'id' => 'community-local-tab',
65 if (Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
67 'label' => DI::l10n()->t('Global Community'),
68 'url' => 'community/global',
69 'sel' => self::$content == 'global' ? 'active' : '',
70 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
71 'id' => 'community-global-tab',
76 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
77 $o = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
79 Nav::setSelected('community');
81 $items = self::getItems();
83 if (!DBA::isResult($items)) {
84 notice(DI::l10n()->t('No results.'));
88 // We need the editor here to be able to reshare an item.
89 if (Session::isAuthenticated()) {
92 'allow_location' => DI::app()->user['allow_location'],
93 'default_location' => DI::app()->user['default-location'],
94 'nickname' => DI::app()->user['nickname'],
95 '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'),
96 'acl' => ACL::getFullSelectorHTML(DI::page(), DI::app()->user, true),
99 'profile_uid' => local_user(),
101 $o .= status_editor(DI::app(), $x, 0, true);
104 $o .= conversation(DI::app(), $items, 'community', false, false, 'commented', local_user());
106 $pager = new BoundariesPager(
108 DI::args()->getQueryString(),
109 $items[0]['commented'],
110 $items[count($items) - 1]['commented'],
114 $o .= $pager->renderMinimal(count($items));
116 DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_accounts.tpl'), [
117 '$content' => self::$content,
118 '$title' => DI::l10n()->t('Accounts'),
119 '$all' => DI::l10n()->t('All Accounts'),
120 '$all_selected' => ($parameters['accounttype'] == '') ? 'selected' : '',
121 '$person' => DI::l10n()->t('Personal Accounts'),
122 '$person_selected' => ($parameters['accounttype'] == 'person') ? 'selected' : '',
123 '$organisation' => DI::l10n()->t('Organisation Accounts'),
124 '$organisation_selected' => ($parameters['accounttype'] == 'organisation') ? 'selected' : '',
125 '$news' => DI::l10n()->t('News Accounts'),
126 '$news_selected' => ($parameters['accounttype'] == 'news') ? 'selected' : '',
127 '$community' => DI::l10n()->t('Communities'),
128 '$community_selected' => ($parameters['accounttype'] == 'community') ? 'selected' : '',
131 if (Feature::isEnabled(local_user(), 'trending_tags')) {
132 DI::page()['aside'] .= TrendingTags::getHTML(self::$content);
135 $t = Renderer::getMarkupTemplate("community.tpl");
136 return Renderer::replaceMacros($t, [
139 '$show_global_community_hint' => (self::$content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
140 '$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.")
145 * Computes module parameters from the request and local configuration
147 * @param array $parameters
148 * @throws HTTPException\BadRequestException
149 * @throws HTTPException\ForbiddenException
151 protected static function parseRequest(array $parameters)
153 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
154 throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
157 self::$page_style = DI::config()->get('system', 'community_page_style');
159 if (self::$page_style == CP_NO_INTERNAL_COMMUNITY) {
160 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
163 switch ($parameters['accounttype'] ?? '') {
165 self::$accounttype = User::ACCOUNT_TYPE_PERSON;
168 self::$accounttype = User::ACCOUNT_TYPE_ORGANISATION;
171 self::$accounttype = User::ACCOUNT_TYPE_NEWS;
174 self::$accounttype = User::ACCOUNT_TYPE_COMMUNITY;
177 self::$accounttype = null;
181 self::$content = $parameters['content'] ?? '';
182 if (!self::$content) {
183 if (!empty(DI::config()->get('system', 'singleuser'))) {
184 // On single user systems only the global page does make sense
185 self::$content = 'global';
187 // When only the global community is allowed, we use this as default
188 self::$content = self::$page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
192 if (!in_array(self::$content, ['local', 'global'])) {
193 throw new HTTPException\BadRequestException(DI::l10n()->t('Community option not available.'));
196 // Check if we are allowed to display the content to visitors
197 if (!Session::isAuthenticated()) {
198 $available = self::$page_style == CP_USERS_AND_GLOBAL;
201 $available = (self::$page_style == CP_USERS_ON_SERVER) && (self::$content == 'local');
205 $available = (self::$page_style == CP_GLOBAL_COMMUNITY) && (self::$content == 'global');
209 throw new HTTPException\ForbiddenException(DI::l10n()->t('Not available.'));
213 if (DI::mode()->isMobile()) {
214 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
215 DI::config()->get('system', 'itemspage_network_mobile'));
217 self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
218 DI::config()->get('system', 'itemspage_network'));
221 // now that we have the user settings, see if the theme forces
222 // a maximum item number which is lower then the user choice
223 if ((DI::app()->force_max_items > 0) && (DI::app()->force_max_items < self::$itemsPerPage)) {
224 self::$itemsPerPage = DI::app()->force_max_items;
227 self::$since_id = $_GET['since_id'] ?? null;
228 self::$max_id = $_GET['max_id'] ?? null;
232 * Computes the displayed items.
234 * Community pages have a restriction on how many successive posts by the same author can show on any given page,
235 * so we may have to retrieve more content beyond the first query
240 protected static function getItems()
242 $items = self::selectItems(self::$since_id, self::$max_id, self::$itemsPerPage);
244 $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
245 if ($maxpostperauthor != 0 && self::$content == 'local') {
247 $previousauthor = '';
249 $selected_items = [];
251 while (count($selected_items) < self::$itemsPerPage && ++$count < 50 && count($items) > 0) {
252 foreach ($items as $item) {
253 if ($previousauthor == $item["author-link"]) {
258 $previousauthor = $item["author-link"];
260 if (($numposts < $maxpostperauthor) && (count($selected_items) < self::$itemsPerPage)) {
261 $selected_items[] = $item;
265 // If we're looking at a "previous page", the lookup continues forward in time because the list is
266 // sorted in chronologically decreasing order
267 if (isset(self::$since_id)) {
268 self::$since_id = $items[0]['commented'];
270 // In any other case, the lookup continues backwards in time
271 self::$max_id = $items[count($items) - 1]['commented'];
274 $items = self::selectItems(self::$since_id, self::$max_id, self::$itemsPerPage);
277 $selected_items = $items;
280 return $selected_items;
284 * Database query for the comunity page
291 * @TODO Move to repository/factory
293 private static function selectItems($since_id, $max_id, $itemspage)
297 if (self::$content == 'local') {
298 if (!is_null(self::$accounttype)) {
299 $condition = ["`wall` AND `origin` AND `private` = ? AND `owner`.`contact-type` = ?", Item::PUBLIC, self::$accounttype];
301 $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
303 } elseif (self::$content == 'global') {
304 if (!is_null(self::$accounttype)) {
305 $condition = ["`uid` = ? AND `private` = ? AND `owner`.`contact-type` = ?", 0, Item::PUBLIC, self::$accounttype];
307 $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
313 if (isset($max_id)) {
314 $condition[0] .= " AND `commented` < ?";
315 $condition[] = $max_id;
318 if (isset($since_id)) {
319 $condition[0] .= " AND `commented` > ?";
320 $condition[] = $since_id;
323 $r = Item::selectThreadForUser(0, ['uri', 'commented', 'author-link'], $condition, ['order' => ['commented' => true], 'limit' => $itemspage]);
325 return DBA::toArray($r);