]> git.mxchange.org Git - friendica.git/blob - mod/network.php
Don't load tag postings on the network page anymore
[friendica.git] / mod / network.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\ForumManager;
25 use Friendica\Content\Nav;
26 use Friendica\Content\Pager;
27 use Friendica\Content\Widget;
28 use Friendica\Content\Text\HTML;
29 use Friendica\Core\ACL;
30 use Friendica\Core\Hook;
31 use Friendica\Core\Logger;
32 use Friendica\Core\Protocol;
33 use Friendica\Core\Renderer;
34 use Friendica\Core\Session;
35 use Friendica\Database\DBA;
36 use Friendica\DI;
37 use Friendica\Model\Contact;
38 use Friendica\Model\Group;
39 use Friendica\Model\Item;
40 use Friendica\Model\Post\Category;
41 use Friendica\Model\Profile;
42 use Friendica\Module\Security\Login;
43 use Friendica\Util\DateTimeFormat;
44 use Friendica\Util\Proxy as ProxyUtils;
45 use Friendica\Util\Strings;
46
47 function network_init(App $a)
48 {
49         if (!local_user()) {
50                 notice(DI::l10n()->t('Permission denied.') . EOL);
51                 return;
52         }
53
54         Hook::add('head', __FILE__, 'network_infinite_scroll_head');
55
56         $is_a_date_query = false;
57
58         $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
59
60         $cid = 0;
61         if (!empty($_GET['contactid'])) {
62                 $cid = $_GET['contactid'];
63                 $_GET['nets'] = '';
64                 $group_id = 0;
65         }
66
67         if ($a->argc > 1) {
68                 for ($x = 1; $x < $a->argc; $x ++) {
69                         if (DI::dtFormat()->isYearMonthDay($a->argv[$x])) {
70                                 $is_a_date_query = true;
71                                 break;
72                         }
73                 }
74         }
75
76         // convert query string to array. remove friendica args
77         $query_array = [];
78         parse_str(parse_url(DI::args()->getQueryString(), PHP_URL_QUERY), $query_array);
79
80         // fetch last used network view and redirect if needed
81         if (!$is_a_date_query) {
82                 $sel_nets = $_GET['nets'] ?? '';
83                 $sel_tabs = network_query_get_sel_tab($a);
84                 $sel_groups = network_query_get_sel_group($a);
85                 $last_sel_tabs = DI::pConfig()->get(local_user(), 'network.view', 'tab.selected');
86
87                 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
88
89                 $net_baseurl = '/network';
90                 $net_args = [];
91
92                 if ($sel_groups !== false) {
93                         $net_baseurl .= '/' . $sel_groups;
94                 }
95
96                 if ($remember_tab) {
97                         // redirect if current selected tab is '/network' and
98                         // last selected tab is _not_ '/network?order=activity'.
99                         // and this isn't a date query
100
101                         $tab_args = [
102                                 'order=activity', //all
103                                 'order=post',     //postord
104                                 'conv=1',         //conv
105                                 'star=1',         //starred
106                         ];
107
108                         $k = array_search('active', $last_sel_tabs);
109
110                         if ($k != 3) {
111                                 // parse out tab queries
112                                 $dest_qa = [];
113                                 $dest_qs = $tab_args[$k];
114                                 parse_str($dest_qs, $dest_qa);
115                                 $net_args = array_merge($net_args, $dest_qa);
116                         } else {
117                                 $remember_tab = false;
118                         }
119                 }
120
121                 if ($sel_nets) {
122                         $net_args['nets'] = $sel_nets;
123                 }
124
125                 if ($remember_tab) {
126                         $net_args = array_merge($query_array, $net_args);
127                         $net_queries = http_build_query($net_args);
128
129                         $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
130
131                         DI::baseUrl()->redirect($redir_url);
132                 }
133         }
134
135         if (empty(DI::page()['aside'])) {
136                 DI::page()['aside'] = '';
137         }
138
139         DI::page()['aside'] .= Group::sidebarWidget('network/0', 'network', 'standard', $group_id);
140         DI::page()['aside'] .= ForumManager::widget(local_user(), $cid);
141         DI::page()['aside'] .= Widget::postedByYear('network', local_user(), false);
142         DI::page()['aside'] .= Widget::networks('network', $_GET['nets'] ?? '');
143         DI::page()['aside'] .= Widget\SavedSearches::getHTML(DI::args()->getQueryString());
144         DI::page()['aside'] .= Widget::fileAs('network', $_GET['file'] ?? '');
145 }
146
147 /**
148  * Return selected tab from query
149  *
150  * urls -> returns
151  *        '/network'                => $no_active = 'active'
152  *        '/network?order=activity' => $activity_active = 'active'
153  *        '/network?order=post'     => $postord_active = 'active'
154  *        '/network?conv=1',        => $conv_active = 'active'
155  *        '/network?star=1',        => $starred_active = 'active'
156  *
157  * @param App $a
158  * @return array ($no_active, $activity_active, $postord_active, $conv_active, $starred_active);
159  */
160 function network_query_get_sel_tab(App $a)
161 {
162         $no_active = '';
163         $starred_active = '';
164         $all_active = '';
165         $conv_active = '';
166         $postord_active = '';
167
168         if (!empty($_GET['star'])) {
169                 $starred_active = 'active';
170         }
171
172         if (!empty($_GET['conv'])) {
173                 $conv_active = 'active';
174         }
175
176         if (($starred_active == '') && ($conv_active == '')) {
177                 $no_active = 'active';
178         }
179
180         if ($no_active == 'active' && !empty($_GET['order'])) {
181                 switch($_GET['order']) {
182                         case 'post' :     $postord_active = 'active'; $no_active=''; break;
183                         case 'activity' : $all_active     = 'active'; $no_active=''; break;
184                 }
185         }
186
187         return [$no_active, $all_active, $postord_active, $conv_active, $starred_active];
188 }
189
190 function network_query_get_sel_group(App $a)
191 {
192         $group = false;
193
194         if ($a->argc >= 2 && is_numeric($a->argv[1])) {
195                 $group = $a->argv[1];
196         }
197
198         return $group;
199 }
200
201 /**
202  * Sets the pager data and returns SQL
203  *
204  * @param App     $a      The global App
205  * @param Pager   $pager
206  * @param integer $update Used for the automatic reloading
207  * @return string SQL with the appropriate LIMIT clause
208  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
209  */
210 function networkPager(App $a, Pager $pager, $update)
211 {
212         if ($update) {
213                 // only setup pagination on initial page view
214                 return ' LIMIT 100';
215         }
216
217         if (DI::mode()->isMobile()) {
218                 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
219                         DI::config()->get('system', 'itemspage_network_mobile'));
220         } else {
221                 $itemspage_network = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
222                         DI::config()->get('system', 'itemspage_network'));
223         }
224
225         //  now that we have the user settings, see if the theme forces
226         //  a maximum item number which is lower then the user choice
227         if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
228                 $itemspage_network = $a->force_max_items;
229         }
230
231         $pager->setItemsPerPage($itemspage_network);
232
233         return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
234 }
235
236 /**
237  * Sets items as seen
238  *
239  * @param array $condition The array with the SQL condition
240  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
241  */
242 function networkSetSeen($condition)
243 {
244         if (empty($condition)) {
245                 return;
246         }
247
248         $unseen = Item::exists($condition);
249
250         if ($unseen) {
251                 Item::update(['unseen' => false], $condition);
252         }
253 }
254
255 /**
256  * Create the conversation HTML
257  *
258  * @param App     $a      The global App
259  * @param array   $items  Items of the conversation
260  * @param Pager   $pager
261  * @param string  $mode   Display mode for the conversation
262  * @param integer $update Used for the automatic reloading
263  * @param string  $ordering
264  * @return string HTML of the conversation
265  * @throws ImagickException
266  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
267  */
268 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
269 {
270         // Set this so that the conversation function can find out contact info for our wall-wall items
271         $a->page_contact = $a->contact;
272
273         if (!is_array($items)) {
274                 Logger::info('Expecting items to be an array.', ['items' => $items]);
275                 $items = [];
276         }
277
278         $o = conversation($a, $items, $mode, $update, false, $ordering, local_user());
279
280         if (!$update) {
281                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
282                         $o .= HTML::scrollLoader();
283                 } else {
284                         $o .= $pager->renderMinimal(count($items));
285                 }
286         }
287
288         return $o;
289 }
290
291 function network_content(App $a, $update = 0, $parent = 0)
292 {
293         if (!local_user()) {
294                 return Login::form();
295         }
296
297         /// @TODO Is this really necessary? $a is already available to hooks
298         $arr = ['query' => DI::args()->getQueryString()];
299         Hook::callAll('network_content_init', $arr);
300
301         if (!empty($_GET['file'])) {
302                 $o = networkFlatView($a, $update);
303         } else {
304                 $o = networkThreadedView($a, $update, $parent);
305         }
306
307         if ($o === '') {
308                 info("No items found");
309         }
310
311         return $o;
312 }
313
314 /**
315  * Get the network content in flat view
316  *
317  * @param App     $a      The global App
318  * @param integer $update Used for the automatic reloading
319  * @return string HTML of the network content in flat view
320  * @throws ImagickException
321  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
322  * @global Pager  $pager
323  */
324 function networkFlatView(App $a, $update = 0)
325 {
326         global $pager;
327         // Rawmode is used for fetching new content at the end of the page
328         $rawmode = (isset($_GET['mode']) && ($_GET['mode'] == 'raw'));
329
330         $o = '';
331
332         $file = $_GET['file'] ?? '';
333
334         if (!$update && !$rawmode) {
335                 $tabs = network_tabs($a);
336                 $o .= $tabs;
337
338                 Nav::setSelected('network');
339
340                 $x = [
341                         'is_owner' => true,
342                         'allow_location' => $a->user['allow_location'],
343                         'default_location' => $a->user['default-location'],
344                         'nickname' => $a->user['nickname'],
345                         'lockstate' => (is_array($a->user) &&
346                         (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
347                         strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
348                         'default_perms' => ACL::getDefaultUserPermissions($a->user),
349                         'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true),
350                         'bang' => '',
351                         'visitor' => 'block',
352                         'profile_uid' => local_user(),
353                         'content' => '',
354                 ];
355
356                 $o .= status_editor($a, $x);
357
358                 if (!DI::config()->get('theme', 'hide_eventlist')) {
359                         $o .= Profile::getBirthdays();
360                         $o .= Profile::getEventsReminderHTML();
361                 }
362         }
363
364         $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
365
366         networkPager($a, $pager, $update);
367
368
369         if (strlen($file)) {
370                 $item_params = ['order' => ['uri-id' => true]];
371                 $term_condition = ['name' => $file, 'type' => Category::FILE, 'uid' => local_user()];
372                 $term_params = ['order' => ['uri-id' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
373                 $result = DBA::select('category-view', ['uri-id'], $term_condition, $term_params);
374
375                 $posts = [];
376                 while ($term = DBA::fetch($result)) {
377                         $posts[] = $term['uri-id'];
378                 }
379                 DBA::close($result);
380
381                 if (count($posts) == 0) {
382                         return '';
383                 }
384                 $item_condition = ['uid' => local_user(), 'uri-id' => $posts];
385         } else {
386                 $item_params = ['order' => ['id' => true]];
387                 $item_condition = ['uid' => local_user()];
388                 $item_params['limit'] = [$pager->getStart(), $pager->getItemsPerPage()];
389
390                 networkSetSeen(['unseen' => true, 'uid' => local_user()]);
391         }
392
393         $result = Item::selectForUser(local_user(), [], $item_condition, $item_params);
394         $items = Item::inArray($result);
395         $o .= networkConversation($a, $items, $pager, 'network-new', $update);
396
397         return $o;
398 }
399
400 /**
401  * Get the network content in threaded view
402  *
403  * @param  App     $a      The global App
404  * @param  integer $update Used for the automatic reloading
405  * @param  integer $parent
406  * @return string HTML of the network content in flat view
407  * @throws ImagickException
408  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
409  * @global Pager   $pager
410  */
411 function networkThreadedView(App $a, $update, $parent)
412 {
413         /// @TODO this will have to be converted to a static property of the converted Module\Network class
414         global $pager;
415
416         // Rawmode is used for fetching new content at the end of the page
417         $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
418
419         if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
420                 $last_received = DateTimeFormat::utc($_GET['last_received']);
421                 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
422                 $last_created = DateTimeFormat::utc($_GET['last_created']);
423                 $last_id = intval($_GET['last_id']);
424         } else {
425                 $last_received = '';
426                 $last_commented = '';
427                 $last_created = '';
428                 $last_id = 0;
429         }
430
431         $datequery = $datequery2 = '';
432
433         $gid = 0;
434
435         $default_permissions = [];
436
437         if ($a->argc > 1) {
438                 for ($x = 1; $x < $a->argc; $x ++) {
439                         if (DI::dtFormat()->isYearMonthDay($a->argv[$x])) {
440                                 if ($datequery) {
441                                         $datequery2 = Strings::escapeHtml($a->argv[$x]);
442                                 } else {
443                                         $datequery = Strings::escapeHtml($a->argv[$x]);
444                                         $_GET['order'] = 'post';
445                                 }
446                         } elseif (intval($a->argv[$x])) {
447                                 $gid = intval($a->argv[$x]);
448                                 $default_permissions['allow_gid'] = [$gid];
449                         }
450                 }
451         }
452
453         $o = '';
454
455         $cid   = intval($_GET['contactid'] ?? 0);
456         $star  = intval($_GET['star']      ?? 0);
457         $conv  = intval($_GET['conv']      ?? 0);
458         $order = Strings::escapeTags(($_GET['order'] ?? '') ?: 'activity');
459         $nets  =        $_GET['nets']      ?? '';
460
461         $allowedCids = [];
462         if ($cid) {
463                 $allowedCids[] = (int) $cid;
464         } elseif ($nets) {
465                 $condition = [
466                         'uid'     => local_user(),
467                         'network' => $nets,
468                         'self'    => false,
469                         'blocked' => false,
470                         'pending' => false,
471                         'archive' => false,
472                         'rel'     => [Contact::SHARING, Contact::FRIEND],
473                 ];
474                 $contactStmt = DBA::select('contact', ['id'], $condition);
475                 while ($contact = DBA::fetch($contactStmt)) {
476                         $allowedCids[] = (int) $contact['id'];
477                 }
478                 DBA::close($contactStmt);
479         }
480
481         if (count($allowedCids)) {
482                 $default_permissions['allow_cid'] = $allowedCids;
483         }
484
485         if (!$update && !$rawmode) {
486                 $tabs = network_tabs($a);
487                 $o .= $tabs;
488
489                 Nav::setSelected('network');
490
491                 $content = '';
492
493                 if ($cid) {
494                         // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
495                         $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
496                         $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
497                         if (DBA::isResult($contact)) {
498                                 if ($contact['addr'] != '') {
499                                         $content = '!' . $contact['addr'];
500                                 } else {
501                                         $content = '!' . $contact['nick'] . '+' . $cid;
502                                 }
503                         }
504                 }
505
506                 $x = [
507                         'is_owner' => true,
508                         'allow_location' => $a->user['allow_location'],
509                         'default_location' => $a->user['default-location'],
510                         'nickname' => $a->user['nickname'],
511                         'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
512                         (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
513                         strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
514                         'default_perms' => ACL::getDefaultUserPermissions($a->user),
515                         'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
516                         'bang' => (($gid || $cid || $nets) ? '!' : ''),
517                         'visitor' => 'block',
518                         'profile_uid' => local_user(),
519                         'content' => $content,
520                 ];
521
522                 $o .= status_editor($a, $x);
523         }
524
525         // We don't have to deal with ACLs on this page. You're looking at everything
526         // that belongs to you, hence you can see all of it. We will filter by group if
527         // desired.
528
529         $sql_post_table = '';
530         $sql_options = ($star ? " AND `thread`.`starred` " : '');
531         $sql_extra = $sql_options;
532         $sql_extra2 = '';
533         $sql_extra3 = '';
534         $sql_table = '`thread`';
535         $sql_parent = '`iid`';
536
537         if ($update) {
538                 $sql_table = '`item`';
539                 $sql_parent = '`parent`';
540                 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
541         }
542
543         $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
544
545         if ($gid) {
546                 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
547                 if (!DBA::isResult($group)) {
548                         if ($update) {
549                                 exit();
550                         }
551                         notice(DI::l10n()->t('No such group') . EOL);
552                         DI::baseUrl()->redirect('network/0');
553                         // NOTREACHED
554                 }
555
556                 $contacts = Group::expand(local_user(), [$gid]);
557
558                 if ((is_array($contacts)) && count($contacts)) {
559                         $contact_str_self = '';
560
561                         $contact_str = implode(',', $contacts);
562                         $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
563                         if (DBA::isResult($self)) {
564                                 $contact_str_self = $self['id'];
565                         }
566
567                         $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
568                         $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
569                         $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . Strings::protectSprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
570                 } else {
571                         $sql_extra3 .= " AND false ";
572                         info(DI::l10n()->t('Group is empty'));
573                 }
574
575                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
576                         '$title' => DI::l10n()->t('Group: %s', $group['name'])
577                 ]) . $o;
578         } elseif ($cid) {
579                 $fields = ['id', 'name', 'network', 'writable', 'nurl',
580                         'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
581                 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
582                 $contact = DBA::selectFirst('contact', $fields, $condition);
583                 if (DBA::isResult($contact)) {
584                         $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
585
586                         $entries[0] = [
587                                 'id' => 'network',
588                                 'name' => $contact['name'],
589                                 'itemurl' => ($contact['addr'] ?? '') ?: $contact['nurl'],
590                                 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
591                                 'details' => $contact['location'],
592                         ];
593
594                         $entries[0]['account_type'] = Contact::getAccountType($contact);
595
596                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
597                                 'contacts' => $entries,
598                                 'id' => 'network',
599                         ]) . $o;
600                 } else {
601                         notice(DI::l10n()->t('Invalid contact.') . EOL);
602                         DI::baseUrl()->redirect('network');
603                         // NOTREACHED
604                 }
605         }
606
607         if (!$gid && !$cid && !$update && !DI::config()->get('theme', 'hide_eventlist')) {
608                 $o .= Profile::getBirthdays();
609                 $o .= Profile::getEventsReminderHTML();
610         }
611
612         if ($datequery) {
613                 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received <= '%s' ",
614                                 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
615         }
616         if ($datequery2) {
617                 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received >= '%s' ",
618                                 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
619         }
620
621         if ($conv) {
622                 $sql_extra3 .= " AND $sql_table.`mention`";
623         }
624
625         // Normal conversation view
626         if ($order === 'post') {
627                 $ordering = '`received`';
628                 $order_mode = 'received';
629         } else {
630                 $ordering = '`commented`';
631                 $order_mode = 'commented';
632         }
633
634         $sql_order = "$sql_table.$ordering";
635
636         if (!empty($_GET['offset'])) {
637                 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
638         } else {
639                 $sql_range = '';
640         }
641
642         $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
643
644         $pager_sql = networkPager($a, $pager, $update);
645
646         $last_date = '';
647
648         switch ($order_mode) {
649                 case 'received':
650                         if ($last_received != '') {
651                                 $last_date = $last_received;
652                                 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
653                                 $pager->setPage(1);
654                                 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
655                         }
656                         break;
657                 case 'commented':
658                         if ($last_commented != '') {
659                                 $last_date = $last_commented;
660                                 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
661                                 $pager->setPage(1);
662                                 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
663                         }
664                         break;
665                 case 'created':
666                         if ($last_created != '') {
667                                 $last_date = $last_created;
668                                 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
669                                 $pager->setPage(1);
670                                 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
671                         }
672                         break;
673                 case 'id':
674                         if (($last_id > 0) && ($sql_table == '`thread`')) {
675                                 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
676                                 $pager->setPage(1);
677                                 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
678                         }
679                         break;
680         }
681
682         // Fetch a page full of parent items for this page
683         if ($update) {
684                 if (!empty($parent)) {
685                         // Load only a single thread
686                         $sql_extra4 = "`item`.`id` = ".intval($parent);
687                 } else {
688                         // Load all unseen items
689                         $sql_extra4 = "`item`.`unseen`";
690                         if (DI::config()->get("system", "like_no_comment")) {
691                                 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
692                         }
693                         if ($order === 'post') {
694                                 // Only show toplevel posts when updating posts in this order mode
695                                 $sql_extra4 .= " AND `item`.`gravity` = " . GRAVITY_PARENT;
696                         }
697                 }
698
699                 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
700                         FROM `item` $sql_post_table
701                         STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
702                                 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
703                                 AND (`item`.`gravity` != %d
704                                         OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
705                                         OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
706                         LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
707                         WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
708                         AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
709                         AND NOT `item`.`moderated` AND $sql_extra4
710                         $sql_extra3 $sql_extra $sql_range $sql_nets
711                         ORDER BY `order_date` DESC LIMIT 100",
712                         intval(GRAVITY_PARENT),
713                         intval(Contact::SHARING),
714                         intval(Contact::FRIEND),
715                         intval(local_user()),
716                         intval(local_user())
717                 );
718         } else {
719                 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
720                         FROM `thread` $sql_post_table
721                         STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
722                                 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
723                         STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
724                                 AND (`item`.`gravity` != %d
725                                         OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
726                                         OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
727                         LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
728                         WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
729                         AND NOT `thread`.`moderated`
730                         AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
731                         $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
732                         ORDER BY `order_date` DESC $pager_sql",
733                         intval(GRAVITY_PARENT),
734                         intval(Contact::SHARING),
735                         intval(Contact::FRIEND),
736                         intval(local_user()),
737                         intval(local_user())
738                 );
739         }
740
741         $parents_str = '';
742         $date_offset = '';
743
744         $items = $r;
745
746         if (DBA::isResult($items)) {
747                 $parents_arr = [];
748
749                 foreach ($items as $item) {
750                         if ($date_offset < $item['order_date']) {
751                                 $date_offset = $item['order_date'];
752                         }
753                         if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
754                                 $parents_arr[] = $item['item_id'];
755                         }
756                 }
757                 $parents_str = implode(', ', $parents_arr);
758         }
759
760         if (!empty($_GET['offset'])) {
761                 $date_offset = $_GET['offset'];
762         }
763
764         $query_string = DI::args()->getQueryString();
765         if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
766                 $query_string .= '&offset=' . urlencode($date_offset);
767         }
768
769         $pager->setQueryString($query_string);
770
771         // We aren't going to try and figure out at the item, group, and page
772         // level which items you've seen and which you haven't. If you're looking
773         // at the top level network page just mark everything seen.
774
775         if (!$gid && !$cid && !$star) {
776                 $condition = ['unseen' => true, 'uid' => local_user()];
777                 networkSetSeen($condition);
778         } elseif ($parents_str) {
779                 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
780                 networkSetSeen($condition);
781         }
782
783
784         $mode = 'network';
785         $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
786
787         return $o;
788 }
789
790 /**
791  * Get the network tabs menu
792  *
793  * @param App $a The global App
794  * @return string Html of the networktab
795  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
796  */
797 function network_tabs(App $a)
798 {
799         // item filter tabs
800         /// @TODO fix this logic, reduce duplication
801         /// DI::page()['content'] .= '<div class="tabs-wrapper">';
802         list($no_active, $all_active, $post_active, $conv_active, $starred_active) = network_query_get_sel_tab($a);
803
804         // if no tabs are selected, defaults to activitys
805         if ($no_active == 'active') {
806                 $all_active = 'active';
807         }
808
809         $cmd = DI::args()->getCommand();
810
811         $def_param = [];
812         if (!empty($_GET['contactid'])) {
813                 $def_param['contactid'] = $_GET['contactid'];
814         }
815
816         // tabs
817         $tabs = [
818                 [
819                         'label' => DI::l10n()->t('Latest Activity'),
820                         'url'   => $cmd . '?' . http_build_query(array_merge($def_param, ['order' => 'activity'])),
821                         'sel'   => $all_active,
822                         'title' => DI::l10n()->t('Sort by latest activity'),
823                         'id'    => 'activity-order-tab',
824                         'accesskey' => 'e',
825                 ],
826                 [
827                         'label' => DI::l10n()->t('Latest Posts'),
828                         'url'   => $cmd . '?' . http_build_query(array_merge($def_param, ['order' => 'post'])),
829                         'sel'   => $post_active,
830                         'title' => DI::l10n()->t('Sort by post received date'),
831                         'id'    => 'post-order-tab',
832                         'accesskey' => 't',
833                 ],
834         ];
835
836         $tabs[] = [
837                 'label' => DI::l10n()->t('Personal'),
838                 'url'   => $cmd . '?' . http_build_query(array_merge($def_param, ['conv' => true])),
839                 'sel'   => $conv_active,
840                 'title' => DI::l10n()->t('Posts that mention or involve you'),
841                 'id'    => 'personal-tab',
842                 'accesskey' => 'r',
843         ];
844
845         $tabs[] = [
846                 'label' => DI::l10n()->t('Starred'),
847                 'url'   => $cmd . '?' . http_build_query(array_merge($def_param, ['star' => true])),
848                 'sel'   => $starred_active,
849                 'title' => DI::l10n()->t('Favourite Posts'),
850                 'id'    => 'starred-posts-tab',
851                 'accesskey' => 'm',
852         ];
853
854         // save selected tab, but only if not in file mode
855         if (empty($_GET['file'])) {
856                 DI::pConfig()->set(local_user(), 'network.view', 'tab.selected', [
857                         $all_active, $post_active, $conv_active, $starred_active
858                 ]);
859         }
860
861         $arr = ['tabs' => $tabs];
862         Hook::callAll('network_tabs', $arr);
863
864         $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
865
866         return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
867
868         // --- end item filter tabs
869 }
870
871 /**
872  * Network hook into the HTML head to enable infinite scroll.
873  *
874  * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
875  * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
876  * networkThreadedView or networkFlatView.
877  *
878  * @param App     $a
879  * @param  string $htmlhead The head tag HTML string
880  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
881  * @global Pager  $pager
882  */
883 function network_infinite_scroll_head(App $a, &$htmlhead)
884 {
885         /// @TODO this will have to be converted to a static property of the converted Module\Network class
886         /**
887          * @var $pager Pager
888          */
889         global $pager;
890
891         if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')
892                 && ($_GET['mode'] ?? '') != 'minimal'
893         ) {
894                 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
895                 $htmlhead .= Renderer::replaceMacros($tpl, [
896                         '$pageno'     => $pager->getPage(),
897                         '$reload_uri' => $pager->getBaseQueryString()
898                 ]);
899         }
900 }