From 12d7b4cb7d6aef2c3dc511a76e9bcef8f668a502 Mon Sep 17 00:00:00 2001 From: Cy Date: Sat, 30 May 2020 23:06:28 +0000 Subject: [PATCH] Handle feature_set errors automatically Recreate the mastodon object if there's an error complaining about a missing feature set. Only happens for status_post I think. Could be further generalized... --- brutaldon/views.py | 50 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/brutaldon/views.py b/brutaldon/views.py index a561e13..4f2ba49 100644 --- a/brutaldon/views.py +++ b/brutaldon/views.py @@ -16,6 +16,7 @@ from brutaldon.forms import ( from brutaldon.models import Client, Account, Preference, Theme from mastodon import ( Mastodon, + MastodonIllegalArgumentError, AttribAccessDict, MastodonError, MastodonAPIError, @@ -83,14 +84,14 @@ def get_usercontext(request, feature_set="mainline"): Account.MultipleObjectsReturned, ): raise NotLoggedInException() - mastodon = get_mastodon()Mastodon( + mastodon = Mastodon( client_id=client.client_id, client_secret=client.client_secret, access_token=user.access_token, api_base_url=client.api_base_id, session=get_session(client.api_base_id), ratelimit_method="throw", - feature_set=feature_set + feature_set=feature_set ) return user, mastodon else: @@ -810,22 +811,27 @@ def settings(request): ) def status_post(account, request, mastodon, **kw): - try: - mastodon.status_post(**kw) - except MastodonIllegalArgumentError as e: - if not 'is only available with feature set' in e.args[0]: - raise - feature_set = e.args[0].rsplit(" ",1)[-1] + while True: + try: + mastodon.status_post(**kw) + except MastodonIllegalArgumentError as e: + if not 'is only available with feature set' in e.args[0]: + raise + feature_set = e.args[0].rsplit(" ",1)[-1] - account, mastodon = get_usercontext(request, - feature_set=feature_set) - - return status_post(account, request, mastodon, **kw) - except TypeError: - kw.pop("content_type") - return status_post(account, request, mastodon, **kw) + account, mastodon = get_usercontext(request, + feature_set=feature_set) + + continue + except TypeError: + # not sure why, but the old code retried status_post without a + # content_type keyword, if there was a TypeError + kw.pop("content_type") + continue + else: + break return account, mastodon - + @never_cache @br_login_required def toot(request, mention=None): @@ -881,15 +887,15 @@ def toot(request, mention=None): ), ) ) - + if form.cleaned_data["visibility"] == "": form.cleaned_data["visibility"] = request.session[ "active_user" ].source.privacy try: - status_post( - account, mastodon, - status=form.cleaned_data["status"], + status_post( + account, request, mastodon, + status=form.cleaned_data["status"], visibility=form.cleaned_data["visibility"], spoiler_text=form.cleaned_data["spoiler_text"], media_ids=media_objects, @@ -913,8 +919,8 @@ def toot(request, mention=None): "preferences": account.preferences, }, ) - else: - return result + else: + return result return redirect(home) else: return render(