]> git.mxchange.org Git - friendica.git/blob - src/Module/Conversation/Network.php
Merge pull request #9419 from MrPetovan/task/9127-network-min_id-max_id
[friendica.git] / src / Module / Conversation / Network.php
1 <?php
2
3 namespace Friendica\Module\Conversation;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\BoundariesPager;
7 use Friendica\Content\ForumManager;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Widget;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\ACL;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Group;
18 use Friendica\Model\Item;
19 use Friendica\Model\Profile;
20 use Friendica\Model\User;
21 use Friendica\Module\Contact as ModuleContact;
22 use Friendica\Module\Security\Login;
23 use Friendica\Util\DateTimeFormat;
24
25 class Network extends BaseModule
26 {
27         /** @var int */
28         private static $groupId;
29         /** @var int */
30         private static $forumContactId;
31         /** @var string */
32         private static $selectedTab;
33         /** @var mixed */
34         private static $min_id;
35         /** @var mixed */
36         private static $max_id;
37         /** @var string */
38         private static $accountTypeString;
39         /** @var int */
40         private static $accountType;
41         /** @var string */
42         private static $network;
43         /** @var int */
44         private static $itemsPerPage;
45         /** @var string */
46         private static $dateFrom;
47         /** @var string */
48         private static $dateTo;
49         /** @var int */
50         private static $star;
51         /** @var int */
52         private static $mention;
53         /** @var string */
54         protected static $order;
55
56         public static function content(array $parameters = [])
57         {
58                 if (!local_user()) {
59                         return Login::form();
60                 }
61
62                 self::parseRequest($parameters, $_GET);
63
64                 $module = 'network';
65
66                 DI::page()['aside'] .= Widget::accounttypes($module, self::$accountTypeString);
67                 DI::page()['aside'] .= Group::sidebarWidget($module, $module . '/group', 'standard', self::$groupId);
68                 DI::page()['aside'] .= ForumManager::widget($module . '/forum', local_user(), self::$forumContactId);
69                 DI::page()['aside'] .= Widget::postedByYear($module . '/archive', local_user(), false);
70                 DI::page()['aside'] .= Widget::networks($module, !self::$forumContactId ? self::$network : '');
71                 DI::page()['aside'] .= Widget\SavedSearches::getHTML(DI::args()->getQueryString());
72                 DI::page()['aside'] .= Widget::fileAs('filed', null);
73
74                 $arr = ['query' => DI::args()->getQueryString()];
75                 Hook::callAll('network_content_init', $arr);
76
77                 $o = '';
78
79                 // Fetch a page full of parent items for this page
80                 $params = ['limit' => self::$itemsPerPage];
81                 $table = 'network-thread-view';
82
83                 $items = self::getItems($table, $params);
84
85                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll') && ($_GET['mode'] ?? '') != 'minimal') {
86                         $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
87                         $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
88                 }
89
90                 if (!(isset($_GET['mode']) AND ($_GET['mode'] == 'raw'))) {
91                         $o .= self::getTabsHTML(self::$selectedTab);
92
93                         Nav::setSelected(DI::args()->get(0));
94
95                         $content = '';
96
97                         if (self::$forumContactId) {
98                                 // If self::$forumContactId belongs to a communitity forum or a privat goup,.add a mention to the status editor
99                                 $condition = ["`id` = ? AND (`forum` OR `prv`)", self::$forumContactId];
100                                 $contact = DBA::selectFirst('contact', ['addr'], $condition);
101                                 if (!empty($contact['addr'])) {
102                                         $content = '!' . $contact['addr'];
103                                 }
104                         }
105
106                         $a = DI::app();
107
108                         $default_permissions = [];
109                         if (self::$groupId) {
110                                 $default_permissions['allow_gid'] = [self::$groupId];
111                         }
112
113                         $allowedCids = [];
114                         if (self::$forumContactId) {
115                                 $allowedCids[] = (int) self::$forumContactId;
116                         } elseif (self::$network) {
117                                 $condition = [
118                                         'uid'     => local_user(),
119                                         'network' => self::$network,
120                                         'self'    => false,
121                                         'blocked' => false,
122                                         'pending' => false,
123                                         'archive' => false,
124                                         'rel'     => [Contact::SHARING, Contact::FRIEND],
125                                 ];
126                                 $contactStmt = DBA::select('contact', ['id'], $condition);
127                                 while ($contact = DBA::fetch($contactStmt)) {
128                                         $allowedCids[] = (int) $contact['id'];
129                                 }
130                                 DBA::close($contactStmt);
131                         }
132
133                         if (count($allowedCids)) {
134                                 $default_permissions['allow_cid'] = $allowedCids;
135                         }
136
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' => (self::$groupId || self::$forumContactId || self::$network || (is_array($a->user) &&
143                                         (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
144                                                 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
145                                 'default_perms' => ACL::getDefaultUserPermissions($a->user),
146                                 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
147                                 'bang' => ((self::$groupId || self::$forumContactId || self::$network) ? '!' : ''),
148                                 'visitor' => 'block',
149                                 'profile_uid' => local_user(),
150                                 'content' => $content,
151                         ];
152
153                         $o .= status_editor($a, $x);
154                 }
155
156                 if (self::$groupId) {
157                         $group = DBA::selectFirst('group', ['name'], ['id' => self::$groupId, 'uid' => local_user()]);
158                         if (!DBA::isResult($group)) {
159                                 notice(DI::l10n()->t('No such group'));
160                         }
161
162                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
163                                 '$title' => DI::l10n()->t('Group: %s', $group['name'])
164                         ]) . $o;
165                 } elseif (self::$forumContactId) {
166                         $contact = Contact::getById(self::$forumContactId);
167                         if (DBA::isResult($contact)) {
168                                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
169                                         'contacts' => [ModuleContact::getContactTemplateVars($contact)],
170                                         'id' => DI::args()->get(0),
171                                 ]) . $o;
172                         } else {
173                                 notice(DI::l10n()->t('Invalid contact.'));
174                         }
175                 } elseif (!DI::config()->get('theme', 'hide_eventlist')) {
176                         $o .= Profile::getBirthdays();
177                         $o .= Profile::getEventsReminderHTML();
178                 }
179
180                 if (self::$order === 'received') {
181                         $ordering = '`received`';
182                 } else {
183                         $ordering = '`commented`';
184                 }
185
186                 $o .= conversation(DI::app(), $items, 'network', false, false, $ordering, local_user());
187
188                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
189                         $o .= HTML::scrollLoader();
190                 } else {
191                         $pager = new BoundariesPager(
192                                 DI::l10n(),
193                                 DI::args()->getQueryString(),
194                                 $items[0][self::$order],
195                                 $items[count($items) - 1][self::$order],
196                                 self::$itemsPerPage
197                         );
198
199                         $o .= $pager->renderMinimal(count($items));
200                 }
201
202                 return $o;
203         }
204
205         /**
206          * Sets items as seen
207          *
208          * @param array $condition The array with the SQL condition
209          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
210          */
211         private static function setItemsSeenByCondition(array $condition)
212         {
213                 if (empty($condition)) {
214                         return;
215                 }
216
217                 $unseen = Item::exists($condition);
218
219                 if ($unseen) {
220                         Item::update(['unseen' => false], $condition);
221                 }
222         }
223
224         /**
225          * Get the network tabs menu
226          *
227          * @param string $selectedTab
228          * @return string Html of the network tabs
229          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
230          */
231         private static function getTabsHTML(string $selectedTab)
232         {
233                 $cmd = DI::args()->getCommand();
234
235                 // tabs
236                 $tabs = [
237                         [
238                                 'label' => DI::l10n()->t('Latest Activity'),
239                                 'url'   => $cmd . '?' . http_build_query(['order' => 'commented']),
240                                 'sel'   => !$selectedTab || $selectedTab == 'commented' ? 'active' : '',
241                                 'title' => DI::l10n()->t('Sort by latest activity'),
242                                 'id'    => 'activity-order-tab',
243                                 'accesskey' => 'e',
244                         ],
245                         [
246                                 'label' => DI::l10n()->t('Latest Posts'),
247                                 'url'   => $cmd . '?' . http_build_query(['order' => 'received']),
248                                 'sel'   => $selectedTab == 'received' ? 'active' : '',
249                                 'title' => DI::l10n()->t('Sort by post received date'),
250                                 'id'    => 'post-order-tab',
251                                 'accesskey' => 't',
252                         ],
253                         [
254                                 'label' => DI::l10n()->t('Personal'),
255                                 'url'   => $cmd . '?' . http_build_query(['mention' => true]),
256                                 'sel'   => $selectedTab == 'mention' ? 'active' : '',
257                                 'title' => DI::l10n()->t('Posts that mention or involve you'),
258                                 'id'    => 'personal-tab',
259                                 'accesskey' => 'r',
260                         ],
261                         [
262                                 'label' => DI::l10n()->t('Starred'),
263                                 'url'   => $cmd . '?' . http_build_query(['star' => true]),
264                                 'sel'   => $selectedTab == 'star' ? 'active' : '',
265                                 'title' => DI::l10n()->t('Favourite Posts'),
266                                 'id'    => 'starred-posts-tab',
267                                 'accesskey' => 'm',
268                         ],
269                 ];
270
271                 $arr = ['tabs' => $tabs];
272                 Hook::callAll('network_tabs', $arr);
273
274                 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
275
276                 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
277         }
278
279         protected static function parseRequest(array $parameters, array $get)
280         {
281                 self::$groupId = $parameters['group_id'] ?? 0;
282
283                 self::$forumContactId = $parameters['contact_id'] ?? 0;
284
285                 self::$selectedTab = DI::pConfig()->get(local_user(), 'network.view', 'selected_tab', '');
286
287                 if (!empty($get['star'])) {
288                         self::$selectedTab = 'star';
289                 }
290
291                 if (!empty($get['mention'])) {
292                         self::$selectedTab = 'mention';
293                 }
294
295                 if (!empty($get['order'])) {
296                         self::$selectedTab = $get['order'];
297                 }
298
299                 DI::pConfig()->set(local_user(), 'network.view', 'selected_tab', self::$selectedTab);
300
301                 self::$star    = intval($get['star']      ?? 0);
302                 self::$mention = intval($_GET['mention']      ?? 0);
303                 self::$order   = in_array(self::$selectedTab, ['received', 'commented', 'created', 'uriid']) ? self::$selectedTab : 'commented';
304
305                 self::$accountTypeString = $_GET['accounttype'] ?? $parameters['accounttype'] ?? '';
306                 self::$accountType = User::getAccountTypeByString(self::$accountTypeString);
307
308                 self::$network = $get['nets'] ?? '';
309
310                 self::$dateFrom = $parameters['from'] ?? '';
311                 self::$dateTo = $parameters['to'] ?? '';
312
313                 if (DI::mode()->isMobile()) {
314                         self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
315                                 DI::config()->get('system', 'itemspage_network_mobile'));
316                 } else {
317                         self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
318                                 DI::config()->get('system', 'itemspage_network'));
319                 }
320
321                 self::$min_id = $_GET['min_id'] ?? null;
322                 self::$max_id = $_GET['max_id'] ?? null;
323
324                 switch (self::$selectedTab) {
325                         case 'received':
326                                 self::$max_id = $_GET['last_received'] ?? self::$max_id;
327                                 break;
328                         case 'commented':
329                                 self::$max_id = $_GET['last_commented'] ?? self::$max_id;
330                                 break;
331                         case 'created':
332                                 self::$max_id = $_GET['last_created'] ?? self::$max_id;
333                                 break;
334                         case 'uriid':
335                                 self::$max_id = $_GET['last_uriid'] ?? self::$max_id;
336                                 break;
337                 }
338         }
339
340         protected static function getItems(string $table, array $params, array $conditionFields = [])
341         {
342                 $conditionFields['uid'] = local_user();
343                 $conditionStrings = [];
344
345                 if (!is_null(self::$accountType)) {
346                         $conditionFields['contact-type'] = self::$accountType;
347                 }
348
349                 if (self::$star) {
350                         $conditionFields['starred'] = true;
351                 }
352                 if (self::$mention) {
353                         $conditionFields['mention'] = true;
354                 }
355                 if (self::$network) {
356                         $conditionFields['network'] = self::$network;
357                 }
358
359                 if (self::$dateFrom) {
360                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
361                 }
362                 if (self::$dateTo) {
363                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
364                 }
365
366                 if (self::$groupId) {
367                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
368                 } elseif (self::$forumContactId) {
369                         $conditionFields['contact-id'] = self::$forumContactId;
370                 }
371
372                 // Currently only the order modes "received" and "commented" are in use
373                 if (isset(self::$max_id)) {
374                         switch (self::$order) {
375                                 case 'received':
376                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", self::$max_id]);
377                                         break;
378                                 case 'commented':
379                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", self::$max_id]);
380                                         break;
381                                 case 'created':
382                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", self::$max_id]);
383                                         break;
384                                 case 'uriid':
385                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", self::$max_id]);
386                                         break;
387                         }
388                 }
389
390                 if (isset(self::$min_id)) {
391                         switch (self::$order) {
392                                 case 'received':
393                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` > ?", self::$min_id]);
394                                         break;
395                                 case 'commented':
396                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` > ?", self::$min_id]);
397                                         break;
398                                 case 'created':
399                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` > ?", self::$min_id]);
400                                         break;
401                                 case 'uriid':
402                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` > ?", self::$min_id]);
403                                         break;
404                         }
405                 }
406
407                 if (isset(self::$min_id) && !isset(self::$max_id)) {
408                         // min_id quirk: querying in reverse order with min_id gets the most recent rows, regardless of how close
409                         // they are to min_id. We change the query ordering to get the expected data, and we need to reverse the
410                         // order of the results.
411                         $params['order'] = [self::$order => false];
412                 } else {
413                         $params['order'] = [self::$order => true];
414                 }
415
416                 $items = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
417
418                 // min_id quirk, continued
419                 if (isset(self::$min_id) && !isset(self::$max_id)) {
420                         $items = array_reverse($items);
421                 }
422
423                 $parents_str = '';
424                 if (DBA::isResult($items)) {
425                         $parents_arr = [];
426
427                         foreach ($items as $item) {
428                                 if (!in_array($item['parent'], $parents_arr) && ($item['parent'] > 0)) {
429                                         $parents_arr[] = $item['parent'];
430                                 }
431                         }
432                         $parents_str = implode(', ', $parents_arr);
433                 }
434
435                 // We aren't going to try and figure out at the item, group, and page
436                 // level which items you've seen and which you haven't. If you're looking
437                 // at the top level network page just mark everything seen.
438                 if (!self::$groupId && !self::$forumContactId && self::$selectedTab != 'star') {
439                         $condition = ['unseen' => true, 'uid' => local_user()];
440                         self::setItemsSeenByCondition($condition);
441                 } elseif ($parents_str) {
442                         $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
443                         self::setItemsSeenByCondition($condition);
444                 }
445
446                 return $items;
447         }
448 }