From 6ab82eaa4938389501be8556089f2407a2f8f1a4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 27 Jul 2020 10:11:12 +0000 Subject: [PATCH 1/2] Ensure that cached avatar fields are set --- src/Model/Contact.php | 48 ++++++++++++++++++++++++++++++++++++++++++ src/Module/Contact.php | 3 +++ 2 files changed, 51 insertions(+) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 1cf8c74237..4f8cb84db7 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1787,6 +1787,54 @@ class Contact self::updateAvatar($cid, $contact['avatar'], true); } + /** + * Check the given contact array for avatar cache fields + * + * @param array $contact + * @return array contact array with avatar cache fields + */ + public static function checkAvatarCacheArray(array $contact) + { + $update = false; + $contact_fields = []; + $fields = ['photo', 'thumb', 'micro']; + foreach ($fields as $field) { + if (isset($contact[$field])) { + $contact_fields[] = $field; + } + if (isset($contact[$field]) && empty($contact[$field])) { + $update = true; + } + } + + if (!$update) { + return $contact; + } + + if (!empty($contact['id']) && !empty($contact['avatar'])) { + self::updateAvatar($contact['id'], $contact['avatar'], true); + + $new_contact = self::getById($contact['id'], $contact_fields); + if (DBA::isResult($new_contact)) { + // We only update the cache fields + $contact = array_merge($contact, $new_contact); + } + } + + /// add the default avatars if the fields aren't filled + if (isset($contact['photo']) && empty($contact['photo'])) { + $contact['photo'] = DI::baseUrl() . '/images/person-300.jpg'; + } + if (isset($contact['thumb']) && empty($contact['thumb'])) { + $contact['thumb'] = DI::baseUrl() . '/images/person-80.jpg'; + } + if (isset($contact['micro']) && empty($contact['micro'])) { + $contact['micro'] = DI::baseUrl() . '/images/person-48.jpg'; + } + + return $contact; + } + /** * Updates the avatar links in a contact only if needed * diff --git a/src/Module/Contact.php b/src/Module/Contact.php index 096f693302..e9f00a1b65 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -36,6 +36,7 @@ use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model; +use Friendica\Model\Contact as ModelContact; use Friendica\Module\Security\Login; use Friendica\Network\HTTPException\BadRequestException; use Friendica\Network\HTTPException\NotFoundException; @@ -278,6 +279,8 @@ class Contact extends BaseModule if ($contact['network'] == Protocol::PHANTOM) { $contact = false; } + + $contact = ModelContact::checkAvatarCacheArray($contact); } if (DBA::isResult($contact)) { From 5b884e834892a75f50adc3122d11b74384a711e4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 27 Jul 2020 10:22:02 +0000 Subject: [PATCH 2/2] Changed function name --- src/Model/Contact.php | 2 +- src/Module/Contact.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 4f8cb84db7..9e89fc30a5 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1793,7 +1793,7 @@ class Contact * @param array $contact * @return array contact array with avatar cache fields */ - public static function checkAvatarCacheArray(array $contact) + public static function checkAvatarCacheByArray(array $contact) { $update = false; $contact_fields = []; diff --git a/src/Module/Contact.php b/src/Module/Contact.php index e9f00a1b65..5a4d0b1e86 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -280,7 +280,7 @@ class Contact extends BaseModule $contact = false; } - $contact = ModelContact::checkAvatarCacheArray($contact); + $contact = ModelContact::checkAvatarCacheByArray($contact); } if (DBA::isResult($contact)) {