]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GContact.php
Ensure APContact::getByUrl always returns an array
[friendica.git] / src / Model / GContact.php
index 0e9acf5670604bfd3c35809099d28cd79c49fc26..95f1080f8d56570cdd87a2e522121c3024f98c79 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'])) {
@@ -627,10 +627,11 @@ class GContact
                        $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'])];
@@ -809,14 +810,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 +839,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 +857,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,6 +872,9 @@ 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);
                        return;
@@ -880,10 +883,17 @@ class GContact
                }
 
                $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 +1135,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);