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']) && ($_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();
438 $pager_sql = networkPager($a, $update);
441 $condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
442 $file, TERM_OBJ_POST, TERM_FILE, local_user()];
443 $params = ['order' => ['tid' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
444 $result = dba::select('term', ['oid'], $condition);
447 while ($term = dba::fetch($result)) {
448 $posts[] = $term['oid'];
452 $condition = ['uid' => local_user(), 'id' => $posts];
454 $condition = ['uid' => local_user()];
457 $params = ['order' => ['id' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
458 $result = Item::selectForUser(local_user(), [], $condition, $params);
459 $items = Item::inArray($result);
461 $condition = ['unseen' => true, 'uid' => local_user()];
462 networkSetSeen($condition);
464 $o .= networkConversation($a, $items, 'network-new', $update);
470 * @brief Get the network content in threaded view
472 * @param App $a The global App
473 * @param integer $update Used for the automatic reloading
474 * @return string HTML of the network content in flat view
476 function networkThreadedView(App $a, $update, $parent)
478 // Rawmode is used for fetching new content at the end of the page
479 $rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
481 if (isset($_GET['last_received']) && isset($_GET['last_commented']) && isset($_GET['last_created']) && isset($_GET['last_id'])) {
482 $last_received = DBM::date($_GET['last_received']);
483 $last_commented = DBM::date($_GET['last_commented']);
484 $last_created = DBM::date($_GET['last_created']);
485 $last_id = intval($_GET['last_id']);
488 $last_commented = '';
493 $datequery = $datequery2 = '';
498 for ($x = 1; $x < $a->argc; $x ++) {
499 if (is_a_date_arg($a->argv[$x])) {
501 $datequery2 = escape_tags($a->argv[$x]);
503 $datequery = escape_tags($a->argv[$x]);
504 $_GET['order'] = 'post';
506 } elseif (intval($a->argv[$x])) {
507 $gid = intval($a->argv[$x]);
508 $def_acl = ['allow_gid' => '<' . $gid . '>'];
515 $cid = intval(defaults($_GET, 'cid' , 0));
516 $star = intval(defaults($_GET, 'star' , 0));
517 $bmark = intval(defaults($_GET, 'bmark', 0));
518 $conv = intval(defaults($_GET, 'conv' , 0));
519 $order = notags(defaults($_GET, 'order', 'comment'));
520 $nets = defaults($_GET, 'nets' , '');
523 $def_acl = ['allow_cid' => '<' . intval($cid) . '>'];
527 $r = dba::select('contact', ['id'], ['uid' => local_user(), 'network' => $nets], ['self' => false]);
530 while ($rr = dba::fetch($r)) {
531 $str .= '<' . $rr['id'] . '>';
534 $def_acl = ['allow_cid' => $str];
538 if (!$update && !$rawmode) {
539 $tabs = network_tabs($a);
543 if (($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
544 notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
545 "Warning: This group contains %s members from a network that doesn't allow non public messages.",
547 notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
551 Nav::setSelected('network');
556 // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
557 $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
558 $contact = dba::selectFirst('contact', ['addr', 'nick'], $condition);
559 if (DBM::is_result($contact)) {
560 if ($contact['addr'] != '') {
561 $content = '!' . $contact['addr'];
563 $content = '!' . $contact['nick'] . '+' . $cid;
570 'allow_location' => $a->user['allow_location'],
571 'default_location' => $a->user['default-location'],
572 'nickname' => $a->user['nickname'],
573 'lockstate' => ((($gid) || ($cid) || ($nets) || (is_array($a->user) &&
574 ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
575 (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
576 'default_perms' => ACL::getDefaultUserPermissions($a->user),
577 'acl' => ACL::getFullSelectorHTML((($gid || $cid || $nets) ? $def_acl : $a->user), true),
578 'bang' => (($gid || $cid || $nets) ? '!' : ''),
579 'visitor' => 'block',
580 'profile_uid' => local_user(),
581 'content' => $content,
584 $o .= status_editor($a, $x);
587 // We don't have to deal with ACLs on this page. You're looking at everything
588 // that belongs to you, hence you can see all of it. We will filter by group if
591 $sql_post_table = '';
592 $sql_options = (($star) ? " AND `thread`.`starred` " : '');
593 $sql_options .= (($bmark) ? " AND `thread`.`bookmark` " : '');
594 $sql_extra = $sql_options;
597 $sql_table = '`thread`';
598 $sql_parent = '`iid`';
601 $sql_table = '`item`';
602 $sql_parent = '`parent`';
603 $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
606 $sql_nets = (($nets) ? sprintf(" AND $sql_table.`network` = '%s' ", dbesc($nets)) : '');
607 $sql_tag_nets = (($nets) ? sprintf(" AND `item`.`network` = '%s' ", dbesc($nets)) : '');
610 $group = dba::selectFirst('group', ['name'], ['id' => $gid, 'uid' => local_user()]);
611 if (!DBM::is_result($group)) {
615 notice(L10n::t('No such group') . EOL);
620 $contacts = Group::expand([$gid]);
622 if ((is_array($contacts)) && count($contacts)) {
623 $contact_str_self = '';
625 $contact_str = implode(',', $contacts);
626 $self = dba::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
627 if (DBM::is_result($self)) {
628 $contact_str_self = $self['id'];
631 $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = " . $sql_table . "." . $sql_parent;
632 $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
633 $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '" . protect_sprintf('%<' . intval($gid) . '>%') . "' AND `temp1`.`private`))";
635 $sql_extra3 .= " AND false ";
636 info(L10n::t('Group is empty'));
639 $o = replace_macros(get_markup_template('section_title.tpl'), [
640 '$title' => L10n::t('Group: %s', $group['name'])
643 $fields = ['id', 'name', 'network', 'writable', 'nurl',
644 'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
645 $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
646 $contact = dba::selectFirst('contact', $fields, $condition);
647 if (DBM::is_result($contact)) {
648 $sql_extra = " AND " . $sql_table . ".`contact-id` = " . intval($cid);
652 'name' => htmlentities($contact['name']),
653 'itemurl' => defaults($contact, 'addr', $contact['nurl']),
654 'thumb' => proxy_url($contact['thumb'], false, PROXY_SIZE_THUMB),
655 'details' => $contact['location'],
658 $entries[0]['account_type'] = Contact::getAccountType($contact);
660 $o = replace_macros(get_markup_template('viewcontact_template.tpl'), [
661 'contacts' => $entries,
665 if ($contact['network'] === NETWORK_OSTATUS && $contact['writable'] && !PConfig::get(local_user(),'system','nowarn_insecure')) {
666 notice(L10n::t('Private messages to this person are at risk of public disclosure.') . EOL);
669 notice(L10n::t('Invalid contact.') . EOL);
675 if (!$gid && !$cid && !$update && !Config::get('theme', 'hide_eventlist')) {
676 $o .= Profile::getBirthdays();
677 $o .= Profile::getEventsReminderHTML();
681 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ",
682 dbesc(DateTimeFormat::convert($datequery, 'UTC', date_default_timezone_get()))));
685 $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ",
686 dbesc(DateTimeFormat::convert($datequery2, 'UTC', date_default_timezone_get()))));
690 $sql_extra3 .= " AND $sql_table.`mention`";
693 // Normal conversation view
694 if ($order === 'post') {
695 $ordering = '`created`';
696 $order_mode = 'created';
698 $ordering = '`commented`';
699 $order_mode = 'commented';
702 $sql_order = "$sql_table.$ordering";
704 if (x($_GET, 'offset')) {
705 $sql_range = sprintf(" AND $sql_order <= '%s'", dbesc($_GET['offset']));
710 $pager_sql = networkPager($a, $update);
714 switch ($order_mode) {
716 if ($last_received != '') {
717 $last_date = $last_received;
718 $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
719 $a->set_pager_page(1);
720 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
724 if ($last_commented != '') {
725 $last_date = $last_commented;
726 $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
727 $a->set_pager_page(1);
728 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
732 if ($last_created != '') {
733 $last_date = $last_created;
734 $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
735 $a->set_pager_page(1);
736 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
740 if (($last_id > 0) && ($sql_table == '`thread`')) {
741 $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
742 $a->set_pager_page(1);
743 $pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
748 // Fetch a page full of parent items for this page
750 if (!empty($parent)) {
751 // Load only a single thread
752 $sql_extra4 = "`item`.`id` = ".intval($parent);
754 // Load all unseen items
755 $sql_extra4 = "`item`.`unseen`";
756 if (Config::get("system", "like_no_comment")) {
757 $sql_extra4 .= " AND `item`.`gravity` IN (" . GRAVITY_PARENT . "," . GRAVITY_COMMENT . ")";
759 if ($order === 'post') {
760 // Only show toplevel posts when updating posts in this order mode
761 $sql_extra4 .= " AND `item`.`id` = `item`.`parent`";
765 $r = q("SELECT `item`.`parent-uri` AS `uri`, `item`.`parent` AS `item_id`, $sql_order AS `order_date`
766 FROM `item` $sql_post_table
767 STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
768 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
769 AND (`item`.`parent-uri` != `item`.`uri`
770 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
771 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
772 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
773 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted`
774 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
775 AND NOT `item`.`moderated` AND $sql_extra4
776 $sql_extra3 $sql_extra $sql_range $sql_nets
777 ORDER BY `order_date` DESC LIMIT 100",
778 intval(CONTACT_IS_SHARING),
779 intval(CONTACT_IS_FRIEND),
780 intval(local_user()),
784 $r = q("SELECT `item`.`uri`, `thread`.`iid` AS `item_id`, $sql_order AS `order_date`
785 FROM `thread` $sql_post_table
786 STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
787 AND (NOT `contact`.`blocked` OR `contact`.`pending`)
788 STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
789 AND (`item`.`parent-uri` != `item`.`uri`
790 OR `contact`.`uid` = `item`.`uid` AND `contact`.`self`
791 OR `contact`.`rel` IN (%d, %d) AND NOT `contact`.`readonly`)
792 LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = %d
793 WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
794 AND NOT `thread`.`moderated`
795 AND (`user-item`.`hidden` IS NULL OR NOT `user-item`.`hidden`)
796 $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
797 ORDER BY `order_date` DESC $pager_sql",
798 intval(CONTACT_IS_SHARING),
799 intval(CONTACT_IS_FRIEND),
800 intval(local_user()),
805 // Only show it when unfiltered (no groups, no networks, ...)
806 if (in_array($nets, ['', NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS]) && (strlen($sql_extra . $sql_extra2 . $sql_extra3) == 0)) {
807 if (DBM::is_result($r)) {
808 $top_limit = current($r)['order_date'];
809 $bottom_limit = end($r)['order_date'];
810 if (empty($_SESSION['network_last_top_limit']) || ($_SESSION['network_last_top_limit'] < $top_limit)) {
811 $_SESSION['network_last_top_limit'] = $top_limit;
814 $top_limit = $bottom_limit = DateTimeFormat::utcNow();
817 // When checking for updates we need to fetch from the newest date to the newest date before
818 // Only do this, when the last stored date isn't too long ago (10 times the update interval)
819 $browser_update = PConfig::get(local_user(), 'system', 'update_interval', 40000) / 1000;
821 if (($browser_update > 0) && $update && !empty($_SESSION['network_last_date']) &&
822 (($bottom_limit < $_SESSION['network_last_date']) || ($top_limit == $bottom_limit)) &&
823 ((time() - $_SESSION['network_last_date_timestamp']) < ($browser_update * 10))) {
824 $bottom_limit = $_SESSION['network_last_date'];
826 $_SESSION['network_last_date'] = defaults($_SESSION, 'network_last_top_limit', $top_limit);
827 $_SESSION['network_last_date_timestamp'] = time();
829 if ($last_date > $top_limit) {
830 $top_limit = $last_date;
831 } elseif ($a->pager['page'] == 1) {
832 // Highest possible top limit when we are on the first page
833 $top_limit = DateTimeFormat::utcNow();
836 $items = dba::p("SELECT `item`.`parent-uri` AS `uri`, 0 AS `item_id`, `item`.$ordering AS `order_date` FROM `item`
837 STRAIGHT_JOIN (SELECT `oid` FROM `term` WHERE `term` IN
838 (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
839 ON `item`.`id` = `term`.`oid`
840 STRAIGHT_JOIN `contact` AS `author` ON `author`.`id` = `item`.`author-id`
841 WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?
842 AND NOT `author`.`hidden` AND NOT `author`.`blocked`" . $sql_tag_nets,
843 local_user(), TERM_OBJ_POST, TERM_HASHTAG,
844 $top_limit, $bottom_limit);
846 $data = dba::inArray($items);
848 if (count($data) > 0) {
849 $tag_top_limit = current($data)['order_date'];
850 if ($_SESSION['network_last_date'] < $tag_top_limit) {
851 $_SESSION['network_last_date'] = $tag_top_limit;
854 logger('Tagged items: ' . count($data) . ' - ' . $bottom_limit . ' - ' . $top_limit . ' - ' . local_user().' - '.(int)$update);
856 foreach ($r as $item) {
857 $s[$item['uri']] = $item;
859 foreach ($data as $item) {
860 // Don't show hash tag posts from blocked or ignored contacts
861 $condition = ["`nurl` = ? AND `uid` = ? AND (`blocked` OR `readonly`)",
862 normalise_link($item['author-link']), local_user()];
863 if (!dba::exists('contact', $condition)) {
864 $s[$item['uri']] = $item;
876 if (DBM::is_result($items)) {
879 foreach ($items as $item) {
880 if ($date_offset < $item['order_date']) {
881 $date_offset = $item['order_date'];
883 if (!in_array($item['item_id'], $parents_arr) && ($item['item_id'] > 0)) {
884 $parents_arr[] = $item['item_id'];
887 $parents_str = implode(', ', $parents_arr);
890 if (x($_GET, 'offset')) {
891 $date_offset = $_GET['offset'];
894 $a->page_offset = $date_offset;
896 // We aren't going to try and figure out at the item, group, and page
897 // level which items you've seen and which you haven't. If you're looking
898 // at the top level network page just mark everything seen.
900 if (!$gid && !$cid && !$star) {
901 $condition = ['unseen' => true, 'uid' => local_user()];
902 networkSetSeen($condition);
903 } elseif ($parents_str) {
904 $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . dbesc($parents_str) . ")", local_user()];
905 networkSetSeen($condition);
910 $o .= networkConversation($a, $items, $mode, $update, $ordering);
916 * @brief Get the network tabs menu
918 * @param App $a The global App
919 * @return string Html of the networktab
921 function network_tabs(App $a)
924 /// @TODO fix this logic, reduce duplication
925 /// $a->page['content'] .= '<div class="tabs-wrapper">';
926 list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active) = network_query_get_sel_tab($a);
928 // if no tabs are selected, defaults to comments
929 if ($no_active == 'active') {
930 $all_active = 'active';
938 'label' => L10n::t('Commented Order'),
939 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
940 'sel' => $all_active,
941 'title' => L10n::t('Sort by Comment Date'),
942 'id' => 'commented-order-tab',
946 'label' => L10n::t('Posted Order'),
947 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
948 'sel' => $postord_active,
949 'title' => L10n::t('Sort by Post Date'),
950 'id' => 'posted-order-tab',
955 if (Feature::isEnabled(local_user(), 'personal_tab')) {
957 'label' => L10n::t('Personal'),
958 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
959 'sel' => $conv_active,
960 'title' => L10n::t('Posts that mention or involve you'),
961 'id' => 'personal-tab',
966 if (Feature::isEnabled(local_user(), 'new_tab')) {
968 'label' => L10n::t('New'),
969 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
970 'sel' => $new_active,
971 'title' => L10n::t('Activity Stream - by date'),
972 'id' => 'activitiy-by-date-tab',
977 if (Feature::isEnabled(local_user(), 'link_tab')) {
979 'label' => L10n::t('Shared Links'),
980 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
981 'sel' => $bookmarked_active,
982 'title' => L10n::t('Interesting Links'),
983 'id' => 'shared-links-tab',
988 if (Feature::isEnabled(local_user(), 'star_posts')) {
990 'label' => L10n::t('Starred'),
991 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
992 'sel' => $starred_active,
993 'title' => L10n::t('Favourite Posts'),
994 'id' => 'starred-posts-tab',
999 // save selected tab, but only if not in file mode
1000 if (!x($_GET, 'file')) {
1001 PConfig::set(local_user(), 'network.view', 'tab.selected', [
1002 $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active
1006 $arr = ['tabs' => $tabs];
1007 Addon::callHooks('network_tabs', $arr);
1009 $tpl = get_markup_template('common_tabs.tpl');
1011 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
1013 // --- end item filter tabs