From 75267e24e7e10bd6f8342d9750d74ec4b61de6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Taavi=20V=C3=A4=C3=A4n=C3=A4nen?= Date: Wed, 5 Feb 2025 19:24:33 +0200 Subject: [PATCH] Convert tokens given to use SearchEngine Summary: Now the page /token/given/ allows to sort tokens by newest (default) and oldest. The default sort is unchanged. The given tokens are also now easily usable into any Dashboard. This introduces creative space to add future filters. refs T15988 Test Plan: Tested that /token/ still renders fine. Order by Creation (Newest First): it works. Order by Creation (Oldest First): it works. Activate the DarkConsole's top bar and open the tab Services to inspect the generated queries, that are like this, and not anything alien, accordingly to the current "order by": SELECT * FROM `token_given` ORDER BY `id` DESC LIMIT 101 SELECT * FROM `token_given` ORDER BY `id` ASC LIMIT 101 Test the page /token/given/ as logged-out: it the Tokens app is configured as public, it still works like before. From the page /token/given/ order by "Creation (oldest first)", use {nav Use Results > Add Dashboard} and see that it works. From the same Dashboard: {nav Create Panel > Query Panel} and select "Tokens Given" with Limit=1 and title "Most Recent Tokenzzz" and see that it works as expected. Visit the Leader Board page at /token/leaders/: it still works like before. Reviewers: O1 Blessed Committers, valerio.bozzolan Reviewed By: O1 Blessed Committers, valerio.bozzolan Subscribers: tobiaswiese, valerio.bozzolan, Matthew, Cigaryno Maniphest Tasks: T15988 Differential Revision: https://we.phorge.it/D25863 --- src/__phutil_library_map__.php | 2 + .../PhabricatorTokensApplication.php | 3 +- .../PhabricatorTokenGivenController.php | 81 ++++------------ .../PhabricatorTokenGivenSearchEngine.php | 96 +++++++++++++++++++ 4 files changed, 116 insertions(+), 66 deletions(-) create mode 100644 src/applications/tokens/query/PhabricatorTokenGivenSearchEngine.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index e1b649ce36..1ebc25692d 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -5047,6 +5047,7 @@ phutil_register_library_map(array( 'PhabricatorTokenGivenEditor' => 'applications/tokens/editor/PhabricatorTokenGivenEditor.php', 'PhabricatorTokenGivenFeedStory' => 'applications/tokens/feed/PhabricatorTokenGivenFeedStory.php', 'PhabricatorTokenGivenQuery' => 'applications/tokens/query/PhabricatorTokenGivenQuery.php', + 'PhabricatorTokenGivenSearchEngine' => 'applications/tokens/query/PhabricatorTokenGivenSearchEngine.php', 'PhabricatorTokenLeaderController' => 'applications/tokens/controller/PhabricatorTokenLeaderController.php', 'PhabricatorTokenQuery' => 'applications/tokens/query/PhabricatorTokenQuery.php', 'PhabricatorTokenReceiverInterface' => 'applications/tokens/interface/PhabricatorTokenReceiverInterface.php', @@ -11804,6 +11805,7 @@ phutil_register_library_map(array( 'PhabricatorTokenGivenEditor' => 'PhabricatorEditor', 'PhabricatorTokenGivenFeedStory' => 'PhabricatorFeedStory', 'PhabricatorTokenGivenQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', + 'PhabricatorTokenGivenSearchEngine' => 'PhabricatorApplicationSearchEngine', 'PhabricatorTokenLeaderController' => 'PhabricatorTokenController', 'PhabricatorTokenQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorTokenReceiverQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', diff --git a/src/applications/tokens/application/PhabricatorTokensApplication.php b/src/applications/tokens/application/PhabricatorTokensApplication.php index 0ef130a282..eb7f386857 100644 --- a/src/applications/tokens/application/PhabricatorTokensApplication.php +++ b/src/applications/tokens/application/PhabricatorTokensApplication.php @@ -30,7 +30,8 @@ final class PhabricatorTokensApplication extends PhabricatorApplication { return array( '/token/' => array( '' => 'PhabricatorTokenGivenController', - 'given/' => 'PhabricatorTokenGivenController', + $this->getQueryRoutePattern('given/') => + 'PhabricatorTokenGivenController', 'give/(?[^/]+)/' => 'PhabricatorTokenGiveController', 'leaders/' => 'PhabricatorTokenLeaderController', ), diff --git a/src/applications/tokens/controller/PhabricatorTokenGivenController.php b/src/applications/tokens/controller/PhabricatorTokenGivenController.php index 8352c35d69..e9c9e345c2 100644 --- a/src/applications/tokens/controller/PhabricatorTokenGivenController.php +++ b/src/applications/tokens/controller/PhabricatorTokenGivenController.php @@ -6,75 +6,26 @@ final class PhabricatorTokenGivenController extends PhabricatorTokenController { return true; } - public function handleRequest(AphrontRequest $request) { - $viewer = $request->getViewer(); + public function handleRequest(AphrontRequest $request) { + $querykey = $request->getURIData('queryKey'); - $pager = id(new AphrontCursorPagerView()) - ->readFromRequest($request); + $controller = id(new PhabricatorApplicationSearchController()) + ->setQueryKey($querykey) + ->setSearchEngine(new PhabricatorTokenGivenSearchEngine()) + ->setNavigation($this->buildSideNav()); - $tokens_given = id(new PhabricatorTokenGivenQuery()) + return $this->delegateToController($controller); + } + + protected function buildSideNav() { + $nav = parent::buildSideNav(); + $viewer = $this->getViewer(); + + id(new PhabricatorTokenGivenSearchEngine()) ->setViewer($viewer) - ->executeWithCursorPager($pager); - - $handles = array(); - if ($tokens_given) { - $object_phids = mpull($tokens_given, 'getObjectPHID'); - $viewer_phids = mpull($tokens_given, 'getAuthorPHID'); - $handle_phids = array_merge($object_phids, $viewer_phids); - $handles = id(new PhabricatorHandleQuery()) - ->setViewer($viewer) - ->withPHIDs($handle_phids) - ->execute(); - } - - $tokens = array(); - if ($tokens_given) { - $token_phids = mpull($tokens_given, 'getTokenPHID'); - $tokens = id(new PhabricatorTokenQuery()) - ->setViewer($viewer) - ->withPHIDs($token_phids) - ->execute(); - $tokens = mpull($tokens, null, 'getPHID'); - } - - $list = new PHUIObjectItemListView(); - foreach ($tokens_given as $token_given) { - $handle = $handles[$token_given->getObjectPHID()]; - $token = idx($tokens, $token_given->getTokenPHID()); - - $item = id(new PHUIObjectItemView()); - $item->setHeader($handle->getFullName()); - $item->setHref($handle->getURI()); - - $item->addAttribute($token->renderIcon()); - - $item->addAttribute( - pht( - 'Given by %s on %s', - $handles[$token_given->getAuthorPHID()]->renderLink(), - phabricator_date($token_given->getDateCreated(), $viewer))); - - $list->addItem($item); - } - $title = pht('Tokens Given'); - - $box = id(new PHUIObjectBoxView()) - ->setHeaderText($title) - ->setObjectList($list); - - $nav = $this->buildSideNav(); - $nav->setCrumbs( - $this->buildApplicationCrumbs() - ->addTextCrumb($title)); - $nav->selectFilter('given/'); - - $nav->appendChild($box); - $nav->appendChild($pager); - - return $this->newPage() - ->setTitle($title) - ->appendChild($nav); + ->addNavigationItems($nav->getMenu()); + return $nav; } } diff --git a/src/applications/tokens/query/PhabricatorTokenGivenSearchEngine.php b/src/applications/tokens/query/PhabricatorTokenGivenSearchEngine.php new file mode 100644 index 0000000000..505ee3ef2a --- /dev/null +++ b/src/applications/tokens/query/PhabricatorTokenGivenSearchEngine.php @@ -0,0 +1,96 @@ +newQuery(); + } + + protected function getRequiredHandlePHIDsForResultList( + array $tokens_given, + PhabricatorSavedQuery $query) { + $object_phids = mpull($tokens_given, 'getObjectPHID'); + $viewer_phids = mpull($tokens_given, 'getAuthorPHID'); + return array_merge($object_phids, $viewer_phids); + } + + protected function renderResultList( + array $tokens_given, + PhabricatorSavedQuery $saved, + array $handles) { + $viewer = $this->requireViewer(); + + $tokens = array(); + if ($tokens_given) { + $token_phids = mpull($tokens_given, 'getTokenPHID'); + $tokens = id(new PhabricatorTokenQuery()) + ->setViewer($viewer) + ->withPHIDs($token_phids) + ->execute(); + $tokens = mpull($tokens, null, 'getPHID'); + } + + $list = new PHUIObjectItemListView(); + foreach ($tokens_given as $token_given) { + $handle = $handles[$token_given->getObjectPHID()]; + $token = idx($tokens, $token_given->getTokenPHID()); + + $item = id(new PHUIObjectItemView()); + $item->setHeader($handle->getFullName()); + $item->setHref($handle->getURI()); + + $item->addAttribute($token->renderIcon()); + + $item->addAttribute( + pht( + 'Given by %s on %s', + $handles[$token_given->getAuthorPHID()]->renderLink(), + phabricator_date($token_given->getDateCreated(), $viewer))); + + $list->addItem($item); + } + + return id(new PhabricatorApplicationSearchResultView()) + ->setObjectList($list); + } + + protected function getBuiltinQueryNames() { + $names = array(); + $names['all'] = pht('All Tokens Given'); + + return $names; + } + + public function buildSavedQueryFromBuiltin($query_key) { + $query = $this->newSavedQuery(); + $query->setQueryKey($query_key); + + switch ($query_key) { + case 'all': + return $query; + } + + return parent::buildSavedQueryFromBuiltin($query_key); + } + + protected function getURI($path) { + return '/token/given/'.$path; + } +}