]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
SearchSub plugin: 'track X', 'untrack X', 'tracking', 'track off' commands now overri...
authorBrion Vibber <brion@pobox.com>
Thu, 17 Mar 2011 00:08:09 +0000 (17:08 -0700)
committerBrion Vibber <brion@pobox.com>
Thu, 17 Mar 2011 00:22:37 +0000 (17:22 -0700)
plugins/SearchSub/SearchSubPlugin.php
plugins/SearchSub/searchsubtrackcommand.php [new file with mode: 0644]
plugins/SearchSub/searchsubtrackingcommand.php [new file with mode: 0644]
plugins/SearchSub/searchsubtrackoffcommand.php [new file with mode: 0644]
plugins/SearchSub/searchsubuntrackcommand.php [new file with mode: 0644]

index f7da8c44d9acf1ccffd0bf3e3ee80d326731dba0..c07f7695da6508c362ccb84bd1c1c69887c582e1 100644 (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.");
+    }
 }
diff --git a/plugins/SearchSub/searchsubtrackcommand.php b/plugins/SearchSub/searchsubtrackcommand.php
new file mode 100644 (file)
index 0000000..bba2cb3
--- /dev/null
@@ -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));
+    }
+}
\ No newline at end of file
diff --git a/plugins/SearchSub/searchsubtrackingcommand.php b/plugins/SearchSub/searchsubtrackingcommand.php
new file mode 100644 (file)
index 0000000..385a22b
--- /dev/null
@@ -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) . '"'));
+    }
+}
\ No newline at end of file
diff --git a/plugins/SearchSub/searchsubtrackoffcommand.php b/plugins/SearchSub/searchsubtrackoffcommand.php
new file mode 100644 (file)
index 0000000..1e5eb97
--- /dev/null
@@ -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.'));
+    }
+}
\ No newline at end of file
diff --git a/plugins/SearchSub/searchsubuntrackcommand.php b/plugins/SearchSub/searchsubuntrackcommand.php
new file mode 100644 (file)
index 0000000..9fb84cd
--- /dev/null
@@ -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));
+    }
+}
\ No newline at end of file