3 * @file mod/community.php
7 use Friendica\Content\Feature;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Pager;
10 use Friendica\Content\Widget\TrendingTags;
11 use Friendica\Core\ACL;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
16 use Friendica\Core\Session;
17 use Friendica\Database\DBA;
19 use Friendica\Model\Item;
20 use Friendica\Model\User;
22 function community_content(App $a, $update = 0)
26 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
27 notice(L10n::t('Public access denied.') . EOL);
31 $page_style = Config::get('system', 'community_page_style');
33 if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
34 notice(L10n::t('Access denied.') . EOL);
41 switch ($a->argv[2]) {
43 $accounttype = User::ACCOUNT_TYPE_PERSON;
46 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
49 $accounttype = User::ACCOUNT_TYPE_NEWS;
52 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
58 $content = $a->argv[1];
60 if (!empty(Config::get('system', 'singleuser'))) {
61 // On single user systems only the global page does make sense
64 // When only the global community is allowed, we use this as default
65 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
69 if (!in_array($content, ['local', 'global'])) {
70 notice(L10n::t('Community option not available.') . EOL);
74 // Check if we are allowed to display the content to visitors
76 $available = $page_style == CP_USERS_AND_GLOBAL;
79 $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
83 $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
87 notice(L10n::t('Not available.') . EOL);
95 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
97 'label' => L10n::t('Local Community'),
98 'url' => 'community/local',
99 'sel' => $content == 'local' ? 'active' : '',
100 'title' => L10n::t('Posts from local users on this server'),
101 'id' => 'community-local-tab',
106 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
108 'label' => L10n::t('Global Community'),
109 'url' => 'community/global',
110 'sel' => $content == 'global' ? 'active' : '',
111 'title' => L10n::t('Posts from users of the whole federated network'),
112 'id' => 'community-global-tab',
117 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
118 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
120 Nav::setSelected('community');
122 // We need the editor here to be able to reshare an item.
126 'allow_location' => $a->user['allow_location'],
127 'default_location' => $a->user['default-location'],
128 'nickname' => $a->user['nickname'],
129 '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'),
130 'acl' => ACL::getFullSelectorHTML($a->page, $a->user, true),
132 'visitor' => 'block',
133 'profile_uid' => local_user(),
135 $o .= status_editor($a, $x, 0, true);
139 // check if we serve a mobile device and get the user settings accordingly
140 if (DI::mode()->isMobile()) {
141 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
143 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
146 // now that we have the user settings, see if the theme forces
147 // a maximum item number which is lower then the user choice
148 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
149 $itemspage_network = $a->force_max_items;
152 $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
154 $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
156 if (!DBA::isResult($r)) {
157 info(L10n::t('No results.') . EOL);
161 $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
163 if (($maxpostperauthor != 0) && ($content == 'local')) {
165 $previousauthor = "";
170 foreach ($r as $item) {
171 if ($previousauthor == $item["author-link"]) {
176 $previousauthor = $item["author-link"];
178 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
182 if (count($s) < $pager->getItemsPerPage()) {
183 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
185 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
190 $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
193 $o .= $pager->renderMinimal(count($r));
196 if (empty($a->page['aside'])) {
197 $a->page['aside'] = '';
200 if (Feature::isEnabled(local_user(), 'trending_tags')) {
201 $a->page['aside'] .= TrendingTags::getHTML($content);
204 $t = Renderer::getMarkupTemplate("community.tpl");
205 return Renderer::replaceMacros($t, [
208 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
209 '$global_community_hint' => L10n::t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
213 function community_getitems($start, $itemspage, $content, $accounttype)
215 if ($content == 'local') {
216 if (!is_null($accounttype)) {
217 $sql_accounttype = " AND `user`.`account-type` = ?";
218 $values = [$accounttype, $start, $itemspage];
220 $sql_accounttype = "";
221 $values = [$start, $itemspage];
224 /// @todo Use "unsearchable" here as well (instead of "hidewall")
225 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
226 STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
227 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
228 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
229 WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
230 AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
231 ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
232 return DBA::toArray($r);
233 } elseif ($content == 'global') {
234 if (!is_null($accounttype)) {
235 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable` AND `owner`.`contact-type` = ?", 0, $accounttype];
237 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
240 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
241 return DBA::toArray($r);
244 // Should never happen