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\Renderer;
13 use Friendica\Core\Session;
14 use Friendica\Database\DBA;
16 use Friendica\Model\Item;
17 use Friendica\Model\User;
19 function community_content(App $a, $update = 0)
23 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
24 notice(DI::l10n()->t('Public access denied.') . EOL);
28 $page_style = DI::config()->get('system', 'community_page_style');
30 if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
31 notice(DI::l10n()->t('Access denied.') . EOL);
38 switch ($a->argv[2]) {
40 $accounttype = User::ACCOUNT_TYPE_PERSON;
43 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
46 $accounttype = User::ACCOUNT_TYPE_NEWS;
49 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
55 $content = $a->argv[1];
57 if (!empty(DI::config()->get('system', 'singleuser'))) {
58 // On single user systems only the global page does make sense
61 // When only the global community is allowed, we use this as default
62 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
66 if (!in_array($content, ['local', 'global'])) {
67 notice(DI::l10n()->t('Community option not available.') . EOL);
71 // Check if we are allowed to display the content to visitors
73 $available = $page_style == CP_USERS_AND_GLOBAL;
76 $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
80 $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
84 notice(DI::l10n()->t('Not available.') . EOL);
92 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
94 'label' => DI::l10n()->t('Local Community'),
95 'url' => 'community/local',
96 'sel' => $content == 'local' ? 'active' : '',
97 'title' => DI::l10n()->t('Posts from local users on this server'),
98 'id' => 'community-local-tab',
103 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
105 'label' => DI::l10n()->t('Global Community'),
106 'url' => 'community/global',
107 'sel' => $content == 'global' ? 'active' : '',
108 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
109 'id' => 'community-global-tab',
114 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
115 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
117 Nav::setSelected('community');
119 // We need the editor here to be able to reshare an item.
123 'allow_location' => $a->user['allow_location'],
124 'default_location' => $a->user['default-location'],
125 'nickname' => $a->user['nickname'],
126 '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'),
127 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
129 'visitor' => 'block',
130 'profile_uid' => local_user(),
132 $o .= status_editor($a, $x, 0, true);
136 // check if we serve a mobile device and get the user settings accordingly
137 if (DI::mode()->isMobile()) {
138 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', 20);
140 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 40);
143 // now that we have the user settings, see if the theme forces
144 // a maximum item number which is lower then the user choice
145 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
146 $itemspage_network = $a->force_max_items;
149 $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
151 $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
153 if (!DBA::isResult($r)) {
154 info(DI::l10n()->t('No results.') . EOL);
158 $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
160 if (($maxpostperauthor != 0) && ($content == 'local')) {
162 $previousauthor = "";
167 foreach ($r as $item) {
168 if ($previousauthor == $item["author-link"]) {
173 $previousauthor = $item["author-link"];
175 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
179 if (count($s) < $pager->getItemsPerPage()) {
180 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
182 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
187 $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
190 $o .= $pager->renderMinimal(count($r));
193 if (empty(DI::page()['aside'])) {
194 DI::page()['aside'] = '';
197 if (Feature::isEnabled(local_user(), 'trending_tags')) {
198 DI::page()['aside'] .= TrendingTags::getHTML($content);
201 $t = Renderer::getMarkupTemplate("community.tpl");
202 return Renderer::replaceMacros($t, [
205 '$show_global_community_hint' => ($content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
206 '$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.")
210 function community_getitems($start, $itemspage, $content, $accounttype)
212 if ($content == 'local') {
213 if (!is_null($accounttype)) {
214 $sql_accounttype = " AND `user`.`account-type` = ?";
215 $values = [$accounttype, $start, $itemspage];
217 $sql_accounttype = "";
218 $values = [$start, $itemspage];
221 /// @todo Use "unsearchable" here as well (instead of "hidewall")
222 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
223 STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
224 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
225 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
226 WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
227 AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
228 ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
229 return DBA::toArray($r);
230 } elseif ($content == 'global') {
231 if (!is_null($accounttype)) {
232 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable` AND `owner`.`contact-type` = ?", 0, $accounttype];
234 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
237 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
238 return DBA::toArray($r);
241 // Should never happen