Skip to content

Commit

Permalink
Merge branch 'asyncfix' into everything4
Browse files Browse the repository at this point in the history
  • Loading branch information
danschwarz committed Mar 6, 2024
2 parents b147785 + fe5b9d1 commit c6800db
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions toot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from urllib.parse import urlparse, urlencode, quote

from toot import App, User, http, CLIENT_NAME, CLIENT_WEBSITE
from toot.exceptions import ConsoleError
from toot.exceptions import ConsoleError, ApiError
from toot.utils import drop_empty_values, str_bool, str_bool_nullable


Expand Down Expand Up @@ -43,6 +43,26 @@ def _account_action(app, user, account, action) -> Response:
return http.post(app, user, url)


def _status_toggle_action(app, user, status_id, action, data=None) -> Response:
url = '/api/v1/statuses/{}/{}'.format(status_id, action)

try:
response = http.post(app, user, url, data=data).json()
except ApiError as e:
# For "toggle" operations, Mastodon returns unhelpful
# 422: "Validation failed: Status has already been taken"
# responses when you try to bookmark a status already
# bookmarked, or favourite a status already favourited
# so we just swallow those errors here
if str(e) == "Validation failed: Status has already been taken":
response = None
else:
# not the error we expected; re-raise the exception
raise e
finally:
return response


def _status_action(app, user, status_id, action, data=None) -> Response:
url = f"/api/v1/statuses/{status_id}/{action}"
return http.post(app, user, url, data=data)
Expand Down Expand Up @@ -310,38 +330,40 @@ def delete_status(app, user, status_id):


def favourite(app, user, status_id):
return _status_action(app, user, status_id, 'favourite')
return _status_toggle_action(app, user, status_id, 'favourite')


def unfavourite(app, user, status_id):
return _status_action(app, user, status_id, 'unfavourite')
return _status_toggle_action(app, user, status_id, 'unfavourite')


def reblog(app, user, status_id, visibility="public"):
return _status_action(app, user, status_id, 'reblog', data={"visibility": visibility})
return _status_toggle_action(app, user, status_id, 'reblog', data={"visibility": visibility})


def unreblog(app, user, status_id):
return _status_action(app, user, status_id, 'unreblog')
return _status_toggle_action(app, user, status_id, 'unreblog')


def pin(app, user, status_id):
return _status_action(app, user, status_id, 'pin')
return _status_toggle_action(app, user, status_id, 'pin')


def unpin(app, user, status_id):
return _status_action(app, user, status_id, 'unpin')
return _status_toggle_action(app, user, status_id, 'unpin')


def bookmark(app, user, status_id):
return _status_action(app, user, status_id, 'bookmark')
return _status_toggle_action(app, user, status_id, 'bookmark')


def unbookmark(app, user, status_id):
return _status_action(app, user, status_id, 'unbookmark')
return _status_toggle_action(app, user, status_id, 'unbookmark')


def translate(app, user, status_id):
# don't use status_toggle_action for translate as this is
# not toggling anything server-side; it's a read only operation.
return _status_action(app, user, status_id, 'translate')


Expand Down

0 comments on commit c6800db

Please sign in to comment.