diff --git a/lib/microappplugin.php b/lib/microappplugin.php index 93b70a0324..fbead58cc5 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(), $options); 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/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')); } 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); 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 '; } diff --git a/plugins/Poll/Poll.php b/plugins/Poll/Poll.php index 65aad4830e..1f26bbf6e4 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 @@ -127,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 6fa95aa0e7..a2e9814555 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 @@ -124,8 +127,15 @@ 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/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'), @@ -155,7 +165,7 @@ class PollPlugin extends MicroAppPlugin function types() { - return array(self::POLL_OBJECT); + return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT); } /** @@ -190,17 +200,124 @@ 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 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. + // This won't work when reading from JSON source + if ($activity->entry) { + $pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll'); + $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response'); + if ($pollElements->length) { + $data = $pollElements->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)); + 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 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"); + } + } } function activityObjectFromNotice($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 activityObjectFromNoticePollResponse($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); + 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; + } + + function activityObjectFromNoticePoll($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); @@ -218,7 +335,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" @@ -235,7 +352,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; } @@ -245,6 +362,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(); @@ -275,6 +404,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..88e6ea890c 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,115 @@ 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(); + } + + /** + * + * @return Poll + */ + function getPoll() + { + return Poll::staticGet('id', $this->poll_id); + } + /** + * 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(); + } + + if (!$poll->isValidSelection($selection)) { + throw new ClientException(_m('Invalid poll selection.')); + } + $opts = $poll->getOptions(); + $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'); 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;