]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #9374 from annando/issue-9358-b
[friendica.git] / src / Model / Contact.php
index a6bdb042a39c053c692ddb51741db23ebb7c3cc0..6bbc0921cad214f5ac99b790fabae4d523941c8f 100644 (file)
@@ -225,7 +225,7 @@ class Contact
                // Add internal fields
                $removal = [];
                if (!empty($fields)) {
-                       foreach (['id', 'avatar', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
+                       foreach (['id', 'avatar', 'created', 'updated', 'last-update', 'success_update', 'failure_update', 'network'] as $internal) {
                                if (!in_array($internal, $fields)) {
                                        $fields[] = $internal;
                                        $removal[] = $internal;
@@ -255,9 +255,8 @@ class Contact
                }
 
                // Update the contact in the background if needed
-               $updated = max($contact['success_update'], $contact['updated'], $contact['last-update'], $contact['failure_update']);
-               if ((($updated < DateTimeFormat::utc('now -7 days')) || empty($contact['avatar'])) &&
-                       in_array($contact['network'], Protocol::FEDERATED)) {
+               $updated = max($contact['success_update'], $contact['created'], $contact['updated'], $contact['last-update'], $contact['failure_update']);
+               if (($updated < DateTimeFormat::utc('now -7 days')) && in_array($contact['network'], Protocol::FEDERATED)) {
                        Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']);
                }
 
@@ -1248,25 +1247,27 @@ class Contact
         *
         * @param string $contact_url Contact URL
         * @param bool   $thread_mode
-        * @param int    $update
+        * @param int    $update      Update mode 
+        * @param int    $parent      Item parent ID for the update mode
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0)
+       public static function getPostsFromUrl($contact_url, $thread_mode = false, $update = 0, $parent = 0)
        {
-               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update);
+               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent);
        }
 
        /**
         * Returns posts from a given contact id
         *
-        * @param integer $cid
-        * @param bool    $thread_mode
-        * @param integer $update
+        * @param int  $cid         Contact ID
+        * @param bool $thread_mode
+        * @param int  $update      Update mode 
+        * @param int  $parent     Item parent ID for the update mode
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromId($cid, $thread_mode = false, $update = 0)
+       public static function getPostsFromId($cid, $thread_mode = false, $update = 0, $parent = 0)
        {
                $a = DI::app();
 
@@ -1291,9 +1292,13 @@ class Contact
                                $cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
                }
 
-               $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
-               if (!empty($last_received)) {
-                       $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
+               if (!empty($parent)) {
+                       $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
+               } else {
+                       $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
+                       if (!empty($last_received)) {
+                               $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
+                       }
                }
 
                if (DI::mode()->isMobile()) {
@@ -1336,7 +1341,7 @@ class Contact
 
                        $items = Item::inArray($r);
 
-                       $o .= conversation($a, $items, 'contact-posts', false);
+                       $o .= conversation($a, $items, 'contact-posts', $update);
                }
 
                if (!$update) {
@@ -1859,10 +1864,6 @@ class Contact
                        return false;
                }
 
-               if (Contact\Relation::isDiscoverable($ret['url'])) {
-                       Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
-               }
-
                if (isset($ret['hide']) && is_bool($ret['hide'])) {
                        $ret['unsearchable'] = $ret['hide'];
                }
@@ -1911,6 +1912,10 @@ class Contact
                if (!$update) {
                        self::updateContact($id, $uid, $ret['url'], ['failed' => false, 'last-update' => $updated, 'success_update' => $updated]);
 
+                       if (Contact\Relation::isDiscoverable($ret['url'])) {
+                               Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
+                       }
+       
                        // Update the public contact
                        if ($uid != 0) {
                                $contact = self::getByURL($ret['url'], false, ['id']);
@@ -1948,6 +1953,10 @@ class Contact
 
                self::updateContact($id, $uid, $ret['url'], $ret);
 
+               if (Contact\Relation::isDiscoverable($ret['url'])) {
+                       Worker::add(PRIORITY_LOW, 'ContactDiscovery', $ret['url']);
+               }
+
                return true;
        }
 
@@ -2578,7 +2587,7 @@ class Contact
        {
                $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
 
-               if (!Session::isAuthenticated() || ($contact['network'] != Protocol::DFRN)) {
+               if (!Session::isAuthenticated()) {
                        return $destination;
                }
 
@@ -2587,6 +2596,14 @@ class Contact
                        return $url;
                }
 
+               if (DI::pConfig()->get(local_user(), 'system', 'stay_local') && ($url == '')) {
+                       return 'contact/' . $contact['id'] . '/conversations';
+               }
+
+               if ($contact['network'] != Protocol::DFRN) {
+                       return $destination;
+               }
+
                if (!empty($contact['uid'])) {
                        return self::magicLink($contact['url'], $url);
                }
@@ -2697,21 +2714,24 @@ class Contact
        {
                $added = 0;
                $updated = 0;
+               $unchanged = 0;
                $count = 0;
 
                foreach ($urls as $url) {
-                       $contact = Contact::getByURL($url, false, ['id']); 
+                       $contact = Contact::getByURL($url, false, ['id', 'updated']);
                        if (empty($contact['id'])) {
                                Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
                                ++$added;
-                       } else {
+                       } elseif ($contact['updated'] < DateTimeFormat::utc('now -7 days')) {
                                Worker::add(PRIORITY_LOW, 'UpdateContact', $contact['id']);
                                ++$updated;
+                       } else {
+                               ++$unchanged;
                        }
                        ++$count;
                }
 
-               return ['count' => $count, 'added' => $added, 'updated' => $updated];
+               return ['count' => $count, 'added' => $added, 'updated' => $updated, 'unchanged' => $unchanged];
        }
 
        /**