From 4915b0cb9ef71e9efadea26ada7a3a936f48cccc Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 9 Mar 2011 18:01:41 -0800 Subject: [PATCH 01/13] Update Autocomplete plugin for reusable notice forms in 1.0.x. It's fun! Now pulls the fetch API URL from a dedicated with a data attribute ('data-url', should pass HTML 5 validators for our future overlords) --- plugins/Autocomplete/Autocomplete.js | 21 ++++++++++++++++++--- plugins/Autocomplete/AutocompletePlugin.php | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/plugins/Autocomplete/Autocomplete.js b/plugins/Autocomplete/Autocomplete.js index c3f022702a..2b031cc313 100644 --- a/plugins/Autocomplete/Autocomplete.js +++ b/plugins/Autocomplete/Autocomplete.js @@ -1,4 +1,15 @@ -$(document).ready(function(){ +(function(SN, $) { + +var origInit = SN.Init.NoticeFormSetup; +SN.Init.NoticeFormSetup = function(form) { + origInit(form); + + // Only attach to traditional-style forms + var textarea = form.find('.notice_data-text:first'); + if (textarea.length == 0) { + return; + } + function fullName(row) { if (typeof row.fullname == "string" && row.fullname != '') { return row.nickname + ' (' + row.fullname + ')'; @@ -6,7 +17,9 @@ $(document).ready(function(){ return row.nickname; } } - $('#notice_data-text').autocomplete($('address .url')[0].href+'main/autocomplete/suggest', { + + var apiUrl = $('#autocomplete-api').attr('data-url'); + textarea.autocomplete(apiUrl, { multiple: true, multipleSeparator: " ", minChars: 1, @@ -35,4 +48,6 @@ $(document).ready(function(){ } } }); -}); +}; + +})(SN, jQuery); diff --git a/plugins/Autocomplete/AutocompletePlugin.php b/plugins/Autocomplete/AutocompletePlugin.php index ca495f79f4..1479ead602 100644 --- a/plugins/Autocomplete/AutocompletePlugin.php +++ b/plugins/Autocomplete/AutocompletePlugin.php @@ -51,6 +51,8 @@ class AutocompletePlugin extends Plugin function onEndShowScripts($action){ if (common_logged_in()) { + $action->element('span', array('id' => 'autocomplete-api', + 'data-url' => common_local_url('autocomplete'))); $action->script($this->path('jquery-autocomplete/jquery.autocomplete.pack.js')); $action->script($this->path('Autocomplete.js')); } From 662ad0f7bb3abb576d4ec188c11e235bf777a99b Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 9 Mar 2011 18:11:08 -0800 Subject: [PATCH 02/13] Update DirectionDetector for reusable notice forms. Also tweaked it to trigger on cut/paste, and to reset when the form resets. --- .../jquery.DirectionDetector.js | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/plugins/DirectionDetector/jquery.DirectionDetector.js b/plugins/DirectionDetector/jquery.DirectionDetector.js index 6808bcabd8..840cd69097 100644 --- a/plugins/DirectionDetector/jquery.DirectionDetector.js +++ b/plugins/DirectionDetector/jquery.DirectionDetector.js @@ -47,15 +47,26 @@ return true; return false; }; - $(document).ready(function(){ - var tArea = $("#notice_data-text"); - var tCleaner = new RegExp('@[^ ]+|![^ ]+|#[^ ]+|^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]+', 'g') - tArea.keyup(function(){ - var cleaned = tArea.val().replace(tCleaner, '').replace(/^[ ]+/, ''); - if($().isRTL(cleaned)) - tArea.css('direction', 'rtl'); - else - tArea.css('direction', 'ltr'); - }); - }); + var origInit = SN.Init.NoticeFormSetup; + SN.Init.NoticeFormSetup = function(form) { + origInit(form); + var tArea = form.find(".notice_data-text:first"); + if (tArea.length > 0) { + var tCleaner = new RegExp('@[^ ]+|![^ ]+|#[^ ]+|^RT[: ]{1}| RT | RT: |^RD[: ]{1}| RD | RD: |[♺♻:]+', 'g') + var ping = function(){ + var cleaned = tArea.val().replace(tCleaner, '').replace(/^[ ]+/, ''); + if($().isRTL(cleaned)) + tArea.css('direction', 'rtl'); + else + tArea.css('direction', 'ltr'); + }; + tArea.bind('keyup cut paste', function() { + // cut/paste trigger before the change + window.setTimeout(ping, 0); + }); + form.bind('reset', function() { + tArea.css('direction', 'ltr'); + }); + } + }; })(jQuery); From b653b2fb6434e6c99ffe2186469a0d4d01b1fdea Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 9 Mar 2011 18:29:10 -0800 Subject: [PATCH 03/13] Update TinyMCE plugin for reusable thingies; doesn't quite arrange properly in the layout, we'll have to figure that out still. --- plugins/TinyMCE/TinyMCEPlugin.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/plugins/TinyMCE/TinyMCEPlugin.php b/plugins/TinyMCE/TinyMCEPlugin.php index 49bbdf90db..4051ff59ee 100644 --- a/plugins/TinyMCE/TinyMCEPlugin.php +++ b/plugins/TinyMCE/TinyMCEPlugin.php @@ -290,9 +290,14 @@ class TinyMCEPlugin extends Plugin // our AJAX form submission. Manually moving it to trigger // on our send button click. $scr = <<'); - $('#notice_action-submit').click(function() { + form.find('.submit:first').click(function() { tinymce.triggerSave(); }); @@ -319,14 +324,15 @@ class TinyMCEPlugin extends Plugin SN.U.Counter(noticeForm); }); - $('#'+SN.C.S.NoticeDataAttach).change(function() { + form.find('input[type=file]').change(function() { var img = ''; var html = tinyMCE.activeEditor.getContent(); ed.setContent(html + img); }); } }); - }); + }; + })(); END_OF_SCRIPT; return $scr; From 2567474ddd255bf755b1edda9eb191be058ea1e5 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 11:22:08 -0800 Subject: [PATCH 04/13] Work in progress: fixing Poll posting URLs, working on AS input --- plugins/Poll/PollPlugin.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index 6fa95aa0e7..193b0b17a2 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -124,8 +124,11 @@ class PollPlugin extends MicroAppPlugin function onRouterInitialized($m) { $m->connect('main/poll/new', - array('action' => 'newpoll'), - array('id' => '[0-9]+')); + array('action' => 'newpoll')); + + $m->connect('main/poll/:id', + array('action' => 'showpoll'), + array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')); $m->connect('main/poll/:id/respond', array('action' => 'respondpoll'), @@ -190,6 +193,32 @@ class PollPlugin extends MicroAppPlugin function saveNoticeFromActivity($activity, $profile, $options=array()) { // @fixme + common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true)); + common_log(LOG_DEBUG, "XXX options: " . var_export($options, true)); + + // 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); + $question = $data->getAttribute('question'); + $opts = array(); + foreach ($data->attributes as $node) { + $name = $node->nodeName; + if (substr($name, 0, 6) == 'option') { + $n = intval(substr($name, 6)); + if ($n > 0) { + $opts[$n - 1] = $node->nodeValue; + } + } + } + common_log(LOG_DEBUG, "YYY question: $question"); + common_log(LOG_DEBUG, "YYY opts: " . var_export($opts, true)); + } else { + common_log(LOG_DEBUG, "YYY no poll data"); + } + } } function activityObjectFromNotice($notice) From 051180035102f5ab02e0913801a716e1291b9469 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 11:35:04 -0800 Subject: [PATCH 05/13] Provisional tweak for polls to go through atom feeds / ostatus Had to fix some calls to MicroAppPlugin->saveNoticeFromActivity() which passed an OStatus_profile instead of a Profile... Imported polls don't get data from upstream or send it back currently. --- lib/microappplugin.php | 10 +++++++--- plugins/Poll/PollPlugin.php | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/microappplugin.php b/lib/microappplugin.php index 93b70a0324..7e8a124d1e 100644 --- a/lib/microappplugin.php +++ b/lib/microappplugin.php @@ -308,8 +308,9 @@ abstract class MicroAppPlugin extends Plugin 'url' => $object->link, 'is_local' => Notice::REMOTE_OMB, 'source' => 'ostatus'); - - $this->saveNoticeFromActivity($activity, $actor); + + // $actor is an ostatus_profile + $this->saveNoticeFromActivity($activity, $actor->localProfile()); return false; } @@ -365,7 +366,8 @@ abstract class MicroAppPlugin extends Plugin 'is_local' => Notice::REMOTE_OMB, 'source' => 'ostatus'); - $this->saveNoticeFromActivity($activity, $actor, $options); + // $actor is an ostatus_profile + $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options); return false; } @@ -389,6 +391,7 @@ abstract class MicroAppPlugin extends Plugin $options = array('source' => 'atompub'); + // $user->getProfile() is a Profile $this->saveNoticeFromActivity($activity, $user->getProfile(), $options); @@ -421,6 +424,7 @@ abstract class MicroAppPlugin extends Plugin 'url' => $object->link, 'source' => 'restore'); + // $user->getProfile() is a Profile $saved = $this->saveNoticeFromActivity($activity, $user->getProfile(), $options); diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index 193b0b17a2..8a69ccd225 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -194,6 +194,7 @@ class PollPlugin extends MicroAppPlugin { // @fixme common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true)); + common_log(LOG_DEBUG, "XXX profile: " . var_export($profile, true)); common_log(LOG_DEBUG, "XXX options: " . var_export($options, true)); // Ok for now, we can grab stuff from the XML entry directly. @@ -215,6 +216,13 @@ class PollPlugin extends MicroAppPlugin } common_log(LOG_DEBUG, "YYY question: $question"); common_log(LOG_DEBUG, "YYY opts: " . var_export($opts, true)); + try { + $notice = Poll::saveNew($profile, $question, $opts, $options); + common_log(LOG_DEBUG, "YYY ok: " . $notice->id); + return $notice; + } catch (Exception $e) { + common_log(LOG_DEBUG, "YYY fail: " . $e->getMessage()); + } } else { common_log(LOG_DEBUG, "YYY no poll data"); } From acd3f957ba0ea24ba83ce7f1b3c658b94d709da3 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 12:05:25 -0800 Subject: [PATCH 06/13] Micro-app fix: pass foreign options (remote source & original URI) with OStatus input --- lib/microappplugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/microappplugin.php b/lib/microappplugin.php index 7e8a124d1e..fbead58cc5 100644 --- a/lib/microappplugin.php +++ b/lib/microappplugin.php @@ -310,7 +310,7 @@ abstract class MicroAppPlugin extends Plugin 'source' => 'ostatus'); // $actor is an ostatus_profile - $this->saveNoticeFromActivity($activity, $actor->localProfile()); + $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options); return false; } From e085ef5ef5c9f8f5a13877bce84bc094eb6a5a34 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 12:38:25 -0800 Subject: [PATCH 07/13] Work on turning poll responses into activities so we can send them over ostatus bridge. Not yet bridging. --- plugins/Poll/Poll.php | 1 + plugins/Poll/PollPlugin.php | 35 ++++++++++- plugins/Poll/Poll_response.php | 112 ++++++++++++++++++++++++++++++++- plugins/Poll/respondpoll.php | 15 +++-- 4 files changed, 151 insertions(+), 12 deletions(-) diff --git a/plugins/Poll/Poll.php b/plugins/Poll/Poll.php index 65aad4830e..f5143c3e57 100644 --- a/plugins/Poll/Poll.php +++ b/plugins/Poll/Poll.php @@ -47,6 +47,7 @@ class Poll extends Managed_DataObject { public $__table = 'poll'; // table name public $id; // char(36) primary key not null -> UUID + public $uri; public $profile_id; // int -> profile.id public $question; // text public $options; // text; newline(?)-delimited diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index 8a69ccd225..be1dd58d59 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -47,7 +47,10 @@ if (!defined('STATUSNET')) { class PollPlugin extends MicroAppPlugin { const VERSION = '0.1'; - const POLL_OBJECT = 'http://apinamespace.org/activitystreams/object/poll'; + + // @fixme which domain should we use for these namespaces? + const POLL_OBJECT = 'http://apinamespace.org/activitystreams/object/poll'; + const POLL_RESPONSE_OBJECT = 'http://apinamespace.org/activitystreams/object/poll-response'; /** * Database schema setup @@ -130,6 +133,10 @@ class PollPlugin extends MicroAppPlugin array('action' => 'showpoll'), array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')); + $m->connect('main/poll/response/:id', + array('action' => 'showpollresponse'), + array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')); + $m->connect('main/poll/:id/respond', array('action' => 'respondpoll'), array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')); @@ -158,7 +165,7 @@ class PollPlugin extends MicroAppPlugin function types() { - return array(self::POLL_OBJECT); + return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT); } /** @@ -282,6 +289,18 @@ class PollPlugin extends MicroAppPlugin * and Bookmark plugin for if that's right. */ function showNotice($notice, $out) + { + switch ($notice->object_type) { + case self::POLL_OBJECT: + return $this->showNoticePoll($notice, $out); + case self::POLL_RESPONSE_OBJECT: + return $this->showNoticePollResponse($notice, $out); + default: + throw new Exception('Unexpected type for poll plugin: ' . $notice->object_type); + } + } + + function showNoticePoll($notice, $out) { $user = common_current_user(); @@ -312,6 +331,18 @@ class PollPlugin extends MicroAppPlugin $out->elementStart('div', array('class' => 'entry-content')); } + function showNoticePollResponse($notice, $out) + { + $user = common_current_user(); + + // @hack we want regular rendering, then just add stuff after that + $nli = new NoticeListItem($notice, $out); + $nli->showNotice(); + + // @fixme + $out->elementStart('div', array('class' => 'entry-content')); + } + function entryForm($out) { return new NewPollForm($out); diff --git a/plugins/Poll/Poll_response.php b/plugins/Poll/Poll_response.php index 44bc421c22..b5204c71f0 100644 --- a/plugins/Poll/Poll_response.php +++ b/plugins/Poll/Poll_response.php @@ -46,7 +46,8 @@ if (!defined('STATUSNET')) { class Poll_response extends Managed_DataObject { public $__table = 'poll_response'; // table name - public $poll_id; // char(36) primary key not null -> UUID + public $id; // char(36) primary key not null -> UUID + public $poll_id; // char(36) -> poll.id UUID public $profile_id; // int -> profile.id public $selection; // int -> choice # public $created; // datetime @@ -94,12 +95,16 @@ class Poll_response extends Managed_DataObject return array( 'description' => 'Record of responses to polls', 'fields' => array( - 'poll_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'), + 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of the response'), + 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'UUID to the response notice'), + 'poll_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'), 'profile_id' => array('type' => 'int'), 'selection' => array('type' => 'int'), 'created' => array('type' => 'datetime', 'not null' => true), ), + 'primary key' => array('id'), 'unique keys' => array( + 'poll_uri_key' => array('uri'), 'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'), ), 'indexes' => array( @@ -107,4 +112,107 @@ class Poll_response extends Managed_DataObject ) ); } + + /** + * Get a poll response based on a notice + * + * @param Notice $notice Notice to check for + * + * @return Poll_response found response or null + */ + + function getByNotice($notice) + { + return self::staticGet('uri', $notice->uri); + } + + /** + * Get the notice that belongs to this response... + * + * @return Notice + */ + function getNotice() + { + return Notice::staticGet('uri', $this->uri); + } + + function bestUrl() + { + return $this->getNotice()->bestUrl(); + } + + /** + * Save a new poll notice + * + * @param Profile $profile + * @param Poll $poll the poll being responded to + * @param int $selection (1-based) + * @param array $opts (poll responses) + * + * @return Notice saved notice + */ + + static function saveNew($profile, $poll, $selection, $options=null) + { + if (empty($options)) { + $options = array(); + } + + $opts = $poll->getOptions(); + if ($selection < 1 || $selection > count($opts)) { + throw new ClientException(_m('Invalid poll selection.')); + } + $answer = $opts[$selection - 1]; + + $pr = new Poll_response(); + $pr->id = UUID::gen(); + $pr->profile_id = $profile->id; + $pr->poll_id = $poll->id; + $pr->selection = $selection; + + if (array_key_exists('created', $options)) { + $pr->created = $options['created']; + } else { + $pr->created = common_sql_now(); + } + + if (array_key_exists('uri', $options)) { + $pr->uri = $options['uri']; + } else { + $pr->uri = common_local_url('showpollresponse', + array('id' => $pr->id)); + } + + common_log(LOG_DEBUG, "Saving poll response: $pr->id $pr->uri"); + $pr->insert(); + + $content = sprintf(_m('voted for "%s"'), + $answer); + $rendered = sprintf(_m('voted for “%s”'), + htmlspecialchars($poll->uri), + htmlspecialchars($answer)); + + $tags = array(); + $replies = array(); + + $options = array_merge(array('urls' => array(), + 'rendered' => $rendered, + 'tags' => $tags, + 'replies' => $replies, + 'reply_to' => $poll->getNotice()->id, + 'object_type' => PollPlugin::POLL_RESPONSE_OBJECT), + $options); + + if (!array_key_exists('uri', $options)) { + $options['uri'] = $pr->uri; + } + + $saved = Notice::saveNew($profile->id, + $content, + array_key_exists('source', $options) ? + $options['source'] : 'web', + $options); + + return $saved; + } } diff --git a/plugins/Poll/respondpoll.php b/plugins/Poll/respondpoll.php index 07a5235406..e521a2a68f 100644 --- a/plugins/Poll/respondpoll.php +++ b/plugins/Poll/respondpoll.php @@ -75,6 +75,9 @@ class RespondPollAction extends Action function prepare($argarray) { parent::prepare($argarray); + if ($this->boolean('ajax')) { + StatusNet::setApi(true); + } $this->user = common_current_user(); @@ -132,20 +135,16 @@ class RespondPollAction extends Action function respondPoll() { try { - $response = new Poll_response(); - $response->poll_id = $this->poll->id; - $response->profile_id = $this->user->id; - $response->selection = $this->selection; - $response->created = common_sql_now(); - $response->insert(); - + $notice = Poll_response::saveNew($this->user->getProfile(), + $this->poll, + $this->selection); } catch (ClientException $ce) { $this->error = $ce->getMessage(); $this->showPage(); return; } - if ($this->arg('ajax')) { + if ($this->boolean('ajax')) { header('Content-Type: text/xml;charset=utf-8'); $this->xw->startDocument('1.0', 'UTF-8'); $this->elementStart('html'); From d6c8c61076f744fbcf96f7edb5b50732515af04e Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 13:41:23 -0800 Subject: [PATCH 08/13] AS output/input for poll responses --- plugins/Poll/Poll.php | 17 +++++++ plugins/Poll/PollPlugin.php | 84 +++++++++++++++++++++++++++++++--- plugins/Poll/Poll_response.php | 4 +- 3 files changed, 96 insertions(+), 9 deletions(-) diff --git a/plugins/Poll/Poll.php b/plugins/Poll/Poll.php index f5143c3e57..1f26bbf6e4 100644 --- a/plugins/Poll/Poll.php +++ b/plugins/Poll/Poll.php @@ -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); diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index be1dd58d59..49f7011156 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -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 + * "uri": "http://..../poll/...." + * "selection": 3 + * } + * + */ + // @fixme there's no way to specify an XML node tree here, like + // @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: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; } diff --git a/plugins/Poll/Poll_response.php b/plugins/Poll/Poll_response.php index b5204c71f0..03dd6f60b5 100644 --- a/plugins/Poll/Poll_response.php +++ b/plugins/Poll/Poll_response.php @@ -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(); From e54eb9117f99416a4000399027654279d1e8512a Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 13:45:31 -0800 Subject: [PATCH 09/13] backwards --- plugins/Poll/PollPlugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index 49f7011156..5714653fb0 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -270,7 +270,7 @@ class PollPlugin extends MicroAppPlugin } } - function activityObjectFromNoticePoll($notice) + function activityObjectFromNoticePollResponse($notice) { $object = new ActivityObject(); $object->id = $notice->uri; @@ -279,7 +279,7 @@ class PollPlugin extends MicroAppPlugin $object->summary = $notice->content; $object->link = $notice->bestUrl(); - $response = Poll_response::getByNotice($notice); + $response = Poll_response:getByNotice($notice); $poll = $response->getPoll(); /** @@ -308,7 +308,7 @@ class PollPlugin extends MicroAppPlugin return $object; } - function activityObjectFromNoticePollResponse($notice) + function activityObjectFromNoticePoll($notice) { $object = new ActivityObject(); $object->id = $notice->uri; From 97df2f394fbb4c178e6b8d4da469b51625dfb45c Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 13:46:20 -0800 Subject: [PATCH 10/13] typo.... maybe that coffee's wearing off? --- plugins/Poll/PollPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index 5714653fb0..6b8bf9608e 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -279,7 +279,7 @@ class PollPlugin extends MicroAppPlugin $object->summary = $notice->content; $object->link = $notice->bestUrl(); - $response = Poll_response:getByNotice($notice); + $response = Poll_response::getByNotice($notice); $poll = $response->getPoll(); /** From 6416f5ea4113cdc2e32132d387ce1cf526f35062 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 13:47:44 -0800 Subject: [PATCH 11/13] fix --- plugins/Poll/PollPlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index 6b8bf9608e..f76faedb05 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -209,8 +209,8 @@ class PollPlugin extends MicroAppPlugin if ($activity->entry) { $pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll'); $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response'); - if ($dataElements->length) { - $data = $dataElements->item(0); + if ($pollElements->length) { + $data = $pollElements->item(0); $question = $data->getAttribute('question'); $opts = array(); foreach ($data->attributes as $node) { From eccadb4a7b672685215dc6d9e481577e9fe92d9c Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 10 Mar 2011 13:55:47 -0800 Subject: [PATCH 12/13] cleanup... Poll main & response events seem to sorta work over ostatus now --- plugins/Poll/PollPlugin.php | 53 ++++++++++++++++++---------------- plugins/Poll/Poll_response.php | 8 +++++ 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/plugins/Poll/PollPlugin.php b/plugins/Poll/PollPlugin.php index f76faedb05..a2e9814555 100644 --- a/plugins/Poll/PollPlugin.php +++ b/plugins/Poll/PollPlugin.php @@ -280,31 +280,34 @@ class PollPlugin extends MicroAppPlugin $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 - * "uri": "http://..../poll/...." - * "selection": 3 - * } - * - */ - // @fixme there's no way to specify an XML node tree here, like - // @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, ''); + if (!$response) { + common_log(LOG_DEBUG, "QQQ notice uri: $notice->uri"); + } else { + $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 + * "uri": "http://..../poll/...." + * "selection": 3 + * } + * + */ + // @fixme there's no way to specify an XML node tree here, like + // @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; } diff --git a/plugins/Poll/Poll_response.php b/plugins/Poll/Poll_response.php index 03dd6f60b5..88e6ea890c 100644 --- a/plugins/Poll/Poll_response.php +++ b/plugins/Poll/Poll_response.php @@ -141,6 +141,14 @@ class Poll_response extends Managed_DataObject return $this->getNotice()->bestUrl(); } + /** + * + * @return Poll + */ + function getPoll() + { + return Poll::staticGet('id', $this->poll_id); + } /** * Save a new poll notice * From 24d00941925b0064c1a55cbcdd501ca471c08590 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Thu, 10 Mar 2011 14:20:35 -0800 Subject: [PATCH 13/13] User Directory - set class current on "All" if there is no selected filter --- plugins/Directory/lib/alphanav.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/Directory/lib/alphanav.php b/plugins/Directory/lib/alphanav.php index dadb589094..87e2f18f18 100644 --- a/plugins/Directory/lib/alphanav.php +++ b/plugins/Directory/lib/alphanav.php @@ -128,8 +128,8 @@ class AlphaNav extends Widget $current = $this->action->arg('filter'); // Highlight the selected filter. If there is no selected - // filter, highlight the first filter in the list - if (!isset($current) && $i == 0 + // filter, highlight the last filter in the list (all) + if (!isset($current) && $i == ($size - 1) || $current === strtolower($filter)) { $classes .= 'current '; }