Add public contact if missing

This commit is contained in:
Michael 2025-03-11 04:43:44 +00:00
parent ecfb82693d
commit 9e4a69150c
3 changed files with 43 additions and 3 deletions

View 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.

View 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;
}

View file

@ -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);