From 8185c591d8fb3a79869356a29f2e31be27867d88 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 23 Oct 2021 17:18:30 +0000 Subject: [PATCH] New option to activate/deactivate contact avatar cache --- src/Model/Contact.php | 99 +++++++++++++----------- src/Module/Admin/Site.php | 3 + static/settings.config.php | 4 + view/templates/admin/site.tpl | 1 + view/theme/frio/templates/admin/site.tpl | 1 + 5 files changed, 64 insertions(+), 44 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index c6a91edc8b..1221261c87 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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]); - } - - // 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 (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]); } - } - if (empty($fields)) { - $update = ($contact['avatar'] != $avatar) || $force; + // 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 (!$update) { - $data = [ - $contact['photo'] ?? '', - $contact['thumb'] ?? '', - $contact['micro'] ?? '', - ]; + if (empty($fields)) { + $update = ($contact['avatar'] != $avatar) || $force; - 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) { + $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) { diff --git a/src/Module/Admin/Site.php b/src/Module/Admin/Site.php index 5819627453..be72184cb7 100644 --- a/src/Module/Admin/Site.php +++ b/src/Module/Admin/Site.php @@ -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.')], diff --git a/static/settings.config.php b/static/settings.config.php index 5d9b430b19..5530163dc8 100644 --- a/static/settings.config.php +++ b/static/settings.config.php @@ -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, diff --git a/view/templates/admin/site.tpl b/view/templates/admin/site.tpl index a1226bdf85..985fa02221 100644 --- a/view/templates/admin/site.tpl +++ b/view/templates/admin/site.tpl @@ -116,6 +116,7 @@ {{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}}

{{$worker_title}}

diff --git a/view/theme/frio/templates/admin/site.tpl b/view/theme/frio/templates/admin/site.tpl index f9ba252f9c..a6eb1d670f 100644 --- a/view/theme/frio/templates/admin/site.tpl +++ b/view/theme/frio/templates/admin/site.tpl @@ -251,6 +251,7 @@ {{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}}