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\Renderer;
14 use Friendica\Core\Session;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Item;
18 use Friendica\Model\User;
20 function community_content(App $a, $update = 0)
24 if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
25 notice(DI::l10n()->t('Public access denied.') . EOL);
29 $page_style = Config::get('system', 'community_page_style');
31 if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
32 notice(DI::l10n()->t('Access denied.') . EOL);
39 switch ($a->argv[2]) {
41 $accounttype = User::ACCOUNT_TYPE_PERSON;
44 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
47 $accounttype = User::ACCOUNT_TYPE_NEWS;
50 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
56 $content = $a->argv[1];
58 if (!empty(Config::get('system', 'singleuser'))) {
59 // On single user systems only the global page does make sense
62 // When only the global community is allowed, we use this as default
63 $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
67 if (!in_array($content, ['local', 'global'])) {
68 notice(DI::l10n()->t('Community option not available.') . EOL);
72 // Check if we are allowed to display the content to visitors
74 $available = $page_style == CP_USERS_AND_GLOBAL;
77 $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
81 $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
85 notice(DI::l10n()->t('Not available.') . EOL);
93 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
95 'label' => DI::l10n()->t('Local Community'),
96 'url' => 'community/local',
97 'sel' => $content == 'local' ? 'active' : '',
98 'title' => DI::l10n()->t('Posts from local users on this server'),
99 'id' => 'community-local-tab',
104 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
106 'label' => DI::l10n()->t('Global Community'),
107 'url' => 'community/global',
108 'sel' => $content == 'global' ? 'active' : '',
109 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
110 'id' => 'community-global-tab',
115 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
116 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
118 Nav::setSelected('community');
120 // We need the editor here to be able to reshare an item.
124 'allow_location' => $a->user['allow_location'],
125 'default_location' => $a->user['default-location'],
126 'nickname' => $a->user['nickname'],
127 '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'),
128 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
130 'visitor' => 'block',
131 'profile_uid' => local_user(),
133 $o .= status_editor($a, $x, 0, true);
137 // check if we serve a mobile device and get the user settings accordingly
138 if (DI::mode()->isMobile()) {
139 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network', 20);
141 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 40);
144 // now that we have the user settings, see if the theme forces
145 // a maximum item number which is lower then the user choice
146 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
147 $itemspage_network = $a->force_max_items;
150 $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
152 $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
154 if (!DBA::isResult($r)) {
155 info(DI::l10n()->t('No results.') . EOL);
159 $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
161 if (($maxpostperauthor != 0) && ($content == 'local')) {
163 $previousauthor = "";
168 foreach ($r as $item) {
169 if ($previousauthor == $item["author-link"]) {
174 $previousauthor = $item["author-link"];
176 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
180 if (count($s) < $pager->getItemsPerPage()) {
181 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
183 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
188 $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
191 $o .= $pager->renderMinimal(count($r));
194 if (empty(DI::page()['aside'])) {
195 DI::page()['aside'] = '';
198 if (Feature::isEnabled(local_user(), 'trending_tags')) {
199 DI::page()['aside'] .= TrendingTags::getHTML($content);
202 $t = Renderer::getMarkupTemplate("community.tpl");
203 return Renderer::replaceMacros($t, [
206 '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
207 '$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.")
211 function community_getitems($start, $itemspage, $content, $accounttype)
213 if ($content == 'local') {
214 if (!is_null($accounttype)) {
215 $sql_accounttype = " AND `user`.`account-type` = ?";
216 $values = [$accounttype, $start, $itemspage];
218 $sql_accounttype = "";
219 $values = [$start, $itemspage];
222 /// @todo Use "unsearchable" here as well (instead of "hidewall")
223 $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
224 STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
225 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
226 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
227 WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
228 AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
229 ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
230 return DBA::toArray($r);
231 } elseif ($content == 'global') {
232 if (!is_null($accounttype)) {
233 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable` AND `owner`.`contact-type` = ?", 0, $accounttype];
235 $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
238 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
239 return DBA::toArray($r);
242 // Should never happen