4 * @file mod/network.php
8 use Friendica\BaseObject;
9 use Friendica\Content\Feature;
10 use Friendica\Content\ForumManager;
11 use Friendica\Content\Nav;
12 use Friendica\Content\Pager;
13 use Friendica\Content\Widget;
14 use Friendica\Content\Text\HTML;
15 use Friendica\Core\ACL;
16 use Friendica\Core\Config;
17 use Friendica\Core\Hook;
18 use Friendica\Core\L10n;
19 use Friendica\Core\Logger;
20 use Friendica\Core\PConfig;
21 use Friendica\Core\Protocol;
22 use Friendica\Core\Renderer;
23 use Friendica\Core\Session;
24 use Friendica\Database\DBA;
25 use Friendica\Model\Contact;
26 use Friendica\Model\Group;
27 use Friendica\Model\Item;
28 use Friendica\Model\Profile;
29 use Friendica\Model\Term;
30 use Friendica\Module\Security\Login;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Proxy as ProxyUtils;
33 use Friendica\Util\Strings;
35 function network_init(App $a)
38 notice(L10n::t('Permission denied.') . EOL);
42 Hook::add('head', __FILE__, 'network_infinite_scroll_head');
44 $is_a_date_query = false;
46 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
49 if (!empty($_GET['cid'])) {
55 /** @var DateTimeFormat $dtFormat */
56 $dtFormat = BaseObject::getClass(DateTimeFormat::class);
59 for ($x = 1; $x < $a->argc; $x ++) {
60 if ($dtFormat->isYearMonth($a->argv[$x])) {
61 $is_a_date_query = true;
67 // convert query string to array. remove friendica args
69 parse_str(parse_url($a->query_string, PHP_URL_QUERY), $query_array);
71 // fetch last used network view and redirect if needed
72 if (!$is_a_date_query) {
73 $sel_nets = $_GET['nets'] ?? '';
74 $sel_tabs = network_query_get_sel_tab($a);
75 $sel_groups = network_query_get_sel_group($a);
76 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
78 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
80 $net_baseurl = '/network';
83 if ($sel_groups !== false) {
84 $net_baseurl .= '/' . $sel_groups;
88 // redirect if current selected tab is '/network' and
89 // last selected tab is _not_ '/network?order=activity'.
90 // and this isn't a date query
101 'order=activity', //all
102 'order=post', //postord
106 'bmark=1', //bookmarked
109 $k = array_search('active', $last_sel_tabs);
112 $net_baseurl .= $tab_baseurls[$k];
114 // parse out tab queries
116 $dest_qs = $tab_args[$k];
117 parse_str($dest_qs, $dest_qa);
118 $net_args = array_merge($net_args, $dest_qa);
120 $remember_tab = false;
125 $net_args['nets'] = $sel_nets;
129 $net_args = array_merge($query_array, $net_args);
130 $net_queries = http_build_query($net_args);
132 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
134 $a->internalRedirect($redir_url);
138 if (empty($a->page['aside'])) {
139 $a->page['aside'] = '';
142 $a->page['aside'] .= Group::sidebarWidget('network/0', 'network', 'standard', $group_id);
143 $a->page['aside'] .= ForumManager::widget(local_user(), $cid);
144 $a->page['aside'] .= Widget::postedByYear('network', local_user(), false);
145 $a->page['aside'] .= Widget::networks('network', $_GET['nets'] ?? '');
146 $a->page['aside'] .= Widget\SavedSearches::getHTML($a->query_string);
147 $a->page['aside'] .= Widget::fileAs('network', $_GET['file'] ?? '');
151 * Return selected tab from query
154 * '/network' => $no_active = 'active'
155 * '/network?order=activity' => $activity_active = 'active'
156 * '/network?order=post' => $postord_active = 'active'
157 * '/network?conv=1', => $conv_active = 'active'
158 * '/network/new', => $new_active = 'active'
159 * '/network?star=1', => $starred_active = 'active'
160 * '/network?bmark=1', => $bookmarked_active = 'active'
163 * @return array ($no_active, $activity_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
165 function network_query_get_sel_tab(App $a)
168 $starred_active = '';
170 $bookmarked_active = '';
173 $postord_active = '';
175 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
176 $new_active = 'active';
179 if (!empty($_GET['star'])) {
180 $starred_active = 'active';
183 if (!empty($_GET['bmark'])) {
184 $bookmarked_active = 'active';
187 if (!empty($_GET['conv'])) {
188 $conv_active = 'active';
191 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
192 $no_active = 'active';
195 if ($no_active == 'active' && !empty($_GET['order'])) {
196 switch($_GET['order']) {
197 case 'post' : $postord_active = 'active'; $no_active=''; break;
198 case 'activity' : $all_active = 'active'; $no_active=''; break;
202 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
205 function network_query_get_sel_group(App $a)
209 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
210 $group = $a->argv[1];
217 * @brief Sets the pager data and returns SQL
219 * @param App $a The global App
220 * @param Pager $pager
221 * @param integer $update Used for the automatic reloading
222 * @return string SQL with the appropriate LIMIT clause
223 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
225 function networkPager(App $a, Pager $pager, $update)
228 // only setup pagination on initial page view
232 // check if we serve a mobile device and get the user settings
235 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
236 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
238 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
239 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
242 // now that we have the user settings, see if the theme forces
243 // a maximum item number which is lower then the user choice
244 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
245 $itemspage_network = $a->force_max_items;
248 $pager->setItemsPerPage($itemspage_network);
250 return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
254 * @brief Sets items as seen
256 * @param array $condition The array with the SQL condition
257 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
259 function networkSetSeen($condition)
261 if (empty($condition)) {
265 $unseen = Item::exists($condition);
268 Item::update(['unseen' => false], $condition);
273 * @brief Create the conversation HTML
275 * @param App $a The global App
276 * @param array $items Items of the conversation
277 * @param Pager $pager
278 * @param string $mode Display mode for the conversation
279 * @param integer $update Used for the automatic reloading
280 * @param string $ordering
281 * @return string HTML of the conversation
282 * @throws ImagickException
283 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
285 function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
287 // Set this so that the conversation function can find out contact info for our wall-wall items
288 $a->page_contact = $a->contact;
290 if (!is_array($items)) {
291 Logger::log("Expecting items to be an array. Got " . print_r($items, true));
295 $o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
298 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
299 $o .= HTML::scrollLoader();
301 $o .= $pager->renderMinimal(count($items));
308 function network_content(App $a, $update = 0, $parent = 0)
311 return Login::form();
314 /// @TODO Is this really necessary? $a is already available to hooks
315 $arr = ['query' => $a->query_string];
316 Hook::callAll('network_content_init', $arr);
321 for ($x = 1; $x < $a->argc; $x ++) {
322 if ($a->argv[$x] === 'new') {
328 if (!empty($_GET['file'])) {
333 $o = networkFlatView($a, $update);
335 $o = networkThreadedView($a, $update, $parent);
339 info("No items found");
346 * @brief Get the network content in flat view
348 * @param App $a The global App
349 * @param integer $update Used for the automatic reloading
350 * @return string HTML of the network content in flat view
351 * @throws ImagickException
352 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
353 * @global Pager $pager
355 function networkFlatView(App $a, $update = 0)
358 // Rawmode is used for fetching new content at the end of the page
359 $rawmode = (isset($_GET['mode']) && ($_GET['mode'] == 'raw'));
363 $file = $_GET['file'] ?? '';
365 if (!$update && !$rawmode) {
366 $tabs = network_tabs($a);
369 Nav::setSelected('network');
373 'allow_location' => $a->user['allow_location'],
374 'default_location' => $a->user['default-location'],
375 'nickname' => $a->user['nickname'],
376 'lockstate' => (is_array($a->user) &&
377 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
378 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid'])) ? 'lock' : 'unlock'),
379 'default_perms' => ACL::getDefaultUserPermissions($a->user),
380 'acl' => ACL::getFullSelectorHTML($a->page, $a->user, true),
382 'visitor' => 'block',
383 'profile_uid' => local_user(),
387 $o .= status_editor($a, $x);
389 if (!Config::get('theme', 'hide_eventlist')) {
390 $o .= Profile::getBirthdays();
391 $o .= Profile::getEventsReminderHTML();
395 $pager = new Pager($a->query_string);
397 networkPager($a, $pager, $update);
399 $item_params = ['order' => ['id' => true]];
402 $term_condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
403 $file, Term::OBJECT_TYPE_POST, Term::FILE, local_user()];
404 $term_params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
405 $result = DBA::select('term', ['oid'], $term_condition, $term_params);
408 while ($term = DBA::fetch($result)) {
409 $posts[] = $term['oid'];
413 if (count($posts) == 0) {
416 $item_condition = ['uid' => local_user(), 'id' => $posts];
418 $item_condition = ['uid' => local_user()];
419 $item_params['limit'] = [$pager->getStart(), $pager->getItemsPerPage()];
421 networkSetSeen(['unseen' => true, 'uid' => local_user()]);
424 $result = Item::selectForUser(local_user(), [], $item_condition, $item_params);
425 $items = Item::inArray($result);
426 $o .= networkConversation($a, $items, $pager, 'network-new', $update);
432 * @brief Get the network content in threaded view
434 * @param App $a The global App
435 * @param integer $update Used for the automatic reloading
436 * @param integer $parent
437 * @return string HTML of the network content in flat view
438 * @throws ImagickException
439 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
440 * @global Pager $pager
442 function networkThreadedView(App $a, $update, $parent)
444 /// @TODO this will have to be converted to a static property of the converted Module\Network class
447 // Rawmode is used for fetching new content at the end of the page
448 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
450 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
451 $last_received = DateTimeFormat::utc($_GET['last_received']);
452 $last_commented = DateTimeFormat::utc($_GET['last_commented']);
453 $last_created = DateTimeFormat::utc($_GET['last_created']);
454 $last_id = intval($_GET['last_id']);
457 $last_commented = '';
462 $datequery = $datequery2 = '';
466 $default_permissions = [];
468 /** @var DateTimeFormat $dtFormat */
469 $dtFormat = BaseObject::getClass(DateTimeFormat::class);
472 for ($x = 1; $x < $a->argc; $x ++) {
473 if ($dtFormat->isYearMonth($a->argv[$x])) {
475 $datequery2 = Strings::escapeHtml($a->argv[$x]);
477 $datequery = Strings::escapeHtml($a->argv[$x]);
478 $_GET['order'] = 'post';
480 } elseif (intval($a->argv[$x])) {
481 $gid = intval($a->argv[$x]);
482 $default_permissions['allow_gid'] = [$gid];
489 $cid = intval($_GET['cid'] ?? 0);
490 $star = intval($_GET['star'] ?? 0);
491 $bmark = intval($_GET['bmark'] ?? 0);
492 $conv = intval($_GET['conv'] ?? 0);
493 $order = Strings::escapeTags(($_GET['order'] ?? '') ?: 'activity');
494 $nets = $_GET['nets'] ?? '';
498 $allowedCids[] = (int) $cid;
501 'uid' => local_user(),
507 'rel' => [Contact::SHARING, Contact::FRIEND],
509 $contactStmt = DBA::select('contact', ['id'], $condition);
510 while ($contact = DBA::fetch($contactStmt)) {
511 $allowedCids[] = (int) $contact['id'];
513 DBA::close($contactStmt);
516 if (count($allowedCids)) {
517 $default_permissions['allow_cid'] = $allowedCids;
520 if (!$update && !$rawmode) {
521 $tabs = network_tabs($a);
524 if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
525 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
526 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
528 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
531 Nav::setSelected('network');
536 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
537 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
538 $contact = DBA::selectFirst('contact', ['addr', 'nick'], $condition);
539 if (DBA::isResult($contact)) {
540 if ($contact['addr'] != '') {
541 $content = '!' . $contact['addr'];
543 $content = '!' . $contact['nick'] . '+' . $cid;
550 'allow_location' => $a->user['allow_location'],
551 'default_location' => $a->user['default-location'],
552 'nickname' => $a->user['nickname'],
553 'lockstate' => ($gid || $cid || $nets || (is_array($a->user) &&
554 (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
555 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
556 'default_perms' => ACL::getDefaultUserPermissions($a->user),
557 'acl' => ACL::getFullSelectorHTML($a->page, $a->user, true, $default_permissions),
558 'bang' => (($gid || $cid || $nets) ? '!' : ''),
559 'visitor' => 'block',
560 'profile_uid' => local_user(),
561 'content' => $content,
564 $o .= status_editor($a, $x);
567 // We don't have to deal with ACLs on this page. You're looking at everything
568 // that belongs to you, hence you can see all of it. We will filter by group if
571 $sql_post_table = '';
572 $sql_options = ($star ? " AND `thread`.`starred` " : '');
573 $sql_options .= ($bmark ? sprintf(" AND `thread`.`post-type` = %d ", Item::PT_PAGE) : '');
574 $sql_extra = $sql_options;
577 $sql_table = '`thread`';
578 $sql_parent = '`iid`';
581 $sql_table = '`item`';
582 $sql_parent = '`parent`';
583 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
586 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", DBA::escape($nets)) : '');
587 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", DBA::escape($nets)) : '');
590 $group = DBA::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
591 if (!DBA::isResult($group)) {
595 notice(L10n::t('No such group') . EOL);
596 $a->internalRedirect('network/0');
600 $contacts = Group::expand(local_user(), [$gid]);
602 if ((is_array($contacts)) && count($contacts)) {
603 $contact_str_self = '';
605 $contact_str = implode(',', $contacts);
606 $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
607 if (DBA::isResult($self)) {
608 $contact_str_self = $self['id'];
611 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
612 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
613 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . Strings::protectSprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
615 $sql_extra3 .= " AND false ";
616 info(L10n::t('Group is empty'));
619 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
620 '$title' => L10n::t('Group: %s', $group['name'])
623 $fields = ['id', 'name', 'network', 'writable', 'nurl',
624 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
625 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
626 $contact = DBA::selectFirst('contact', $fields, $condition);
627 if (DBA::isResult($contact)) {
628 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
632 'name' => $contact['name'],
633 'itemurl' => ($contact['addr'] ?? '') ?: $contact['nurl'],
634 'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
635 'details' => $contact['location'],
638 $entries[0]['account_type'] = Contact::getAccountType($contact);
640 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
641 'contacts' => $entries,
645 if ($contact['network'] === Protocol::OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
646 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
649 notice(L10n::t('Invalid contact.') . EOL);
650 $a->internalRedirect('network');
655 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
656 $o .= Profile::getBirthdays();
657 $o .= Profile::getEventsReminderHTML();
661 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received <= '%s' ",
662 DBA::escape(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
665 $sql_extra3 .= Strings::protectSprintf(sprintf(" AND $sql_table.received >= '%s' ",
666 DBA::escape(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
670 $sql_extra3 .= " AND $sql_table.`mention`";
673 // Normal conversation view
674 if ($order === 'post') {
675 $ordering = '`received`';
676 $order_mode = 'received';
678 $ordering = '`commented`';
679 $order_mode = 'commented';
682 $sql_order = "$sql_table.$ordering";
684 if (!empty($_GET['offset'])) {
685 $sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
690 $pager = new Pager($a->query_string);
692 $pager_sql = networkPager($a, $pager, $update);
696 switch ($order_mode) {
698 if ($last_received != '') {
699 $last_date = $last_received;
700 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
702 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
706 if ($last_commented != '') {
707 $last_date = $last_commented;
708 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
710 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
714 if ($last_created != '') {
715 $last_date = $last_created;
716 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
718 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
722 if (($last_id > 0) && ($sql_table == '`thread`')) {
723 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
725 $pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
730 // Fetch a page full of parent items for this page
732 if (!empty($parent)) {
733 // Load only a single thread
734 $sql_extra4 = "`item`.`id` = ".intval($parent);
736 // Load all unseen items
737 $sql_extra4 = "`item`.`unseen`";
738 if (Config::get("system", "like_no_comment")) {
739 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
741 if ($order === 'post') {
742 // Only show toplevel posts when updating posts in this order mode
743 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
747 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
748 FROM `item` $sql_post_table
749 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
750 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
751 AND (`item`.`gravity` != %d
752 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
753 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
754 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
755 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
756 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
757 AND NOT `item`.`moderated` AND $sql_extra4
758 $sql_extra3 $sql_extra $sql_range $sql_nets
759 ORDER BY `order_date` DESC LIMIT 100",
760 intval(GRAVITY_PARENT),
761 intval(Contact::SHARING),
762 intval(Contact::FRIEND),
763 intval(local_user()),
767 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
768 FROM `thread` $sql_post_table
769 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
770 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
771 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
772 AND (`item`.`gravity` != %d
773 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
774 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
775 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
776 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
777 AND NOT `thread`.`moderated`
778 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
779 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
780 ORDER BY `order_date` DESC $pager_sql",
781 intval(GRAVITY_PARENT),
782 intval(Contact::SHARING),
783 intval(Contact::FRIEND),
784 intval(local_user()),
789 // Only show it when unfiltered (no groups, no networks, ...)
790 if (in_array($nets, ['', Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
791 if (DBA::isResult($r)) {
792 $top_limit = current($r)['order_date'];
793 $bottom_limit = end($r)['order_date'];
794 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
795 $_SESSION['network_last_top_limit'] = $top_limit;
798 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
801 // When checking for updates we need to fetch from the newest date to the newest date before
802 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
803 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
805 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
806 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
807 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
808 $bottom_limit = $_SESSION['network_last_date'];
810 $_SESSION['network_last_date'] = Session::get('network_last_top_limit', $top_limit);
811 $_SESSION['network_last_date_timestamp'] = time();
813 if ($last_date > $top_limit) {
814 $top_limit = $last_date;
815 } elseif ($pager->getPage() == 1) {
816 // Highest possible top limit when we are on the first page
817 $top_limit = DateTimeFormat::utcNow();
820 $items = DBA::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date`, `author`.`url` AS `author-link` FROM `item`
821 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
822 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
823 ON `item`.`id` = `term`.`oid`
824 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
825 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ? AND `item`.`gravity` = ?
826 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
827 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
828 $top_limit, $bottom_limit, GRAVITY_PARENT);
830 $data = DBA::toArray($items);
832 if (count($data) > 0) {
833 $tag_top_limit = current($data)['order_date'];
834 if ($_SESSION['network_last_date'] < $tag_top_limit) {
835 $_SESSION['network_last_date'] = $tag_top_limit;
838 Logger::log('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
840 foreach ($r as $item) {
841 $s[$item['uri']] = $item;
843 foreach ($data as $item) {
844 // Don't show hash tag posts from blocked or ignored contacts
845 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
846 Strings::normaliseLink($item['author-link']), local_user()];
847 if (!DBA::exists('contact', $condition)) {
848 $s[$item['uri']] = $item;
860 if (DBA::isResult($items)) {
863 foreach ($items as $item) {
864 if ($date_offset < $item['order_date']) {
865 $date_offset = $item['order_date'];
867 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
868 $parents_arr[] = $item['item_id'];
871 $parents_str = implode(', ', $parents_arr);
874 if (!empty($_GET['offset'])) {
875 $date_offset = $_GET['offset'];
878 $query_string = $a->query_string;
879 if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
880 $query_string .= '&offset=' . urlencode($date_offset);
883 $pager->setQueryString($query_string);
885 // We aren't going to try and figure out at the item, group, and page
886 // level which items you've seen and which you haven't. If you're looking
887 // at the top level network page just mark everything seen.
889 if (!$gid && !$cid && !$star) {
890 $condition = ['unseen' => true, 'uid' => local_user()];
891 networkSetSeen($condition);
892 } elseif ($parents_str) {
893 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
894 networkSetSeen($condition);
899 $o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
905 * @brief Get the network tabs menu
907 * @param App $a The global App
908 * @return string Html of the networktab
909 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
911 function network_tabs(App $a)
914 /// @TODO fix this logic, reduce duplication
915 /// $a->page['content'] .= '<div class="tabs-wrapper">';
916 list($no_active, $all_active, $post_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
918 // if no tabs are selected, defaults to activitys
919 if ($no_active == 'active') {
920 $all_active = 'active';
928 'label' => L10n::t('Latest Activity'),
929 'url' => str_replace('/new', '', $cmd) . '?order=activity' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
930 'sel' => $all_active,
931 'title' => L10n::t('Sort by latest activity'),
932 'id' => 'activity-order-tab',
936 'label' => L10n::t('Latest Posts'),
937 'url' => str_replace('/new', '', $cmd) . '?order=post' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''),
938 'sel' => $post_active,
939 'title' => L10n::t('Sort by post received date'),
940 'id' => 'post-order-tab',
946 'label' => L10n::t('Personal'),
947 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
948 'sel' => $conv_active,
949 'title' => L10n::t('Posts that mention or involve you'),
950 'id' => 'personal-tab',
954 if (Feature::isEnabled(local_user(), 'new_tab')) {
956 'label' => L10n::t('New'),
957 'url' => 'network/new' . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : ''),
958 'sel' => $new_active,
959 'title' => L10n::t('Activity Stream - by date'),
960 'id' => 'activitiy-by-date-tab',
965 if (Feature::isEnabled(local_user(), 'link_tab')) {
967 'label' => L10n::t('Shared Links'),
968 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
969 'sel' => $bookmarked_active,
970 'title' => L10n::t('Interesting Links'),
971 'id' => 'shared-links-tab',
977 'label' => L10n::t('Starred'),
978 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?cid=' . $_GET['cid'] : '/?f=') . '&star=1',
979 'sel' => $starred_active,
980 'title' => L10n::t('Favourite Posts'),
981 'id' => 'starred-posts-tab',
985 // save selected tab, but only if not in file mode
986 if (empty($_GET['file'])) {
987 PConfig::set(local_user(), 'network.view', 'tab.selected', [
988 $all_active, $post_active, $conv_active, $new_active, $starred_active, $bookmarked_active
992 $arr = ['tabs' => $tabs];
993 Hook::callAll('network_tabs', $arr);
995 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
997 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
999 // --- end item filter tabs
1003 * Network hook into the HTML head to enable infinite scroll.
1005 * Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
1006 * of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
1007 * networkThreadedView or networkFlatView.
1010 * @param string $htmlhead The head tag HTML string
1011 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1012 * @global Pager $pager
1014 function network_infinite_scroll_head(App $a, &$htmlhead)
1016 /// @TODO this will have to be converted to a static property of the converted Module\Network class
1022 if (PConfig::get(local_user(), 'system', 'infinite_scroll')
1023 && ($_GET['mode'] ?? '') != 'minimal'
1025 $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
1026 $htmlhead .= Renderer::replaceMacros($tpl, [
1027 '$pageno' => $pager->getPage(),
1028 '$reload_uri' => $pager->getBaseQueryString()