collection resolve endpoint

This commit is contained in:
jessop 2019-11-13 21:16:27 -05:00 committed by Lex Berezhny
parent 25a91f89fd
commit f7c94c9eb5
3 changed files with 81 additions and 30 deletions

View file

@ -2011,7 +2011,7 @@ class Daemon(metaclass=JSONRPCServerType):
[--allow_duplicate_purchase] [--override_max_key_fee] [--preview] [--blocking] [--allow_duplicate_purchase] [--override_max_key_fee] [--preview] [--blocking]
Options: Options:
--claim_id=<claim_id> : (str) id of claim to purchase --claim_id=<claim_id> : (str) claim id of claim to purchase
--url=<url> : (str) lookup claim to purchase by url --url=<url> : (str) lookup claim to purchase by url
--wallet_id=<wallet_id> : (str) restrict operation to specific wallet --wallet_id=<wallet_id> : (str) restrict operation to specific wallet
--funding_account_ids=<funding_account_ids>: (list) ids of accounts to fund this transaction --funding_account_ids=<funding_account_ids>: (list) ids of accounts to fund this transaction
@ -3574,20 +3574,20 @@ class Daemon(metaclass=JSONRPCServerType):
return await self.jsonrpc_stream_abandon(*args, **kwargs) return await self.jsonrpc_stream_abandon(*args, **kwargs)
@requires(WALLET_COMPONENT) @requires(WALLET_COMPONENT)
def jsonrpc_collection_list(self, resolve_claims=False, account_id=None, wallet_id=None, page=None, page_size=None): def jsonrpc_collection_list(self, resolve_claims=0, account_id=None, wallet_id=None, page=None, page_size=None):
""" """
List my collection claims. List my collection claims.
Usage: Usage:
collection_list [--resolve_claims] [<account_id> | --account_id=<account_id>] [--wallet_id=<wallet_id>] collection_list [--resolve_claims=<resolve_claims>] [<account_id> | --account_id=<account_id>] [--wallet_id=<wallet_id>]
[--page=<page>] [--page_size=<page_size>] [--page=<page>] [--page_size=<page_size>]
Options: Options:
--resolve_claims : (bool) resolve every claim --resolve_claims=<resolve_claims> : (int) resolve every claim
--account_id=<account_id> : (str) id of the account to use --account_id=<account_id> : (str) id of the account to use
--wallet_id=<wallet_id> : (str) restrict results to specific wallet --wallet_id=<wallet_id> : (str) restrict results to specific wallet
--page=<page> : (int) page to return during paginating --page=<page> : (int) page to return during paginating
--page_size=<page_size> : (int) number of items on page during pagination --page_size=<page_size> : (int) number of items on page during pagination
Returns: {Paginated[Output]} Returns: {Paginated[Output]}
""" """
@ -3599,8 +3599,45 @@ class Daemon(metaclass=JSONRPCServerType):
else: else:
collections = partial(self.ledger.get_collections, wallet=wallet, accounts=wallet.accounts) collections = partial(self.ledger.get_collections, wallet=wallet, accounts=wallet.accounts)
collection_count = partial(self.ledger.get_collection_count, wallet=wallet, accounts=wallet.accounts) collection_count = partial(self.ledger.get_collection_count, wallet=wallet, accounts=wallet.accounts)
return paginate_rows(collections, collection_count, page, page_size, resolve=resolve_claims) return paginate_rows(collections, collection_count, page, page_size, resolve_claims=resolve_claims)
async def jsonrpc_collection_resolve(
self, claim_id=None, url=None, wallet_id=None, page=1, page_size=DEFAULT_PAGE_SIZE):
"""
Resolve claims in the collection.
Usage:
collection_resolve (--claim_id=<claim_id> | --url=<url>)
[--wallet_id=<wallet_id>] [--page=<page>] [--page_size=<page_size>]
Options:
--claim_id=<claim_id> : (str) claim id of the collection
--url=<url> : (str) url of the collection
--wallet_id=<wallet_id> : (str) restrict results to specific wallet
--page=<page> : (int) page to return during paginating
--page_size=<page_size> : (int) number of items on page during pagination
Returns: {Paginated[Output]}
"""
wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
if claim_id:
txo = await self.ledger.get_claim_by_claim_id(wallet.accounts, claim_id)
if not isinstance(txo, Output) or not txo.is_claim:
raise Exception(f"Could not find collection with claim_id '{claim_id}'. ")
elif url:
txo = (await self.ledger.resolve(wallet.accounts, [url]))[url]
if not isinstance(txo, Output) or not txo.is_claim:
raise Exception(f"Could not find collection with url '{url}'. ")
else:
raise Exception(f"Missing argument claim_id or url. ")
page_num, page_size = abs(page), min(abs(page_size), 50)
items = await self.ledger.resolve_collection(txo, page_size * (page_num - 1), page_size)
total_items = len(txo.claim.collection.claims.ids)
return {"items": items, 'total_pages': int((total_items + (page_size - 1)) / page_size),
'total_items': total_items, 'page_size': page_size, 'page': page_num}
SUPPORT_DOC = """ SUPPORT_DOC = """
Create, list and abandon all types of supports. Create, list and abandon all types of supports.

