Add Attachments to remote notices
Fix Delete Notice
This commit is contained in:
parent
f25d8278b1
commit
442e66d112
@ -55,6 +55,8 @@ const ACTIVITYPUB_PUBLIC_TO = ['https://www.w3.org/ns/activitystreams#Public',
|
||||
*/
|
||||
class ActivityPubPlugin extends Plugin
|
||||
{
|
||||
public static $store_images_from_remote_notes_attachments = true;
|
||||
|
||||
/**
|
||||
* Returns a Actor's URI from its local $profile
|
||||
* Works both for local and remote users.
|
||||
|
@ -51,6 +51,9 @@ if (isset($res['latitude'])) {
|
||||
if (isset($res['longitude'])) {
|
||||
$settings['longitude'] = $res['longitude'];
|
||||
}
|
||||
if (isset($res['attachment'])) {
|
||||
$settings['attachment'] = $res['attachment'];
|
||||
}
|
||||
|
||||
try {
|
||||
Activitypub_notice::create_notice(
|
||||
|
@ -30,7 +30,7 @@ if (!defined('GNUSOCIAL')) {
|
||||
}
|
||||
|
||||
try {
|
||||
$notice = ActivityPubPlugin::grab_notice_from_url($data['object']['id']);
|
||||
$notice = ActivityPubPlugin::grab_notice_from_url($data['object']);
|
||||
$notice->deleteAs($actor_profile);
|
||||
ActivityPubReturn::answer();
|
||||
} catch (Exception $e) {
|
||||
|
@ -47,13 +47,13 @@ class Activitypub_delete extends Managed_DataObject
|
||||
* @param array $object
|
||||
* @return pretty array to be used in a response
|
||||
*/
|
||||
public static function delete_to_array($object)
|
||||
public static function delete_to_array($actor, $object)
|
||||
{
|
||||
$res = [
|
||||
'@context' => 'https://www.w3.org/ns/activitystreams',
|
||||
'id' => $object['id'].'/delete',
|
||||
'id' => $object.'/delete',
|
||||
'type' => 'Delete',
|
||||
'actor' => $object['actor'],
|
||||
'actor' => $actor,
|
||||
'object' => $object
|
||||
];
|
||||
return $res;
|
||||
|
@ -116,7 +116,7 @@ class Activitypub_notice extends Managed_DataObject
|
||||
* @param string $url
|
||||
* @param string $content
|
||||
* @param array|string $cc
|
||||
* @param array $settings possible keys: ['inReplyTo', 'latitude', 'longitude']
|
||||
* @param array $settings possible keys: ['inReplyTo', 'latitude', 'longitude', 'attachment']
|
||||
* @return Notice
|
||||
* @throws Exception
|
||||
*/
|
||||
@ -126,8 +126,44 @@ class Activitypub_notice extends Managed_DataObject
|
||||
$act->verb = ActivityVerb::POST;
|
||||
$act->time = time();
|
||||
$act->actor = $actor_profile->asActivityObject();
|
||||
|
||||
$act->context = new ActivityContext();
|
||||
$options = ['source' => 'ActivityPub', 'uri' => $id, 'url' => $url];
|
||||
|
||||
// Do we have an attachment?
|
||||
if (isset($settings['attachment'][0])) {
|
||||
$attach = $settings['attachment'][0];
|
||||
$attach_url = $settings['attachment'][0]['url'];
|
||||
// Is it an image?
|
||||
if (ActivityPubPlugin::$store_images_from_remote_notes_attachments && substr($attach["mediaType"], 0, 5) == "image") {
|
||||
$temp_filename = tempnam(sys_get_temp_dir(), 'apCreateNoteAttach_');
|
||||
try {
|
||||
$imgData = HTTPClient::quickGet($attach_url);
|
||||
// Make sure it's at least an image file. ImageFile can do the rest.
|
||||
if (false === getimagesizefromstring($imgData)) {
|
||||
common_debug('ActivityPub Create Notice: Failed because the downloaded image: '.$attach_url. 'is not valid.');
|
||||
throw new UnsupportedMediaException('Downloaded image was not an image.');
|
||||
}
|
||||
file_put_contents($temp_filename, $imgData);
|
||||
unset($imgData); // No need to carry this in memory.
|
||||
common_debug('ActivityPub Create Notice: Stored dowloaded image in: '.$temp_filename);
|
||||
|
||||
$id = $actor_profile->getID();
|
||||
|
||||
$imagefile = new ImageFile(null, $temp_filename);
|
||||
$filename = hash(File::FILEHASH_ALG, $imgData).image_type_to_extension($imagefile->type);
|
||||
rename($temp_filename, File::path($filename));
|
||||
common_debug('ActivityPub Create Notice: Moved image from: '.$temp_filename.' to '.$filename);
|
||||
$mediaFile = new MediaFile($filename, $attach['mediaType']);
|
||||
$act->enclosures[] = $mediaFile->getEnclosure();
|
||||
} catch (Exception $e) {
|
||||
common_debug('ActivityPub Create Notice: Something went wrong while processing the image from: '.$attach_url.' details: '.$e->getMessage());
|
||||
unlink($temp_filename);
|
||||
$content .= ($content==='' ? '' : ' ') . $attach_url;
|
||||
}
|
||||
} else {
|
||||
$content .= ($content==='' ? '' : ' ') . $attach_url;
|
||||
}
|
||||
}
|
||||
|
||||
// Is this a reply?
|
||||
if (isset($settings['inReplyTo'])) {
|
||||
@ -181,8 +217,6 @@ class Activitypub_notice extends Managed_DataObject
|
||||
//throw new Exception('That\'s too long. Maximum notice size is %d character.');
|
||||
}
|
||||
|
||||
$options = ['source' => 'ActivityPub', 'uri' => $id, 'url' => $url];
|
||||
|
||||
$actobj = new ActivityObject();
|
||||
$actobj->type = ActivityObject::NOTE;
|
||||
$actobj->content = strip_tags($content, '<p><b><i><u><a><ul><ol><li>');
|
||||
@ -191,7 +225,11 @@ class Activitypub_notice extends Managed_DataObject
|
||||
$act->objects[] = $actobj;
|
||||
|
||||
try {
|
||||
return Notice::saveActivity($act, $actor_profile, $options);
|
||||
$note = Notice::saveActivity($act, $actor_profile, $options);
|
||||
if (ActivityPubPlugin::$store_images_from_remote_notes_attachments && isset($mediaFile)) {
|
||||
$mediaFile->attachToNotice($note);
|
||||
}
|
||||
return $note;
|
||||
} catch (Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
@ -294,7 +294,7 @@ class Activitypub_explorer
|
||||
$imgData = HTTPClient::quickGet($url);
|
||||
// Make sure it's at least an image file. ImageFile can do the rest.
|
||||
if (false === getimagesizefromstring($imgData)) {
|
||||
common_debug('ActivityPub Explorer: Failed because the downloaded avatar: '.$url. 'is not a validad image.');
|
||||
common_debug('ActivityPub Explorer: Failed because the downloaded avatar: '.$url. 'is not a valid image.');
|
||||
throw new UnsupportedMediaException('Downloaded avatar was not an image.');
|
||||
}
|
||||
file_put_contents($temp_filename, $imgData);
|
||||
|
@ -244,15 +244,18 @@ class Activitypub_postman
|
||||
*/
|
||||
public function delete($notice)
|
||||
{
|
||||
$data = Activitypub_delete::delete_to_array(Activitypub_notice::notice_to_array($notice));
|
||||
$data = Activitypub_delete::delete_to_array(
|
||||
ActivityPubPlugin::actor_uri($notice->getProfile()),
|
||||
$notice->getUrl()
|
||||
);
|
||||
$errors = [];
|
||||
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
||||
foreach ($this->to_inbox() as $inbox) {
|
||||
$res = $this->send($data, $inbox);
|
||||
if (!$res->getStatusCode() == 200) {
|
||||
$res_body = json_decode($res->getBody()->getContents());
|
||||
if (isset($res_body[0]->error)) {
|
||||
$errors[] = ($res_body[0]->error);
|
||||
$res_body = json_decode($res->getBody()->getContents(), true);
|
||||
if (isset($res_body[0]['error'])) {
|
||||
$errors[] = ($res_body[0]['error']);
|
||||
continue;
|
||||
}
|
||||
$errors[] = ("An unknown error occurred.");
|
||||
|
Reference in New Issue
Block a user