From 23723f80418a2684357a1ed73388563af73137a9 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 10 Aug 2020 18:37:21 -0400 Subject: [PATCH] added support_search to api --- lbry/db/queries/search.py | 2 +- lbry/service/api.py | 55 +++++++++++++++++++++++++++++++++++++++ lbry/service/base.py | 3 +++ lbry/service/full_node.py | 3 +++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lbry/db/queries/search.py b/lbry/db/queries/search.py index 98f5098c6..1bfcd80d9 100644 --- a/lbry/db/queries/search.py +++ b/lbry/db/queries/search.py @@ -41,7 +41,7 @@ def select_supports(cols: List = None, **constraints) -> Select: def search_supports(**constraints) -> Tuple[List[Output], Optional[int]]: total = None - if not constraints.pop('no_totals', False): + if constraints.pop('include_total', False): total = search_support_count(**constraints) rows = context().fetchall(select_supports(**constraints)) txos = rows_to_txos(rows, include_tx=False) diff --git a/lbry/service/api.py b/lbry/service/api.py index 7affa0bc2..014f7d39b 100644 --- a/lbry/service/api.py +++ b/lbry/service/api.py @@ -367,6 +367,18 @@ def txo_filter_kwargs( pass +@expander +def support_filter_kwargs( + claim_id: StrOrList = None, # full claim id + txid: str = None, # transaction id + nout: int = None, # position in the transaction + height: int = None, # last updated block height (supports equality constraints) + timestamp: int = None, # last updated timestamp (supports equality constraints) + amount: str = None, # claim amount (supports equality constraints) +): + pass + + class API: def __init__(self, service: Service): @@ -2514,6 +2526,49 @@ class API: kwargs['is_my_output'] = True return await self.txo_list(*args, **kwargs) + async def support_search( + self, + wallet_id: str = None, # wallet to check if support is owned by user + order_by: StrOrList = None, # field to order by + **support_filter_and_pagination_kwargs + ) -> Paginated[Output]: # search results + """ + Search for supports on the blockchain. + + Arguments marked with "supports equality constraints" allow prepending the + value with an equality constraint such as '>', '>=', '<' and '<=' + eg. --height=">400000" would limit results to only supports above 400k block height. + + Usage: + support search [--wallet_id=] [--order_by=...] + {kwargs} + + """ + support_filter_dict, kwargs = pop_kwargs('support_filter', support_filter_kwargs( + **support_filter_and_pagination_kwargs + )) + pagination, kwargs = pop_kwargs('pagination', pagination_kwargs(**kwargs)) + wallet = self.wallets.get_or_default(wallet_id) + page_num = abs(pagination['page'] or 1) + page_size = min(abs(pagination['page_size'] or DEFAULT_PAGE_SIZE), 50) + support_filter_dict.update({ + 'offset': page_size * (page_num - 1), 'limit': page_size, + 'include_total': pagination['include_total'], + 'order_by': order_by + }) + result = await self.service.search_supports( + wallet.accounts, **remove_nulls(support_filter_dict) + ) + d = { + "items": result.rows, + "page": page_num, + "page_size": page_size + } + if pagination['include_total']: + d['total_pages'] = int((result.total + (page_size - 1)) / page_size) + d['total_items'] = result.total + return d + async def support_abandon( self, keep: str = None, # amount of lbc to keep as support diff --git a/lbry/service/base.py b/lbry/service/base.py index 62c3b2493..cae882ccf 100644 --- a/lbry/service/base.py +++ b/lbry/service/base.py @@ -194,6 +194,9 @@ class Service: async def search_claims(self, accounts, **kwargs) -> Tuple[List[Output], Optional[int], Censor]: raise NotImplementedError + async def search_supports(self, accounts, **kwargs) -> Tuple[List[Output], Optional[int]]: + raise NotImplementedError + async def get_claim_by_claim_id(self, accounts, claim_id, **kwargs) -> Output: for claim in (await self.search_claims(accounts, claim_id=claim_id, **kwargs))[0]: return claim diff --git a/lbry/service/full_node.py b/lbry/service/full_node.py index a51f20ec0..07bf1c1fd 100644 --- a/lbry/service/full_node.py +++ b/lbry/service/full_node.py @@ -57,6 +57,9 @@ class FullNode(Service): async def protobuf_search_claims(self, **kwargs): return await self.db.protobuf_search_claims(**kwargs) + async def search_supports(self, accounts, **kwargs): + return await self.db.search_supports(**kwargs) + async def resolve(self, urls, **kwargs): return await self.db.resolve(urls, **kwargs)