From bfdd84870f6e20c26dc6c4b73d884e79f8b5de89 Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Tue, 14 Mar 2023 19:14:58 -0400 Subject: [PATCH] Added "toot list_delete" and "toot list_create" commands --- requirements.txt | 1 + toot/api.py | 21 +++++++++++++++------ toot/commands.py | 16 ++++++++++++++-- toot/console.py | 42 +++++++++++++++++++++++++++++++++++++++--- toot/http.py | 4 ++-- toot/output.py | 6 +++--- toot/tui/scroll.py | 2 +- 7 files changed, 75 insertions(+), 17 deletions(-) diff --git a/requirements.txt b/requirements.txt index 67ddf98..3616ac3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ requests>=2.13,<3.0 beautifulsoup4>=4.5.0,<5.0 wcwidth>=0.1.7 urwid>=2.0.0,<3.0 + diff --git a/toot/api.py b/toot/api.py index d0edb88..b4e0bd1 100644 --- a/toot/api.py +++ b/toot/api.py @@ -534,9 +534,18 @@ def find_list_id(app, user, title): return None -def get_list_accounts(app, user, title): - id = find_list_id(app, user, title) - if id: - path = "/api/v1/{id}/accounts" - return _get_response_list(app, user, path) - return [] +def get_list_accounts(app, user, list_id): + path = f"/api/v1/lists/{list_id}/accounts" + return _get_response_list(app, user, path) + + +def create_list(app, user, title, replies_policy): + url = "/api/v1/lists" + json = {'title': title} + if replies_policy: + json['replies_policy'] = replies_policy + return http.post(app, user, url, json=json).json() + + +def delete_list(app, user, id): + return http.delete(app, user, f"/api/v1/lists/{id}") diff --git a/toot/commands.py b/toot/commands.py index cc513ed..5ac4aca 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -430,8 +430,20 @@ def lists(app, user, args): def list_accounts(app, user, args): - response = api.get_list_accounts(app, user, args.title) - print_list_accounts(args.title[0], response) + id = args.id if args.id else api.find_list_id(app, user, args.title) + response = api.get_list_accounts(app, user, id) + print_list_accounts(response) + + +def list_create(app, user, args): + api.create_list(app, user, title=args.title, replies_policy=args.replies_policy) + print_out(f"✓ List \"{args.title}\" created.") + + +def list_delete(app, user, args): + id = args.id if args.id else api.find_list_id(app, user, args.title) + api.delete_list(app, user, id) + print_out(f"✓ List \"{args.title}\" deleted.") def mute(app, user, args): diff --git a/toot/console.py b/toot/console.py index 146e5b6..b53f339 100644 --- a/toot/console.py +++ b/toot/console.py @@ -727,16 +727,52 @@ TAG_COMMANDS = [ LIST_COMMANDS = [ Command( name="lists", - description="List all user lists", + description="List all lists", arguments=[], require_auth=True, ), Command( name="list_accounts", description="List the accounts in a list", - arguments=[ + arguments=[(["--id"], { + "type": str, + "help": "ID of the list" + }), + (["--title"], { + "type": str, + "help": "title of the list" + }), + ], + require_auth=True, + ), + Command( + name="list_create", + description="Create a list", + arguments=[ + (["--id"], { + "type": str, + "help": "ID of the list" + }), + (["--title"], { + "type": str, + "help": "title of the list" + }), + (["--replies-policy"], { + "type": str, + "help": "replies policy: 'followed', 'list', or 'none' (defaults to 'none')" + }), + ], + require_auth=True, + ), + Command( + name="list_delete", + description="Delete a list", + arguments=[ + (["--id"], { + "type": str, + "help": "ID of the list" + }), (["--title"], { - "action": "append", "type": str, "help": "title of the list" }), diff --git a/toot/http.py b/toot/http.py index 597edc9..4e62bda 100644 --- a/toot/http.py +++ b/toot/http.py @@ -92,13 +92,13 @@ def patch(app, user, path, headers=None, files=None, data=None, json=None): return process_response(response) -def delete(app, user, path, data=None, headers=None): +def delete(app, user, path, data=None, json=None, headers=None): url = app.base_url + path headers = headers or {} headers["Authorization"] = f"Bearer {user.access_token}" - request = Request('DELETE', url, headers=headers, json=data) + request = Request('DELETE', url, headers=headers, data=data, json=json) response = send_request(request) return process_response(response) diff --git a/toot/output.py b/toot/output.py index 1114748..25f7252 100644 --- a/toot/output.py +++ b/toot/output.py @@ -214,15 +214,15 @@ def print_list_list(lists): if lists: for list_item in lists: replies_policy = list_item['replies_policy'] if list_item['replies_policy'] else '' - print_out(f"Name: \"{list_item['title']}\"\t" + print_out(f"Title: \"{list_item['title']}\"\t" + f"ID: {list_item['id']}\t" + f"Replies policy: {replies_policy}") else: print_out("You have no lists defined.") -def print_list_accounts(list_title, accounts): - print_out(f"Accounts in list \"{list_title}\":\n") +def print_list_accounts(accounts): + print_out("Accounts in list:\n") if accounts: print_acct_list(accounts) else: diff --git a/toot/tui/scroll.py b/toot/tui/scroll.py index 7626e84..fe89be8 100644 --- a/toot/tui/scroll.py +++ b/toot/tui/scroll.py @@ -1,7 +1,7 @@ # scroll.py # # Copied from the stig project by rndusr@github -# https://github.com/rndusr/sti +# https://github.com/rndusr/stig # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by