4 * @file mod/network.php
8 use Friendica\Content\Feature;
9 use Friendica\Content\ForumManager;
10 use Friendica\Content\Nav;
11 use Friendica\Content\Widget;
12 use Friendica\Core\ACL;
13 use Friendica\Core\Addon;
14 use Friendica\Core\Config;
15 use Friendica\Core\L10n;
16 use Friendica\Core\PConfig;
17 use Friendica\Core\System;
18 use Friendica\Database\DBM;
19 use Friendica\Model\Contact;
20 use Friendica\Model\Group;
21 use Friendica\Model\Item;
22 use Friendica\Model\Profile;
23 use Friendica\Module\Login;
24 use Friendica\Util\DateTimeFormat;
26 require_once 'include/conversation.php';
27 require_once 'include/items.php';
29 function network_init(App $a)
32 notice(L10n::t('Permission denied.') . EOL);
36 $search = (x($_GET, 'search') ? escape_tags($_GET['search']) : '');
38 if (($search != '') && !empty($_GET['submit'])) {
39 goaway('search?search=' . urlencode($search));
42 if (x($_GET, 'save')) {
43 $exists = dba::exists('search', ['uid' => local_user(), 'term' => $search]);
45 dba::insert('search', ['uid' => local_user(), 'term' => $search]);
48 if (x($_GET, 'remove')) {
49 dba::delete('search', ['uid' => local_user(), 'term' => $search]);
52 $is_a_date_query = false;
54 $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
57 if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
59 $_GET['nets'] = 'all';
64 for ($x = 1; $x < $a->argc; $x ++) {
65 if (is_a_date_arg($a->argv[$x])) {
66 $is_a_date_query = true;
72 // convert query string to array. remove friendica args
74 $query_string = str_replace($a->cmd . '?', '', $a->query_string);
75 parse_str($query_string, $query_array);
76 array_shift($query_array);
78 // fetch last used network view and redirect if needed
79 if (!$is_a_date_query) {
80 $sel_nets = defaults($_GET, 'nets', false);
81 $sel_tabs = network_query_get_sel_tab($a);
82 $sel_groups = network_query_get_sel_group($a);
83 $last_sel_tabs = PConfig::get(local_user(), 'network.view', 'tab.selected');
85 $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
87 $net_baseurl = '/network';
90 if ($sel_groups !== false) {
91 $net_baseurl .= '/' . $sel_groups;
95 // redirect if current selected tab is '/network' and
96 // last selected tab is _not_ '/network?f=&order=comment'.
97 // and this isn't a date query
108 'f=&order=comment', //all
109 'f=&order=post', //postord
112 'f=&star=1', //starred
113 'f=&bmark=1', //bookmarked
116 $k = array_search('active', $last_sel_tabs);
119 $net_baseurl .= $tab_baseurls[$k];
121 // parse out tab queries
123 $dest_qs = $tab_args[$k];
124 parse_str($dest_qs, $dest_qa);
125 $net_args = array_merge($net_args, $dest_qa);
127 $remember_tab = false;
131 if ($sel_nets !== false) {
132 $net_args['nets'] = $sel_nets;
136 $net_args = array_merge($query_array, $net_args);
137 $net_queries = build_querystring($net_args);
139 $redir_url = ($net_queries ? $net_baseurl . '?' . $net_queries : $net_baseurl);
141 goaway(System::baseUrl() . $redir_url);
145 // If nets is set to all, unset it
146 if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
147 unset($_GET['nets']);
150 if (!x($a->page, 'aside')) {
151 $a->page['aside'] = '';
154 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'groups') ?
155 Group::sidebarWidget('network/0', 'network', 'standard', $group_id) : '');
156 $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
157 $a->page['aside'] .= posted_date_widget('network', local_user(), false);
158 $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : ''));
159 $a->page['aside'] .= saved_searches($search);
160 $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : ''));
163 function saved_searches($search)
165 if (!Feature::isEnabled(local_user(), 'savedsearch')) {
171 $srchurl = '/network?f='
172 . ((x($_GET, 'cid')) ? '&cid=' . $_GET['cid'] : '')
173 . ((x($_GET, 'star')) ? '&star=' . $_GET['star'] : '')
174 . ((x($_GET, 'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
175 . ((x($_GET, 'conv')) ? '&conv=' . $_GET['conv'] : '')
176 . ((x($_GET, 'nets')) ? '&nets=' . $_GET['nets'] : '')
177 . ((x($_GET, 'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
178 . ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
179 . ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '');
184 $terms = dba::select('search', ['id', 'term'], ['uid' => local_user()]);
187 while ($rr = dba::fetch($terms)) {
190 'term' => $rr['term'],
191 'encodedterm' => urlencode($rr['term']),
192 'delete' => L10n::t('Remove term'),
193 'selected' => ($search == $rr['term']),
197 $tpl = get_markup_template('saved_searches_aside.tpl');
198 $o = replace_macros($tpl, [
199 '$title' => L10n::t('Saved Searches'),
200 '$add' => L10n::t('add'),
201 '$searchbox' => search($search, 'netsearch-box', $srchurl, true),
209 * Return selected tab from query
212 * '/network' => $no_active = 'active'
213 * '/network?f=&order=comment' => $comment_active = 'active'
214 * '/network?f=&order=post' => $postord_active = 'active'
215 * '/network?f=&conv=1', => $conv_active = 'active'
216 * '/network/new', => $new_active = 'active'
217 * '/network?f=&star=1', => $starred_active = 'active'
218 * '/network?f=&bmark=1', => $bookmarked_active = 'active'
220 * @return Array ($no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active);
222 function network_query_get_sel_tab(App $a)
225 $starred_active = '';
227 $bookmarked_active = '';
230 $postord_active = '';
232 if (($a->argc > 1 && $a->argv[1] === 'new') || ($a->argc > 2 && $a->argv[2] === 'new')) {
233 $new_active = 'active';
236 if (x($_GET, 'star')) {
237 $starred_active = 'active';
240 if (x($_GET, 'bmark')) {
241 $bookmarked_active = 'active';
244 if (x($_GET, 'conv')) {
245 $conv_active = 'active';
248 if (($new_active == '') && ($starred_active == '') && ($bookmarked_active == '') && ($conv_active == '')) {
249 $no_active = 'active';
252 if ($no_active == 'active' && x($_GET, 'order')) {
253 switch($_GET['order']) {
254 case 'post' : $postord_active = 'active'; $no_active=''; break;
255 case 'comment' : $all_active = 'active'; $no_active=''; break;
259 return [$no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active];
262 function network_query_get_sel_group(App $a)
266 if ($a->argc >= 2 && is_numeric($a->argv[1])) {
267 $group = $a->argv[1];
274 * @brief Sets the pager data and returns SQL
276 * @param App $a The global App
277 * @param integer $update Used for the automatic reloading
278 * @return string SQL with the appropriate LIMIT clause
280 function networkPager($a, $update)
283 // only setup pagination on initial page view
287 // check if we serve a mobile device and get the user settings
290 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network');
291 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
293 $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network');
294 $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
297 // now that we have the user settings, see if the theme forces
298 // a maximum item number which is lower then the user choice
299 if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
300 $itemspage_network = $a->force_max_items;
303 $a->set_pager_itemspage($itemspage_network);
305 return sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
309 * @brief Sets items as seen
311 * @param array $condition The array with the SQL condition
313 function networkSetSeen($condition)
315 if (empty($condition)) {
319 $unseen = dba::exists('item', $condition);
322 $r = Item::update(['unseen' => false], $condition);
327 * @brief Create the conversation HTML
329 * @param App $a The global App
330 * @param array $items Items of the conversation
331 * @param string $mode Display mode for the conversation
332 * @param integer $update Used for the automatic reloading
333 * @return string HTML of the conversation
335 function networkConversation($a, $items, $mode, $update, $ordering = '')
337 // Set this so that the conversation function can find out contact info for our wall-wall items
338 $a->page_contact = $a->contact;
340 $o = conversation($a, $items, $mode, $update, false, $ordering, local_user());
343 if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
344 $o .= scroll_loader();
346 $o .= alt_pager($a, count($items));
353 function network_content(App $a, $update = 0, $parent = 0)
356 return Login::form();
359 /// @TODO Is this really necessary? $a is already available to hooks
360 $arr = ['query' => $a->query_string];
361 Addon::callHooks('network_content_init', $arr);
366 for ($x = 1; $x < $a->argc; $x ++) {
367 if ($a->argv[$x] === 'new') {
373 if (x($_GET, 'file')) {
378 $o = networkFlatView($a, $update);
380 $o = networkThreadedView($a, $update, $parent);
387 * @brief Get the network content in flat view
389 * @param App $a The global App
390 * @param integer $update Used for the automatic reloading
391 * @return string HTML of the network content in flat view
393 function networkFlatView(App $a, $update = 0)
395 // Rawmode is used for fetching new content at the end of the page
396 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
398 if (isset($_GET['last_id'])) {
399 $last_id = intval($_GET['last_id']);
406 $file = ((x($_GET, 'file')) ? $_GET['file'] : '');
408 if (!$update && !$rawmode) {
409 $tabs = network_tabs($a);
412 Nav::setSelected('network');
416 'allow_location' => $a->user['allow_location'],
417 'default_location' => $a->user['default-location'],
418 'nickname' => $a->user['nickname'],
419 'lockstate' => (((is_array($a->user) &&
420 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
421 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
422 'default_perms' => ACL::getDefaultUserPermissions($a->user),
423 'acl' => ACL::getFullSelectorHTML($a->user, true),
425 'visitor' => 'block',
426 'profile_uid' => local_user(),
430 $o .= status_editor($a, $x);
432 if (!Config::get('theme', 'hide_eventlist')) {
433 $o .= Profile::getBirthdays();
434 $o .= Profile::getEventsReminderHTML();
439 $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
440 dbesc(protect_sprintf($file)), intval(TERM_OBJ_POST), intval(TERM_FILE), intval(local_user()));
442 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
445 $pager_sql = networkPager($a, $update);
447 // show all items unthreaded in reverse created date order
448 $items = q("SELECT %s FROM `item` $sql_post_table %s
449 WHERE %s AND `item`.`uid` = %d
450 ORDER BY `item`.`id` DESC $pager_sql ",
451 item_fieldlists(), item_joins(local_user()), item_condition(),
455 $condition = ['unseen' => true, 'uid' => local_user()];
456 networkSetSeen($condition);
458 $mode = 'network-new';
459 $o .= networkConversation($a, $items, $mode, $update);
465 * @brief Get the network content in threaded view
467 * @param App $a The global App
468 * @param integer $update Used for the automatic reloading
469 * @return string HTML of the network content in flat view
471 function networkThreadedView(App $a, $update, $parent)
473 // Rawmode is used for fetching new content at the end of the page
474 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
476 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
477 $last_received = DBM::date($_GET['last_received']);
478 $last_commented = DBM::date($_GET['last_commented']);
479 $last_created = DBM::date($_GET['last_created']);
480 $last_id = intval($_GET['last_id']);
483 $last_commented = '';
488 $datequery = $datequery2 = '';
493 for ($x = 1; $x < $a->argc; $x ++) {
494 if (is_a_date_arg($a->argv[$x])) {
496 $datequery2 = escape_tags($a->argv[$x]);
498 $datequery = escape_tags($a->argv[$x]);
499 $_GET['order'] = 'post';
501 } elseif (intval($a->argv[$x])) {
502 $gid = intval($a->argv[$x]);
503 $def_acl = ['allow_gid' => '<' . $gid . '>'];
510 $cid = intval(defaults($_GET, 'cid' , 0));
511 $star = intval(defaults($_GET, 'star' , 0));
512 $bmark = intval(defaults($_GET, 'bmark', 0));
513 $conv = intval(defaults($_GET, 'conv' , 0));
514 $order = notags(defaults($_GET, 'order', 'comment'));
515 $nets = defaults($_GET, 'nets' , '');
518 $def_acl = ['allow_cid' => '<' . intval($cid) . '>'];
522 $r = dba::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
525 while ($rr = dba::fetch($r)) {
526 $str .= '<' . $rr['id'] . '>';
529 $def_acl = ['allow_cid' => $str];
533 if (!$update && !$rawmode) {
534 $tabs = network_tabs($a);
538 if (($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
539 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
540 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
542 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
546 Nav::setSelected('network');
551 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
552 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
553 $contact = dba::selectFirst('contact', ['addr', 'nick'], $condition);
554 if (DBM::is_result($contact)) {
555 if ($contact['addr'] != '') {
556 $content = '!' . $contact['addr'];
558 $content = '!' . $contact['nick'] . '+' . $cid;
565 'allow_location' => $a->user['allow_location'],
566 'default_location' => $a->user['default-location'],
567 'nickname' => $a->user['nickname'],
568 'lockstate' => ((($gid) || ($cid) || ($nets) || (is_array($a->user) &&
569 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
570 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
571 'default_perms' => ACL::getDefaultUserPermissions($a->user),
572 'acl' => ACL::getFullSelectorHTML((($gid || $cid || $nets) ? $def_acl : $a->user), true),
573 'bang' => (($gid || $cid || $nets) ? '!' : ''),
574 'visitor' => 'block',
575 'profile_uid' => local_user(),
576 'content' => $content,
579 $o .= status_editor($a, $x);
582 // We don't have to deal with ACLs on this page. You're looking at everything
583 // that belongs to you, hence you can see all of it. We will filter by group if
586 $sql_post_table = '';
587 $sql_options = (($star) ? " AND `thread`.`starred` " : '');
588 $sql_options .= (($bmark) ? " AND `thread`.`bookmark` " : '');
589 $sql_extra = $sql_options;
592 $sql_table = '`thread`';
593 $sql_parent = '`iid`';
596 $sql_table = '`item`';
597 $sql_parent = '`parent`';
598 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
601 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", dbesc($nets)) : '');
602 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", dbesc($nets)) : '');
605 $group = dba::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
606 if (!DBM::is_result($group)) {
610 notice(L10n::t('No such group') . EOL);
615 $contacts = Group::expand([$gid]);
617 if ((is_array($contacts)) && count($contacts)) {
618 $contact_str_self = '';
620 $contact_str = implode(',', $contacts);
621 $self = dba::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
622 if (DBM::is_result($self)) {
623 $contact_str_self = $self['id'];
626 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
627 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
628 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . protect_sprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
630 $sql_extra3 .= " AND false ";
631 info(L10n::t('Group is empty'));
634 $o = replace_macros(get_markup_template('section_title.tpl'), [
635 '$title' => L10n::t('Group: %s', $group['name'])
638 $fields = ['id', 'name', 'network', 'writable', 'nurl',
639 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
640 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
641 $contact = dba::selectFirst('contact', $fields, $condition);
642 if (DBM::is_result($contact)) {
643 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
647 'name' => htmlentities($contact['name']),
648 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
649 'thumb' => proxy_url($contact['thumb'], false, PROXY_SIZE_THUMB),
650 'details' => $contact['location'],
653 $entries[0]['account_type'] = Contact::getAccountType($contact);
655 $o = replace_macros(get_markup_template('viewcontact_template.tpl'), [
656 'contacts' => $entries,
660 if ($contact['network'] === NETWORK_OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
661 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
664 notice(L10n::t('Invalid contact.') . EOL);
670 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
671 $o .= Profile::getBirthdays();
672 $o .= Profile::getEventsReminderHTML();
676 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ",
677 dbesc(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
680 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ",
681 dbesc(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
685 $sql_extra3 .= " AND $sql_table.`mention`";
688 // Normal conversation view
689 if ($order === 'post') {
690 $ordering = '`created`';
691 $order_mode = 'created';
693 $ordering = '`commented`';
694 $order_mode = 'commented';
697 if ($sql_order == '') {
698 $sql_order = "$sql_table.$ordering";
701 if (x($_GET, 'offset')) {
702 $sql_range = sprintf(" AND $sql_order <= '%s'", dbesc($_GET['offset']));
707 $pager_sql = networkPager($a, $update);
711 switch ($order_mode) {
713 if ($last_received != '') {
714 $last_date = $last_received;
715 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
716 $a->set_pager_page(1);
717 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
721 if ($last_commented != '') {
722 $last_date = $last_commented;
723 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
724 $a->set_pager_page(1);
725 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
729 if ($last_created != '') {
730 $last_date = $last_created;
731 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
732 $a->set_pager_page(1);
733 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
737 if (($last_id > 0) && ($sql_table == '`thread`')) {
738 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
739 $a->set_pager_page(1);
740 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
745 // Fetch a page full of parent items for this page
747 if (!empty($parent)) {
748 // Load only a single thread
749 $sql_extra4 = "`item`.`id` = ".intval($parent);
751 // Load all unseen items
752 $sql_extra4 = "`item`.`unseen`";
753 if (Config::get("system", "like_no_comment")) {
754 $sql_extra4 .= " AND `item`.`verb` = '".ACTIVITY_POST."'";
756 if ($order === 'post') {
757 // Only show toplevel posts when updating posts in this order mode
758 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
762 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
763 FROM `item` $sql_post_table
764 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
765 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
766 AND (`item`.`parent-uri` != `item`.`uri`
767 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
768 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
769 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
770 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
771 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
772 AND NOT `item`.`moderated` AND $sql_extra4
773 $sql_extra3 $sql_extra $sql_range $sql_nets
774 ORDER BY `order_date` DESC LIMIT 100",
775 intval(CONTACT_IS_SHARING),
776 intval(CONTACT_IS_FRIEND),
777 intval(local_user()),
781 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
782 FROM `thread` $sql_post_table
783 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
784 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
785 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
786 AND (`item`.`parent-uri` != `item`.`uri`
787 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
788 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
789 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
790 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
791 AND NOT `thread`.`moderated`
792 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
793 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
794 ORDER BY `order_date` DESC $pager_sql",
795 intval(CONTACT_IS_SHARING),
796 intval(CONTACT_IS_FRIEND),
797 intval(local_user()),
802 // Only show it when unfiltered (no groups, no networks, ...)
803 if (in_array($nets, ['', NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
804 if (DBM::is_result($r)) {
805 $top_limit = current($r)['order_date'];
806 $bottom_limit = end($r)['order_date'];
807 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
808 $_SESSION['network_last_top_limit'] = $top_limit;
811 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
814 // When checking for updates we need to fetch from the newest date to the newest date before
815 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
816 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
818 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
819 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
820 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
821 $bottom_limit = $_SESSION['network_last_date'];
823 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
824 $_SESSION['network_last_date_timestamp'] = time();
826 if ($last_date > $top_limit) {
827 $top_limit = $last_date;
828 } elseif ($a->pager['page'] == 1) {
829 // Highest possible top limit when we are on the first page
830 $top_limit = DateTimeFormat::utcNow();
833 $items = dba::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date` FROM `item`
834 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
835 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
836 ON `item`.`id` = `term`.`oid`
837 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
838 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
839 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
840 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
841 $top_limit, $bottom_limit);
843 $data = dba::inArray($items);
845 if (count($data) > 0) {
846 $tag_top_limit = current($data)['order_date'];
847 if ($_SESSION['network_last_date'] < $tag_top_limit) {
848 $_SESSION['network_last_date'] = $tag_top_limit;
851 logger('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
853 foreach ($r as $item) {
854 $s[$item['uri']] = $item;
856 foreach ($data as $item) {
857 // Don't show hash tag posts from blocked or ignored contacts
858 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
859 normalise_link($item['author-link']), local_user()];
860 if (!dba::exists('contact', $condition)) {
861 $s[$item['uri']] = $item;
873 if (DBM::is_result($items)) {
876 foreach ($items as $item) {
877 if ($date_offset < $item['order_date']) {
878 $date_offset = $item['order_date'];
880 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
881 $parents_arr[] = $item['item_id'];
884 $parents_str = implode(', ', $parents_arr);
887 if (x($_GET, 'offset')) {
888 $date_offset = $_GET['offset'];
891 $a->page_offset = $date_offset;
893 // We aren't going to try and figure out at the item, group, and page
894 // level which items you've seen and which you haven't. If you're looking
895 // at the top level network page just mark everything seen.
897 if (!$gid && !$cid && !$star) {
898 $condition = ['unseen' => true, 'uid' => local_user()];
899 networkSetSeen($condition);
900 } elseif ($parents_str) {
901 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . dbesc($parents_str) . ")", local_user()];
902 networkSetSeen($condition);
907 $o .= networkConversation($a, $items, $mode, $update, $ordering);
913 * @brief Get the network tabs menu
915 * @param App $a The global App
916 * @return string Html of the networktab
918 function network_tabs(App $a)
921 /// @TODO fix this logic, reduce duplication
922 /// $a->page['content'] .= '<div class="tabs-wrapper">';
923 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
925 // if no tabs are selected, defaults to comments
926 if ($no_active == 'active') {
927 $all_active = 'active';
935 'label' => L10n::t('Commented Order'),
936 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
937 'sel' => $all_active,
938 'title' => L10n::t('Sort by Comment Date'),
939 'id' => 'commented-order-tab',
943 'label' => L10n::t('Posted Order'),
944 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
945 'sel' => $postord_active,
946 'title' => L10n::t('Sort by Post Date'),
947 'id' => 'posted-order-tab',
952 if (Feature::isEnabled(local_user(), 'personal_tab')) {
954 'label' => L10n::t('Personal'),
955 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
956 'sel' => $conv_active,
957 'title' => L10n::t('Posts that mention or involve you'),
958 'id' => 'personal-tab',
963 if (Feature::isEnabled(local_user(), 'new_tab')) {
965 'label' => L10n::t('New'),
966 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
967 'sel' => $new_active,
968 'title' => L10n::t('Activity Stream - by date'),
969 'id' => 'activitiy-by-date-tab',
974 if (Feature::isEnabled(local_user(), 'link_tab')) {
976 'label' => L10n::t('Shared Links'),
977 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
978 'sel' => $bookmarked_active,
979 'title' => L10n::t('Interesting Links'),
980 'id' => 'shared-links-tab',
985 if (Feature::isEnabled(local_user(), 'star_posts')) {
987 'label' => L10n::t('Starred'),
988 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
989 'sel' => $starred_active,
990 'title' => L10n::t('Favourite Posts'),
991 'id' => 'starred-posts-tab',
996 // save selected tab, but only if not in file mode
997 if (!x($_GET, 'file')) {
998 PConfig::set(local_user(), 'network.view', 'tab.selected', [
999 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1003 $arr = ['tabs' => $tabs];
1004 Addon::callHooks('network_tabs', $arr);
1006 $tpl = get_markup_template('common_tabs.tpl');
1008 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
1010 // --- end item filter tabs