From bbda69a736de66d689f449604e8643f7e755865f Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 9 Jan 2022 10:38:36 +0000 Subject: [PATCH] Don't store the default header in the contact table --- src/Model/Contact.php | 88 ++++++++++++++++++++++++++----------------- src/Module/Photo.php | 7 ++-- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index fea8d72a07..1bd57d8561 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1660,6 +1660,59 @@ class Contact return $contact; } + /** + * Fetch the default header for the given contact + * + * @param array $contact contact array + * @return string avatar URL + */ + public static function getDefaultHeader(array $contact): string + { + if (!empty($contact['header']) || in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::TWITTER])) { + return DI::baseUrl() . '/images/blank.png'; + } + + if (!empty($contact['gsid'])) { + // Use default banners for certain platforms + $gserver = DBA::selectFirst('gserver', ['platform'], ['id' => $contact['gsid']]); + $platform = strtolower($gserver['platform'] ?? ''); + } else { + $platform = ''; + } + + switch ($platform) { + case 'friendica': + case 'friendika': + /** + * Picture credits + * @author Lostinlight + * @license CC0 https://creativecommons.org/share-your-work/public-domain/cc0/ + * @link https://gitlab.com/lostinlight/per_aspera_ad_astra/-/blob/master/friendica-404/friendica-promo-bubbles.jpg + */ + $header = DI::baseUrl() . '/images/friendica-banner.jpg'; + break; + case 'diaspora': + /** + * Picture credits + * @author John Liu + * @license CC BY 2.0 https://creativecommons.org/licenses/by/2.0/ + * @link https://www.flickr.com/photos/8047705@N02/5572197407 + */ + $header = DI::baseUrl() . '/images/diaspora-banner.jpg'; + break; + default: + /** + * Use random pictures for networks that don't provide banners. + * The service provides random pictures from Unsplash. + * @license https://unsplash.com/license + */ + $header = 'https://picsum.photos/seed/' . hash('ripemd128', $contact['url']) . '/960/300'; + break; + } + + return $header; + } + /** * Fetch the default avatar for the given contact and size * @@ -2202,41 +2255,6 @@ class Contact $update = false; $guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], parse_url($ret['url'], PHP_URL_HOST)); - if (empty($ret['header']) && !empty($gsid)) { - // Use default banners for certain platforms - $gserver = DBA::selectFirst('gserver', ['platform'], ['id' => $gsid]); - switch (strtolower($gserver['platform'] ?? '')) { - case 'friendica': - case 'friendika': - /** - * Picture credits - * @author Lostinlight - * @license CC0 https://creativecommons.org/share-your-work/public-domain/cc0/ - * @link https://gitlab.com/lostinlight/per_aspera_ad_astra/-/blob/master/friendica-404/friendica-promo-bubbles.jpg - */ - $ret['header'] = DI::baseUrl() . '/images/friendica-banner.jpg'; - break; - case 'diaspora': - /** - * Picture credits - * @author John Liu - * @license CC BY 2.0 https://creativecommons.org/licenses/by/2.0/ - * @link https://www.flickr.com/photos/8047705@N02/5572197407 - */ - $ret['header'] = DI::baseUrl() . '/images/diaspora-banner.jpg'; - break; - } - } - - if (empty($ret['header']) && !in_array($ret['network'], [Protocol::ACTIVITYPUB, Protocol::TWITTER])) { - /** - * Use random pictures for networks that don't provide banners. - * The service provides random pictures from Unsplash. - * @license https://unsplash.com/license - */ - $ret['header'] = 'https://picsum.photos/seed/' . hash('ripemd128', $ret['url']) . '/960/300'; - } - // make sure to not overwrite existing values with blank entries except some technical fields $keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl']; foreach ($ret as $key => $val) { diff --git a/src/Module/Photo.php b/src/Module/Photo.php index a863a88475..6f58cb7f57 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -310,17 +310,18 @@ class Photo extends BaseModule } return MPhoto::createPhotoForExternalResource($url, 0, $mimetext); case "header": - $contact = Contact::getById($id, ['uid', 'url', 'header']); + $fields = ['uid', 'url', 'header', 'network', 'gsid']; + $contact = Contact::getById($id, $fields); if (empty($contact)) { return false; } If (($contact['uid'] != 0) && empty($contact['header'])) { - $contact = Contact::getByURL($contact['url'], false, ['header']); + $contact = Contact::getByURL($contact['url'], false, $fields); } if (!empty($contact['header'])) { $url = $contact['header']; } else { - $url = DI::baseUrl() . '/images/blank.png'; + $url = Contact::getDefaultHeader($contact); } return MPhoto::createPhotoForExternalResource($url); case "banner":