View file

@ -186,29 +186,33 @@ class MainNetLedger(BaseLedger):
def get_channel_count(self, **constraints): def get_channel_count(self, **constraints):
return self.db.get_channel_count(**constraints) return self.db.get_channel_count(**constraints)
async def get_collections(self, resolve=False, **constraints): async def resolve_collection(self, collection, offset=0, page_size=1):
claim_ids = collection.claim.collection.claims.ids[offset:page_size+offset]
try:
resolve_results, _, _ = await self.claim_search([], claim_ids=claim_ids)
except:
log.exception("Resolve failed while looking up collection claim ids:")
return []
claims = []
for claim_id in claim_ids:
found = False
for txo in resolve_results:
if txo.claim_id == claim_id:
claims.append(txo)
found = True
break
if not found:
claims.append(None)
return claims
async def get_collections(self, resolve_claims=0, **constraints):
collections = await self.db.get_collections(**constraints) collections = await self.db.get_collections(**constraints)
if resolve: if resolve_claims > 0:
for collection in collections: for collection in collections:
claim_ids = collection.claim.collection.claims.ids; collection.claims = await self.resolve_collection(collection, page_size=resolve_claims)
try:
resolve_results, _, _ = await self.claim_search([], claim_ids=collection.claim.collection.claims.ids)
except:
log.exception("Resolve failed while looking up collection claim ids:")
claims = []
for claim_id in claim_ids:
found = False
for txo in resolve_results:
if txo.claim_id == claim_id:
claims.append(txo)
found = True
break
if not found:
claims.append(None)
collection.claims = claims
return collections return collections
def get_collection_count(self, resolve=False, **constraints): def get_collection_count(self, resolve_claims=0, **constraints):
return self.db.get_collection_count(**constraints) return self.db.get_collection_count(**constraints)
def get_supports(self, **constraints): def get_supports(self, **constraints):

View file

@ -1293,13 +1293,18 @@ class CollectionCommands(CommandTestCase):
self.assertEqual(collections['items'][0]['value']['claims'], claim_ids) self.assertEqual(collections['items'][0]['value']['claims'], claim_ids)
self.assertNotIn('claims', collections['items'][0]) self.assertNotIn('claims', collections['items'][0])
await self.collection_create('radjingles', claims=claim_ids, allow_duplicate_name=True) tx = await self.collection_create('radjingles', claims=claim_ids, allow_duplicate_name=True)
claim_id2 = self.get_claim_id(tx)
self.assertItemCount(await self.daemon.jsonrpc_collection_list(), 2) self.assertItemCount(await self.daemon.jsonrpc_collection_list(), 2)
await self.collection_abandon(claim_id) await self.collection_abandon(claim_id)
self.assertItemCount(await self.daemon.jsonrpc_collection_list(), 1) self.assertItemCount(await self.daemon.jsonrpc_collection_list(), 1)
collections = await self.out(self.daemon.jsonrpc_collection_list(resolve_claims=True)) collections = await self.out(self.daemon.jsonrpc_collection_list(resolve_claims=2))
self.assertEquals(len(collections['items'][0]['claims']), 2)
collections = await self.out(self.daemon.jsonrpc_collection_list(resolve_claims=10))
self.assertEqual(len(collections['items'][0]['claims']), 4)
self.assertEqual(collections['items'][0]['claims'][0]['name'], 'stream-one') self.assertEqual(collections['items'][0]['claims'][0]['name'], 'stream-one')
self.assertEqual(collections['items'][0]['claims'][1]['name'], 'stream-two') self.assertEqual(collections['items'][0]['claims'][1]['name'], 'stream-two')
self.assertEqual(collections['items'][0]['claims'][2]['name'], 'stream-one') self.assertEqual(collections['items'][0]['claims'][2]['name'], 'stream-one')
@ -1309,3 +1314,8 @@ class CollectionCommands(CommandTestCase):
self.assertEqual(claims['items'][0]['name'], 'radjingles') self.assertEqual(claims['items'][0]['name'], 'radjingles')
self.assertEqual(claims['items'][1]['name'], 'stream-two') self.assertEqual(claims['items'][1]['name'], 'stream-two')
self.assertEqual(claims['items'][2]['name'], 'stream-one') self.assertEqual(claims['items'][2]['name'], 'stream-one')
claims = await self.out(self.daemon.jsonrpc_collection_resolve(claim_id2))
self.assertEqual(claims['items'][0]['name'], 'stream-one')
self.assertEqual(claims['items'][1]['name'], 'stream-two')
self.assertEqual(claims['items'][2]['name'], 'stream-one')