Skip to content

Commit

Permalink
Merge pull request #1535 from cweiske/annotation-micropub
Browse files Browse the repository at this point in the history
Support annotation creation via micropub (like/reply(comment))
  • Loading branch information
benwerd committed Sep 25, 2016
2 parents 1d95fc7 + 10f0fa5 commit 2281676
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions IdnoPlugins/IndiePub/Pages/MicroPub/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ function post()

// Get details
$type = $this->getInput('h', 'entry');
if ($type == 'annotation') {
return $this->postCreateAnnotation();
}

$content = $this->getInput('content');
$name = $this->getInput('name');
$in_reply_to = $this->getInput('in-reply-to');
Expand Down Expand Up @@ -250,6 +254,67 @@ function post()
}
}

/**
* Add a "like" or a "reply" to a post
*/
function postCreateAnnotation()
{
$url = $this->getInput('url');
$content = $this->getInput('content');
$type = $this->getInput('type');
$username = $this->getInput('username');
$userurl = $this->getInput('userurl');
$userphoto = $this->getInput('userphoto');

$notEmpty = array('url', 'type', 'username', 'userurl', 'userphoto');
foreach ($notEmpty as $varName) {
if ($$varName == '') {
$this->error(
400, 'invalid_request',
'"' . $varName . '" must not be empty'
);
}
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$this->error(400, 'invalid_request', 'URL is invalid');
}
$entity = \Idno\Common\Entity::getByUUID($url);
if ($entity === false) {
$this->error(400, 'not_found');
}

if ($type !== 'like' && $type !== 'reply') {
$this->error(
400, 'invalid_request',
'Annotation type must be "like" or "reply"'
);
}
if ($type === 'reply' && $content == '') {
$this->error(
400, 'invalid_request', '"content" must not be empty'
);
}
if (!filter_var($userurl, FILTER_VALIDATE_URL)) {
$this->error(400, 'invalid_request', '"userurl" is invalid');
}
if (!filter_var($userphoto, FILTER_VALIDATE_URL)) {
$this->error(400, 'invalid_request', '"userphoto" is invalid');
}

$ok = $entity->addAnnotation(
$type, $username, $userurl, $userphoto, $content
);
if (!$ok) {
$this->error(
500, 'internal_error',
'Saving annotation failed'
);
}
$entity->save();
$this->setResponse(204);
exit();
}

/**
* Micropub optionally allows uploading photos from a
* URL. This method downloads the file at a URL to a
Expand Down

0 comments on commit 2281676

Please sign in to comment.