]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #8926 from annando/avatar-cache
authorHypolite Petovan <hypolite@mrpetovan.com>
Sat, 25 Jul 2020 12:33:31 +0000 (08:33 -0400)
committerGitHub <noreply@github.com>
Sat, 25 Jul 2020 12:33:31 +0000 (08:33 -0400)
Store avatar cache fields only when needed

mod/dfrn_confirm.php
mod/dfrn_request.php
src/Model/Contact.php
src/Model/Item.php
src/Module/Contact/Advanced.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php
src/Protocol/OStatus.php

index 668c6d2e274bee42ca7b96247b2f5fd9e39d9268..bd52a67cff70fb233d20a54a833a547dcd6182b9 100644 (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');
 
index e93df399f233814f3cc9cb9fa180afeaf1c95680..1e485e3040aea218635b09d93f8c027c014d7b72 100644 (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);
                                        }
                                }
                        }
index 09d527733192565a7584d60b1a4f328b996cd662..708c375b070f473a57c3eef72f2297e8a4697055 100644 (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]);
 
index b5a954099117e471c90ac553f1a049c6a1f4087e..1a561c7f13982958970b5b0e5b70c76bb35fc9cc 100644 (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);
 
index 5fa86ab3789959dc63e0c36c2d1cca77adc48e92..be1e874a57939c98af964e20e703970a01914017 100644 (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) {
index 8190806a044b109d9c613971ff32d0b8e020a075..f213af8db3068299739b33eaf56da5f5253b3b2b 100644 (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.');
 
index 98a315ce21741c508fdd40e43af63b508c7e579c..f3b95db68f2fb1c5756cb494e73b96b51b121de5 100644 (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.
 
index 9c87f367adb7a559a656d3a81d1a447fccc7e804..1cf2894eb548742058e39435a0085579b0027ace 100644 (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"]);
                                }
                        }