From 50543d8aa02ee123bae6fc9b2411a132f63f1895 Mon Sep 17 00:00:00 2001 From: Rodolfo Miranda Date: Thu, 4 Jul 2024 22:03:18 -0300 Subject: [PATCH] notification filters --- src/keria/app/notifying.py | 13 +++++++++-- tests/app/test_notifying.py | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/keria/app/notifying.py b/src/keria/app/notifying.py index a8781c5f..373d534b 100644 --- a/src/keria/app/notifying.py +++ b/src/keria/app/notifying.py @@ -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 @@ -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 diff --git a/tests/app/test_notifying.py b/tests/app/test_notifying.py index baa0845e..e75cabd3 100644 --- a/tests/app/test_notifying.py +++ b/tests/app/test_notifying.py @@ -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 @@ -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"