]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GContact.php
Merge pull request #8007 from MrPetovan/task/7984-add-profile-url-search
[friendica.git] / src / Model / GContact.php
index 0e9acf5670604bfd3c35809099d28cd79c49fc26..61e44b4106dff1f1cc5147e817c34c8e4ed8925e 100644 (file)
@@ -126,7 +126,7 @@ class GContact
                        throw new Exception('URL is empty');
                }
 
-               $gcontact['server_url'] = defaults($gcontact, 'server_url', '');
+               $gcontact['server_url'] = $gcontact['server_url'] ?? '';
 
                $urlparts = parse_url($gcontact['url']);
                if (empty($urlparts['scheme'])) {
@@ -580,9 +580,6 @@ class GContact
        public static function getId($contact)
        {
                $gcontact_id = 0;
-               $doprobing = false;
-               $last_failure_str = '';
-               $last_contact_str = '';
 
                if (empty($contact['network'])) {
                        Logger::notice('Empty network', ['url' => $contact['url'], 'callstack' => System::callstack()]);
@@ -613,40 +610,26 @@ class GContact
                $gcnt = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($contact['url'])]);
                if (DBA::isResult($gcnt)) {
                        $gcontact_id = $gcnt['id'];
-
-                       // Update every 90 days
-                       if (empty($gcnt['network']) || in_array($gcnt['network'], Protocol::FEDERATED)) {
-                               $last_failure_str = $gcnt['last_failure'];
-                               $last_failure = strtotime($gcnt['last_failure']);
-                               $last_contact_str = $gcnt['last_contact'];
-                               $last_contact = strtotime($gcnt['last_contact']);
-                               $doprobing = (((time() - $last_contact) > (90 * 86400)) && ((time() - $last_failure) > (90 * 86400)));
-                       }
                } else {
                        $contact['location'] = $contact['location'] ?? '';
                        $contact['about'] = $contact['about'] ?? '';
                        $contact['generation'] = $contact['generation'] ?? 0;
 
-                       $fields = ['name' => $contact['name'], 'nick' => $contact['nick'], 'addr' => $contact['addr'], 'network' => $contact['network'],
+                       $fields = ['name' => $contact['name'], 'nick' => $contact['nick'] ?? '', 'addr' => $contact['addr'] ?? '', 'network' => $contact['network'],
                                'url' => $contact['url'], 'nurl' => Strings::normaliseLink($contact['url']), 'photo' => $contact['photo'],
                                'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(), 'location' => $contact['location'],
                                'about' => $contact['about'], 'hide' => $contact['hide'], 'generation' => $contact['generation']];
+
                        DBA::insert('gcontact', $fields);
 
                        $condition = ['nurl' => Strings::normaliseLink($contact['url'])];
                        $cnt = DBA::selectFirst('gcontact', ['id', 'network'], $condition, ['order' => ['id']]);
                        if (DBA::isResult($cnt)) {
                                $gcontact_id = $cnt['id'];
-                               $doprobing = (empty($cnt['network']) || in_array($cnt['network'], Protocol::FEDERATED));
                        }
                }
                DBA::unlock();
 
-               if ($doprobing) {
-                       Logger::notice('Probing', ['contact' => $last_contact_str, "failure" => $last_failure_str, "checking" => $contact['url']]);
-                       Worker::add(PRIORITY_LOW, 'GProbe', $contact['url']);
-               }
-
                return $gcontact_id;
        }
 
@@ -800,7 +783,7 @@ class GContact
                        return;
                }
 
-               if (!$force && !PortableContact::updateNeeded($gcontact['created'], $gcontact['updated'], $gcontact['last_failure'], $gcontact['last_contact'])) {
+               if (!$force && !GServer::updateNeeded($gcontact['created'], $gcontact['updated'], $gcontact['last_failure'], $gcontact['last_contact'])) {
                        Logger::info("Don't update profile", ['url' => $data['url'], 'updated' => $gcontact['updated']]);
                        return;
                }
@@ -809,14 +792,11 @@ class GContact
                        return;
                }
 
-               // When the profile doesn't have got a feed, then we exit here
-               if (empty($data['poll'])) {
-                       return;
-               }
-
-               if ($data['network'] == Protocol::ACTIVITYPUB) {
+               if (!empty($data['outbox'])) {
+                       self::updateFromOutbox($data['outbox'], $data);
+               } elseif (!empty($data['poll']) && ($data['network'] == Protocol::ACTIVITYPUB)) {
                        self::updateFromOutbox($data['poll'], $data);
-               } else {
+               } elseif (!empty($data['poll'])) {
                        self::updateFromFeed($data);
                }
        }
@@ -841,7 +821,7 @@ class GContact
 
                if ($curlResult->isSuccess() && !empty($curlResult->getBody())) {
                        $noscrape = json_decode($curlResult->getBody(), true);
-                       if (!empty($noscrape)) {
+                       if (!empty($noscrape) && !empty($noscrape['updated'])) {
                                $noscrape['updated'] = DateTimeFormat::utc($noscrape['updated'], DateTimeFormat::MYSQL);
                                $fields = ['last_contact' => DateTimeFormat::utcNow(), 'updated' => $noscrape['updated']];
                                DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($data['url'])]);
@@ -859,7 +839,9 @@ class GContact
        /**
         * Update a global contact via an ActivityPub Outbox
         *
-        * @param string $data Probing result
+        * @param string $feed
+        * @param array  $data Probing result
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function updateFromOutbox(string $feed, array $data)
        {
@@ -872,18 +854,32 @@ class GContact
                        $items = $outbox['orderedItems'];
                } elseif (!empty($outbox['first']['orderedItems'])) {
                        $items = $outbox['first']['orderedItems'];
+               } elseif (!empty($outbox['first']['href'])) {
+                       self::updateFromOutbox($outbox['first']['href'], $data);
+                       return;
                } elseif (!empty($outbox['first'])) {
-                       self::updateFromOutbox($outbox['first'], $data);
+                       if (is_string($outbox['first'])) {
+                               self::updateFromOutbox($outbox['first'], $data);
+                       } else {
+                               Logger::warning('Unexpected data', ['outbox' => $outbox]);
+                       }
                        return;
                } else {
                        $items = [];
                }
 
                $last_updated = '';
-
                foreach ($items as $activity) {
-                       if ($last_updated < $activity['published']) {
-                               $last_updated = $activity['published'];
+                       if (!empty($activity['published'])) {
+                               $published =  DateTimeFormat::utc($activity['published']);
+                       } elseif (!empty($activity['object']['published'])) {
+                               $published =  DateTimeFormat::utc($activity['object']['published']);
+                       } else {
+                               continue;
+                       }
+
+                       if ($last_updated < $published) {
+                               $last_updated = $published;
                        }
                }
 
@@ -1125,8 +1121,8 @@ class GContact
                                'birthday' => $userdata['dob'], 'photo' => $userdata['photo'],
                                "notify" => $userdata['notify'], 'url' => $userdata['url'],
                                "hide" => ($userdata['hidewall'] || !$userdata['net-publish']),
-                               'nick' => $userdata['nickname'], 'addr' => $addr,
-                               "connect" => $addr, "server_url" => System::baseUrl(),
+                               'nick' => $userdata['nickname'], 'addr' => $userdata['addr'],
+                               "connect" => $userdata['addr'], "server_url" => System::baseUrl(),
                                "generation" => 1, 'network' => Protocol::DFRN];
 
                self::update($gcontact);