From 6360e4d07d3977d869f3965f81ef01dd713a1e3b Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Sun, 17 Feb 2019 14:18:51 +0100 Subject: [PATCH] Add a --clear option to "notifications" command --- tests/test_console.py | 11 +++++++++++ toot/api.py | 4 ++++ toot/commands.py | 5 +++++ toot/console.py | 8 +++++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_console.py b/tests/test_console.py index ba02c54..7258e9b 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -546,6 +546,17 @@ def test_notifications(mock_get, capsys): ]) +@mock.patch('toot.http.post') +def test_notifications_clear(mock_post, capsys): + console.run_command(app, user, 'notifications', ['--clear']) + out, err = capsys.readouterr() + out = uncolorize(out) + + mock_post.assert_called_once_with(app, user, '/api/v1/notifications/clear') + assert not err + assert out == 'Cleared notifications\n' + + def u(user_id, access_token="abc"): username, instance = user_id.split("@") return { diff --git a/toot/api.py b/toot/api.py index b27fd49..ab02edd 100644 --- a/toot/api.py +++ b/toot/api.py @@ -260,6 +260,10 @@ def get_notifications(app, user): return http.get(app, user, '/api/v1/notifications').json() +def clear_notifications(app, user): + http.post(app, user, '/api/v1/notifications/clear') + + def get_instance(domain, scheme="https"): url = "{}://{}/api/v1/instance".format(scheme, domain) return http.anon_get(url).json() diff --git a/toot/commands.py b/toot/commands.py index 027538e..3805589 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -299,6 +299,11 @@ def instance(app, user, args): def notifications(app, user, args): + if args.clear: + api.clear_notifications(app, user) + print_out("Cleared notifications") + return + width = 100 for notification in sorted(api.get_notifications(app, user), key=lambda n: datetime.strptime( diff --git a/toot/console.py b/toot/console.py index 755c1c3..c73a19f 100644 --- a/toot/console.py +++ b/toot/console.py @@ -170,7 +170,13 @@ READ_COMMANDS = [ Command( name="notifications", description="Notifications for logged in user", - arguments=[], + arguments=[ + (["--clear"], { + "help": "delete all notifications from the server", + "action": 'store_true', + "default": False, + }), + ], require_auth=True, ), Command(