Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#640] add search query param to the drep/getVotes #713

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
Loading