From 9edc75e29dfdd860d9722ce99102ca3088c1dd79 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Tue, 29 Jun 2021 06:15:45 +0000
Subject: [PATCH] Improved avatar handling (timestamps for caching)

---
 src/Model/Contact.php               | 68 +++++++++++++++++++++++------
 src/Model/Profile.php               |  4 +-
 src/Object/Api/Mastodon/Account.php |  5 ++-
 3 files changed, 59 insertions(+), 18 deletions(-)

diff --git a/src/Model/Contact.php b/src/Model/Contact.php
index 86f848da3..60213cacb 100644
--- a/src/Model/Contact.php
+++ b/src/Model/Contact.php
@@ -1502,18 +1502,22 @@ class Contact
 		if (!empty($contact)) {
 			$contact = self::checkAvatarCacheByArray($contact, $no_update);
 			if (!empty($contact[$field])) {
-				$avatar = $contact[$field];
+				return $contact[$field];
+			} elseif (!empty($contact['id'])) {
+				return self::getAvatarUrlForId($contact['id'], $size, $contact['updated'] ?? '');
+			} elseif (!empty($contact['avatar'])) {
+				$avatar = $contact['avatar'];
 			}
 		}
 
-		if ($no_update && empty($avatar) && !empty($contact['avatar'])) {
-			$avatar = $contact['avatar'];
+		if (empty($avatar)) {
+			$avatar = self::getDefaultAvatar([], $size);
 		}
 
-		if (!empty($avatar) && Proxy::isLocalImage($avatar)) {
+		if (Proxy::isLocalImage($avatar)) {
 			return $avatar;
 		} else {
-			return self::getAvatarUrlForId($contact['id'], $size);
+			return Proxy::proxifyUrl($avatar, false, $size);
 		}
 	}
 
@@ -1661,12 +1665,20 @@ class Contact
 	/**
 	 * Get avatar link for given contact id
 	 *
-	 * @param integer $cid  contact id
-	 * @param string  $size One of the ProxyUtils::SIZE_* constants
+	 * @param integer $cid     contact id
+	 * @param string  $size    One of the ProxyUtils::SIZE_* constants
+	 * @param string  $updated Contact update date
 	 * @return string avatar link
 	 */
-	public static function getAvatarUrlForId(int $cid, string $size = ''):string
+	public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = ''):string
 	{
+		// We have to fetch the "updated" variable when it wasn't provided
+		// The parameter can be provided to improve performance
+		if (empty($updated)) {
+			$contact = self::getById($cid, ['updated']);
+			$updated = $contact['updated'] ?? '';
+		}
+
 		$url = DI::baseUrl() . '/photo/contact/';
 		switch ($size) {
 			case Proxy::SIZE_MICRO:
@@ -1685,7 +1697,7 @@ class Contact
 				$url .= Proxy::PIXEL_LARGE . '/';
 				break;
 		}
-		return $url . $cid;
+		return $url . $cid . ($updated ? '?ts=' . strtotime($updated) : '');
 	}
 
 	/**
@@ -1700,19 +1712,47 @@ class Contact
 	{
 		$condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
 			Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
-		$contact = self::selectFirst(['id'], $condition);
-		return self::getAvatarUrlForId($contact['id'] ?? 0, $size);
+		$contact = self::selectFirst(['id', 'updated'], $condition);
+		return self::getAvatarUrlForId($contact['id'] ?? 0, $size, $contact['updated']);
 	}
 
 	/**
 	 * Get header link for given contact id
 	 *
-	 * @param integer $cid contact id
+	 * @param integer $cid     contact id
+	 * @param string  $size    One of the ProxyUtils::SIZE_* constants
+	 * @param string  $updated Contact update date
 	 * @return string header link
 	 */
-	public static function getHeaderUrlForId(int $cid):string
+	public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = ''):string
 	{
-		return DI::baseUrl() . '/photo/header/' . $cid;
+		// We have to fetch the "updated" variable when it wasn't provided
+		// The parameter can be provided to improve performance
+		if (empty($updated)) {
+			$contact = self::getById($cid, ['updated']);
+			$updated = $contact['updated'] ?? '';
+		}
+
+		$url = DI::baseUrl() . '/photo/header/';
+		switch ($size) {
+			case Proxy::SIZE_MICRO:
+				$url .= Proxy::PIXEL_MICRO . '/';
+				break;
+			case Proxy::SIZE_THUMB:
+				$url .= Proxy::PIXEL_THUMB . '/';
+				break;
+			case Proxy::SIZE_SMALL:
+				$url .= Proxy::PIXEL_SMALL . '/';
+				break;
+			case Proxy::SIZE_MEDIUM:
+				$url .= Proxy::PIXEL_MEDIUM . '/';
+				break;
+			case Proxy::SIZE_LARGE:
+				$url .= Proxy::PIXEL_LARGE . '/';
+				break;
+		}
+
+		return $url . $cid . ($updated ? '?ts=' . strtotime($updated) : '');
 	}
 
 	/**
diff --git a/src/Model/Profile.php b/src/Model/Profile.php
index b8a5bc822..4aacc9f9b 100644
--- a/src/Model/Profile.php
+++ b/src/Model/Profile.php
@@ -507,9 +507,9 @@ class Profile
 			$p['address'] = BBCode::convert($p['address']);
 		}
 
-		$p['photo'] = Contact::getAvatarUrlForId($cid);
+		$p['photo'] = Contact::getAvatarUrlForId($cid, ProxyUtils::SIZE_SMALL);
 
-		$p['url'] = Contact::magicLink($profile_url);
+		$p['url'] = Contact::magicLinkById($cid);
 
 		$tpl = Renderer::getMarkupTemplate('profile/vcard.tpl');
 		$o .= Renderer::replaceMacros($tpl, [
diff --git a/src/Object/Api/Mastodon/Account.php b/src/Object/Api/Mastodon/Account.php
index 2ea9da704..d2053647a 100644
--- a/src/Object/Api/Mastodon/Account.php
+++ b/src/Object/Api/Mastodon/Account.php
@@ -28,6 +28,7 @@ use Friendica\Content\Text\BBCode;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Proxy;
 
 /**
  * Class Account
@@ -112,9 +113,9 @@ class Account extends BaseDataTransferObject
 
 		$this->note            = BBCode::convert($publicContact['about'], false);
 		$this->url             = $publicContact['url'];
-		$this->avatar          = $userContact['photo'] ?? '' ?: $publicContact['photo'] ?: Contact::getAvatarUrlForId($userContact['id'] ?? 0 ?: $publicContact['id']);
+		$this->avatar          = Contact::getAvatarUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], Proxy::SIZE_SMALL, $userContact['updated'] ?? '' ?: $publicContact['updated']);
 		$this->avatar_static   = $this->avatar;
-		$this->header          = Contact::getHeaderUrlForId($userContact['id'] ?? 0 ?: $publicContact['id']);
+		$this->header          = Contact::getHeaderUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], '', $userContact['updated'] ?? '' ?: $publicContact['updated']);
 		$this->header_static   = $this->header;
 		$this->followers_count = $apcontact['followers_count'] ?? 0;
 		$this->following_count = $apcontact['following_count'] ?? 0;