We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit d6c8c610 authored by Brion Vibber's avatar Brion Vibber

AS output/input for poll responses

parent e085ef5e
......@@ -128,6 +128,23 @@ class Poll extends Managed_DataObject
return explode("\n", $this->options);
}
/**
* Is this a valid selection index?
*
* @param numeric $selection (1-based)
* @return boolean
*/
function isValidSelection($selection)
{
if ($selection != intval($selection)) {
return false;
}
if ($selection < 1 || $selection > count($this->getOptions())) {
return false;
}
return true;
}
function getNotice()
{
return Notice::staticGet('uri', $this->uri);
......
......@@ -207,9 +207,10 @@ class PollPlugin extends MicroAppPlugin
// Ok for now, we can grab stuff from the XML entry directly.
// This won't work when reading from JSON source
if ($activity->entry) {
$elements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'data');
if ($elements->length) {
$data = $elements->item(0);
$pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll');
$responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response');
if ($dataElements->length) {
$data = $dataElements->item(0);
$question = $data->getAttribute('question');
$opts = array();
foreach ($data->attributes as $node) {
......@@ -230,6 +231,25 @@ class PollPlugin extends MicroAppPlugin
} catch (Exception $e) {
common_log(LOG_DEBUG, "YYY fail: " . $e->getMessage());
}
} else if ($responseElements->length) {
$data = $responseElements->item(0);
$pollUri = $data->getAttribute('poll');
$selection = intval($data->getAttribute('selection'));
if (!$pollUri) {
throw new Exception('Invalid poll response: no poll reference.');
}
$poll = Poll::staticGet('uri', $pollUri);
if (!$poll) {
throw new Exception('Invalid poll response: poll is unknown.');
}
try {
$notice = Poll_response::saveNew($profile, $poll, $selection, $options);
common_log(LOG_DEBUG, "YYY response ok: " . $notice->id);
return $notice;
} catch (Exception $e) {
common_log(LOG_DEBUG, "YYY response fail: " . $e->getMessage());
}
} else {
common_log(LOG_DEBUG, "YYY no poll data");
}
......@@ -240,11 +260,61 @@ class PollPlugin extends MicroAppPlugin
{
assert($this->isMyNotice($notice));
switch ($notice->object_type) {
case self::POLL_OBJECT:
return $this->activityObjectFromNoticePoll($notice);
case self::POLL_RESPONSE_OBJECT:
return $this->activityObjectFromNoticePollResponse($notice);
default:
throw new Exception('Unexpected type for poll plugin: ' . $notice->object_type);
}
}
function activityObjectFromNoticePoll($notice)
{
$object = new ActivityObject();
$object->id = $notice->uri;
$object->type = self::POLL_OBJECT;
$object->title = 'Poll title';
$object->summary = 'Poll summary';
$object->title = $notice->content;
$object->summary = $notice->content;
$object->link = $notice->bestUrl();
$response = Poll_response::getByNotice($notice);
$poll = $response->getPoll();
/**
* For the moment, using a kind of icky-looking schema that happens to
* work with out code for generating both Atom and JSON forms, though
* I don't like it:
*
* <poll:response xmlns:poll="http://apinamespace.org/activitystreams/object/poll"
* poll="http://..../poll/...."
* selection="3" />
*
* "poll:response": {
* "xmlns:poll": http://apinamespace.org/activitystreams/object/poll
* "uri": "http://..../poll/...."
* "selection": 3
* }
*
*/
// @fixme there's no way to specify an XML node tree here, like <poll><option/><option/></poll>
// @fixme there's no way to specify a JSON array or multi-level tree unless you break the XML attribs
// @fixme XML node contents don't get shown in JSON
$data = array('xmlns:poll' => self::POLL_OBJECT,
'poll' => $poll->uri,
'selection' => intval($response->selection));
$object->extra[] = array('poll:response', $data, '');
return $object;
}
function activityObjectFromNoticePollResponse($notice)
{
$object = new ActivityObject();
$object->id = $notice->uri;
$object->type = self::POLL_RESPONSE_OBJECT;
$object->title = $notice->content;
$object->summary = $notice->content;
$object->link = $notice->bestUrl();
$poll = Poll::getByNotice($notice);
......@@ -262,7 +332,7 @@ class PollPlugin extends MicroAppPlugin
* option2="Option two"
* option3="Option three"></poll:data>
*
* "poll:data": {
* "poll:response": {
* "xmlns:poll": http://apinamespace.org/activitystreams/object/poll
* "question": "Who wants a poll question?"
* "option1": "Option one"
......@@ -279,7 +349,7 @@ class PollPlugin extends MicroAppPlugin
foreach ($poll->getOptions() as $i => $opt) {
$data['option' . ($i + 1)] = $opt;
}
$object->extra[] = array('poll:data', $data, '');
$object->extra[] = array('poll:poll', $data, '');
return $object;
}
......
......@@ -158,10 +158,10 @@ class Poll_response extends Managed_DataObject
$options = array();
}
$opts = $poll->getOptions();
if ($selection < 1 || $selection > count($opts)) {
if (!$poll->isValidSelection($selection)) {
throw new ClientException(_m('Invalid poll selection.'));
}
$opts = $poll->getOptions();
$answer = $opts[$selection - 1];
$pr = new Poll_response();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment