From c877fe2c9007c038adffe69b2e62216796286e62 Mon Sep 17 00:00:00 2001 From: Derek Smith Date: Thu, 16 May 2024 13:20:36 -0400 Subject: [PATCH] Revert recent prism-list and Prism.get_all changes. --- bolt12-prism.py | 38 ++++++++++++++++++++++---------------- lib.py | 35 +++++++++++++++++------------------ 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/bolt12-prism.py b/bolt12-prism.py index f6d8562..81b0f9b 100755 --- a/bolt12-prism.py +++ b/bolt12-prism.py @@ -58,27 +58,33 @@ def createprism(plugin, members, prism_id="", outlay_factor: float = 1.0, pay_to @plugin.method("prism-list") def listprisms(plugin, prism_id=None): '''List prisms.''' + # if a prism_id is not supplied, we return all prism policy objects (like in listoffers) + if prism_id == None: + try: + prism_ids = Prism.find_all(plugin) + prisms = [] + for prism_id in prism_ids: + prism = Prism.get(plugin=plugin, prism_id=prism_id) + prisms.append(prism) + + return { + "prisms": [prism.to_dict() for prism in prisms] + } + + except RpcError as e: + plugin.log(e) + return e + else: + # otherwise we return a single document. + prism = Prism.get(plugin=plugin, prism_id=prism_id) - prisms = None - - try: - prisms = Prism.get(plugin=plugin, prism_id=prism_id) - except RpcError as e: - plugin.log(e) - return e - - if type(prisms) is not list: - if prism_id is not None: + if prism is None: raise Exception(f"Prism with id {prism_id} not found.") return { - "prisms": [] - } + "prisms": prism.to_dict() + } - # otherwise, enumerate the prism(s) - return { - "prisms": [prism.to_dict() for prism in prisms] - } @plugin.method("prism-update") diff --git a/lib.py b/lib.py index d78c2fd..c4a6a58 100644 --- a/lib.py +++ b/lib.py @@ -152,29 +152,28 @@ def from_db_string(plugin: Plugin, prism_string: str): outlay_factor = prism_dict.get("outlay_factor") return Prism(plugin, outlay_factor=outlay_factor, timestamp=timestamp, prism_id=prism_id, members=members) + + @staticmethod + def get(plugin: Plugin, prism_id: str): + prism_record = plugin.rpc.listdatastore( + key=Prism.datastore_key(id=prism_id))["datastore"] + + if not prism_record: + return None + + return Prism.from_db_string(plugin, prism_record[0]["string"]) @staticmethod - def get(plugin: Plugin, prism_id: str = None): - """ - Get a `Prism` object for a given `prism_id`, or all `Prism` objects in the db if no `prism_id` is provided. - """ - if not prism_id: - # look up all prisms - key = ["prism", prism_db_version, "prism"] - else: - # look up prism by id - key = Prism.datastore_key(id=prism_id) - + def find_all(plugin: Plugin): + key = ["prism", prism_db_version, "prism"] prism_records = plugin.rpc.listdatastore(key=key).get("datastore", []) - if not prism_records: - return None - - if prism_id: - return Prism.from_db_string(plugin, prism_records[0]["string"]) - else: - return [Prism.from_db_string(plugin, prism_record["string"]) for prism_record in prism_records] + prism_ids = [] + for prism in prism_records: + prism_id = prism["key"][3] + prism_ids.append(prism_id) + return prism_ids @staticmethod def create(plugin: Plugin, outlay_factor, prism_id: str = None, members: List[Member] = None):