]> git.mxchange.org Git - friendica.git/blob - mod/community.php
Remove pager parameter from conversation()
[friendica.git] / mod / community.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 use Friendica\App;
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;
31 use Friendica\DI;
32 use Friendica\Model\Item;
33 use Friendica\Model\User;
34
35 function community_content(App $a, $update = 0)
36 {
37         $o = '';
38
39         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
40                 notice(DI::l10n()->t('Public access denied.') . EOL);
41                 return;
42         }
43
44         $page_style = DI::config()->get('system', 'community_page_style');
45
46         if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
47                 notice(DI::l10n()->t('Access denied.') . EOL);
48                 return;
49         }
50
51         $accounttype = null;
52
53         if ($a->argc > 2) {
54                 switch ($a->argv[2]) {
55                         case 'person':
56                                 $accounttype = User::ACCOUNT_TYPE_PERSON;
57                                 break;
58                         case 'organisation':
59                                 $accounttype = User::ACCOUNT_TYPE_ORGANISATION;
60                                 break;
61                         case 'news':
62                                 $accounttype = User::ACCOUNT_TYPE_NEWS;
63                                 break;
64                         case 'community':
65                                 $accounttype = User::ACCOUNT_TYPE_COMMUNITY;
66                                 break;
67                 }
68         }
69
70         if ($a->argc > 1) {
71                 $content = $a->argv[1];
72         } else {
73                 if (!empty(DI::config()->get('system', 'singleuser'))) {
74                         // On single user systems only the global page does make sense
75                         $content = 'global';
76                 } else {
77                         // When only the global community is allowed, we use this as default
78                         $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
79                 }
80         }
81
82         if (!in_array($content, ['local', 'global'])) {
83                 notice(DI::l10n()->t('Community option not available.') . EOL);
84                 return;
85         }
86
87         // Check if we are allowed to display the content to visitors
88         if (!local_user()) {
89                 $available = $page_style == CP_USERS_AND_GLOBAL;
90
91                 if (!$available) {
92                         $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
93                 }
94
95                 if (!$available) {
96                         $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
97                 }
98
99                 if (!$available) {
100                         notice(DI::l10n()->t('Not available.') . EOL);
101                         return;
102                 }
103         }
104
105         if (!$update) {
106                 $tabs = [];
107
108                 if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
109                         $tabs[] = [
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',
115                                 'accesskey' => 'l'
116                         ];
117                 }
118
119                 if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
120                         $tabs[] = [
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',
126                                 'accesskey' => 'g'
127                         ];
128                 }
129
130                 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
131                 $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
132
133                 Nav::setSelected('community');
134
135                 // We need the editor here to be able to reshare an item.
136                 if (local_user()) {
137                         $x = [
138                                 'is_owner' => true,
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),
144                                 'bang' => '',
145                                 'visitor' => 'block',
146                                 'profile_uid' => local_user(),
147                         ];
148                         $o .= status_editor($a, $x, 0, true);
149                 }
150         }
151
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);
155         } else {
156                 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network', 40);
157         }
158
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;
163         }
164
165         $pager = new Pager(DI::args()->getQueryString(), $itemspage_network);
166
167         $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
168
169         if (!DBA::isResult($r)) {
170                 info(DI::l10n()->t('No results.') . EOL);
171                 return $o;
172         }
173
174         $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
175
176         if (($maxpostperauthor != 0) && ($content == 'local')) {
177                 $count = 1;
178                 $previousauthor = "";
179                 $numposts = 0;
180                 $s = [];
181
182                 do {
183                         foreach ($r as $item) {
184                                 if ($previousauthor == $item["author-link"]) {
185                                         ++$numposts;
186                                 } else {
187                                         $numposts = 0;
188                                 }
189                                 $previousauthor = $item["author-link"];
190
191                                 if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
192                                         $s[] = $item;
193                                 }
194                         }
195                         if (count($s) < $pager->getItemsPerPage()) {
196                                 $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
197                         }
198                 } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
199         } else {
200                 $s = $r;
201         }
202
203         $o .= conversation($a, $s, 'community', $update, false, 'commented', local_user());
204
205         if (!$update) {
206                 $o .= $pager->renderMinimal(count($r));
207         }
208
209         if (empty(DI::page()['aside'])) {
210                 DI::page()['aside'] = '';
211         }
212
213         if (Feature::isEnabled(local_user(), 'trending_tags')) {
214                 DI::page()['aside'] .= TrendingTags::getHTML($content);
215         }
216
217         $t = Renderer::getMarkupTemplate("community.tpl");
218         return Renderer::replaceMacros($t, [
219                 '$content' => $o,
220                 '$header' => '',
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.")
223         ]);
224 }
225
226 function community_getitems($start, $itemspage, $content, $accounttype)
227 {
228         if ($content == 'local') {
229                 if (!is_null($accounttype)) {
230                         $sql_accounttype = " AND `user`.`account-type` = ?";
231                         $values = [$accounttype, $start, $itemspage];
232                 } else {
233                         $sql_accounttype = "";
234                         $values = [$start, $itemspage];
235                 }
236
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];
249                 } else {
250                         $condition = ["`uid` = ? AND NOT `author`.`unsearchable` AND NOT `owner`.`unsearchable`", 0];
251                 }
252
253                 $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
254                 return DBA::toArray($r);
255         }
256
257         // Should never happen
258         return [];
259 }