From 4b88b86b6fa29de143c4c588b90633a74a131576 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Thu, 14 Nov 2024 09:39:10 +0100 Subject: [PATCH] Add --json option to notifications --- toot/api.py | 4 ++-- toot/cli/timelines.py | 23 +++++++++++++++-------- toot/output.py | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/toot/api.py b/toot/api.py index 339710f..5e3ba74 100644 --- a/toot/api.py +++ b/toot/api.py @@ -697,9 +697,9 @@ def verify_credentials(app, user) -> Response: return http.get(app, user, '/api/v1/accounts/verify_credentials') -def get_notifications(app, user, types=[], exclude_types=[], limit=20): +def get_notifications(app, user, types=[], exclude_types=[], limit=20) -> Response: params = {"types[]": types, "exclude_types[]": exclude_types, "limit": limit} - return http.get(app, user, '/api/v1/notifications', params).json() + return http.get(app, user, '/api/v1/notifications', params) def clear_notifications(app, user): diff --git a/toot/cli/timelines.py b/toot/cli/timelines.py index aee83de..89f7493 100644 --- a/toot/cli/timelines.py +++ b/toot/cli/timelines.py @@ -2,12 +2,12 @@ import sys import click from toot import api -from toot.cli import InstanceParamType, cli, get_context, pass_context, Context +from toot.cli import InstanceParamType, cli, get_context, pass_context, Context, json_option from typing import Optional from toot.cli.validators import validate_instance from toot.entities import Notification, Status, from_dict -from toot.output import print_notifications, print_timeline +from toot.output import print_notifications, print_timeline, print_warning @cli.command() @@ -123,12 +123,14 @@ def bookmarks( "--mentions", "-m", is_flag=True, help="Show only mentions" ) +@json_option @pass_context def notifications( ctx: Context, clear: bool, reverse: bool, - mentions: int, + mentions: bool, + json: bool, ): """Show notifications""" if clear: @@ -142,17 +144,22 @@ def notifications( # https://docs.joinmastodon.org/methods/notifications/ exclude = ["follow", "favourite", "reblog", "poll", "follow_request"] - notifications = api.get_notifications(ctx.app, ctx.user, exclude_types=exclude) + response = api.get_notifications(ctx.app, ctx.user, exclude_types=exclude) - if not notifications: - click.echo("You have no notifications") + if json: + if reverse: + print_warning("--reverse is not supported alongside --json, ignoring") + click.echo(response.text) return + notifications = [from_dict(Notification, n) for n in response.json()] if reverse: notifications = reversed(notifications) - notifications = [from_dict(Notification, n) for n in notifications] - print_notifications(notifications) + if notifications: + print_notifications(notifications) + else: + click.echo("You have no notifications") def _show_timeline(generator, reverse, once): diff --git a/toot/output.py b/toot/output.py index 60fb005..3818e25 100644 --- a/toot/output.py +++ b/toot/output.py @@ -281,7 +281,7 @@ def print_notification(notification: Notification): print_status(notification.status) -def print_notifications(notifications: t.List[Notification]): +def print_notifications(notifications: t.Iterable[Notification]): for notification in notifications: if notification.type not in ["pleroma:emoji_reaction"]: print_divider()