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;
18 use Friendica\Model\Item;
19 use Friendica\Model\User;
21 function community_content(App $a, $update = 0)
25 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
26 notice(L10n::t('Public access denied.') . EOL);
30 $page_style = Config::get('system', 'community_page_style');
32 if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
33 notice(L10n::t('Access denied.') . EOL);
40 switch ($a->argv[2]) {
42 $accounttype = User::ACCOUNT_TYPE_PERSON;
45 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
48 $accounttype = User::ACCOUNT_TYPE_NEWS;
51 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
57 $content = $a->argv[1];
59 if (!empty(Config::get('system', 'singleuser'))) {
60 // On single user systems only the global page does make sense
63 // When only the global community is allowed, we use this as default
64 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
68 if (!in_array($content, ['local', 'global'])) {
69 notice(L10n::t('Community option not available.') . EOL);
73 // Check if we are allowed to display the content to visitors
75 $available = $page_style == CP_USERS_AND_GLOBAL;
78 $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
82 $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
86 notice(L10n::t('Not available.') . EOL);
94 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
96 'label' => L10n::t('Local Community'),
97 'url' => 'community/local',
98 'sel' => $content == 'local' ? 'active' : '',
99 'title' => L10n::t('Posts from local users on this server'),
100 'id' => 'community-local-tab',
105 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
107 'label' => L10n::t('Global Community'),
108 'url' => 'community/global',
109 'sel' => $content == 'global' ? 'active' : '',
110 'title' => L10n::t('Posts from users of the whole federated network'),
111 'id' => 'community-global-tab',
116 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
117 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
119 Nav::setSelected('community');
121 // We need the editor here to be able to reshare an item.
125 'allow_location' => $a->user['allow_location'],
126 'default_location' => $a->user['default-location'],
127 'nickname' => $a->user['nickname'],
128 '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'),
129 'acl' => ACL::getFullSelectorHTML($a->page, $a->user, true),
131 'visitor' => 'block',
132 'profile_uid' => local_user(),
134 $o .= status_editor($a, $x, 0, true);
138 // check if we serve a mobile device and get the user settings accordingly
140 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
142 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
145 // now that we have the user settings, see if the theme forces
146 // a maximum item number which is lower then the user choice
147 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
148 $itemspage_network = $a->force_max_items;
151 $pager = new Pager($a->query_string, $itemspage_network);
153 $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
155 if (!DBA::isResult($r)) {
156 info(L10n::t('No results.') . EOL);
160 $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
162 if (($maxpostperauthor != 0) && ($content == 'local')) {
164 $previousauthor = "";
169 foreach ($r as $item) {
170 if ($previousauthor == $item["author-link"]) {
175 $previousauthor = $item["author-link"];
177 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
181 if (count($s) < $pager->getItemsPerPage()) {
182 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
184 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
189 $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
192 $o .= $pager->renderMinimal(count($r));
195 if (empty($a->page['aside'])) {
196 $a->page['aside'] = '';
199 if (Feature::isEnabled(local_user(), 'trending_tags')) {
200 $a->page['aside'] .= TrendingTags::getHTML($content);
203 $t = Renderer::getMarkupTemplate("community.tpl");
204 return Renderer::replaceMacros($t, [
207 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
208 '$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.")
212 function community_getitems($start, $itemspage, $content, $accounttype)
214 if ($content == 'local') {
215 if (!is_null($accounttype)) {
216 $sql_accounttype = " AND `user`.`account-type` = ?";
217 $values = [$accounttype, $start, $itemspage];
219 $sql_accounttype = "";
220 $values = [$start, $itemspage];
223 /// @todo Use "unsearchable" here as well (instead of "hidewall")
224 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
225 STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
226 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
227 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
228 WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
229 AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
230 ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
231 return DBA::toArray($r);
232 } elseif ($content == 'global') {
233 if (!is_null($accounttype)) {
234 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable` AND `owner`.`contact-type` = ?", 0, $accounttype];
236 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
239 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
240 return DBA::toArray($r);
243 // Should never happen