From 9eafcf8484f460cd465ace51c873ce615c93a554 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Thu, 28 Apr 2022 16:46:52 +0000
Subject: [PATCH] Update contact when the avatar hadn't been found

---
 src/Module/Contact/Profile.php |  4 ++--
 src/Module/Photo.php           | 25 +++++++++++++++++++++----
 src/Util/ParseUrl.php          |  1 +
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/Module/Contact/Profile.php b/src/Module/Contact/Profile.php
index 8ab8dae60e..757ea64d87 100644
--- a/src/Module/Contact/Profile.php
+++ b/src/Module/Contact/Profile.php
@@ -165,7 +165,7 @@ class Profile extends BaseModule
 				Module\Contact::updateContactFromPoll($contact['id']);
 			}
 
-			if ($cmd === 'updateprofile' && $localRelationship->rel !== Contact::NOTHING) {
+			if ($cmd === 'updateprofile') {
 				self::updateContactFromProbe($contact['id']);
 			}
 
@@ -481,7 +481,7 @@ class Profile extends BaseModule
 	 */
 	private static function updateContactFromProbe(int $contact_id)
 	{
-		$contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => local_user(), 'deleted' => false]);
+		$contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => [0, local_user()], 'deleted' => false]);
 		if (!DBA::isResult($contact)) {
 			return;
 		}
diff --git a/src/Module/Photo.php b/src/Module/Photo.php
index f2765280b6..73bd154c56 100644
--- a/src/Module/Photo.php
+++ b/src/Module/Photo.php
@@ -23,6 +23,7 @@ namespace Friendica\Module;
 
 use Friendica\BaseModule;
 use Friendica\Core\Logger;
+use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
@@ -31,8 +32,10 @@ use Friendica\Model\Post;
 use Friendica\Model\Profile;
 use Friendica\Core\Storage\Type\ExternalResource;
 use Friendica\Core\Storage\Type\SystemResource;
+use Friendica\Core\Worker;
 use Friendica\Model\User;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Network\HTTPException;
 use Friendica\Network\HTTPException\NotModifiedException;
 use Friendica\Object\Image;
@@ -266,14 +269,15 @@ class Photo extends BaseModule
 
 				return MPhoto::createPhotoForExternalResource($link['url'], (int)local_user(), $link['mimetype']);
 			case "contact":
-				$contact = Contact::getById($id, ['uid', 'url', 'nurl', 'avatar', 'photo', 'xmpp', 'addr']);
+				$fields = ['uid', 'url', 'nurl', 'avatar', 'photo', 'xmpp', 'addr', 'network', 'failed', 'updated'];
+				$contact = Contact::getById($id, $fields);
 				if (empty($contact)) {
 					return false;
 				}
 
 				// For local users directly use the photo record that is marked as the profile
 				if (Network::isLocalLink($contact['url'])) {
-					$contact = Contact::selectFirst(['uid', 'url', 'avatar', 'photo', 'xmpp', 'addr'], ['nurl' => $contact['nurl'], 'self' => true]);
+					$contact = Contact::selectFirst($fields, ['nurl' => $contact['nurl'], 'self' => true]);
 					if (!empty($contact)) {
 						if ($customsize <= Proxy::PIXEL_MICRO) {
 							$scale = 6;
@@ -290,7 +294,7 @@ class Photo extends BaseModule
 				}
 
 				if (!empty($contact['uid']) && empty($contact['photo']) && empty($contact['avatar'])) {
-					$contact = Contact::getByURL($contact['url'], false, ['avatar', 'photo', 'xmpp', 'addr']);
+					$contact = Contact::getByURL($contact['url'], false, $fields);
 				}
 
 				if (!empty($contact['photo']) && !empty($contact['avatar'])) {
@@ -313,7 +317,20 @@ class Photo extends BaseModule
 					if (!empty($mime)) {
 						$mimetext = $mime[0] . '/' . $mime[1];
 					} else {
-						Logger::info('Invalid file', ['url' => $url]);
+						// Only update federated accounts that hadn't failed before and hadn't been updated recently
+						$update = in_array($contact['network'], Protocol::FEDERATED) && !$contact['failed']
+							&& ((time() - strtotime($contact['updated']) > 86400));
+						if ($update) {
+							$curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]);
+							$update = !$curlResult->isSuccess() && ($curlResult->getReturnCode() == 404);
+							Logger::debug('Got return code for avatar', ['return code' => $curlResult->getReturnCode(), 'cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
+						}
+						if ($update) {
+							Logger::info('Invalid file, contact update initiated', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
+							Worker::add(PRIORITY_LOW, "UpdateContact", $id);
+						} else {
+							Logger::info('Invalid file', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
+						}
 					}
 					if (!empty($mimetext) && ($mime[0] != 'image') && ($mimetext != 'application/octet-stream')) {
 						Logger::info('Unexpected Content-Type', ['mime' => $mimetext, 'url' => $url]);
diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php
index 1cc35fadcc..abd9154da1 100644
--- a/src/Util/ParseUrl.php
+++ b/src/Util/ParseUrl.php
@@ -69,6 +69,7 @@ class ParseUrl
 		}
 
 		if (!$curlResult->isSuccess()) {
+			Logger::debug('Got HTTP Error', ['http error' => $curlResult->getReturnCode(), 'url' => $url]);
 			return [];
 		}
 
-- 
2.39.5