From: Brion Vibber Date: Tue, 15 Mar 2011 21:58:09 +0000 (-0700) Subject: copy-paste TagSub to SearchSub :D X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=579fc11862173c8be3a623ebc3248ce9d61835a8;p=quix0rs-gnu-social.git copy-paste TagSub to SearchSub :D --- diff --git a/plugins/SearchSub/SearchSub.php b/plugins/SearchSub/SearchSub.php new file mode 100644 index 0000000000..cbf64d39cc --- /dev/null +++ b/plugins/SearchSub/SearchSub.php @@ -0,0 +1,140 @@ + + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://status.net/ + * + * StatusNet - the distributed open-source microblogging tool + * Copyright (C) 2011, StatusNet, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * For storing the search subscriptions + * + * @category PollPlugin + * @package StatusNet + * @author Brion Vibber + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://status.net/ + * + * @see DB_DataObject + */ + +class SearchSub extends Managed_DataObject +{ + public $__table = 'searchsub'; // table name + public $search; // text + public $profile_id; // int -> profile.id + public $created; // datetime + + /** + * Get an instance by key + * + * This is a utility method to get a single instance with a given key value. + * + * @param string $k Key to use to lookup (usually 'user_id' for this class) + * @param mixed $v Value to lookup + * + * @return SearchSub object found, or null for no hits + * + */ + function staticGet($k, $v=null) + { + return Memcached_DataObject::staticGet('SearchSub', $k, $v); + } + + /** + * Get an instance by compound key + * + * This is a utility method to get a single instance with a given set of + * key-value pairs. Usually used for the primary key for a compound key; thus + * the name. + * + * @param array $kv array of key-value mappings + * + * @return SearchSub object found, or null for no hits + * + */ + function pkeyGet($kv) + { + return Memcached_DataObject::pkeyGet('SearchSub', $kv); + } + + /** + * The One True Thingy that must be defined and declared. + */ + public static function schemaDef() + { + return array( + 'description' => 'SearchSubPlugin search subscription records', + 'fields' => array( + 'search' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash search associated with this subscription'), + 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'), + 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), + ), + 'primary key' => array('search', 'profile_id'), + 'foreign keys' => array( + 'searchsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')), + ), + 'indexes' => array( + 'searchsub_created_idx' => array('created'), + 'searchsub_profile_id_tag_idx' => array('profile_id', 'search'), + ), + ); + } + + /** + * Start a search subscription! + * + * @param profile $profile subscriber + * @param string $search subscribee + * @return SearchSub + */ + static function start(Profile $profile, $search) + { + $ts = new SearchSub(); + $ts->search = $search; + $ts->profile_id = $profile->id; + $ts->created = common_sql_now(); + $ts->insert(); + return $ts; + } + + /** + * End a search subscription! + * + * @param profile $profile subscriber + * @param string $search subscribee + */ + static function cancel(Profile $profile, $search) + { + $ts = SearchSub::pkeyGet(array('search' => $search, + 'profile_id' => $profile->id)); + if ($ts) { + $ts->delete(); + } + } +} diff --git a/plugins/SearchSub/SearchSubPlugin.php b/plugins/SearchSub/SearchSubPlugin.php new file mode 100644 index 0000000000..59db0ffcbb --- /dev/null +++ b/plugins/SearchSub/SearchSubPlugin.php @@ -0,0 +1,182 @@ +. + * + * @category SearchSubPlugin + * @package StatusNet + * @author Brion Vibber + * @copyright 2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * SearchSub plugin main class + * + * @category SearchSubPlugin + * @package StatusNet + * @author Brion Vibber + * @copyright 2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ +class SearchSubPlugin extends Plugin +{ + const VERSION = '0.1'; + + /** + * Database schema setup + * + * @see Schema + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function onCheckSchema() + { + $schema = Schema::get(); + $schema->ensureTable('searchsub', SearchSub::schemaDef()); + return true; + } + + /** + * Load related modules when needed + * + * @param string $cls Name of the class to be loaded + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function onAutoload($cls) + { + $dir = dirname(__FILE__); + + switch ($cls) + { + case 'SearchSub': + include_once $dir.'/'.$cls.'.php'; + return false; + case 'SearchsubAction': + case 'SearchunsubAction': + case 'SearchSubForm': + case 'SearchUnsubForm': + include_once $dir.'/'.strtolower($cls).'.php'; + return false; + default: + return true; + } + } + + /** + * Map URLs to actions + * + * @param Net_URL_Mapper $m path-to-action mapper + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function onRouterInitialized($m) + { + $m->connect('search/:search/subscribe', + array('action' => 'searchsub'), + array('search' => Router::REGEX_TAG)); + $m->connect('search/:search/unsubscribe', + array('action' => 'searchunsub'), + array('search' => Router::REGEX_TAG)); + + return true; + } + + /** + * Plugin version data + * + * @param array &$versions array of version data + * + * @return value + */ + function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'SearchSub', + 'version' => self::VERSION, + 'author' => 'Brion Vibber', + 'homepage' => 'http://status.net/wiki/Plugin:SearchSub', + 'rawdescription' => + // TRANS: Plugin description. + _m('Plugin to allow following all messages with a given search.')); + return true; + } + + /** + * Hook inbox delivery setup so search subscribers receive all + * notices with that search in their inbox. + * + * Currently makes no distinction between local messages and + * remote ones which happen to come in to the system. Remote + * notices that don't come in at all won't ever reach this. + * + * @param Notice $notice + * @param array $ni in/out map of profile IDs to inbox constants + * @return boolean hook result + */ + function onStartNoticeWhoGets(Notice $notice, array &$ni) + { + foreach ($notice->getTags() as $search) { + $searchsub = new SearchSub(); + $searchsub->search = $search; + $searchsub->find(); + + while ($searchsub->fetch()) { + // These constants are currently not actually used, iirc + $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB; + } + } + return true; + } + + /** + * + * @param SearchAction $action + * @return boolean hook result + */ + function onStartTagShowContent(SearchAction $action) + { + $user = common_current_user(); + if ($user) { + $search = $action->trimmed('search'); + $searchsub = SearchSub::pkeyGet(array('search' => $search, + 'profile_id' => $user->id)); + if ($searchsub) { + $form = new SearchUnsubForm($action, $search); + } else { + $form = new SearchSubForm($action, $search); + } + $action->elementStart('div', 'entity_actions'); + $action->elementStart('ul'); + $action->elementStart('li', 'entity_subscribe'); + $form->show(); + $action->elementEnd('li'); + $action->elementEnd('ul'); + $action->elementEnd('div'); + } + return true; + } +} diff --git a/plugins/SearchSub/searchsubaction.php b/plugins/SearchSub/searchsubaction.php new file mode 100644 index 0000000000..67bc178df6 --- /dev/null +++ b/plugins/SearchSub/searchsubaction.php @@ -0,0 +1,149 @@ +. + * + * PHP version 5 + * + * @category Action + * @package StatusNet + * @author Brion Vibber + * @author Evan Prodromou + * @copyright 2008-2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Search subscription action + * + * Takes parameters: + * + * - token: session token to prevent CSRF attacks + * - ajax: boolean; whether to return Ajax or full-browser results + * + * Only works if the current user is logged in. + * + * @category Action + * @package StatusNet + * @author Evan Prodromou + * @author Brion Vibber + * @copyright 2008-2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ +class SearchsubAction extends Action +{ + var $user; + var $search; + + /** + * Check pre-requisites and instantiate attributes + * + * @param Array $args array of arguments (URL, GET, POST) + * + * @return boolean success flag + */ + function prepare($args) + { + parent::prepare($args); + if ($this->boolean('ajax')) { + StatusNet::setApi(true); + } + + // Only allow POST requests + + if ($_SERVER['REQUEST_METHOD'] != 'POST') { + // TRANS: Client error displayed trying to perform any request method other than POST. + // TRANS: Do not translate POST. + $this->clientError(_('This action only accepts POST requests.')); + return false; + } + + // CSRF protection + + $token = $this->trimmed('token'); + + if (!$token || $token != common_session_token()) { + // TRANS: Client error displayed when the session token is not okay. + $this->clientError(_('There was a problem with your session token.'. + ' Try again, please.')); + return false; + } + + // Only for logged-in users + + $this->user = common_current_user(); + + if (empty($this->user)) { + // TRANS: Client error displayed trying to subscribe when not logged in. + $this->clientError(_('Not logged in.')); + return false; + } + + // Profile to subscribe to + + $this->search = $this->arg('search'); + + if (empty($this->search)) { + // TRANS: Client error displayed trying to subscribe to a non-existing profile. + $this->clientError(_('No such profile.')); + return false; + } + + return true; + } + + /** + * Handle request + * + * Does the subscription and returns results. + * + * @param Array $args unused. + * + * @return void + */ + function handle($args) + { + // Throws exception on error + + SearchSub::start($this->user->getProfile(), + $this->search); + + if ($this->boolean('ajax')) { + $this->startHTML('text/xml;charset=utf-8'); + $this->elementStart('head'); + // TRANS: Page title when search subscription succeeded. + $this->element('title', null, _m('Subscribed')); + $this->elementEnd('head'); + $this->elementStart('body'); + $unsubscribe = new SearchUnsubForm($this, $this->search); + $unsubscribe->show(); + $this->elementEnd('body'); + $this->elementEnd('html'); + } else { + $url = common_local_url('search', + array('search' => $this->search)); + common_redirect($url, 303); + } + } +} diff --git a/plugins/SearchSub/searchsubform.php b/plugins/SearchSub/searchsubform.php new file mode 100644 index 0000000000..8078cdde1b --- /dev/null +++ b/plugins/SearchSub/searchsubform.php @@ -0,0 +1,142 @@ +. + * + * @category SearchSubPlugin + * @package StatusNet + * @author Brion Vibber + * @author Evan Prodromou + * @author Sarven Capadisli + * @copyright 2009-2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET') && !defined('LACONICA')) { + exit(1); +} + +/** + * Form for subscribing to a user + * + * @category SearchSubPlugin + * @package StatusNet + * @author Brion Vibber + * @author Evan Prodromou + * @author Sarven Capadisli + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + * + * @see UnsubscribeForm + */ + +class SearchSubForm extends Form +{ + /** + * Name of search to subscribe to + */ + + var $search = ''; + + /** + * Constructor + * + * @param HTMLOutputter $out output channel + * @param string $search name of search to subscribe to + */ + + function __construct($out=null, $search=null) + { + parent::__construct($out); + + $this->search = $search; + } + + /** + * ID of the form + * + * @return int ID of the form + */ + + function id() + { + return 'search-subscribe-' . $this->search; + } + + + /** + * class of the form + * + * @return string of the form class + */ + + function formClass() + { + // class to match existing styles... + return 'form_user_subscribe ajax'; + } + + + /** + * Action of the form + * + * @return string URL of the action + */ + + function action() + { + return common_local_url('searchsub', array('search' => $this->search)); + } + + + /** + * Legend of the Form + * + * @return void + */ + function formLegend() + { + $this->out->element('legend', null, _m('Subscribe to this search')); + } + + /** + * Data elements of the form + * + * @return void + */ + + function formData() + { + $this->out->hidden('subscribeto-' . $this->search, + $this->search, + 'subscribeto'); + } + + /** + * Action elements + * + * @return void + */ + + function formActions() + { + $this->out->submit('submit', _('Subscribe'), 'submit', null, _m('Subscribe to this search')); + } +} diff --git a/plugins/SearchSub/searchunsubaction.php b/plugins/SearchSub/searchunsubaction.php new file mode 100644 index 0000000000..f7f006e21c --- /dev/null +++ b/plugins/SearchSub/searchunsubaction.php @@ -0,0 +1,89 @@ +. + * + * PHP version 5 + * + * @category Action + * @package StatusNet + * @author Brion Vibber + * @author Evan Prodromou + * @copyright 2008-2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Search unsubscription action + * + * Takes parameters: + * + * - token: session token to prevent CSRF attacks + * - ajax: boolean; whether to return Ajax or full-browser results + * + * Only works if the current user is logged in. + * + * @category Action + * @package StatusNet + * @author Evan Prodromou + * @author Brion Vibber + * @copyright 2008-2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ +class SearchunsubAction extends SearchsubAction +{ + /** + * Handle request + * + * Does the subscription and returns results. + * + * @param Array $args unused. + * + * @return void + */ + function handle($args) + { + // Throws exception on error + + SearchSub::cancel($this->user->getProfile(), + $this->search); + + if ($this->boolean('ajax')) { + $this->startHTML('text/xml;charset=utf-8'); + $this->elementStart('head'); + // TRANS: Page title when search unsubscription succeeded. + $this->element('title', null, _m('Unsubscribed')); + $this->elementEnd('head'); + $this->elementStart('body'); + $subscribe = new SearchSubForm($this, $this->search); + $subscribe->show(); + $this->elementEnd('body'); + $this->elementEnd('html'); + } else { + $url = common_local_url('search', + array('search' => $this->search)); + common_redirect($url, 303); + } + } +} diff --git a/plugins/SearchSub/searchunsubform.php b/plugins/SearchSub/searchunsubform.php new file mode 100644 index 0000000000..296b74f4a1 --- /dev/null +++ b/plugins/SearchSub/searchunsubform.php @@ -0,0 +1,109 @@ +. + * + * @category SearchSubPlugin + * @package StatusNet + * @author Brion Vibber + * @author Evan Prodromou + * @author Sarven Capadisli + * @copyright 2009-2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET') && !defined('LACONICA')) { + exit(1); +} + +/** + * Form for subscribing to a user + * + * @category SearchSubPlugin + * @package StatusNet + * @author Brion Vibber + * @author Evan Prodromou + * @author Sarven Capadisli + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + * + * @see UnsubscribeForm + */ + +class SearchUnsubForm extends SearchSubForm +{ + /** + * ID of the form + * + * @return int ID of the form + */ + + function id() + { + return 'search-unsubscribe-' . $this->search; + } + + + /** + * class of the form + * + * @return string of the form class + */ + + function formClass() + { + // class to match existing styles... + return 'form_user_unsubscribe ajax'; + } + + + /** + * Action of the form + * + * @return string URL of the action + */ + + function action() + { + return common_local_url('searchunsub', array('search' => $this->search)); + } + + + /** + * Legend of the Form + * + * @return void + */ + function formLegend() + { + $this->out->element('legend', null, _m('Unsubscribe from this search')); + } + + /** + * Action elements + * + * @return void + */ + + function formActions() + { + $this->out->submit('submit', _('Unsubscribe'), 'submit', null, _m('Unsubscribe from this search')); + } +}