From 47904f99c2d9456013f9437ced48e7cc8bea8e35 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 4 Jul 2021 10:40:03 +0000 Subject: [PATCH] Removal of duplicated photo entries for a contact --- src/Module/Photo.php | 5 +++-- src/Worker/RemoveUnusedAvatars.php | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 3ea7578ce1..44be9a31a2 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -214,7 +214,7 @@ class Photo extends BaseModule If (($contact['uid'] != 0) && empty($contact['photo']) && empty($contact['avatar'])) { $contact = Contact::getByURL($contact['url'], false, ['avatar', 'photo', 'xmpp', 'addr']); } - if (!empty($contact['photo'])) { + if (!empty($contact['photo']) && !empty($contact['avatar'])) { // Fetch photo directly $resourceid = MPhoto::ridFromURI($contact['photo']); if (!empty($resourceid)) { @@ -223,7 +223,8 @@ class Photo extends BaseModule return $photo; } } - $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']; } elseif ($customsize <= Proxy::PIXEL_MICRO) { diff --git a/src/Worker/RemoveUnusedAvatars.php b/src/Worker/RemoveUnusedAvatars.php index ad5b04781d..d8efbe3fb9 100644 --- a/src/Worker/RemoveUnusedAvatars.php +++ b/src/Worker/RemoveUnusedAvatars.php @@ -58,6 +58,7 @@ class RemoveUnusedAvatars Logger::notice('Removal done', ['count' => $count, 'total' => $total]); self::fixPhotoContacts(); + self::deleteDuplicates(); } private static function fixPhotoContacts() @@ -67,7 +68,7 @@ class RemoveUnusedAvatars $updated1 = 0; $updated2 = 0; Logger::notice('Starting contact fix'); - $photos = DBA::select('photo', [], ["`uid` = ? AND `contact-id` IN (SELECT `id` FROM `contact` WHERE `uid` != ?)", 0, 0]); + $photos = DBA::select('photo', [], ["`uid` = ? AND `contact-id` IN (SELECT `id` FROM `contact` WHERE `uid` != ?) AND `contact-id` != ? AND `scale` IN (?, ?, ?)", 0, 0, 0, 4, 5, 6]); while ($photo = DBA::fetch($photos)) { $total++; $photo_contact = Contact::getById($photo['contact-id']); @@ -101,4 +102,27 @@ class RemoveUnusedAvatars DBA::close($photos); Logger::notice('Contact fix done', ['total' => $total, 'updated1' => $updated1, 'updated2' => $updated2, 'deleted' => $deleted]); } + + public static function deleteDuplicates() + { + $size = [4 => 'photo', 5 => 'thumb', 6 => 'micro']; + + $total = 0; + $deleted = 0; + Logger::notice('Starting duplicate removal'); + $photos = DBA::p("SELECT `photo`.`id`, `photo`.`uid`, `photo`.`scale`, `photo`.`album`, `photo`.`contact-id`, `photo`.`resource-id`, `contact`.`photo`, `contact`.`thumb`, `contact`.`micro` FROM `photo` INNER JOIN `contact` ON `contact`.`id` = `photo`.`contact-id` and `photo`.`contact-id` != ? AND `photo`.`scale` IN (?, ?, ?)", 0, 4, 5, 6); + while ($photo = DBA::fetch($photos)) { + $resource = Photo::ridFromURI($photo[$size[$photo['scale']]]); + if ($resource != $photo['resource-id'] && !empty($resource)) { + $total++; + if (DBA::exists('photo', ['resource-id' => $resource, 'scale' => $photo['scale']])) { + Logger::notice('Photo deleted', ['id' => $photo['id']]); + Photo::delete(['id' => $photo['id']]); + $deleted++; + } + } + } + DBA::close($photos); + Logger::notice('Duplicate removal done', ['total' => $total, 'deleted' => $deleted]); + } }