From 7b194880a0d4c98373cff2df286cf26f3df5a250 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Sun, 27 Nov 2022 09:56:58 +0100 Subject: [PATCH] Remove duplicate search api fn --- tests/test_console.py | 1 + toot/api.py | 21 +++++++++------------ toot/commands.py | 11 ++++------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/tests/test_console.py b/tests/test_console.py index cd26670..56927ca 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -350,6 +350,7 @@ def test_search(mock_get, capsys): mock_get.assert_called_once_with(app, user, '/api/v2/search', { 'q': 'freddy', + 'type': None, 'resolve': False, }) diff --git a/toot/api.py b/toot/api.py index 12d325f..cdbc537 100644 --- a/toot/api.py +++ b/toot/api.py @@ -278,21 +278,18 @@ def upload_media(app, user, file, description=None): ).json() -def search(app, user, query, resolve): - return http.get(app, user, '/api/v2/search', { - 'q': query, - 'resolve': resolve, +def search(app, user, query, resolve=False, type=None): + """ + Perform a search. + https://docs.joinmastodon.org/methods/search/#v2 + """ + return http.get(app, user, "/api/v2/search", { + "q": query, + "resolve": resolve, + "type": type }).json() -def search_accounts(app, user, query): - return http.get(app, user, '/api/v2/search', { - 'q': query, - 'type': 'accounts', - 'resolve': True, - }).json()['accounts'] - - def follow(app, user, account): return _account_action(app, user, account, 'follow') diff --git a/toot/commands.py b/toot/commands.py index b48f2a2..2504f11 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -255,14 +255,11 @@ def _find_account(app, user, account_name): if not account_name: raise ConsoleError("Empty account name given") - accounts = api.search_accounts(app, user, account_name) + normalized_name = account_name.lstrip("@").lower() - if account_name[0] == "@": - account_name = account_name[1:] - - for account in accounts: - # Normalise string matching because usernames are case insensitive - if account['acct'].lower() == account_name.lower(): + response = api.search(app, user, account_name, type="accounts", resolve=True) + for account in response["accounts"]: + if account["acct"].lower() == normalized_name: return account raise ConsoleError("Account not found")