SearchSub plugin: 'track X', 'untrack X', 'tracking', 'track off' commands now overridden :D

This commit is contained in:
Brion Vibber 2011-03-16 17:08:09 -07:00
parent bd12546984
commit 0754edf9a2
5 changed files with 185 additions and 0 deletions

View File

@ -81,6 +81,10 @@ class SearchSubPlugin extends Plugin
case 'SearchsubsAction':
case 'SearchSubForm':
case 'SearchUnsubForm':
case 'SearchSubTrackCommand':
case 'SearchSubTrackOffCommand':
case 'SearchSubTrackingCommand':
case 'SearchSubUntrackCommand':
include_once $dir.'/'.strtolower($cls).'.php';
return false;
default:
@ -269,4 +273,49 @@ class SearchSubPlugin extends Plugin
}
return true;
}
/**
* Replace the built-in stub track commands with ones that control
* search subscriptions.
*
* @param CommandInterpreter $cmd
* @param string $arg
* @param User $user
* @param Command $result
* @return boolean hook result
*/
function onEndInterpretCommand($cmd, $arg, $user, &$result)
{
if ($result instanceof TrackCommand) {
$result = new SearchSubTrackCommand($user, $arg);
return false;
} else if ($result instanceof TrackOffCommand) {
$result = new SearchSubTrackOffCommand($user);
return false;
} else if ($result instanceof TrackingCommand) {
$result = new SearchSubTrackingCommand($user);
return false;
} else if ($result instanceof UntrackCommand) {
$result = new SearchSubUntrackCommand($user, $arg);
return false;
} else {
return true;
}
}
function onHelpCommandMessages($cmd, &$commands)
{
// TRANS: Help message for IM/SMS command "track <word>"
$commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
// TRANS: Help message for IM/SMS command "untrack <word>"
$commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
// TRANS: Help message for IM/SMS command "track off"
$commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
// TRANS: Help message for IM/SMS command "untrack all"
$commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
// TRANS: Help message for IM/SMS command "tracks"
$commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
// TRANS: Help message for IM/SMS command "tracking"
$commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
}
}

View File

@ -0,0 +1,38 @@
<?php
class SearchSubTrackCommand extends Command
{
var $keyword = null;
function __construct($user, $keyword)
{
parent::__construct($user);
$this->keyword = $keyword;
}
function handle($channel)
{
$cur = $this->user;
$searchsub = SearchSub::pkeyGet(array('search' => $this->keyword,
'profile_id' => $cur->id));
if ($searchsub) {
// TRANS: Error text shown a user tries to track a search query they're already subscribed to.
$channel->error($cur, sprintf(_m('You are already tracking the search "%s".'), $this->keyword));
return;
}
try {
SearchSub::start($cur->getProfile(), $this->keyword);
} catch (Exception $e) {
// TRANS: Message given having failed to set up a search subscription by track command.
$channel->error($cur, sprintf(_m('Could not start a search subscription for query "%s".'),
$this->keyword));
return;
}
// TRANS: Message given having added a search subscription by track command.
$channel->output($cur, sprintf(_m('You are subscribed to the search "%s".'),
$this->keyword));
}
}

View File

@ -0,0 +1,27 @@
<?php
class SearchSubTrackingCommand extends Command
{
function handle($channel)
{
$cur = $this->user;
$all = new SearchSub();
$all->profile_id = $cur->id;
$all->find();
if ($all->N == 0) {
// TRANS: Error text shown a user tries to disable all a search subscriptions with track off command, but has none.
$channel->error($cur, _m('You are not tracking any searches.'));
return;
}
$list = array();
while ($all->fetch()) {
$list[] = $all->search;
}
// TRANS: Message given having disabled all search subscriptions with 'track off'.
$channel->output($cur, sprintf(_m('You are tracking searches for: %s'),
'"' . implode('", "', $list) . '"'));
}
}

View File

@ -0,0 +1,33 @@
<?php
class SearchSubTrackoffCommand extends Command
{
function handle($channel)
{
$cur = $this->user;
$all = new SearchSub();
$all->profile_id = $cur->id;
$all->find();
if ($all->N == 0) {
// TRANS: Error text shown a user tries to disable all a search subscriptions with track off command, but has none.
$channel->error($cur, _m('You are not tracking any searches.'));
return;
}
$profile = $cur->getProfile();
while ($all->fetch()) {
try {
SearchSub::cancel($profile, $all->search);
} catch (Exception $e) {
// TRANS: Message given having failed to cancel one of the search subs with 'track off' command.
$channel->error($cur, sprintf(_m('Error disabling search subscription for query "%s".'),
$all->search));
return;
}
}
// TRANS: Message given having disabled all search subscriptions with 'track off'.
$channel->output($cur, _m('Disabled all your search subscriptions.'));
}
}

View File

@ -0,0 +1,38 @@
<?php
class SearchSubUntrackCommand extends Command
{
var $keyword = null;
function __construct($user, $keyword)
{
parent::__construct($user);
$this->keyword = $keyword;
}
function handle($channel)
{
$cur = $this->user;
$searchsub = SearchSub::pkeyGet(array('search' => $this->keyword,
'profile_id' => $cur->id));
if (!$searchsub) {
// TRANS: Error text shown a user tries to untrack a search query they're not subscribed to.
$channel->error($cur, sprintf(_m('You are not tracking the search "%s".'), $this->keyword));
return;
}
try {
SearchSub::cancel($cur->getProfile(), $this->keyword);
} catch (Exception $e) {
// TRANS: Message given having failed to cancel a search subscription by untrack command.
$channel->error($cur, sprintf(_m('Could not end a search subscription for query "%s".'),
$this->keyword));
return;
}
// TRANS: Message given having removed a search subscription by untrack command.
$channel->output($cur, sprintf(_m('You are no longer subscribed to the search "%s".'),
$this->keyword));
}
}