1
0
Fork 0

Merge pull request #11455 from annando/update-avatar

Update contact when the avatar hadn't been found
This commit is contained in:
Hypolite Petovan 2022-04-28 14:29:46 -04:00 committed by GitHub
commit aa65e3032d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 6 deletions

View file

@ -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;
}

View file

@ -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]);

View file

@ -69,6 +69,7 @@ class ParseUrl
}
if (!$curlResult->isSuccess()) {
Logger::debug('Got HTTP Error', ['http error' => $curlResult->getReturnCode(), 'url' => $url]);
return [];
}