]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact.php
Merge pull request #12572 from mexon/mat/network-failure-error
[friendica.git] / src / Model / Contact.php
index b703f82a005dd764fdf80d2a8f417f53e5cdfdaf..bf90edcd4c3e20fd78a60677e653d158b228e7ec 100644 (file)
@@ -329,7 +329,7 @@ class Contact
                // Add internal fields
                $removal = [];
                if (!empty($fields)) {
-                       foreach (['id', 'next-update', 'network'] as $internal) {
+                       foreach (['id', 'next-update', 'network', 'local-data'] as $internal) {
                                if (!in_array($internal, $fields)) {
                                        $fields[] = $internal;
                                        $removal[] = $internal;
@@ -358,8 +358,10 @@ class Contact
                        return [];
                }
 
+               $background_update = DI::config()->get('system', 'update_active_contacts') ? $contact['local-data'] : true;
+
                // Update the contact in the background if needed
-               if (Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
+               if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
                        Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
                }
 
@@ -1266,12 +1268,14 @@ class Contact
                        return 0;
                }
 
-               $contact = self::getByURL($url, false, ['id', 'network', 'uri-id', 'next-update'], $uid);
+               $contact = self::getByURL($url, false, ['id', 'network', 'uri-id', 'next-update', 'local-data'], $uid);
 
                if (!empty($contact)) {
                        $contact_id = $contact['id'];
 
-                       if (Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
+                       $background_update = DI::config()->get('system', 'update_active_contacts') ? $contact['local-data'] : true;
+
+                       if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
                                Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']);
                        }
 
@@ -1386,14 +1390,18 @@ class Contact
                if ($data['network'] == Protocol::DIASPORA) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['url' => $url, 'data' => $data]);
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data]);
                        }
                } elseif (!empty($data['networks'][Protocol::DIASPORA])) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data['networks'][Protocol::DIASPORA]);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
+                               Logger::notice($e->getMessage(), ['url' => $url, 'data' => $data['networks'][Protocol::DIASPORA]]);
                        }
                }
 
@@ -1540,8 +1548,8 @@ class Contact
                $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
 
                if ($thread_mode) {
-                       $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), DI::userSession()->getLocalUserId()];
+                       $condition = ["((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
+                               $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA, DI::userSession()->getLocalUserId()];
                } else {
                        $condition = ["`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
                                $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, DI::userSession()->getLocalUserId()];
@@ -2477,6 +2485,44 @@ class Contact
                return true;
        }
 
+       /**
+        * Perform a contact update if the contact is outdated
+        *
+        * @param integer $id contact id
+        * @return bool
+        */
+       public static function updateByIdIfNeeded(int $id): bool
+       {
+               $contact = self::selectFirst(['url'], ["`id` = ? AND `next-update` < ?", $id, DateTimeFormat::utcNow()]);
+               if (empty($contact['url'])) {
+                       return false;
+               }
+
+               if (self::isLocal($contact['url'])) {
+                       return true;
+               }
+
+               $stamp = (float)microtime(true);
+               self::updateFromProbe($id);
+               Logger::debug('Contact data is updated.', ['duration' => round((float)microtime(true) - $stamp, 3), 'id' => $id, 'url' => $contact['url'], 'callstack' => System::callstack(20)]);
+               return true;
+       }
+
+       /**
+        * Perform a contact update if the contact is outdated
+        *
+        * @param string $url contact url
+        * @return bool
+        */
+       public static function updateByUrlIfNeeded(string $url): bool
+       {
+               $id = self::getIdForURL($url, 0, false);
+               if (!empty($id)) {
+                       return self::updateByIdIfNeeded($id);
+               }
+               return (bool)self::getIdForURL($url);
+       }
+
        /**
         * Updates contact record by provided id and optional network
         *
@@ -2498,14 +2544,18 @@ class Contact
                if ($data['network'] == Protocol::DIASPORA) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        }
                } elseif (!empty($data['networks'][Protocol::DIASPORA])) {
                        try {
                                DI::dsprContact()->updateFromProbeArray($data['networks'][Protocol::DIASPORA]);
+                       } catch (HTTPException\NotFoundException $e) {
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        } catch (\InvalidArgumentException $e) {
-                               Logger::error($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
+                               Logger::notice($e->getMessage(), ['id' => $id, 'network' => $network, 'contact' => $contact, 'data' => $data]);
                        }
                }
 
@@ -2903,7 +2953,7 @@ class Contact
                }
 
                if (($network != '') && ($ret['network'] != $network)) {
-                       Logger::notice('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
+                       $result['message'] = DI::l10n()->t('Expected network %s does not match actual network %s', $network, $ret['network']);
                        return $result;
                }