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/>.
23 use Friendica\Content\Feature;
24 use Friendica\Content\Nav;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Widget\TrendingTags;
27 use Friendica\Core\ACL;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session;
30 use Friendica\Database\DBA;
32 use Friendica\Model\Item;
33 use Friendica\Model\User;
35 function community_content(App $a, $update = 0)
39 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
40 notice(DI::l10n()->t('Public access denied.') . EOL);
44 $page_style = DI::config()->get('system', 'community_page_style');
46 if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
47 notice(DI::l10n()->t('Access denied.') . EOL);
54 switch ($a->argv[2]) {
56 $accounttype = User::ACCOUNT_TYPE_PERSON;
59 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
62 $accounttype = User::ACCOUNT_TYPE_NEWS;
65 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
71 $content = $a->argv[1];
73 if (!empty(DI::config()->get('system', 'singleuser'))) {
74 // On single user systems only the global page does make sense
77 // When only the global community is allowed, we use this as default
78 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
82 if (!in_array($content, ['local', 'global'])) {
83 notice(DI::l10n()->t('Community option not available.') . EOL);
87 // Check if we are allowed to display the content to visitors
89 $available = $page_style == CP_USERS_AND_GLOBAL;
92 $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
96 $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
100 notice(DI::l10n()->t('Not available.') . EOL);
108 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
110 'label' => DI::l10n()->t('Local Community'),
111 'url' => 'community/local',
112 'sel' => $content == 'local' ? 'active' : '',
113 'title' => DI::l10n()->t('Posts from local users on this server'),
114 'id' => 'community-local-tab',
119 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
121 'label' => DI::l10n()->t('Global Community'),
122 'url' => 'community/global',
123 'sel' => $content == 'global' ? 'active' : '',
124 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
125 'id' => 'community-global-tab',
130 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
131 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
133 Nav::setSelected('community');
135 // We need the editor here to be able to reshare an item.
139 'allow_location' => $a->user['allow_location'],
140 'default_location' => $a->user['default-location'],
141 'nickname' => $a->user['nickname'],
142 'lockstate' => (is_array($a->user) && (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) || strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
143 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
145 'visitor' => 'block',
146 'profile_uid' => local_user(),
148 $o .= status_editor($a, $x, 0, true);
152 // check if we serve a mobile device and get the user settings accordingly
153 if (DI::mode()->isMobile()) {
154 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', 20);
156 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 40);
159 // now that we have the user settings, see if the theme forces
160 // a maximum item number which is lower then the user choice
161 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
162 $itemspage_network = $a->force_max_items;
165 $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
167 $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
169 if (!DBA::isResult($r)) {
170 info(DI::l10n()->t('No results.') . EOL);
174 $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
176 if (($maxpostperauthor != 0) && ($content == 'local')) {
178 $previousauthor = "";
183 foreach ($r as $item) {
184 if ($previousauthor == $item["author-link"]) {
189 $previousauthor = $item["author-link"];
191 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
195 if (count($s) < $pager->getItemsPerPage()) {
196 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
198 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
203 $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
206 $o .= $pager->renderMinimal(count($r));
209 if (empty(DI::page()['aside'])) {
210 DI::page()['aside'] = '';
213 if (Feature::isEnabled(local_user(), 'trending_tags')) {
214 DI::page()['aside'] .= TrendingTags::getHTML($content);
217 $t = Renderer::getMarkupTemplate("community.tpl");
218 return Renderer::replaceMacros($t, [
221 '$show_global_community_hint' => ($content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
222 '$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.")
226 function community_getitems($start, $itemspage, $content, $accounttype)
228 if ($content == 'local') {
229 if (!is_null($accounttype)) {
230 $sql_accounttype = " AND `user`.`account-type` = ?";
231 $values = [$accounttype, $start, $itemspage];
233 $sql_accounttype = "";
234 $values = [$start, $itemspage];
237 /// @todo Use "unsearchable" here as well (instead of "hidewall")
238 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
239 STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
240 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
241 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
242 WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
243 AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
244 ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
245 return DBA::toArray($r);
246 } elseif ($content == 'global') {
247 if (!is_null($accounttype)) {
248 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable` AND `owner`.`contact-type` = ?", 0, $accounttype];
250 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
253 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
254 return DBA::toArray($r);
257 // Should never happen