123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- defined('GNUSOCIAL') || die();
- class SearchSubPlugin extends Plugin
- {
- const PLUGIN_VERSION = '0.1.0';
-
- public function onCheckSchema(): bool
- {
- $schema = Schema::get();
- $schema->ensureTable('searchsub', SearchSub::schemaDef());
- return true;
- }
-
- public function onRouterInitialized(URLMapper $m): bool
- {
- $m->connect(
- 'search/:search/subscribe',
- ['action' => 'searchsub'],
- ['search' => Router::REGEX_TAG]
- );
- $m->connect(
- 'search/:search/unsubscribe',
- ['action' => 'searchunsub'],
- ['search' => Router::REGEX_TAG]
- );
- $m->connect(
- ':nickname/search-subscriptions',
- ['action' => 'searchsubs'],
- ['nickname' => Nickname::DISPLAY_FMT]
- );
- return true;
- }
-
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = array('name' => 'SearchSub',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Brion Vibber',
- 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/SearchSub',
- 'rawdescription' =>
-
- _m('Module to allow following all messages with a given search.'));
- return true;
- }
-
- public function onStartNoticeWhoGets(Notice $notice, array &$ni): bool
- {
-
-
- $sub = new SearchSub();
- $sub->groupBy('search');
- $sub->selectAdd();
- $sub->selectAdd('search');
- $sub->find();
- while ($sub->fetch()) {
- $search = $sub->search;
- if ($this->matchSearch($notice, $search)) {
-
-
- $searchsub = new SearchSub();
- $searchsub->search = $search;
- $searchsub->find();
- while ($searchsub->fetch()) {
-
- $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
- }
- }
- }
- return true;
- }
-
- public function matchSearch(Notice $notice, $search): bool
- {
- return (mb_stripos($notice->content, $search) !== false);
- }
-
- public function onStartNoticeSearchShowResults($action, $q, $notice): bool
- {
- $user = common_current_user();
- if ($user) {
- $search = $q;
- $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;
- }
-
- public function onEndSubGroupNav($widget): bool
- {
- $action = $widget->out;
- $action_name = $action->trimmed('action');
- $action->menuItem(
- common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
-
- _m('MENU', 'Searches'),
-
- _m('Configure search subscriptions'),
- $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname
- );
- return true;
- }
-
- public function onEndInterpretCommand($cmd, $arg, $user, &$result): bool
- {
- if ($result instanceof TrackCommand) {
- $result = new SearchSubTrackCommand($user, $arg);
- return false;
- } elseif ($result instanceof TrackOffCommand) {
- $result = new SearchSubTrackOffCommand($user);
- return false;
- } elseif ($result instanceof TrackingCommand) {
- $result = new SearchSubTrackingCommand($user);
- return false;
- } elseif ($result instanceof UntrackCommand) {
- $result = new SearchSubUntrackCommand($user, $arg);
- return false;
- } else {
- return true;
- }
- }
- public function onHelpCommandMessages($cmd, &$commands): void
- {
-
- $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
-
- $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
-
- $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
-
- $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
-
- $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
-
- $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
- }
- public function onEndDefaultLocalNav($menu, $user): bool
- {
- $user = common_current_user();
- if (!empty($user)) {
- $searches = SearchSub::forProfile($user->getProfile());
- if (!empty($searches) && count($searches) > 0) {
- $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
-
- $menu->submenu(_m('MENU', 'Searches'), $searchSubMenu);
- }
- }
- return true;
- }
- }
|