]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GContact.php
Merge pull request #7372 from nupplaphil/task/simplify_config
[friendica.git] / src / Model / GContact.php
index dd1e4285c7f1f0be88b21586765d49284f0a75bd..6ffa0b2e7d0feaac0b87d2c7c36de59f41db9fa7 100644 (file)
@@ -864,23 +864,48 @@ class GContact
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function updateFromPublicContact($cid)
+       public static function updateFromPublicContactID($cid)
+       {
+               self::updateFromPublicContact(['id' => $cid]);
+       }
+
+       /**
+        * @brief Updates the gcontact entry from a given public contact url
+        *
+        * @param string $url contact url
+        * @return integer gcontact id
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function updateFromPublicContactURL($url)
+       {
+               return self::updateFromPublicContact(['nurl' => Strings::normaliseLink($url)]);
+       }
+
+       /**
+        * @brief Helper function for updateFromPublicContactID and updateFromPublicContactURL
+        *
+        * @param array $condition contact condition
+        * @return integer gcontact id
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function updateFromPublicContact($condition)
        {
                $fields = ['name', 'nick', 'url', 'nurl', 'location', 'about', 'keywords', 'gender',
-                       'bd', 'contact-type', 'network', 'addr', 'notify', 'alias', 'archive',
-                       'created', 'updated', 'avatar', 'success_update', 'failure_update', 'forum', 'prv'];
-               $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => 0, 'network' => Protocol::FEDERATED]);
+                       'bd', 'contact-type', 'network', 'addr', 'notify', 'alias', 'archive', 'term-date',
+                       'created', 'updated', 'avatar', 'success_update', 'failure_update', 'forum', 'prv',
+                       'baseurl', 'sensitive', 'unsearchable'];
+
+               $contact = DBA::selectFirst('contact', $fields, array_merge($condition, ['uid' => 0, 'network' => Protocol::FEDERATED]));
                if (!DBA::isResult($contact)) {
-                       return;
+                       return 0;
                }
 
-               // These fields cannot be updated, since they don't exist in the contact table
-               // hide, nsfw, server_url
-               // "connect" does exist, but seems to contain the same as "addr"
-
                $fields = ['name', 'nick', 'url', 'nurl', 'location', 'about', 'keywords', 'gender', 'generation',
                        'birthday', 'contact-type', 'network', 'addr', 'notify', 'alias', 'archived', 'archive_date',
-                       'created', 'updated', 'photo', 'last_contact', 'last_failure', 'community', 'connect'];
+                       'created', 'updated', 'photo', 'last_contact', 'last_failure', 'community', 'connect',
+                       'server_url', 'nsfw', 'hide', 'id'];
 
                $old_gcontact = DBA::selectFirst('gcontact', $fields, ['nurl' => $contact['nurl']]);
                $do_insert = !DBA::isResult($old_gcontact);
@@ -888,22 +913,27 @@ class GContact
                        $old_gcontact = [];
                }
 
-               $gcontact = $contact;
+               $gcontact = [];
+
+               // These fields are identical in both contact and gcontact
+               $fields = ['name', 'nick', 'url', 'nurl', 'location', 'about', 'keywords', 'gender',
+                       'contact-type', 'network', 'addr', 'notify', 'alias', 'created', 'updated'];
+
+               foreach ($fields as $field) {
+                       $gcontact[$field] = $contact[$field];
+               }
 
                // These fields are having different names but the same content
-               $gcontact['archived'] = $gcontact['archive'];
-               unset($gcontact['archive']);
-               $gcontact['birthday'] = $gcontact['bd'];
-               unset($gcontact['bd']);
-               $gcontact['photo'] = $gcontact['avatar'];
-               unset($gcontact['avatar']);
-               $gcontact['last_contact'] = $gcontact['success_update'];
-               unset($gcontact['success_update']);
-               $gcontact['last_failure'] = $gcontact['failure_update'];
-               unset($gcontact['failure_update']);
-               $gcontact['community'] = ($gcontact['forum'] || $gcontact['prv']);
-               unset($gcontact['forum']);
-               unset($gcontact['prv']);
+               $gcontact['server_url'] = $contact['baseurl'] ?? ''; // "baseurl" can be null, "server_url" not
+               $gcontact['nsfw'] = $contact['sensitive'];
+               $gcontact['hide'] = $contact['unsearchable'];
+               $gcontact['archived'] = $contact['archive'];
+               $gcontact['archive_date'] = $contact['term-date'];
+               $gcontact['birthday'] = $contact['bd'];
+               $gcontact['photo'] = $contact['avatar'];
+               $gcontact['last_contact'] = $contact['success_update'];
+               $gcontact['last_failure'] = $contact['failure_update'];
+               $gcontact['community'] = ($contact['forum'] || $contact['prv']);
 
                foreach (['last_contact', 'last_failure', 'updated'] as $field) {
                        if (!empty($old_gcontact[$field]) && ($old_gcontact[$field] >= $gcontact[$field])) {
@@ -911,9 +941,7 @@ class GContact
                        }
                }
 
-               if ($gcontact['archived'] && (empty($old_gcontact['archive_date']) || ($old_gcontact['archive_date'] <= DBA::NULL_DATETIME))) {
-                       $gcontact['archive_date'] = DateTimeFormat::utcNow();
-               } elseif (!$gcontact['archived']) {
+               if (!$gcontact['archived']) {
                        $gcontact['archive_date'] = DBA::NULL_DATETIME;
                }
 
@@ -932,8 +960,10 @@ class GContact
 
                if (!$do_insert) {
                        DBA::update('gcontact', $gcontact, ['nurl' => $contact['nurl']], $old_gcontact);
+                       return $old_gcontact['id'];
                } elseif (!$gcontact['archived']) {
                        DBA::insert('gcontact', $gcontact);
+                       return DBA::lastInsertId();
                }
        }