Merge pull request #8926 from annando/avatar-cache

Store avatar cache fields only when needed
This commit is contained in:
Hypolite Petovan 2020-07-25 08:33:31 -04:00 committed by GitHub
commit eaf159fc20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 19 deletions

View file

@ -304,7 +304,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
*
* We will also update the contact record with the nature and scope of the relationship.
*/
Contact::updateAvatar($contact['photo'], $uid, $contact_id);
Contact::updateAvatar($contact_id, $contact['photo']);
Logger::log('dfrn_confirm: confirm - imported photos');
@ -484,7 +484,7 @@ function dfrn_confirm_post(App $a, $handsfree = null)
$photo = DI::baseUrl() . '/images/person-300.jpg';
}
Contact::updateAvatar($photo, $local_uid, $dfrn_record);
Contact::updateAvatar($dfrn_record, $photo);
Logger::log('dfrn_confirm: request - photos imported');

View file

@ -189,7 +189,7 @@ function dfrn_request_post(App $a)
Group::addMember(User::getDefaultGroup(local_user(), $r[0]["network"]), $r[0]['id']);
if (isset($photo)) {
Contact::updateAvatar($photo, local_user(), $r[0]["id"], true);
Contact::updateAvatar($r[0]["id"], $photo, true);
}
$forward_path = "contact/" . $r[0]['id'];
@ -420,7 +420,7 @@ function dfrn_request_post(App $a)
);
if (DBA::isResult($r)) {
$contact_record = $r[0];
Contact::updateAvatar($photo, $uid, $contact_record["id"], true);
Contact::updateAvatar($contact_record["id"], $photo, true);
}
}
}

View file

@ -1464,7 +1464,7 @@ class Contact
}
if (!empty($data['photo']) && ($data['network'] != Protocol::FEED)) {
self::updateAvatar($data['photo'], $uid, $contact_id);
self::updateAvatar($contact_id, $data['photo']);
}
if (in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
@ -1775,12 +1775,32 @@ class Contact
return $return;
}
/**
* Ensure that cached avatar exist
*
* @param integer $cid
*/
public static function checkAvatarCache(int $cid)
{
$contact = DBA::selectFirst('contact', ['url', 'avatar', 'photo', 'thumb', 'micro'], ['id' => $cid, 'uid' => 0, 'self' => false]);
if (!DBA::isResult($contact)) {
return;
}
if (empty($contact['avatar']) || (!empty($contact['photo']) && !empty($contact['thumb']) && !empty($contact['micro']))) {
return;
}
Logger::info('Adding avatar cache', ['id' => $cid, 'contact' => $contact]);
self::updateAvatar($cid, $contact['avatar'], true);
}
/**
* Updates the avatar links in a contact only if needed
*
* @param string $avatar Link to avatar picture
* @param int $uid User id of contact owner
* @param int $cid Contact id
* @param string $avatar Link to avatar picture
* @param bool $force force picture update
*
* @return void
@ -1788,13 +1808,22 @@ class Contact
* @throws HTTPException\NotFoundException
* @throws \ImagickException
*/
public static function updateAvatar($avatar, $uid, $cid, $force = false)
public static function updateAvatar(int $cid, string $avatar, bool $force = false)
{
$contact = DBA::selectFirst('contact', ['avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid, 'self' => false]);
$contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid, 'self' => false]);
if (!DBA::isResult($contact)) {
return;
}
$uid = $contact['uid'];
// Only update the cached photo links of public contacts when they already are cached
if (($uid == 0) && !$force && empty($contact['photo']) && empty($contact['thumb']) && empty($contact['micro'])) {
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
return;
}
$data = [
$contact['photo'] ?? '',
$contact['thumb'] ?? '',
@ -2021,7 +2050,7 @@ class Contact
}
if (!empty($ret['photo']) && ($ret['network'] != Protocol::FEED)) {
self::updateAvatar($ret['photo'], $uid, $id, $update || $force);
self::updateAvatar($id, $ret['photo'], $update || $force);
}
if (!$update) {
@ -2311,7 +2340,7 @@ class Contact
Group::addMember(User::getDefaultGroup($user['uid'], $contact["network"]), $contact_id);
// Update the avatar
self::updateAvatar($ret['photo'], $user['uid'], $contact_id);
self::updateAvatar($contact_id, $ret['photo']);
// pull feed and consume it, which should subscribe to the hub.
@ -2493,7 +2522,7 @@ class Contact
// Ensure to always have the correct network type, independent from the connection request method
self::updateFromProbe($contact_id, '', true);
Contact::updateAvatar($photo, $importer["uid"], $contact_id, true);
self::updateAvatar($contact_id, $photo, true);
$contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]);

View file

@ -1703,6 +1703,10 @@ class Item
'photo' => $item['owner-avatar'], 'network' => $item['network']];
$item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
// Ensure that there is an avatar cache
Contact::checkAvatarCache($item['author-id']);
Contact::checkAvatarCache($item['owner-id']);
// The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
$item["contact-id"] = self::contactId($item);

View file

@ -87,7 +87,7 @@ class Advanced extends BaseModule
if ($photo) {
DI::logger()->notice('Updating photo.', ['photo' => $photo]);
Model\Contact::updateAvatar($photo, local_user(), $contact['id'], true);
Model\Contact::updateAvatar($contact['id'], $photo, true);
}
if (!$r) {

View file

@ -1680,11 +1680,11 @@ class DFRN
$condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($contact_old['url'])];
DBA::update('contact', $fields, $condition, true);
Contact::updateAvatar($author['avatar'], $importer['importer_uid'], $contact['id']);
Contact::updateAvatar($contact['id'], $author['avatar']);
$pcid = Contact::getIdForURL($contact_old['url']);
if (!empty($pcid)) {
Contact::updateAvatar($author['avatar'], 0, $pcid);
Contact::updateAvatar($pcid, $author['avatar']);
}
/*
@ -1962,7 +1962,7 @@ class DFRN
DBA::update('contact', $fields, $condition);
Contact::updateAvatar($relocate["avatar"], $importer["importer_uid"], $importer["id"], true);
Contact::updateAvatar($importer["id"], $relocate["avatar"], true);
Logger::log('Contacts are updated.');

View file

@ -2410,7 +2410,7 @@ class Diaspora
$image_url = "http://".$handle_parts[1].$image_url;
}
Contact::updateAvatar($image_url, $importer["uid"], $contact["id"]);
Contact::updateAvatar($contact["id"], $image_url);
// Generic birthday. We don't know the timezone. The year is irrelevant.

View file

@ -216,7 +216,7 @@ class OStatus
if (!empty($author["author-avatar"]) && ($author["author-avatar"] != $current['avatar'])) {
Logger::log("Update profile picture for contact ".$contact["id"], Logger::DEBUG);
Contact::updateAvatar($author["author-avatar"], $importer["uid"], $contact["id"]);
Contact::updateAvatar($contact["id"], $author["author-avatar"]);
}
// Ensure that we are having this contact (with uid=0)
@ -237,7 +237,7 @@ class OStatus
// Update the avatar
if (!empty($author["author-avatar"])) {
Contact::updateAvatar($author["author-avatar"], 0, $cid);
Contact::updateAvatar($cid, $author["author-avatar"]);
}
}