Search only accounts when looking for users - toot - Unnamed repository; edit this file 'description' to name the repository.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
(DIR) commit 6766cf83b4fcd2af297906de4213b9dc5923049d
(DIR) parent 02e364b52196c5391e20e08dc2fb9de227fa3bed
(HTM) Author: Ivan Habunek <ivan@habunek.com>
Date: Sun, 7 May 2017 10:42:04 +0200
Search only accounts when looking for users
Instead of using general search.
Diffstat:
tests/test_console.py | 44 +++++++++++++------------------
toot/commands.py | 17 +++++++++++++----
2 files changed, 31 insertions(+), 30 deletions(-)
---
(DIR) diff --git a/tests/test_console.py b/tests/test_console.py
@@ -179,16 +179,14 @@ def test_search(monkeypatch, capsys):
def test_follow(monkeypatch, capsys):
def mock_get(url, params, headers):
- assert url == 'https://habunek.com/api/v1/search'
- assert params == {'q': 'blixa', 'resolve': False}
+ assert url == 'https://habunek.com/api/v1/accounts/search'
+ assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'}
- return MockResponse({
- 'accounts': [
- {'id': 123, 'acct': 'blixa@other.acc'},
- {'id': 321, 'acct': 'blixa'},
- ]
- })
+ return MockResponse([
+ {'id': 123, 'acct': 'blixa@other.acc'},
+ {'id': 321, 'acct': 'blixa'},
+ ])
def mock_prepare(request):
assert request.url == 'https://habunek.com/api/v1/accounts/321/follow'
@@ -208,13 +206,11 @@ def test_follow(monkeypatch, capsys):
def test_follow_not_found(monkeypatch, capsys):
def mock_get(url, params, headers):
- assert url == 'https://habunek.com/api/v1/search'
- assert params == {'q': 'blixa', 'resolve': False}
+ assert url == 'https://habunek.com/api/v1/accounts/search'
+ assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'}
- return MockResponse({
- 'accounts': []
- })
+ return MockResponse([])
monkeypatch.setattr(requests, 'get', mock_get)
@@ -225,16 +221,14 @@ def test_follow_not_found(monkeypatch, capsys):
def test_unfollow(monkeypatch, capsys):
def mock_get(url, params, headers):
- assert url == 'https://habunek.com/api/v1/search'
- assert params == {'q': 'blixa', 'resolve': False}
+ assert url == 'https://habunek.com/api/v1/accounts/search'
+ assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'}
- return MockResponse({
- 'accounts': [
- {'id': 123, 'acct': 'blixa@other.acc'},
- {'id': 321, 'acct': 'blixa'},
- ]
- })
+ return MockResponse([
+ {'id': 123, 'acct': 'blixa@other.acc'},
+ {'id': 321, 'acct': 'blixa'},
+ ])
def mock_prepare(request):
assert request.url == 'https://habunek.com/api/v1/accounts/321/unfollow'
@@ -254,13 +248,11 @@ def test_unfollow(monkeypatch, capsys):
def test_unfollow_not_found(monkeypatch, capsys):
def mock_get(url, params, headers):
- assert url == 'https://habunek.com/api/v1/search'
- assert params == {'q': 'blixa', 'resolve': False}
+ assert url == 'https://habunek.com/api/v1/accounts/search'
+ assert params == {'q': 'blixa'}
assert headers == {'Authorization': 'Bearer xxx'}
- return MockResponse({
- 'accounts': []
- })
+ return MockResponse([])
monkeypatch.setattr(requests, 'get', mock_get)
(DIR) diff --git a/toot/commands.py b/toot/commands.py
@@ -266,11 +266,20 @@ def _do_upload(app, user, file):
def _find_account(app, user, account_name):
- """For a given account name, returns the Account object or raises an exception if not found."""
- response = api.search(app, user, account_name, False)
+ """For a given account name, returns the Account object.
- for account in response['accounts']:
- if account['acct'] == account_name or "@" + account['acct'] == account_name:
+ Raises an exception if not found.
+ """
+ if not account_name:
+ raise ConsoleError("Empty account name given")
+
+ accounts = api.search_accounts(app, user, account_name)
+
+ if account_name[0] == "@":
+ account_name = account_name[1:]
+
+ for account in accounts:
+ if account['acct'] == account_name:
return account
raise ConsoleError("Account not found")