AtomPub should work now, at least for post/note

This commit is contained in:
Mikael Nordfeldth 2015-10-09 16:19:26 +02:00
parent 2dfd34b12d
commit dd149a76f3
2 changed files with 20 additions and 150 deletions

View File

@ -319,8 +319,8 @@ class ApiTimelineUserAction extends ApiBareAuthAction
function handlePost() function handlePost()
{ {
if (empty($this->scoped) || if (!$this->scoped instanceof Profile ||
$this->target->sameAs($this->scoped)) { !$this->target->sameAs($this->scoped)) {
// TRANS: Client error displayed trying to add a notice to another user's timeline. // TRANS: Client error displayed trying to add a notice to another user's timeline.
$this->clientError(_('Only the user can add to their own timeline.'), 403); $this->clientError(_('Only the user can add to their own timeline.'), 403);
} }
@ -354,156 +354,28 @@ class ApiTimelineUserAction extends ApiBareAuthAction
$activity = new Activity($dom->documentElement); $activity = new Activity($dom->documentElement);
$saved = null; common_debug('AtomPub: Ignoring right now, but this POST was made to collection: '.$activity->id);
if (Event::handle('StartAtomPubNewActivity', array(&$activity, $this->target->getUser(), &$saved))) { // Reset activity data so we can handle it in the same functions as with OStatus
if ($activity->verb != ActivityVerb::POST) { // because we don't let clients set their own UUIDs... Not sure what AtomPub thinks
// TRANS: Client error displayed when not using the POST verb. Do not translate POST. // about that though.
$this->clientError(_('Can only handle POST activities.')); $activity->id = null;
} $activity->actor = null; // not used anyway, we use $this->target
$activity->objects[0]->id = null;
$note = $activity->objects[0]; $stored = null;
if (Event::handle('StartAtomPubNewActivity', array($activity, $this->target, &$stored))) {
if (!in_array($note->type, array(ActivityObject::NOTE, // TRANS: Client error displayed when not using the POST verb. Do not translate POST.
ActivityObject::BLOGENTRY, throw new ClientException(_('Could not handle this Atom Activity.'));
ActivityObject::STATUS))) {
// TRANS: Client error displayed when using an unsupported activity object type.
// TRANS: %s is the unsupported activity object type.
$this->clientError(sprintf(_('Cannot handle activity object type "%s".'),
$note->type));
}
$saved = $this->postNote($activity);
Event::handle('EndAtomPubNewActivity', array($activity, $this->target->getUser(), $saved));
} }
if (!$stored instanceof Notice) {
if (!empty($saved)) { throw new ServerException('Server did not create a Notice object from handled AtomPub activity.');
header('HTTP/1.1 201 Created');
header("Location: " . common_local_url('ApiStatusesShow', array('id' => $saved->id,
'format' => 'atom')));
$this->showSingleAtomStatus($saved);
} }
} Event::handle('EndAtomPubNewActivity', array($activity, $this->target, $stored));
function postNote($activity) header('HTTP/1.1 201 Created');
{ header("Location: " . common_local_url('ApiStatusesShow', array('id' => $stored->getID(),
$note = $activity->objects[0]; 'format' => 'atom')));
$this->showSingleAtomStatus($stored);
// Use summary as fallback for content
if (!empty($note->content)) {
$sourceContent = $note->content;
} else if (!empty($note->summary)) {
$sourceContent = $note->summary;
} else if (!empty($note->title)) {
$sourceContent = $note->title;
} else {
// @fixme fetch from $sourceUrl?
// TRANS: Client error displayed when posting a notice without content through the API.
// TRANS: %d is the notice ID (number).
$this->clientError(sprintf(_('No content for notice %d.'), $note->id));
}
// Get (safe!) HTML and text versions of the content
$rendered = common_purify($sourceContent);
$content = common_strip_html($rendered);
$shortened = $this->target->shortenLinks($content);
$options = array('is_local' => Notice::LOCAL_PUBLIC,
'rendered' => $rendered,
'replies' => array(),
'groups' => array(),
'tags' => array(),
'urls' => array());
// accept remote URI (not necessarily a good idea)
common_debug("Note ID is {$note->id}");
if (!empty($note->id)) {
$notice = Notice::getKV('uri', trim($note->id));
if (!empty($notice)) {
// TRANS: Client error displayed when using another format than AtomPub.
// TRANS: %s is the notice URI.
$this->clientError(sprintf(_('Notice with URI "%s" already exists.'), $note->id));
}
common_log(LOG_NOTICE, "Saving client-supplied notice URI '$note->id'");
$options['uri'] = $note->id;
}
// accept remote create time (also maybe not such a good idea)
if (!empty($activity->time)) {
common_log(LOG_NOTICE, "Saving client-supplied create time {$activity->time}");
$options['created'] = common_sql_date($activity->time);
}
// Check for optional attributes...
if ($activity->context instanceof ActivityContext) {
foreach ($activity->context->attention as $uri=>$type) {
try {
$profile = Profile::fromUri($uri);
if ($profile->isGroup()) {
$options['groups'][] = $profile->id;
} else {
$options['replies'][] = $uri;
}
} catch (UnknownUriException $e) {
common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri));
}
}
// Maintain direct reply associations
// @fixme what about conversation ID?
if (!empty($activity->context->replyToID)) {
$orig = Notice::getKV('uri',
$activity->context->replyToID);
if (!empty($orig)) {
$options['reply_to'] = $orig->id;
}
}
$location = $activity->context->location;
if ($location) {
$options['lat'] = $location->lat;
$options['lon'] = $location->lon;
if ($location->location_id) {
$options['location_ns'] = $location->location_ns;
$options['location_id'] = $location->location_id;
}
}
}
// Atom categories <-> hashtags
foreach ($activity->categories as $cat) {
if ($cat->term) {
$term = common_canonical_tag($cat->term);
if ($term) {
$options['tags'][] = $term;
}
}
}
// Atom enclosures -> attachment URLs
foreach ($activity->enclosures as $href) {
// @fixme save these locally or....?
$options['urls'][] = $href;
}
$saved = Notice::saveNew($this->target->getID(),
$content,
'atompub', // TODO: deal with this
$options);
return $saved;
} }
} }

View File

@ -452,8 +452,6 @@ abstract class ActivityHandlerPlugin extends Plugin
$notice = $this->saveNoticeFromActivity($activity, $scoped, $options); $notice = $this->saveNoticeFromActivity($activity, $scoped, $options);
Event::handle('EndAtomPubNewActivity', array($activity, $scoped, $notice));
return false; return false;
} }