Store the cached avatars at the public contact

This commit is contained in:
Michael 2020-08-21 18:41:48 +00:00
parent 7990d08ad6
commit e9d6fb9aaa

View File

@ -1552,15 +1552,16 @@ class Contact
* @param int $cid Contact id * @param int $cid Contact id
* @param string $avatar Link to avatar picture * @param string $avatar Link to avatar picture
* @param bool $force force picture update * @param bool $force force picture update
* @param bool $create_cache Enforces the creation of cached avatar fields
* *
* @return void * @return void
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException * @throws HTTPException\NotFoundException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function updateAvatar(int $cid, string $avatar, bool $force = false) public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
{ {
$contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url'], ['id' => $cid, 'self' => false]); $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url', 'network'], ['id' => $cid, 'self' => false]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
return; return;
} }
@ -1568,7 +1569,7 @@ class Contact
$uid = $contact['uid']; $uid = $contact['uid'];
// Only update the cached photo links of public contacts when they already are cached // Only update the cached photo links of public contacts when they already are cached
if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro'])) { if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
if ($contact['avatar'] != $avatar) { if ($contact['avatar'] != $avatar) {
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]); DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]); Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
@ -1576,9 +1577,14 @@ class Contact
return; return;
} }
$local_uid = User::getIdForURL($contact['url']); // User contacts use are updated through the public contacts
if (!empty($local_uid)) { if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
$fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]); $pcid = self::getIdForURL($contact['url'], false);
if (!empty($pcid)) {
Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
self::updateAvatar($pcid, $avatar, $force, true);
return;
}
} }
// Replace cached avatar pictures from the default avatar with the default avatars in different sizes // Replace cached avatar pictures from the default avatar with the default avatars in different sizes
@ -1587,29 +1593,32 @@ class Contact
'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO, 'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO,
'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB, 'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB,
'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO]; 'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO];
Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
} }
if (!empty($fields)) { // Use the data from the self account
if ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) { if (empty($fields)) {
DBA::update('contact', $fields, ['id' => $cid]); $local_uid = User::getIdForURL($contact['url']);
Photo::delete(['uid' => $uid, 'contact-id' => $cid, 'album' => Photo::CONTACT_PHOTOS]); if (!empty($local_uid)) {
$fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
} }
return;
} }
if (empty($fields)) {
$update = ($contact['avatar'] != $avatar) || $force;
if (!$update) {
$data = [ $data = [
$contact['photo'] ?? '', $contact['photo'] ?? '',
$contact['thumb'] ?? '', $contact['thumb'] ?? '',
$contact['micro'] ?? '', $contact['micro'] ?? '',
]; ];
$update = ($contact['avatar'] != $avatar) || $force;
if (!$update) {
foreach ($data as $image_uri) { foreach ($data as $image_uri) {
$image_rid = Photo::ridFromURI($image_uri); $image_rid = Photo::ridFromURI($image_uri);
if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) { if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]); Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
$update = true; $update = true;
} }
} }
@ -1619,13 +1628,42 @@ class Contact
$photos = Photo::importProfilePhoto($avatar, $uid, $cid, true); $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
if ($photos) { if ($photos) {
$fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()]; $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
DBA::update('contact', $fields, ['id' => $cid]); $update = !empty($fields);
} elseif (empty($contact['avatar'])) { Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
// Ensure that the avatar field is set } else {
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]); $update = false;
Logger::info('Failed profile import', ['id' => $cid, 'force' => $force, 'avatar' => $avatar, 'contact' => $contact]);
} }
} }
} else {
$update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
}
if (!$update) {
return;
}
$cids = [];
$uids = [];
if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
// Collect all user contacts of the given public contact
$personal_contacts = DBA::select('contact', ['id', 'uid'],
["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
while ($personal_contact = DBA::fetch($personal_contacts)) {
$cids[] = $personal_contact['id'];
$uids[] = $personal_contact['uid'];
}
DBA::close($personal_contacts);
if (!empty($cids)) {
// Delete possibly existing cached user contact avatars
Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'album' => Photo::CONTACT_PHOTOS]);
}
}
$cids[] = $cid;
$uids[] = $uid;
Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
DBA::update('contact', $fields, ['id' => $cids]);
} }
/** /**