From 9b86f40a5fbbde547ba71bff310cf13f338175f8 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Jul 2020 11:48:52 +0000 Subject: [PATCH] Store avatar cache fields only when needed --- mod/dfrn_confirm.php | 4 +-- mod/dfrn_request.php | 4 +-- src/Model/Contact.php | 45 +++++++++++++++++++++++++++------ src/Model/Item.php | 4 +++ src/Module/Contact/Advanced.php | 2 +- src/Protocol/DFRN.php | 6 ++--- src/Protocol/Diaspora.php | 2 +- src/Protocol/OStatus.php | 4 +-- 8 files changed, 52 insertions(+), 19 deletions(-) diff --git a/mod/dfrn_confirm.php b/mod/dfrn_confirm.php index 668c6d2e27..bd52a67cff 100644 --- a/mod/dfrn_confirm.php +++ b/mod/dfrn_confirm.php @@ -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'); diff --git a/mod/dfrn_request.php b/mod/dfrn_request.php index e93df399f2..1e485e3040 100644 --- a/mod/dfrn_request.php +++ b/mod/dfrn_request.php @@ -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); } } } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 09d5277331..708c375b07 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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]); diff --git a/src/Model/Item.php b/src/Model/Item.php index b5a9540991..1a561c7f13 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -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); diff --git a/src/Module/Contact/Advanced.php b/src/Module/Contact/Advanced.php index 5fa86ab378..be1e874a57 100644 --- a/src/Module/Contact/Advanced.php +++ b/src/Module/Contact/Advanced.php @@ -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) { diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 8190806a04..f213af8db3 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -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.'); diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 98a315ce21..f3b95db68f 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -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. diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index 9c87f367ad..1cf2894eb5 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -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"]); } }