Skip to content

Commit

Permalink
[#640] add search query param to the drep/getVotes
Browse files Browse the repository at this point in the history
  • Loading branch information
jankun4 committed Apr 15, 2024
1 parent 773c9ca commit 94d1a53
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ changes.

### Added

- added search query param to the `drep/getVotes` [Issue 640](https://github.com/IntersectMBO/govtool/issues/640)
- added drepView and txHash to the `ada-holder/get-current-delegation` [Issue 689](https://github.com/IntersectMBO/govtool/issues/689)
- addded latestTxHash to the `drep/info` and `drep/list` endpoints [Issue 627](https://github.com/IntersectMBO/govtool/issues/627)
- added `txHash` to `drep/getVotes` [Issue 626](https://github.com/IntersectMBO/govtool/issues/626)
Expand Down
53 changes: 28 additions & 25 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import Control.Monad.Reader
import Data.Bool (Bool)
import Data.List (sortOn)
import qualified Data.Map as Map
import Data.Maybe (Maybe (Nothing), fromMaybe)
import Data.Maybe (Maybe (Nothing), fromMaybe, catMaybes)
import Data.Ord (Down (..))
import Data.Text hiding (drop, elem, filter, length, map,
null, take)
null, take, any)
import qualified Data.Text as Text

import Numeric.Natural (Natural)
Expand All @@ -45,7 +45,12 @@ import VVA.Types (App, AppEnv (..),
type VVAApi =
"drep" :> "list" :> QueryParam "drepView" Text :> Get '[JSON] [DRep]
:<|> "drep" :> "get-voting-power" :> Capture "drepId" HexText :> Get '[JSON] Integer
:<|> "drep" :> "getVotes" :> Capture "drepId" HexText :> QueryParams "type" GovernanceActionType :> QueryParam "sort" GovernanceActionSortMode :> Get '[JSON] [VoteResponse]
:<|> "drep" :> "getVotes"
:> Capture "drepId" HexText
:> QueryParams "type" GovernanceActionType
:> QueryParam "sort" GovernanceActionSortMode
:> QueryParam "search" Text
:> Get '[JSON] [VoteResponse]
:<|> "drep" :> "info" :> Capture "drepId" HexText :> Get '[JSON] DRepInfoResponse
:<|> "ada-holder" :> "get-current-delegation" :> Capture "stakeKey" HexText :> Get '[JSON] (Maybe DelegationResponse)
:<|> "ada-holder" :> "get-voting-power" :> Capture "stakeKey" HexText :> Get '[JSON] Integer
Expand Down Expand Up @@ -192,12 +197,12 @@ mapSortAndFilterProposals selectedTypes sortMode proposals =
Just MostYesVotes -> sortOn (Down . proposalResponseYesVotes) filteredProposals
in sortedProposals

getVotes :: App m => HexText -> [GovernanceActionType] -> Maybe GovernanceActionSortMode -> m [VoteResponse]
getVotes (unHexText -> dRepId) selectedTypes sortMode = do
getVotes :: App m => HexText -> [GovernanceActionType] -> Maybe GovernanceActionSortMode -> Maybe Text -> m [VoteResponse]
getVotes (unHexText -> dRepId) selectedTypes sortMode mSearch = do
CacheEnv {dRepGetVotesCache} <- asks vvaCache
(votes, proposals) <- cacheRequest dRepGetVotesCache dRepId $ DRep.getVotes dRepId []
let voteMap = Map.fromList $ map (\vote@Types.Vote {..} -> (voteProposalId, vote)) votes
let processedProposals = mapSortAndFilterProposals selectedTypes sortMode proposals
let processedProposals = filter (isProposalSearchedFor mSearch) $ mapSortAndFilterProposals selectedTypes sortMode proposals
return $
[ VoteResponse
{ voteResponseVote = voteToResponse (voteMap Map.! read (unpack proposalResponseId))
Expand Down Expand Up @@ -237,6 +242,21 @@ getStakeKeyVotingPower (unHexText -> stakeKey) = do
cacheRequest adaHolderVotingPowerCache stakeKey $ AdaHolder.getStakeKeyVotingPower stakeKey


isProposalSearchedFor :: Maybe Text -> ProposalResponse -> Bool
isProposalSearchedFor Nothing _ = True
isProposalSearchedFor (Just searchQuery) (ProposalResponse{..}) = fromMaybe False $ do
let normalisedSearchQuery = Text.toLower searchQuery
let govActionId = unHexText proposalResponseTxHash <> "#" <> Text.pack (show proposalResponseIndex)
let valuesToCheck = catMaybes
[ Just govActionId
, proposalResponseTitle
, proposalResponseAbout
, proposalResponseMotivation
, proposalResponseRationale
]

pure $ any (\x -> normalisedSearchQuery `isInfixOf` Text.toLower x) valuesToCheck

listProposals
:: App m
=> [GovernanceActionType]
Expand All @@ -255,32 +275,15 @@ listProposals selectedTypes sortMode mPage mPageSize mDrepRaw mSearchQuery = do
Nothing -> return []
Just drepId ->
map (voteParamsProposalId . voteResponseVote)
<$> getVotes drepId [] Nothing



let filterF ProposalResponse{..} = case Text.toLower <$> mSearchQuery of
Nothing -> True
Just searchQuery -> fromMaybe False $ do
title <- Text.toLower <$> proposalResponseTitle
about <- Text.toLower <$> proposalResponseAbout
motivation <- Text.toLower <$> proposalResponseMotivation
rationale <- Text.toLower <$> proposalResponseRationale
let govActionId = unHexText proposalResponseTxHash <> "#" <> Text.pack (show proposalResponseIndex)
let result = searchQuery `isInfixOf` title
|| searchQuery `isInfixOf` about
|| searchQuery `isInfixOf` motivation
|| searchQuery `isInfixOf` rationale
|| searchQuery `isInfixOf` govActionId
<$> getVotes drepId [] Nothing Nothing

pure result

CacheEnv {proposalListCache} <- asks vvaCache
mappedAndSortedProposals <-
filter
( \p@ProposalResponse {proposalResponseId} ->
proposalResponseId `notElem` proposalsToRemove
&& filterF p
&& isProposalSearchedFor mSearchQuery p
) . mapSortAndFilterProposals selectedTypes sortMode <$> cacheRequest proposalListCache () Proposal.listProposals

let total = length mappedAndSortedProposals :: Int
Expand Down

0 comments on commit 94d1a53

Please sign in to comment.