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

Add filters in notifications endpoint #267

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
13 changes: 11 additions & 2 deletions src/keria/app/notifying.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def on_get(req, rep):
"""
agent = req.context.agent

read = req.get_param_as_bool("read")
route = req.get_param("route")
order = req.get_param("order")

rng = req.get_header("Range")
if rng is None:
rep.status = falcon.HTTP_200
Expand All @@ -58,8 +62,13 @@ def on_get(req, rep):
rep.status = falcon.HTTP_206
start, end = httping.parseRangeHeader(rng, "notes")

count = agent.notifier.getNoteCnt()
notes = agent.notifier.getNotes(start=start, end=end)
notes = agent.notifier.getNotes(start=0, end=-1)
notes = [n for n in notes if read is None or n.read == read]
notes = [n for n in notes if route is None or ("r" in n.attrs and n.attrs["r"] == route)]
notes.sort(key=lambda n: n.datetime, reverse=order == "desc")
count = len(notes)
notes = notes[start:end + 1]

out = []
for note in notes:
attrs = note.pad
Expand Down
46 changes: 46 additions & 0 deletions tests/app/test_notifying.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ def test_notifications(helpers):
res = client.simulate_put(path=f"/notifications/{last}")
assert res.status_code == 404

# Load in reverse order
headers = dict(Range=f"notes=0-3")
params = {"order": "desc"}
res = client.simulate_get(path="/notifications", params=params, headers=headers)
assert res.status_code == 200
notes = res.json
assert len(notes) == 4
assert notes[3]['a'] == dict(a=1, b=2, c=3)
assert notes[2]['a'] == dict(a=1)
assert notes[1]['a'] == dict(a=2)
assert notes[0]['a'] == dict(a=3)
assert res.headers["Accept-Ranges"] == "notes"
assert res.headers["Content-Range"] == "notes 0-3/4"

last = notes[1]['i']
res = client.simulate_delete(path=f"/notifications/{last}")
assert res.status_code == 202
Expand All @@ -91,3 +105,35 @@ def test_notifications(helpers):
assert notes[0]['r'] is False
assert notes[1]['r'] is True
assert notes[2]['r'] is not True # just for fun

# Load unread notes
params = {"read": False}
res = client.simulate_get(path="/notifications", params=params)
assert res.status_code == 200
notes = res.json
assert len(notes) == 2
assert notes[0]['r'] == False
assert notes[1]['r'] == False
assert res.headers["Accept-Ranges"] == "notes"
assert res.headers["Content-Range"] == "notes 0-1/2"

# Load read notes
params = {"read": True}
res = client.simulate_get(path="/notifications", params=params)
assert res.status_code == 200
notes = res.json
assert len(notes) == 1
assert notes[0]['r'] == True
assert res.headers["Accept-Ranges"] == "notes"
assert res.headers["Content-Range"] == "notes 0-0/1"

# filter by route
assert agent.notifier.add(attrs=dict(r="/multisig/rev")) is True
params = {"route": "/multisig/rev"}
res = client.simulate_get(path="/notifications", params=params)
assert res.status_code == 200
notes = res.json
assert len(notes) == 1
assert notes[0]['a']['r'] == "/multisig/rev"
assert res.headers["Accept-Ranges"] == "notes"
assert res.headers["Content-Range"] == "notes 0-0/1"
Loading