Merge pull request #7282 from annando/issue-6477

Issue 6477: Automatically repair self contact avatar
This commit is contained in:
Hypolite Petovan 2019-06-19 16:12:56 -04:00 committed by GitHub
commit 2ba4116118
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 84 additions and 90 deletions

View file

@ -593,11 +593,13 @@ class Contact extends BaseObject
return; return;
} }
$file_suffix = 'jpg';
$fields = ['name' => $profile['name'], 'nick' => $user['nickname'], $fields = ['name' => $profile['name'], 'nick' => $user['nickname'],
'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile), 'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile),
'about' => $profile['about'], 'keywords' => $profile['pub_keywords'], 'about' => $profile['about'], 'keywords' => $profile['pub_keywords'],
'gender' => $profile['gender'], 'avatar' => $profile['photo'], 'gender' => $profile['gender'], 'contact-type' => $user['account-type'],
'contact-type' => $user['account-type'], 'xmpp' => $profile['xmpp']]; 'xmpp' => $profile['xmpp']];
$avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]); $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
if (DBA::isResult($avatar)) { if (DBA::isResult($avatar)) {
@ -609,8 +611,6 @@ class Contact extends BaseObject
$types = Image::supportedTypes(); $types = Image::supportedTypes();
if (isset($types[$avatar['type']])) { if (isset($types[$avatar['type']])) {
$file_suffix = $types[$avatar['type']]; $file_suffix = $types[$avatar['type']];
} else {
$file_suffix = 'jpg';
} }
// We are adding a timestamp value so that other systems won't use cached content // We are adding a timestamp value so that other systems won't use cached content
@ -629,6 +629,7 @@ class Contact extends BaseObject
$fields['micro'] = System::baseUrl() . '/images/person-48.jpg'; $fields['micro'] = System::baseUrl() . '/images/person-48.jpg';
} }
$fields['avatar'] = System::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix;
$fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY; $fields['forum'] = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
$fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP; $fields['prv'] = $user['page-flags'] == User::PAGE_FLAGS_PRVGROUP;
@ -661,8 +662,8 @@ class Contact extends BaseObject
DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]); DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
// Update the profile // Update the profile
$fields = ['photo' => System::baseUrl() . '/photo/profile/' .$uid . '.jpg', $fields = ['photo' => System::baseUrl() . '/photo/profile/' .$uid . '.' . $file_suffix,
'thumb' => System::baseUrl() . '/photo/avatar/' . $uid .'.jpg']; 'thumb' => System::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix];
DBA::update('profile', $fields, ['uid' => $uid, 'is-default' => true]); DBA::update('profile', $fields, ['uid' => $uid, 'is-default' => true]);
} }
} }
@ -1725,7 +1726,7 @@ class Contact extends BaseObject
*/ */
public static function updateAvatar($avatar, $uid, $cid, $force = false) public static function updateAvatar($avatar, $uid, $cid, $force = false)
{ {
$contact = DBA::selectFirst('contact', ['avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid]); $contact = DBA::selectFirst('contact', ['avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid, 'self' => false]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
return false; return false;
} else { } else {
@ -2153,7 +2154,7 @@ class Contact extends BaseObject
return false; return false;
} }
$fields = ['url', 'name', 'nick', 'photo', 'network', 'blocked']; $fields = ['url', 'name', 'nick', 'avatar', 'photo', 'network', 'blocked'];
$pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]); $pub_contact = DBA::selectFirst('contact', $fields, ['id' => $datarray['author-id']]);
if (!DBA::isResult($pub_contact)) { if (!DBA::isResult($pub_contact)) {
// Should never happen // Should never happen
@ -2167,7 +2168,7 @@ class Contact extends BaseObject
$url = defaults($datarray, 'author-link', $pub_contact['url']); $url = defaults($datarray, 'author-link', $pub_contact['url']);
$name = $pub_contact['name']; $name = $pub_contact['name'];
$photo = $pub_contact['photo']; $photo = defaults($pub_contact, 'avatar', $pub_contact["photo"]);
$nick = $pub_contact['nick']; $nick = $pub_contact['nick'];
$network = $pub_contact['network']; $network = $pub_contact['network'];

View file

@ -16,6 +16,7 @@ use Friendica\Core\Protocol;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Photo;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Util\Crypto; use Friendica\Util\Crypto;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -149,10 +150,11 @@ class User
* @brief Get owner data by user id * @brief Get owner data by user id
* *
* @param int $uid * @param int $uid
* @param boolean $check_valid Test if data is invalid and correct it
* @return boolean|array * @return boolean|array
* @throws Exception * @throws Exception
*/ */
public static function getOwnerDataById($uid) { public static function getOwnerDataById($uid, $check_valid = true) {
$r = DBA::fetchFirst("SELECT $r = DBA::fetchFirst("SELECT
`contact`.*, `contact`.*,
`user`.`prvkey` AS `uprvkey`, `user`.`prvkey` AS `uprvkey`,
@ -180,12 +182,34 @@ class User
return false; return false;
} }
// Check if the returned data is valid, otherwise fix it. See issue #6122 if (!$check_valid) {
$url = System::baseUrl() . '/profile/' . $r['nickname']; return $r;
$addr = $r['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3); }
if (($addr != $r['addr']) || ($r['url'] != $url) || ($r['nurl'] != Strings::normaliseLink($r['url']))) { // Check if the returned data is valid, otherwise fix it. See issue #6122
// Check for correct url and normalised nurl
$url = System::baseUrl() . '/profile/' . $r['nickname'];
$repair = ($r['url'] != $url) || ($r['nurl'] != Strings::normaliseLink($r['url']));
if (!$repair) {
// Check if "addr" is present and correct
$addr = $r['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
$repair = ($addr != $r['addr']);
}
if (!$repair) {
// Check if the avatar field is filled and the photo directs to the correct path
$avatar = Photo::selectFirst(['resource-id'], ['uid' => $uid, 'profile' => true]);
if (DBA::isResult($avatar)) {
$repair = empty($r['avatar']) || !strpos($r['photo'], $avatar['resource-id']);
}
}
if ($repair) {
Contact::updateSelfFromUserID($uid); Contact::updateSelfFromUserID($uid);
// Return the corrected data and avoid a loop
$r = self::getOwnerDataById($uid, false);
} }
return $r; return $r;

View file

@ -4,9 +4,11 @@ namespace Friendica\Module;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Database\DBA;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Model\Photo;
use Friendica\Protocol\Salmon; use Friendica\Protocol\Salmon;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -61,67 +63,69 @@ class Xrd extends BaseModule
throw new \Friendica\Network\HTTPException\NotFoundException(); throw new \Friendica\Network\HTTPException\NotFoundException();
} }
$profileURL = $app->getBaseURL() . '/profile/' . $user['nickname']; $owner = User::getOwnerDataById($user['uid']);
$alias = str_replace('/profile/', '/~', $profileURL);
$addr = 'acct:' . $user['nickname'] . '@' . $app->getHostName(); $alias = str_replace('/profile/', '/~', $owner['url']);
if ($app->getURLPath()) {
$addr .= '/' . $app->getURLPath(); $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
if (!DBA::isResult($avatar)) {
$avatar = ['type' => 'image/jpeg'];
} }
if ($mode == 'xml') { if ($mode == 'xml') {
self::printXML($addr, $alias, $profileURL, $app->getBaseURL(), $user); self::printXML($alias, $app->getBaseURL(), $user, $owner, $avatar);
} else { } else {
self::printJSON($addr, $alias, $profileURL, $app->getBaseURL(), $user); self::printJSON($alias, $app->getBaseURL(), $owner, $avatar);
} }
} }
private static function printJSON($uri, $alias, $orofileURL, $baseURL, $user) private static function printJSON($alias, $baseURL, $owner, $avatar)
{ {
$salmon_key = Salmon::salmonKey($user['spubkey']); $salmon_key = Salmon::salmonKey($owner['spubkey']);
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
header('Content-type: application/json; charset=utf-8'); header('Content-type: application/json; charset=utf-8');
$json = [ $json = [
'subject' => $uri, 'subject' => 'acct:' . $owner['addr'],
'aliases' => [ 'aliases' => [
$alias, $alias,
$orofileURL, $owner['url'],
], ],
'links' => [ 'links' => [
[ [
'rel' => NAMESPACE_DFRN, 'rel' => NAMESPACE_DFRN,
'href' => $orofileURL, 'href' => $owner['url'],
], ],
[ [
'rel' => NAMESPACE_FEED, 'rel' => NAMESPACE_FEED,
'type' => 'application/atom+xml', 'type' => 'application/atom+xml',
'href' => $baseURL . '/dfrn_poll/' . $user['nickname'], 'href' => $owner['poll'],
], ],
[ [
'rel' => 'http://webfinger.net/rel/profile-page', 'rel' => 'http://webfinger.net/rel/profile-page',
'type' => 'text/html', 'type' => 'text/html',
'href' => $orofileURL, 'href' => $owner['url'],
], ],
[ [
'rel' => 'self', 'rel' => 'self',
'type' => 'application/activity+json', 'type' => 'application/activity+json',
'href' => $orofileURL, 'href' => $owner['url'],
], ],
[ [
'rel' => 'http://microformats.org/profile/hcard', 'rel' => 'http://microformats.org/profile/hcard',
'type' => 'text/html', 'type' => 'text/html',
'href' => $baseURL . '/hcard/' . $user['nickname'], 'href' => $baseURL . '/hcard/' . $owner['nickname'],
], ],
[ [
'rel' => NAMESPACE_POCO, 'rel' => NAMESPACE_POCO,
'href' => $baseURL . '/poco/' . $user['nickname'], 'href' => $owner['poco'],
], ],
[ [
'rel' => 'http://webfinger.net/rel/avatar', 'rel' => 'http://webfinger.net/rel/avatar',
'type' => 'image/jpeg', 'type' => $avatar['type'],
'href' => $baseURL . '/photo/profile/' . $user['uid'] . '.jpg', 'href' => $owner['photo'],
], ],
[ [
'rel' => 'http://joindiaspora.com/seed_location', 'rel' => 'http://joindiaspora.com/seed_location',
@ -130,15 +134,15 @@ class Xrd extends BaseModule
], ],
[ [
'rel' => 'salmon', 'rel' => 'salmon',
'href' => $baseURL . '/salmon/' . $user['nickname'], 'href' => $baseURL . '/salmon/' . $owner['nickname'],
], ],
[ [
'rel' => 'http://salmon-protocol.org/ns/salmon-replies', 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
'href' => $baseURL . '/salmon/' . $user['nickname'], 'href' => $baseURL . '/salmon/' . $owner['nickname'],
], ],
[ [
'rel' => 'http://salmon-protocol.org/ns/salmon-mention', 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
'href' => $baseURL . '/salmon/' . $user['nickname'] . '/mention', 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
], ],
[ [
'rel' => 'http://ostatus.org/schema/1.0/subscribe', 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
@ -160,9 +164,9 @@ class Xrd extends BaseModule
exit(); exit();
} }
private static function printXML($uri, $alias, $profileURL, $baseURL, $user) private static function printXML($alias, $baseURL, $user, $owner, $avatar)
{ {
$salmon_key = Salmon::salmonKey($user['spubkey']); $salmon_key = Salmon::salmonKey($owner['spubkey']);
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
header('Content-type: text/xml'); header('Content-type: text/xml');
@ -170,16 +174,17 @@ class Xrd extends BaseModule
$tpl = Renderer::getMarkupTemplate('xrd_person.tpl'); $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
$o = Renderer::replaceMacros($tpl, [ $o = Renderer::replaceMacros($tpl, [
'$nick' => $user['nickname'], '$nick' => $owner['nickname'],
'$accturi' => $uri, '$accturi' => 'acct:' . $owner['addr'],
'$alias' => $alias, '$alias' => $alias,
'$profile_url' => $profileURL, '$profile_url' => $owner['url'],
'$hcard_url' => $baseURL . '/hcard/' . $user['nickname'], '$hcard_url' => $baseURL . '/hcard/' . $owner['nickname'],
'$atom' => $baseURL . '/dfrn_poll/' . $user['nickname'], '$atom' => $owner['poll'],
'$poco_url' => $baseURL . '/poco/' . $user['nickname'], '$poco_url' => $owner['poco'],
'$photo' => $baseURL . '/photo/profile/' . $user['uid'] . '.jpg', '$photo' => $owner['photo'],
'$salmon' => $baseURL . '/salmon/' . $user['nickname'], '$type' => $avatar['type'],
'$salmen' => $baseURL . '/salmon/' . $user['nickname'] . '/mention', '$salmon' => $baseURL . '/salmon/' . $owner['nickname'],
'$salmen' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
'$subscribe' => $baseURL . '/follow?url={uri}', '$subscribe' => $baseURL . '/follow?url={uri}',
'$openwebauth' => $baseURL . '/owa', '$openwebauth' => $baseURL . '/owa',
'$modexp' => 'data:application/magic-public-key,' . $salmon_key '$modexp' => 'data:application/magic-public-key,' . $salmon_key

View file

@ -229,11 +229,6 @@ class Transmitter
return []; return [];
} }
// On old installations and never changed contacts this might not be filled
if (empty($contact['avatar'])) {
$contact['avatar'] = $contact['photo'];
}
$data = ['@context' => ActivityPub::CONTEXT]; $data = ['@context' => ActivityPub::CONTEXT];
$data['id'] = $contact['url']; $data['id'] = $contact['url'];
$data['diaspora:guid'] = $user['guid']; $data['diaspora:guid'] = $user['guid'];
@ -254,7 +249,7 @@ class Transmitter
'publicKeyPem' => $user['pubkey']]; 'publicKeyPem' => $user['pubkey']];
$data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox']; $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
$data['icon'] = ['type' => 'Image', $data['icon'] = ['type' => 'Image',
'url' => $contact['avatar']]; 'url' => $contact['photo']];
$data['generator'] = self::getService(); $data['generator'] = self::getService();

View file

@ -1,31 +0,0 @@
<entry>
<author>
<name>{{$name}}</name>
<uri>{{$profile_page}}</uri>
<link rel="photo" type="image/jpeg" media:width="80" media:height="80" href="{{$thumb}}" />
<link rel="avatar" type="image/jpeg" media:width="80" media:height="80" href="{{$thumb}}" />
</author>
<id>{{$item_id}}</id>
<title>{{$title}}</title>
<published>{{$published}}</published>
<content type="{{$type}}" >{{$content nofilter}}</content>
<link rel="mentioned" href="{{$accturi}}" />
<as:actor>
<as:object-type>http://activitystrea.ms/schema/1.0/person</as:object-type>
<id>{{$profile_page}}</id>
<title></title>
<link rel="avatar" type="image/jpeg" media:width="175" media:height="175" href="{{$photo}}"/>
<link rel="avatar" type="image/jpeg" media:width="80" media:height="80" href="{{$thumb}}"/>
<poco:preferredUsername>{{$nick}}</poco:preferredUsername>
<poco:displayName>{{$name}}</poco:displayName>
</as:actor>
<as:verb>{{$verb}}</as:verb>
<as:object>
<as:object-type></as:object-type>
</as:object>
<as:target>
<as:object-type></as:object-type>
</as:target>
</entry>

View file

@ -18,7 +18,7 @@
<Link rel="http://portablecontacts.net/spec/1.0" <Link rel="http://portablecontacts.net/spec/1.0"
href="{{$poco_url}}" /> href="{{$poco_url}}" />
<Link rel="http://webfinger.net/rel/avatar" <Link rel="http://webfinger.net/rel/avatar"
type="image/jpeg" type="{{$type}}"
href="{{$photo}}" /> href="{{$photo}}" />
<Link rel="http://joindiaspora.com/seed_location" <Link rel="http://joindiaspora.com/seed_location"
type="text/html" type="text/html"