Add Remote Avatar

This commit is contained in:
Diogo Cordeiro 2018-08-01 21:24:36 +01:00
parent 12e5dc4b59
commit 9f61de3c87
4 changed files with 77 additions and 21 deletions

View File

@ -84,7 +84,7 @@ if (isset($data->object->cc) && is_array($data->object->cc)) {
try { try {
$to_profiles = array_merge($to_profiles, $discovery->lookup($cc_url)); $to_profiles = array_merge($to_profiles, $discovery->lookup($cc_url));
} catch (Exception $e) { } catch (Exception $e) {
// Invalid actor found, just let it go. // Invalid actor found, just let it go. // TODO: Fallback to OStatus
} }
} }
} elseif (empty($data->object->cc) || in_array($data->object->cc, $public_to)) { } elseif (empty($data->object->cc) || in_array($data->object->cc, $public_to)) {
@ -93,7 +93,7 @@ if (isset($data->object->cc) && is_array($data->object->cc)) {
try { try {
$to_profiles = array_merge($to_profiles, $discovery->lookup($data->object->cc)); $to_profiles = array_merge($to_profiles, $discovery->lookup($data->object->cc));
} catch (Exception $e) { } catch (Exception $e) {
// Invalid actor found, just let it go. // Invalid actor found, just let it go. // TODO: Fallback to OStatus
} }
} }

View File

@ -50,26 +50,19 @@ class Activitypub_attachment extends Managed_DataObject
public static function attachment_to_array($attachment) public static function attachment_to_array($attachment)
{ {
$res = [ $res = [
'@context' => [ 'type' => 'Document',
"https://www.w3.org/ns/activitystreams", 'mediaType' => $attachment->mimetype,
[ 'url' => $attachment->getUrl(),
"@language" => "en" 'size' => intval($attachment->size), // $attachment->getSize ()
] 'name' => $attachment->getTitle(),
], ];
'id' => $attachment->getID(),
'mimetype' => $attachment->mimetype,
'url' => $attachment->getUrl(),
'size' => intval($attachment->size), // $attachment->getSize ()
'title' => $attachment->getTitle(),
'meta' => null
];
// Image // Image
if (substr($res["mimetype"], 0, 5) == "image") { if (substr($res["mediaType"], 0, 5) == "image") {
$res["meta"]= [ $res["meta"]= [
'width' => $attachment->width, 'width' => $attachment->width,
'height' => $attachment->height 'height' => $attachment->height
]; ];
} }
return $res; return $res;

View File

@ -120,8 +120,10 @@ class Activitypub_profile extends Managed_DataObject
'attachment' => [], 'attachment' => [],
'icon' => [ 'icon' => [
'type' => 'Image', 'type' => 'Image',
'mediaType' => 'image/jpeg', 'mediaType' => 'image/png',
'url' => $profile->avatarUrl() 'height' => AVATAR_PROFILE_SIZE,
'width' => AVATAR_PROFILE_SIZE,
'url' => $profile->avatarUrl(AVATAR_PROFILE_SIZE)
] ]
]; ];

View File

@ -241,9 +241,70 @@ class Activitypub_explorer
$apRSA->public_key = $res['publicKey']['publicKeyPem']; $apRSA->public_key = $res['publicKey']['publicKeyPem'];
$apRSA->store_keys(); $apRSA->store_keys();
// Avatar
if (isset($res['icon']['url'])) {
$this->_store_avatar($profile, $res['icon']['url']);
}
return $profile; return $profile;
} }
/**
* Download and update given avatar image
*
* @author GNU Social
* @param string $url
* @return Avatar The Avatar we have on disk. (seldom used)
* @throws Exception in various failure cases
*/
private function _store_avatar($profile, $url)
{
if (!common_valid_http_url($url)) {
// TRANS: Server exception. %s is a URL.
throw new ServerException(sprintf('Invalid avatar URL %s.'), $url);
}
// @todo FIXME: This should be better encapsulated
// ripped from oauthstore.php (for old OMB client)
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
try {
$imgData = HTTPClient::quickGet($url);
// Make sure it's at least an image file. ImageFile can do the rest.
if (false === getimagesizefromstring($imgData)) {
throw new UnsupportedMediaException('Downloaded group avatar was not an image.');
}
file_put_contents($temp_filename, $imgData);
unset($imgData); // No need to carry this in memory.
$id = $profile->getID();
$imagefile = new ImageFile(null, $temp_filename);
$filename = Avatar::filename($id,
image_type_to_extension($imagefile->type),
null,
common_timestamp());
rename($temp_filename, Avatar::path($filename));
} catch (Exception $e) {
unlink($temp_filename);
throw $e;
}
// @todo FIXME: Hardcoded chmod is lame, but seems to be necessary to
// keep from accidentally saving images from command-line (queues)
// that can't be read from web server, which causes hard-to-notice
// problems later on:
//
// http://status.net/open-source/issues/2663
chmod(Avatar::path($filename), 0644);
$profile->setOriginal($filename);
$orig = clone($profile);
$profile->avatar = $url;
$profile->update($orig);
return Avatar::getUploaded($profile);
}
/** /**
* Validates a remote response in order to determine whether this * Validates a remote response in order to determine whether this
* response is a valid profile or not * response is a valid profile or not