Skip to content

Commit

Permalink
Revert recent prism-list and Prism.get_all changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
farscapian committed May 16, 2024
1 parent 2a05b58 commit c877fe2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
38 changes: 22 additions & 16 deletions bolt12-prism.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 17 additions & 18 deletions lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c877fe2

Please sign in to comment.