]> git.mxchange.org Git - friendica.git/blobdiff - mod/network.php
Merge pull request #4208 from annando/thr-parent
[friendica.git] / mod / network.php
index e74df1633aab0daebfeb440826aa1ac1788f9a2f..84dc9255e2df5ec235e15cc07352211e22f6c632 100644 (file)
@@ -3,15 +3,17 @@
  * @file mod/network.php
  */
 use Friendica\App;
+use Friendica\Content\Feature;
 use Friendica\Content\ForumManager;
 use Friendica\Core\System;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Database\DBM;
-use Friendica\Object\Contact;
+use Friendica\Model\Contact;
+use Friendica\Model\Group;
+use Friendica\Module\Login;
 
 require_once 'include/conversation.php';
-require_once 'include/group.php';
 require_once 'include/contact_widgets.php';
 require_once 'include/items.php';
 require_once 'include/acl_selectors.php';
@@ -156,8 +158,8 @@ function network_init(App $a) {
                $a->page['aside'] = '';
        }
 
-       $a->page['aside'] .= (feature_enabled(local_user(),'groups') ? group_side('network/0','network','standard',$group_id) : '');
-       $a->page['aside'] .= (feature_enabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
+       $a->page['aside'] .= (Feature::isEnabled(local_user(),'groups') ? Group::sidebarWidget('network/0','network','standard',$group_id) : '');
+       $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : '');
        $a->page['aside'] .= posted_date_widget('network',local_user(),false);
        $a->page['aside'] .= networks_widget('network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
        $a->page['aside'] .= saved_searches($search);
@@ -166,7 +168,7 @@ function network_init(App $a) {
 
 function saved_searches($search) {
 
-       if (!feature_enabled(local_user(),'savedsearch')) {
+       if (!Feature::isEnabled(local_user(),'savedsearch')) {
                return '';
        }
 
@@ -364,7 +366,7 @@ function networkConversation($a, $items, $mode, $update) {
        // Set this so that the conversation function can find out contact info for our wall-wall items
        $a->page_contact = $a->contact;
 
-       $o .= conversation($a, $items, $mode, $update);
+       $o = conversation($a, $items, $mode, $update);
 
        if (!$update) {
                if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
@@ -379,8 +381,7 @@ function networkConversation($a, $items, $mode, $update) {
 
 function network_content(App $a, $update = 0) {
        if (!local_user()) {
-               $_SESSION['return_url'] = $a->query_string;
-               return login(false);
+               return Login::form();
        }
 
        /// @TODO Is this really necessary? $a is already available to hooks
@@ -453,11 +454,10 @@ function networkFlatView(App $a, $update = 0) {
                        'bang'  => '',
                        'visitor' => 'block',
                        'profile_uid' => local_user(),
-                       'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
                        'content' => '',
                );
 
-               $o .= status_editor($a,$x);
+               $o .= status_editor($a, $x);
 
                if (!Config::get('theme','hide_eventlist')) {
                        $o .= get_birthdays();
@@ -566,10 +566,10 @@ function networkThreadedView(App $a, $update = 0) {
                $o .= $tabs;
 
                if ($group) {
-                       if (($t = group_public_members($group)) && !PConfig::get(local_user(),'system','nowarn_insecure')) {
-                               notice(sprintf(tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
+                       if (($t = Contact::getOStatusCountByGroupId($group)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
+                               notice(tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
                                                "Warning: This group contains %s members from a network that doesn't allow non public messages.",
-                                               $t), $t).EOL);
+                                               $t) . EOL);
                                notice(t("Messages in this group won't be send to these receivers.").EOL);
                        }
                }
@@ -580,8 +580,8 @@ function networkThreadedView(App $a, $update = 0) {
 
                if ($cid) {
                        // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
-                       $condition = array("`id` = ? AND (`forum` OR `prv`)", $cid);
-                       $contact = dba::select('contact', array('addr', 'nick'), $condition, array('limit' => 1));
+                       $condition = ["`id` = ? AND (`forum` OR `prv`)", $cid];
+                       $contact = dba::selectFirst('contact', ['addr', 'nick'], $condition);
                        if (DBM::is_result($contact)) {
                                if ($contact["addr"] != '') {
                                        $content = "!".$contact["addr"];
@@ -604,12 +604,10 @@ function networkThreadedView(App $a, $update = 0) {
                        'bang'  => (($group || $cid || $nets) ? '!' : ''),
                        'visitor' => 'block',
                        'profile_uid' => local_user(),
-                       'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
                        'content' => $content,
                );
 
-               $o .= status_editor($a,$x);
-
+               $o .= status_editor($a, $x);
        }
 
        // We don't have to deal with ACLs on this page. You're looking at everything
@@ -634,7 +632,7 @@ function networkThreadedView(App $a, $update = 0) {
        $sql_nets = (($nets) ? sprintf(" and $sql_table.`network` = '%s' ", dbesc($nets)) : '');
 
        if ($group) {
-               $r = dba::select('group', array('name'), array('id' => $group, 'uid' => $_SESSION['uid']), array('limit' => 1));
+               $r = dba::selectFirst('group', ['name'], ['id' => $group, 'uid' => $_SESSION['uid']]);
                if (!DBM::is_result($r)) {
                        if ($update)
                                killme();
@@ -643,13 +641,13 @@ function networkThreadedView(App $a, $update = 0) {
                        // NOTREACHED
                }
 
-               $contacts = expand_groups(array($group));
+               $contacts = Group::expand(array($group));
 
                if ((is_array($contacts)) && count($contacts)) {
                        $contact_str_self = "";
 
                        $contact_str = implode(',',$contacts);
-                       $self = dba::select('contact', array('id'), array('uid' => $_SESSION['uid'], 'self' => true), array('limit' => 1));
+                       $self = dba::selectFirst('contact', ['id'], ['uid' => $_SESSION['uid'], 'self' => true]);
                        if (DBM::is_result($self)) {
                                $contact_str_self = $self["id"];
                        }
@@ -663,14 +661,14 @@ function networkThreadedView(App $a, $update = 0) {
                }
 
                $o = replace_macros(get_markup_template("section_title.tpl"),array(
-                       '$title' => sprintf(t('Group: %s'), $r['name'])
+                       '$title' => t('Group: %s', $r['name'])
                )) . $o;
 
        } elseif ($cid) {
-               $fields = array('id', 'name', 'network', 'writable', 'nurl',
-                               'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location');
-               $condition = array("`id` = ? AND (NOT `blocked` OR `pending`)", $cid);
-               $r = dba::select('contact', $fields, $condition, array('limit' => 1));
+               $fields = ['id', 'name', 'network', 'writable', 'nurl',
+                               'forum', 'prv', 'contact-type', 'addr', 'thumb', 'location'];
+               $condition = ["`id` = ? AND (NOT `blocked` OR `pending`)", $cid];
+               $r = dba::selectFirst('contact', $fields, $condition);
                if (DBM::is_result($r)) {
                        $sql_extra = " AND ".$sql_table.".`contact-id` = ".intval($cid);
 
@@ -715,13 +713,6 @@ function networkThreadedView(App $a, $update = 0) {
        $sql_order = "";
        $order_mode = "received";
 
-       if (strlen($file)) {
-               $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` ",
-                               dbesc(protect_sprintf($file)), intval(TERM_OBJ_POST), intval(TERM_FILE), intval(local_user()));
-               $sql_order = "`item`.`id`";
-               $order_mode = "id";
-       }
-
        if ($conv) {
                $sql_extra3 .= " AND $sql_table.`mention`";
        }
@@ -743,37 +734,44 @@ function networkThreadedView(App $a, $update = 0) {
                $sql_order = "$sql_table.$ordering";
        }
 
-       if (($_GET["offset"] != "")) {
-               $sql_extra3 .= sprintf(" AND $sql_order <= '%s'", dbesc($_GET["offset"]));
+       if (x($_GET, 'offset')) {
+               $sql_range = sprintf(" AND $sql_order <= '%s'", dbesc($_GET["offset"]));
+       } else {
+               $sql_range = '';
        }
 
        $pager_sql = networkPager($a, $update);
 
+       $last_date = '';
+
        switch ($order_mode) {
                case 'received':
                        if ($last_received != '') {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
+                               $last_date = $last_received;
+                               $sql_range .= sprintf(" AND $sql_table.`received` < '%s'", dbesc($last_received));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
                        break;
                case 'commented':
                        if ($last_commented != '') {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
+                               $last_date = $last_commented;
+                               $sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", dbesc($last_commented));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
                        break;
                case 'created':
                        if ($last_created != '') {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
+                               $last_date = $last_created;
+                               $sql_range .= sprintf(" AND $sql_table.`created` < '%s'", dbesc($last_created));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
                        break;
                case 'id':
                        if (($last_id > 0) && ($sql_table == "`thread`")) {
-                               $sql_extra3 .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
+                               $sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", dbesc($last_id));
                                $a->set_pager_page(1);
                                $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
                        }
@@ -787,37 +785,67 @@ function networkThreadedView(App $a, $update = 0) {
                } else {
                        $sql_extra4 = "";
                }
-
-               $r = q("SELECT `item`.`parent` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`
+               $r = q("SELECT `item`.`parent` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`, $sql_order AS `order_date`
                        FROM $sql_table $sql_post_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
                        AND (NOT `contact`.`blocked` OR `contact`.`pending`)
                        WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` $sql_extra4
                        AND NOT `item`.`moderated` AND `item`.`unseen`
-                       $sql_extra3 $sql_extra $sql_nets
-                       ORDER BY `item_id` DESC LIMIT 100",
+                       $sql_extra3 $sql_extra $sql_range $sql_nets
+                       ORDER BY `order_date` DESC LIMIT 100",
                        intval(local_user())
                );
        } else {
-               $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`
+               $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`, $sql_order AS `order_date`
                        FROM $sql_table $sql_post_table STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
                        AND (NOT `contact`.`blocked` OR `contact`.`pending`)
                        WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
                        AND NOT `thread`.`moderated`
-                       $sql_extra2 $sql_extra3 $sql_extra $sql_nets
+                       $sql_extra2 $sql_extra3 $sql_range $sql_extra $sql_nets
                        ORDER BY $sql_order DESC $pager_sql",
                        intval(local_user())
                );
        }
 
+       // Only show it when unfiltered (no groups, no networks, ...)
+       if (Config::get('system', 'comment_public') && (count($r) > 0) && (strlen($sql_extra . $sql_extra2 . $sql_extra3 . $sql_extra4 . $sql_nets) == 0)) {
+               $top_limit = current($r)['order_date'];
+               $bottom_limit = end($r)['order_date'];
+
+               // When checking for updates we need to fetch from the newest date to the newest date before
+               if ($update && !empty($_SESSION['network_last_date']) && ($bottom_limit > $_SESSION['network_last_date'])) {
+                       $bottom_limit = $_SESSION['network_last_date'];
+               }
+               $_SESSION['network_last_date'] = $top_limit;
+
+               if ($last_date > $top_limit) {
+                       $top_limit = $last_date;
+               }
+
+               $items = dba::p("SELECT `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid` FROM `item`
+                       INNER JOIN (SELECT `oid` FROM `term` WHERE `term` IN
+                               (SELECT SUBSTR(`term`, 2) FROM `search` WHERE `uid` = ? AND `term` LIKE '#%') AND `otype` = ? AND `type` = ? AND `uid` = 0) AS `term`
+                       ON `item`.`id` = `term`.`oid`
+                       INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                       WHERE `item`.`uid` = 0 AND `item`.$ordering < ? AND `item`.$ordering > ?",
+                       local_user(), TERM_OBJ_POST, TERM_HASHTAG, $top_limit, $bottom_limit);
+               $data = dba::inArray($items);
+
+               if (count($data) > 0) {
+                       logger('Tagged items: '.count($data).' - '.$bottom_limit." - ".$top_limit.' - '.local_user()); //$last_date);
+                       $r = array_merge($r, $data);
+               }
+       }
+
        // Then fetch all the children of the parents that are on this page
 
        $parents_arr = array();
        $parents_str = '';
        $date_offset = "";
 
+       $items = array();
        if (DBM::is_result($r)) {
                foreach ($r as $rr) {
-                       if (!in_array($rr['item_id'],$parents_arr)) {
+                       if (!in_array($rr['item_id'], $parents_arr)) {
                                $parents_arr[] = $rr['item_id'];
                        }
                }
@@ -832,13 +860,9 @@ function networkThreadedView(App $a, $update = 0) {
                        $max_comments = 100;
                }
 
-               $items = array();
-
                foreach ($parents_arr AS $parents) {
-                       $thread_items = dba::p(item_query()." AND `item`.`uid` = ?
-                               AND `item`.`parent` = ?
-                               ORDER BY `item`.`commented` DESC LIMIT ".intval($max_comments + 1),
-                               local_user(),
+                       $thread_items = dba::p(item_query() . " AND `item`.`parent` = ?
+                               ORDER BY `item`.`commented` DESC LIMIT " . intval($max_comments + 1),
                                $parents
                        );
 
@@ -846,15 +870,15 @@ function networkThreadedView(App $a, $update = 0) {
                                $items = array_merge($items, dba::inArray($thread_items));
                        }
                }
-               $items = conv_sort($items,$ordering);
-       } else {
-               $items = array();
+               $items = conv_sort($items, $ordering);
        }
 
-       if ($_GET["offset"] == "") {
+       if (x($_GET, 'offset')) {
+               $date_offset = $_GET["offset"];
+       } elseif(count($items)) {
                $date_offset = $items[0][$order_mode];
        } else {
-               $date_offset = $_GET["offset"];
+               $date_offset = '';
        }
 
        $a->page_offset = $date_offset;
@@ -918,7 +942,7 @@ function network_tabs(App $a)
                ),
        );
 
-       if (feature_enabled(local_user(),'personal_tab')) {
+       if (Feature::isEnabled(local_user(),'personal_tab')) {
                $tabs[] = array(
                        'label' => t('Personal'),
                        'url'   => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
@@ -929,7 +953,7 @@ function network_tabs(App $a)
                );
        }
 
-       if (feature_enabled(local_user(),'new_tab')) {
+       if (Feature::isEnabled(local_user(),'new_tab')) {
                $tabs[] = array(
                        'label' => t('New'),
                        'url'   => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
@@ -940,7 +964,7 @@ function network_tabs(App $a)
                );
        }
 
-       if (feature_enabled(local_user(),'link_tab')) {
+       if (Feature::isEnabled(local_user(),'link_tab')) {
                $tabs[] = array(
                        'label' => t('Shared Links'),
                        'url'   => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
@@ -951,7 +975,7 @@ function network_tabs(App $a)
                );
        }
 
-       if (feature_enabled(local_user(),'star_posts')) {
+       if (Feature::isEnabled(local_user(),'star_posts')) {
                $tabs[] = array(
                        'label' => t('Starred'),
                        'url'   => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',