From fae414fedde736bb7e3d7ad92e036288bcb54782 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 10 May 2022 18:18:24 +0000 Subject: [PATCH] Fix local avatar, improve speed for fetching cached photos --- src/Content/Conversation.php | 25 ++------------------- src/Content/Item.php | 42 +++++++++++++++++++++++++++++++++++- src/Model/Contact.php | 4 ++++ src/Module/Photo.php | 7 ++++-- src/Object/Post.php | 25 ++------------------- 5 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/Content/Conversation.php b/src/Content/Conversation.php index 89d9ca4c1b..04cdab4785 100644 --- a/src/Content/Conversation.php +++ b/src/Content/Conversation.php @@ -660,27 +660,6 @@ class Conversation $pinned = ''; } - if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) { - $owner_avatar = $author_avatar = $item['contact-id']; - $owner_updated = $author_updated = ''; - $owner_thumb = $author_thumb = $item['contact-avatar']; - } else { - $owner_avatar = $item['owner-id']; - $owner_updated = $item['owner-updated']; - $owner_thumb = $item['owner-avatar']; - $author_avatar = $item['author-id']; - $author_updated = $item['author-updated']; - $author_thumb = $item['author-avatar']; - } - - if (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) { - $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated); - } - - if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) { - $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated); - } - $tmp_item = [ 'template' => $tpl, 'id' => ($preview ? 'P0' : $item['id']), @@ -698,7 +677,7 @@ class Conversation 'name' => $profile_name, 'sparkle' => $sparkle, 'lock' => false, - 'thumb' => $this->baseURL->remove($author_thumb), + 'thumb' => $this->baseURL->remove($this->item->getAuthorAvatar($item)), 'title' => $title, 'body_html' => $body_html, 'tags' => $tags['tags'], @@ -719,7 +698,7 @@ class Conversation 'indent' => '', 'owner_name' => '', 'owner_url' => '', - 'owner_photo' => $this->baseURL->remove($owner_thumb), + 'owner_photo' => $this->baseURL->remove($this->item->getOwnerAvatar($item)), 'plink' => ItemModel::getPlink($item), 'edpost' => false, 'pinned' => $pinned, diff --git a/src/Content/Item.php b/src/Content/Item.php index d361128651..240de4c741 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -26,15 +26,16 @@ use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\Protocol; -use Friendica\Core\Session; use Friendica\Database\DBA; use Friendica\Model\Contact; use Friendica\Model\Group; use Friendica\Model\Item as ModelItem; +use Friendica\Model\Photo; use Friendica\Model\Tag; use Friendica\Model\Post; use Friendica\Protocol\Activity; use Friendica\Util\Profiler; +use Friendica\Util\Proxy; use Friendica\Util\XML; /** @@ -564,4 +565,43 @@ class Item } return $item; } + + public function getAuthorAvatar(array $item): string + { + if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) { + $author_avatar = $item['contact-id']; + $author_updated = ''; + $author_thumb = $item['contact-avatar']; + } else { + $author_avatar = $item['author-id']; + $author_updated = $item['author-updated']; + $author_thumb = $item['author-avatar']; + } + + + if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) { + $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated); + } + + return $author_thumb; + } + + public function getOwnerAvatar(array $item): string + { + if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) { + $owner_avatar = $item['contact-id']; + $owner_updated = ''; + $owner_thumb = $item['contact-avatar']; + } else { + $owner_avatar = $item['owner-id']; + $owner_updated = $item['owner-updated']; + $owner_thumb = $item['owner-avatar']; + } + + if (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) { + $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated); + } + + return $owner_thumb; + } } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 6a98286994..b0ccfd40bf 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1700,6 +1700,10 @@ class Contact } } + if (Network::isLocalLink($contact['url']) && !empty($contact['avatar'])) { + return $contact; + } + /// add the default avatars if the fields aren't filled if (isset($contact['photo']) && empty($contact['photo'])) { $contact['photo'] = self::getDefaultAvatar($contact, Proxy::SIZE_SMALL); diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 73bd154c56..f721b1cea9 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -304,10 +304,12 @@ class Photo extends BaseModule $photo = MPhoto::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]); if (!empty($photo)) { return $photo; + } else { + $url = $contact['avatar']; } + } else { + $url = $contact['photo']; } - // We continue with the avatar link when the photo link is invalid - $url = $contact['avatar']; } elseif (!empty($contact['avatar'])) { $url = $contact['avatar']; } @@ -340,6 +342,7 @@ class Photo extends BaseModule } } if (empty($mimetext)) { + Logger::info('Blubb'); if ($customsize <= Proxy::PIXEL_MICRO) { $url = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO); } elseif ($customsize <= Proxy::PIXEL_THUMB) { diff --git a/src/Object/Post.php b/src/Object/Post.php index 3c01028292..3f4b2760b5 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -451,27 +451,6 @@ class Post $browsershare = null; } - if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) { - $owner_avatar = $author_avatar = $item['contact-id']; - $owner_updated = $author_updated = ''; - $owner_thumb = $author_thumb = $item['contact-avatar']; - } else { - $owner_avatar = $item['owner-id']; - $owner_updated = $item['owner-updated']; - $owner_thumb = $item['owner-avatar']; - $author_avatar = $item['author-id']; - $author_updated = $item['author-updated']; - $author_thumb = $item['author-avatar']; - } - - if (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) { - $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated); - } - - if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) { - $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated); - } - $tmp_item = [ 'template' => $this->getTemplate(), 'type' => implode("", array_slice(explode("/", $item['verb']), -1)), @@ -503,7 +482,7 @@ class Post 'profile_url' => $profile_link, 'name' => $profile_name, 'item_photo_menu_html' => DI::contentItem()->photoMenu($item, $formSecurityToken), - 'thumb' => DI::baseUrl()->remove($author_thumb), + 'thumb' => DI::baseUrl()->remove(DI::contentItem()->getAuthorAvatar($item)), 'osparkle' => $osparkle, 'sparkle' => $sparkle, 'title' => $title, @@ -520,7 +499,7 @@ class Post 'shiny' => $shiny, 'owner_self' => $item['author-link'] == Session::get('my_url'), 'owner_url' => $this->getOwnerUrl(), - 'owner_photo' => DI::baseUrl()->remove($owner_thumb), + 'owner_photo' => DI::baseUrl()->remove(DI::contentItem()->getOwnerAvatar($item)), 'owner_name' => $this->getOwnerName(), 'plink' => Item::getPlink($item), 'browsershare' => $browsershare, -- 2.39.5