]> git.mxchange.org Git - friendica.git/commitdiff
New option to activate/deactivate contact avatar cache
authorMichael <heluecht@pirati.ca>
Sat, 23 Oct 2021 17:18:30 +0000 (17:18 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 23 Oct 2021 17:18:30 +0000 (17:18 +0000)
src/Model/Contact.php
src/Module/Admin/Site.php
static/settings.config.php
view/templates/admin/site.tpl
view/theme/frio/templates/admin/site.tpl

index c6a91edc8ba48119eaff9d1cf40877b551710f13..1221261c87ebe5ed8fd21fad3a190b061083987d 100644 (file)
@@ -1553,18 +1553,22 @@ class Contact
         */
        public static function checkAvatarCache(int $cid)
        {
-               $contact = DBA::selectFirst('contact', ['url', 'avatar', 'photo', 'thumb', 'micro'], ['id' => $cid, 'uid' => 0, 'self' => false]);
+               $contact = DBA::selectFirst('contact', ['url', 'network', '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']))) {
+               if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || DI::config()->get('system', 'cache_contact_avatar')) {
+                       if (!empty($contact['avatar']) && (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']))) {
+                               Logger::info('Adding avatar cache', ['id' => $cid, 'contact' => $contact]);
+                               self::updateAvatar($cid, $contact['avatar'], true);
+                               return;
+                       }
+               } elseif (!empty($contact['photo']) || !empty($contact['thumb']) || !empty($contact['micro'])) {
+                       Logger::info('Removing avatar cache', ['id' => $cid, 'contact' => $contact]);
+                       self::updateAvatar($cid, $contact['avatar'], true);
                        return;
                }
-
-               Logger::info('Adding avatar cache', ['id' => $cid, 'contact' => $contact]);
-
-               self::updateAvatar($cid, $contact['avatar'], true);
        }
 
        /**
@@ -1868,54 +1872,61 @@ class Contact
                        $avatar = self::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
                }
 
-               if ($default_avatar && Proxy::isLocalImage($avatar)) {
-                       $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
-                               'photo' => $avatar,
-                               'thumb' => self::getDefaultAvatar($contact, Proxy::SIZE_THUMB),
-                               'micro' => self::getDefaultAvatar($contact, Proxy::SIZE_MICRO)];
-                       Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
-               }
+               if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || DI::config()->get('system', 'cache_contact_avatar')) {
+                       if ($default_avatar && Proxy::isLocalImage($avatar)) {
+                               $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
+                                       'photo' => $avatar,
+                                       'thumb' => self::getDefaultAvatar($contact, Proxy::SIZE_THUMB),
+                                       'micro' => self::getDefaultAvatar($contact, Proxy::SIZE_MICRO)];
+                               Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
+                       }
 
-               // Use the data from the self account
-               if (empty($fields)) {
-                       $local_uid = User::getIdForURL($contact['url']);
-                       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]);
+                       // Use the data from the self account
+                       if (empty($fields)) {
+                               $local_uid = User::getIdForURL($contact['url']);
+                               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]);
+                               }
                        }
-               }
 
-               if (empty($fields)) {
-                       $update = ($contact['avatar'] != $avatar) || $force;
-
-                       if (!$update) {
-                               $data = [
-                                       $contact['photo'] ?? '',
-                                       $contact['thumb'] ?? '',
-                                       $contact['micro'] ?? '',
-                               ];
-
-                               foreach ($data as $image_uri) {
-                                       $image_rid = Photo::ridFromURI($image_uri);
-                                       if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
-                                               Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
-                                               $update = true;
+                       if (empty($fields)) {
+                               $update = ($contact['avatar'] != $avatar) || $force;
+
+                               if (!$update) {
+                                       $data = [
+                                               $contact['photo'] ?? '',
+                                               $contact['thumb'] ?? '',
+                                               $contact['micro'] ?? '',
+                                       ];
+
+                                       foreach ($data as $image_uri) {
+                                               $image_rid = Photo::ridFromURI($image_uri);
+                                               if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
+                                                       Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
+                                                       $update = true;
+                                               }
                                        }
                                }
-                       }
 
-                       if ($update) {
-                               $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
-                               if ($photos) {
-                                       $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
-                                       $update = !empty($fields);
-                                       Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
-                               } else {
-                                       $update = false;
+                               if ($update) {
+                                       $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
+                                       if ($photos) {
+                                               $fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
+                                               $update = !empty($fields);
+                                               Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
+                                       } else {
+                                               $update = false;
+                                       }
                                }
+                       } else {
+                               $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
                        }
                } else {
-                       $update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
+                       Photo::delete(['uid' => $uid, 'contact-id' => $cid, 'photo-type' => Photo::CONTACT_AVATAR]);
+                       $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
+                               'photo' => '', 'thumb' => '', 'micro' => ''];
+                       $update = ($avatar != $contact['avatar'] . $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
                }
 
                if (!$update) {
index 5819627453773524e1c0a2821f8f55d5b1c2977b..be72184cb750d05e20fb2b0f570da4064a468762 100644 (file)
@@ -163,6 +163,7 @@ class Site extends BaseAdmin
                $allow_users_remote_self = !empty($_POST['allow_users_remote_self']);
                $explicit_content       = !empty($_POST['explicit_content']);
                $proxify_content        = !empty($_POST['proxify_content']);
+               $cache_contact_avatar   = !empty($_POST['cache_contact_avatar']);
 
                $enable_multi_reg       = !empty($_POST['enable_multi_reg']);
                $enable_openid          = !empty($_POST['enable_openid']);
@@ -330,6 +331,7 @@ class Site extends BaseAdmin
                DI::config()->set('system', 'allow_users_remote_self', $allow_users_remote_self);
                DI::config()->set('system', 'explicit_content'       , $explicit_content);
                DI::config()->set('system', 'proxify_content'        , $proxify_content);
+               DI::config()->set('system', 'cache_contact_avatar'        , $cache_contact_avatar);
                DI::config()->set('system', 'check_new_version_url'  , $check_new_version_url);
 
                DI::config()->set('system', 'block_extended_register', !$enable_multi_reg);
@@ -554,6 +556,7 @@ class Site extends BaseAdmin
                        '$disable_embedded'       => ['disable_embedded', DI::l10n()->t('Don\'t embed private images in posts'), DI::config()->get('system', 'disable_embedded'), DI::l10n()->t('Don\'t replace locally-hosted private photos in posts with an embedded copy of the image. This means that contacts who receive posts containing private photos will have to authenticate and load each image, which may take a while.')],
                        '$explicit_content'       => ['explicit_content', DI::l10n()->t('Explicit Content'), DI::config()->get('system', 'explicit_content'), DI::l10n()->t('Set this to announce that your node is used mostly for explicit content that might not be suited for minors. This information will be published in the node information and might be used, e.g. by the global directory, to filter your node from listings of nodes to join. Additionally a note about this will be shown at the user registration page.')],
                        '$proxify_content'        => ['proxify_content', DI::l10n()->t('Proxify external content'), DI::config()->get('system', 'proxify_content'), DI::l10n()->t('Route external content via the proxy functionality. This is used for example for some OEmbed accesses and in some other rare cases.')],
+                       '$cache_contact_avatar'   => ['cache_contact_avatar', DI::l10n()->t('Cache contact avatars'), DI::config()->get('system', 'cache_contact_avatar'), DI::l10n()->t('Locally store the avatar pictures of the contacts. This uses a lot of storage space but it increases the performance.')],
                        '$allow_users_remote_self'=> ['allow_users_remote_self', DI::l10n()->t('Allow Users to set remote_self'), DI::config()->get('system', 'allow_users_remote_self'), DI::l10n()->t('With checking this, every user is allowed to mark every contact as a remote_self in the repair contact dialog. Setting this flag on a contact causes mirroring every posting of that contact in the users stream.')],
                        '$enable_multi_reg'       => ['enable_multi_reg', DI::l10n()->t('Enable multiple registrations'), !DI::config()->get('system', 'block_extended_register'), DI::l10n()->t('Enable users to register additional accounts for use as pages.')],
                        '$enable_openid'          => ['enable_openid', DI::l10n()->t('Enable OpenID'), !DI::config()->get('system', 'no_openid'), DI::l10n()->t('Enable OpenID support for registration and logins.')],
index 5d9b430b190ee1e1f5fcef48d7cf435f4c6495ae..5530163dc89b8bd13f7a8ec62c83e4c08ecc6040 100644 (file)
@@ -64,6 +64,10 @@ return [
                // Themes users can change to in their settings.
                'allowed_themes' => 'frio,quattro,vier,duepuntozero,smoothly',
 
+               // cache_contact_avatar (Boolean)
+               // Cache versions of the contact avatars. Uses a lot of storage space
+               'cache_contact_avatar' => true,
+
                // curl_timeout (Integer)
                // Value is in seconds. Set to 0 for unlimited (not recommended).
                'curl_timeout' =>  60,
index a1226bdf852de5d94903f0f0770fc74128f96c0e..985fa022213f20d72a1def829bd7e18b6c0785b6 100644 (file)
                {{include file="field_input.tpl" field=$dbclean_unclaimed}}
                {{include file="field_input.tpl" field=$dbclean_expire_conv}}
                {{include file="field_checkbox.tpl" field=$optimize_tables}}
+               {{include file="field_checkbox.tpl" field=$cache_contact_avatar}}
                <div class="submit"><input type="submit" name="page_site" value="{{$submit}}"/></div>
 
                <h2>{{$worker_title}}</h2>
index f9ba252f9cfe2e7f0bcd93b636674090d5ef1e20..a6eb1d670f38da8c1e20a8d46ffd4d84ba2e6139 100644 (file)
                                                {{include file="field_input.tpl" field=$dbclean_unclaimed}}
                                                {{include file="field_input.tpl" field=$dbclean_expire_conv}}
                                                {{include file="field_checkbox.tpl" field=$optimize_tables}}
+                                               {{include file="field_checkbox.tpl" field=$cache_contact_avatar}}
                                        </div>
                                        <div class="panel-footer">
                                                <input type="submit" name="page_site" class="btn btn-primary" value="{{$submit}}"/>