]> git.mxchange.org Git - friendica.git/blob - src/Module/Conversation/Community.php
Merge pull request #9280 from annando/account-selector
[friendica.git] / src / Module / Conversation / 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  * See update_profile.php for documentation
21  */
22
23 namespace Friendica\Module\Conversation;
24
25 use Friendica\BaseModule;
26 use Friendica\Content\BoundariesPager;
27 use Friendica\Content\Feature;
28 use Friendica\Content\Nav;
29 use Friendica\Content\Widget\TrendingTags;
30 use Friendica\Core\ACL;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session;
33 use Friendica\Database\DBA;
34 use Friendica\DI;
35 use Friendica\Model\Item;
36 use Friendica\Model\User;
37 use Friendica\Network\HTTPException;
38
39 class Community extends BaseModule
40 {
41         protected static $page_style;
42         protected static $content;
43         protected static $accounttype;
44         protected static $itemsPerPage;
45         protected static $since_id;
46         protected static $max_id;
47
48         public static function content(array $parameters = [])
49         {
50                 self::parseRequest($parameters);
51
52                 $tabs = [];
53
54                 if ((Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(DI::config()->get('system', 'singleuser'))) {
55                         $tabs[] = [
56                                 'label' => DI::l10n()->t('Local Community'),
57                                 'url' => 'community/local',
58                                 'sel' => self::$content == 'local' ? 'active' : '',
59                                 'title' => DI::l10n()->t('Posts from local users on this server'),
60                                 'id' => 'community-local-tab',
61                                 'accesskey' => 'l'
62                         ];
63                 }
64
65                 if (Session::isAuthenticated() || in_array(self::$page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
66                         $tabs[] = [
67                                 'label' => DI::l10n()->t('Global Community'),
68                                 'url' => 'community/global',
69                                 'sel' => self::$content == 'global' ? 'active' : '',
70                                 'title' => DI::l10n()->t('Posts from users of the whole federated network'),
71                                 'id' => 'community-global-tab',
72                                 'accesskey' => 'g'
73                         ];
74                 }
75
76                 $tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
77                 $o = Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
78
79                 Nav::setSelected('community');
80
81                 $items = self::getItems();
82
83                 if (!DBA::isResult($items)) {
84                         notice(DI::l10n()->t('No results.'));
85                         return $o;
86                 }
87
88                 // We need the editor here to be able to reshare an item.
89                 if (Session::isAuthenticated()) {
90                         $x = [
91                                 'is_owner' => true,
92                                 'allow_location' => DI::app()->user['allow_location'],
93                                 'default_location' => DI::app()->user['default-location'],
94                                 'nickname' => DI::app()->user['nickname'],
95                                 'lockstate' => (is_array(DI::app()->user) && (strlen(DI::app()->user['allow_cid']) || strlen(DI::app()->user['allow_gid']) || strlen(DI::app()->user['deny_cid']) || strlen(DI::app()->user['deny_gid'])) ? 'lock' : 'unlock'),
96                                 'acl' => ACL::getFullSelectorHTML(DI::page(), DI::app()->user, true),
97                                 'bang' => '',
98                                 'visitor' => 'block',
99                                 'profile_uid' => local_user(),
100                         ];
101                         $o .= status_editor(DI::app(), $x, 0, true);
102                 }
103
104                 $o .= conversation(DI::app(), $items, 'community', false, false, 'commented', local_user());
105
106                 $pager = new BoundariesPager(
107                         DI::l10n(),
108                         DI::args()->getQueryString(),
109                         $items[0]['commented'],
110                         $items[count($items) - 1]['commented'],
111                         self::$itemsPerPage
112                 );
113
114                 $o .= $pager->renderMinimal(count($items));
115
116                 DI::page()['aside'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/community_accounts.tpl'), [
117                         '$title'        => DI::l10n()->t('Accounts'),
118                         '$content'      => self::$content,
119                         '$accounttype'  => $parameters['accounttype'],
120                         '$all'          => DI::l10n()->t('All Accounts'),
121                         '$person'       => DI::l10n()->t('Personal Accounts'),
122                         '$organisation' => DI::l10n()->t('Organisation Accounts'),
123                         '$news'         => DI::l10n()->t('News Accounts'),
124                         '$community'    => DI::l10n()->t('Communities'),
125                 ]);
126
127                 if (Feature::isEnabled(local_user(), 'trending_tags')) {
128                         DI::page()['aside'] .= TrendingTags::getHTML(self::$content);
129                 }
130
131                 $t = Renderer::getMarkupTemplate("community.tpl");
132                 return Renderer::replaceMacros($t, [
133                         '$content' => $o,
134                         '$header' => '',
135                         '$show_global_community_hint' => (self::$content == 'global') && DI::config()->get('system', 'show_global_community_hint'),
136                         '$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.")
137                 ]);
138         }
139
140         /**
141          * Computes module parameters from the request and local configuration
142          *
143          * @param array $parameters
144          * @throws HTTPException\BadRequestException
145          * @throws HTTPException\ForbiddenException
146          */
147         protected static function parseRequest(array $parameters)
148         {
149                 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
150                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.'));
151                 }
152
153                 self::$page_style = DI::config()->get('system', 'community_page_style');
154
155                 if (self::$page_style == CP_NO_INTERNAL_COMMUNITY) {
156                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
157                 }
158
159                 switch ($parameters['accounttype'] ?? '') {
160                         case 'person':
161                                 self::$accounttype = User::ACCOUNT_TYPE_PERSON;
162                                 break;
163                         case 'organisation':
164                                 self::$accounttype = User::ACCOUNT_TYPE_ORGANISATION;
165                                 break;
166                         case 'news':
167                                 self::$accounttype = User::ACCOUNT_TYPE_NEWS;
168                                 break;
169                         case 'community':
170                                 self::$accounttype = User::ACCOUNT_TYPE_COMMUNITY;
171                                 break;
172                         default:
173                                 self::$accounttype = null;
174                                 break;
175                 }
176
177                 self::$content = $parameters['content'] ?? '';
178                 if (!self::$content) {
179                         if (!empty(DI::config()->get('system', 'singleuser'))) {
180                                 // On single user systems only the global page does make sense
181                                 self::$content = 'global';
182                         } else {
183                                 // When only the global community is allowed, we use this as default
184                                 self::$content = self::$page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
185                         }
186                 }
187
188                 if (!in_array(self::$content, ['local', 'global'])) {
189                         throw new HTTPException\BadRequestException(DI::l10n()->t('Community option not available.'));
190                 }
191
192                 // Check if we are allowed to display the content to visitors
193                 if (!Session::isAuthenticated()) {
194                         $available = self::$page_style == CP_USERS_AND_GLOBAL;
195
196                         if (!$available) {
197                                 $available = (self::$page_style == CP_USERS_ON_SERVER) && (self::$content == 'local');
198                         }
199
200                         if (!$available) {
201                                 $available = (self::$page_style == CP_GLOBAL_COMMUNITY) && (self::$content == 'global');
202                         }
203
204                         if (!$available) {
205                                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Not available.'));
206                         }
207                 }
208
209                 if (DI::mode()->isMobile()) {
210                         self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
211                                 DI::config()->get('system', 'itemspage_network_mobile'));
212                 } else {
213                         self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
214                                 DI::config()->get('system', 'itemspage_network'));
215                 }
216
217                 // now that we have the user settings, see if the theme forces
218                 // a maximum item number which is lower then the user choice
219                 if ((DI::app()->force_max_items > 0) && (DI::app()->force_max_items < self::$itemsPerPage)) {
220                         self::$itemsPerPage = DI::app()->force_max_items;
221                 }
222
223                 self::$since_id = $_GET['since_id'] ?? null;
224                 self::$max_id   = $_GET['max_id']   ?? null;
225         }
226
227         /**
228          * Computes the displayed items.
229          *
230          * Community pages have a restriction on how many successive posts by the same author can show on any given page,
231          * so we may have to retrieve more content beyond the first query
232          *
233          * @return array
234          * @throws \Exception
235          */
236         protected static function getItems()
237         {
238                 $items = self::selectItems(self::$since_id, self::$max_id, self::$itemsPerPage);
239
240                 $maxpostperauthor = (int) DI::config()->get('system', 'max_author_posts_community_page');
241                 if ($maxpostperauthor != 0 && self::$content == 'local') {
242                         $count = 1;
243                         $previousauthor = '';
244                         $numposts = 0;
245                         $selected_items = [];
246
247                         while (count($selected_items) < self::$itemsPerPage && ++$count < 50 && count($items) > 0) {
248                                 foreach ($items as $item) {
249                                         if ($previousauthor == $item["author-link"]) {
250                                                 ++$numposts;
251                                         } else {
252                                                 $numposts = 0;
253                                         }
254                                         $previousauthor = $item["author-link"];
255
256                                         if (($numposts < $maxpostperauthor) && (count($selected_items) < self::$itemsPerPage)) {
257                                                 $selected_items[] = $item;
258                                         }
259                                 }
260
261                                 // If we're looking at a "previous page", the lookup continues forward in time because the list is
262                                 // sorted in chronologically decreasing order
263                                 if (isset(self::$since_id)) {
264                                         self::$since_id = $items[0]['commented'];
265                                 } else {
266                                         // In any other case, the lookup continues backwards in time
267                                         self::$max_id = $items[count($items) - 1]['commented'];
268                                 }
269
270                                 $items = self::selectItems(self::$since_id, self::$max_id, self::$itemsPerPage);
271                         }
272                 } else {
273                         $selected_items = $items;
274                 }
275
276                 return $selected_items;
277         }
278
279         /**
280          * Database query for the comunity page
281          *
282          * @param $since_id
283          * @param $max_id
284          * @param $itemspage
285          * @return array
286          * @throws \Exception
287          * @TODO Move to repository/factory
288          */
289         private static function selectItems($since_id, $max_id, $itemspage)
290         {
291                 $r = false;
292
293                 if (self::$content == 'local') {
294                         if (!is_null(self::$accounttype)) {
295                                 $condition = ["`wall` AND `origin` AND `private` = ? AND `owner`.`contact-type` = ?", Item::PUBLIC, self::$accounttype];
296                         } else {
297                                 $condition = ["`wall` AND `origin` AND `private` = ?", Item::PUBLIC];
298                         }
299                 } elseif (self::$content == 'global') {
300                         if (!is_null(self::$accounttype)) {
301                                 $condition = ["`uid` = ? AND `private` = ? AND `owner`.`contact-type` = ?", 0, Item::PUBLIC, self::$accounttype];
302                         } else {
303                                 $condition = ["`uid` = ? AND `private` = ?", 0, Item::PUBLIC];
304                         }
305                 } else {
306                         return [];
307                 }
308
309                 if (local_user() && DI::config()->get('system', 'community_no_followers')) {
310                         $condition[0] .= " AND NOT EXISTS (SELECT `uri-id` FROM `thread` AS t1 WHERE `t1`.`uri-id` = `thread`.`uri-id` AND `t1`.`uid` = ?)";
311                         $condition[] = local_user();
312                 }
313
314                 if (isset($max_id)) {
315                         $condition[0] .= " AND `commented` < ?";
316                         $condition[] = $max_id;
317                 }
318
319                 if (isset($since_id)) {
320                         $condition[0] .= " AND `commented` > ?";
321                         $condition[] = $since_id;
322                 }
323
324                 $r = Item::selectThreadForUser(0, ['uri', 'commented', 'author-link'], $condition, ['order' => ['commented' => true], 'limit' => $itemspage]);
325
326                 return DBA::toArray($r);
327         }
328 }