]> git.mxchange.org Git - friendica.git/commitdiff
Add public contact if missing
authorMichael <heluecht@pirati.ca>
Tue, 11 Mar 2025 04:43:44 +0000 (04:43 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 11 Mar 2025 04:51:20 +0000 (04:51 +0000)
src/Model/Contact.php
src/Model/User.php
src/Protocol/ATProtocol/Processor.php

index 2f98a0d3e458293da89f21cee0fa95bd4ca94bf8..cced0ed6263f303b063447b046e0e94bc67024c5 100644 (file)
@@ -154,6 +154,46 @@ class Contact
                return DBA::selectFirst('account-user-view', $fields, $condition, $params);
        }
 
+       /**
+        * Fetch data from the "account-user-view" for a given contact id. Creates missing data if needed.
+        * @param int   $id     Contact id
+        * @param array $fields selected fields
+        * @return array|bool
+        */
+       public static function selectAccountUserById(int $id, array $fields = [])
+       {
+               $data = self::selectFirstAccountUser($fields, ['id' => $id]);
+               if (!empty($data) || !self::createPublicContactFromUserContact($id)) {
+                       return $data;
+               }
+
+               return self::selectFirstAccountUser($fields, ['id' => $id]);
+       }
+
+       /**
+        * Add missing public contact for a given user contact.
+        * @param int $cid ID of the user contact
+        * @return bool true if the public user had been created
+        */
+       public static function createPublicContactFromUserContact(int $cid): bool
+       {
+               $fields = [
+                       'created', 'updated', 'network', 'name', 'nick', 'location', 'about', 'keywords', 'xmpp',
+                       'matrix', 'avatar', 'blurhash', 'header', 'url', 'nurl', 'uri-id', 'addr', 'alias', 'pubkey',
+                       'batch', 'notify', 'poll', 'subscribe', 'last-update', 'next-update', 'success_update',
+                       'failure_update', 'failed', 'term-date', 'last-item', 'last-discovery', 'local-data',
+                       'readonly', 'contact-type', 'manually-approve', 'archive', 'unsearchable', 'sensitive',
+                       'baseurl', 'gsid', 'bd', 'photo', 'thumb', 'micro', 'name-date', 'uri-date', 'avatar-date',
+                       'request', 'confirm', 'poco', 'writable', 'forum', 'prv', 'bdyear'
+               ];
+               $contact = self::selectFirst($fields, ['id' => $cid]);
+               if (empty($contact)) {
+                       return false;
+               }
+               $contact['uid'] = 0;
+               return (bool)self::insert($contact);
+       }
+
        /**
         * Insert a row into the contact table
         * Important: You can't use DBA::lastInsertId() after this call since it will be set to 0.
index 8ee52ee9221c1586a089247780a0e1100e1efdbb..927a1a82bddaf0bc128c66fb33275dc8f640dfb7 100644 (file)
@@ -405,7 +405,7 @@ class User
         */
        public static function getIdForContactId(int $cid): int
        {
-               $account = Contact::selectFirstAccountUser(['pid', 'self', 'uid'], ['id' => $cid]);
+               $account = Contact::selectAccountUserById($cid, ['pid', 'self', 'uid']);
                if (empty($account['pid'])) {
                        return 0;
                }
index f551f6a273c1fb709a06b499d9dbb4954b44bddb..502b31ea4a78f28c73f1c86c5c5823ced31d522b 100755 (executable)
@@ -363,7 +363,7 @@ class Processor
                        return [];
                }
 
-               $account          = Contact::selectFirstAccountUser(['pid'], ['id' => $contact['id']]);
+               $account          = Contact::selectAccountUserById($contact['id'], ['pid']);
                $item['owner-id'] = $item['author-id'] = $account['pid'];
                $item['uri-id']   = ItemURI::getIdByURI($item['uri']);
 
@@ -424,7 +424,7 @@ class Processor
                        return [];
                }
 
-               $account = Contact::selectFirstAccountUser(['pid'], ['id' => $contact['id']]);
+               $account = Contact::selectAccountUserById($contact['id'], ['pid']);
 
                $item['owner-id'] = $item['author-id'] = $account['pid'];
                $item['uri-id']   = ItemURI::getIdByURI($uri);