Merge pull request #9044 from annando/avatar-stuff

Some stuff with contact avatars
This commit is contained in:
Hypolite Petovan 2020-08-22 14:08:20 -04:00 committed by GitHub
commit 9923e17aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 45 deletions

View File

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2020.09-dev (Red Hot Poker) -- Friendica 2020.09-dev (Red Hot Poker)
-- DB_UPDATE_VERSION 1362 -- DB_UPDATE_VERSION 1363
-- ------------------------------------------ -- ------------------------------------------
@ -922,7 +922,8 @@ CREATE TABLE IF NOT EXISTS `photo` (
INDEX `uid_profile` (`uid`,`profile`), INDEX `uid_profile` (`uid`,`profile`),
INDEX `uid_album_scale_created` (`uid`,`album`(32),`scale`,`created`), INDEX `uid_album_scale_created` (`uid`,`album`(32),`scale`,`created`),
INDEX `uid_album_resource-id_created` (`uid`,`album`(32),`resource-id`,`created`), INDEX `uid_album_resource-id_created` (`uid`,`album`(32),`resource-id`,`created`),
INDEX `resource-id` (`resource-id`) INDEX `resource-id` (`resource-id`),
FOREIGN KEY (`contact-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='photo storage'; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='photo storage';
-- --

View File

@ -1549,18 +1549,19 @@ class Contact
/** /**
* Updates the avatar links in a contact only if needed * Updates the avatar links in a contact only if needed
* *
* @param int $cid Contact id * @param int $cid Contact id
* @param string $avatar Link to avatar picture * @param string $avatar Link to avatar picture
* @param bool $force force picture update * @param bool $force force picture update
* @param bool $create_cache Enforces the creation of cached avatar fields
* *
* @return void * @return void
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException * @throws HTTPException\NotFoundException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function updateAvatar(int $cid, string $avatar, bool $force = false) public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false)
{ {
$contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url'], ['id' => $cid, 'self' => false]); $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'nurl', 'url', 'network'], ['id' => $cid, 'self' => false]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
return; return;
} }
@ -1568,7 +1569,7 @@ class Contact
$uid = $contact['uid']; $uid = $contact['uid'];
// Only update the cached photo links of public contacts when they already are cached // Only update the cached photo links of public contacts when they already are cached
if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro'])) { if (($uid == 0) && !$force && empty($contact['thumb']) && empty($contact['micro']) && !$create_cache) {
if ($contact['avatar'] != $avatar) { if ($contact['avatar'] != $avatar) {
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]); DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]); Logger::info('Only update the avatar', ['id' => $cid, 'avatar' => $avatar, 'contact' => $contact]);
@ -1576,9 +1577,14 @@ class Contact
return; return;
} }
$local_uid = User::getIdForURL($contact['url']); // User contacts use are updated through the public contacts
if (!empty($local_uid)) { if (($uid != 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
$fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]); $pcid = self::getIdForURL($contact['url'], false);
if (!empty($pcid)) {
Logger::debug('Update the private contact via the public contact', ['id' => $cid, 'uid' => $uid, 'public' => $pcid]);
self::updateAvatar($pcid, $avatar, $force, true);
return;
}
} }
// Replace cached avatar pictures from the default avatar with the default avatars in different sizes // Replace cached avatar pictures from the default avatar with the default avatars in different sizes
@ -1587,45 +1593,77 @@ class Contact
'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO, 'photo' => DI::baseUrl() . self::DEFAULT_AVATAR_PHOTO,
'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB, 'thumb' => DI::baseUrl() . self::DEFAULT_AVATAR_THUMB,
'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO]; 'micro' => DI::baseUrl() . self::DEFAULT_AVATAR_MICRO];
Logger::debug('Use default avatar', ['id' => $cid, 'uid' => $uid]);
} }
if (!empty($fields)) { // Use the data from the self account
if ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) { if (empty($fields)) {
DBA::update('contact', $fields, ['id' => $cid]); $local_uid = User::getIdForURL($contact['url']);
Photo::delete(['uid' => $uid, 'contact-id' => $cid, 'album' => Photo::CONTACT_PHOTOS]); if (!empty($local_uid)) {
$fields = self::selectFirst(['avatar', 'avatar-date', 'photo', 'thumb', 'micro'], ['self' => true, 'uid' => $local_uid]);
Logger::debug('Use owner data', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
} }
}
if (empty($fields)) {
$update = ($contact['avatar'] != $avatar) || $force;
if (!$update) {
$data = [
$contact['photo'] ?? '',
$contact['thumb'] ?? '',
$contact['micro'] ?? '',
];
foreach ($data as $image_uri) {
$image_rid = Photo::ridFromURI($image_uri);
if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
Logger::debug('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
$update = true;
}
}
}
if ($update) {
$photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
if ($photos) {
$fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()];
$update = !empty($fields);
Logger::debug('Created new cached avatars', ['id' => $cid, 'uid' => $uid, 'owner-uid' => $local_uid]);
} else {
$update = false;
}
}
} else {
$update = ($fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
}
if (!$update) {
return; return;
} }
$data = [ $cids = [];
$contact['photo'] ?? '', $uids = [];
$contact['thumb'] ?? '', if (($uid == 0) && !in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
$contact['micro'] ?? '', // Collect all user contacts of the given public contact
]; $personal_contacts = DBA::select('contact', ['id', 'uid'],
["`nurl` = ? AND `id` != ? AND NOT `self`", $contact['nurl'], $cid]);
while ($personal_contact = DBA::fetch($personal_contacts)) {
$cids[] = $personal_contact['id'];
$uids[] = $personal_contact['uid'];
}
DBA::close($personal_contacts);
$update = ($contact['avatar'] != $avatar) || $force; if (!empty($cids)) {
// Delete possibly existing cached user contact avatars
if (!$update) { Photo::delete(['uid' => $uids, 'contact-id' => $cids, 'album' => Photo::CONTACT_PHOTOS]);
foreach ($data as $image_uri) {
$image_rid = Photo::ridFromURI($image_uri);
if ($image_rid && !Photo::exists(['resource-id' => $image_rid, 'uid' => $uid])) {
Logger::info('Regenerating avatar', ['contact uid' => $uid, 'cid' => $cid, 'missing photo' => $image_rid, 'avatar' => $contact['avatar']]);
$update = true;
}
} }
} }
if ($update) { $cids[] = $cid;
$photos = Photo::importProfilePhoto($avatar, $uid, $cid, true); $uids[] = $uid;
if ($photos) { Logger::info('Updating cached contact avatars', ['cid' => $cids, 'uid' => $uids, 'fields' => $fields]);
$fields = ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()]; DBA::update('contact', $fields, ['id' => $cids]);
DBA::update('contact', $fields, ['id' => $cid]);
} elseif (empty($contact['avatar'])) {
// Ensure that the avatar field is set
DBA::update('contact', ['avatar' => $avatar], ['id' => $cid]);
Logger::info('Failed profile import', ['id' => $cid, 'force' => $force, 'avatar' => $avatar, 'contact' => $contact]);
}
}
} }
/** /**

View File

@ -22,6 +22,7 @@
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Photo;
use Friendica\Model\User; use Friendica\Model\User;
/** /**
@ -51,6 +52,8 @@ class ExpireAndRemoveUsers
DBA::delete('contact', ['nurl' => $self['nurl'], 'self' => false]); DBA::delete('contact', ['nurl' => $self['nurl'], 'self' => false]);
} }
Photo::delete(['uid' => $user['uid']]);
DBA::delete('user', ['uid' => $user['uid']]); DBA::delete('user', ['uid' => $user['uid']]);
} }
DBA::close($users); DBA::close($users);

View File

@ -23,8 +23,8 @@ namespace Friendica\Worker;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Core\Protocol;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Photo;
/** /**
* Removes orphaned data from deleted contacts * Removes orphaned data from deleted contacts
@ -33,7 +33,7 @@ class RemoveContact {
public static function execute($id) { public static function execute($id) {
// Only delete if the contact is to be deleted // Only delete if the contact is to be deleted
$contact = DBA::selectFirst('contact', ['uid'], ['deleted' => true]); $contact = DBA::selectFirst('contact', ['uid'], ['deleted' => true, 'id' => $id]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
return; return;
} }
@ -49,6 +49,7 @@ class RemoveContact {
DBA::close($items); DBA::close($items);
} while (Item::exists($condition)); } while (Item::exists($condition));
Photo::delete(['uid' => $contact['uid'], 'contact-id' => $id]);
DBA::delete('contact', ['id' => $id]); DBA::delete('contact', ['id' => $id]);
} }
} }

View File

@ -54,7 +54,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1362); define('DB_UPDATE_VERSION', 1363);
} }
return [ return [
@ -984,7 +984,7 @@ return [
"fields" => [ "fields" => [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
"uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "Owner User id"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "Owner User id"],
"contact-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "relation" => ["contact" => "id"], "comment" => "contact.id"], "contact-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "contact.id"],
"guid" => ["type" => "char(16)", "not null" => "1", "default" => "", "comment" => "A unique identifier for this photo"], "guid" => ["type" => "char(16)", "not null" => "1", "default" => "", "comment" => "A unique identifier for this photo"],
"resource-id" => ["type" => "char(32)", "not null" => "1", "default" => "", "comment" => ""], "resource-id" => ["type" => "char(32)", "not null" => "1", "default" => "", "comment" => ""],
"created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "creation date"], "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "creation date"],

View File

@ -49,6 +49,7 @@ use Friendica\Database\DBStructure;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Model\Storage; use Friendica\Model\Storage;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -573,3 +574,9 @@ function pre_update_1358()
return Update::SUCCESS; return Update::SUCCESS;
} }
function pre_update_1363()
{
Photo::delete(["`contact-id` != ? AND NOT `contact-id` IN (SELECT `id` FROM `contact`)", 0]);
return Update::SUCCESS;
}