AS output/input for poll responses

This commit is contained in:
Brion Vibber 2011-03-10 13:41:23 -08:00
parent e085ef5ef5
commit d6c8c61076
3 changed files with 96 additions and 9 deletions

View File

@ -128,6 +128,23 @@ class Poll extends Managed_DataObject
return explode("\n", $this->options); 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() function getNotice()
{ {
return Notice::staticGet('uri', $this->uri); return Notice::staticGet('uri', $this->uri);

View File

@ -207,9 +207,10 @@ class PollPlugin extends MicroAppPlugin
// Ok for now, we can grab stuff from the XML entry directly. // Ok for now, we can grab stuff from the XML entry directly.
// This won't work when reading from JSON source // This won't work when reading from JSON source
if ($activity->entry) { if ($activity->entry) {
$elements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'data'); $pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll');
if ($elements->length) { $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response');
$data = $elements->item(0); if ($dataElements->length) {
$data = $dataElements->item(0);
$question = $data->getAttribute('question'); $question = $data->getAttribute('question');
$opts = array(); $opts = array();
foreach ($data->attributes as $node) { foreach ($data->attributes as $node) {
@ -230,6 +231,25 @@ class PollPlugin extends MicroAppPlugin
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_DEBUG, "YYY fail: " . $e->getMessage()); 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 { } else {
common_log(LOG_DEBUG, "YYY no poll data"); common_log(LOG_DEBUG, "YYY no poll data");
} }
@ -240,11 +260,61 @@ class PollPlugin extends MicroAppPlugin
{ {
assert($this->isMyNotice($notice)); 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 = new ActivityObject();
$object->id = $notice->uri; $object->id = $notice->uri;
$object->type = self::POLL_OBJECT; $object->type = self::POLL_OBJECT;
$object->title = 'Poll title'; $object->title = $notice->content;
$object->summary = 'Poll summary'; $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(); $object->link = $notice->bestUrl();
$poll = Poll::getByNotice($notice); $poll = Poll::getByNotice($notice);
@ -262,7 +332,7 @@ class PollPlugin extends MicroAppPlugin
* option2="Option two" * option2="Option two"
* option3="Option three"></poll:data> * option3="Option three"></poll:data>
* *
* "poll:data": { * "poll:response": {
* "xmlns:poll": http://apinamespace.org/activitystreams/object/poll * "xmlns:poll": http://apinamespace.org/activitystreams/object/poll
* "question": "Who wants a poll question?" * "question": "Who wants a poll question?"
* "option1": "Option one" * "option1": "Option one"
@ -279,7 +349,7 @@ class PollPlugin extends MicroAppPlugin
foreach ($poll->getOptions() as $i => $opt) { foreach ($poll->getOptions() as $i => $opt) {
$data['option' . ($i + 1)] = $opt; $data['option' . ($i + 1)] = $opt;
} }
$object->extra[] = array('poll:data', $data, ''); $object->extra[] = array('poll:poll', $data, '');
return $object; return $object;
} }

View File

@ -158,10 +158,10 @@ class Poll_response extends Managed_DataObject
$options = array(); $options = array();
} }
$opts = $poll->getOptions(); if (!$poll->isValidSelection($selection)) {
if ($selection < 1 || $selection > count($opts)) {
throw new ClientException(_m('Invalid poll selection.')); throw new ClientException(_m('Invalid poll selection.'));
} }
$opts = $poll->getOptions();
$answer = $opts[$selection - 1]; $answer = $opts[$selection - 1];
$pr = new Poll_response(); $pr = new Poll_response();