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...
This commit is contained in:
Cy 2020-05-30 23:06:28 +00:00
parent 2dfdb0b859
commit 12d7b4cb7d
No known key found for this signature in database
GPG Key ID: F66D599380F88521

View File

@ -16,6 +16,7 @@ from brutaldon.forms import (
from brutaldon.models import Client, Account, Preference, Theme from brutaldon.models import Client, Account, Preference, Theme
from mastodon import ( from mastodon import (
Mastodon, Mastodon,
MastodonIllegalArgumentError,
AttribAccessDict, AttribAccessDict,
MastodonError, MastodonError,
MastodonAPIError, MastodonAPIError,
@ -83,14 +84,14 @@ def get_usercontext(request, feature_set="mainline"):
Account.MultipleObjectsReturned, Account.MultipleObjectsReturned,
): ):
raise NotLoggedInException() raise NotLoggedInException()
mastodon = get_mastodon()Mastodon( mastodon = Mastodon(
client_id=client.client_id, client_id=client.client_id,
client_secret=client.client_secret, client_secret=client.client_secret,
access_token=user.access_token, access_token=user.access_token,
api_base_url=client.api_base_id, api_base_url=client.api_base_id,
session=get_session(client.api_base_id), session=get_session(client.api_base_id),
ratelimit_method="throw", ratelimit_method="throw",
feature_set=feature_set feature_set=feature_set
) )
return user, mastodon return user, mastodon
else: else:
@ -810,22 +811,27 @@ def settings(request):
) )
def status_post(account, request, mastodon, **kw): def status_post(account, request, mastodon, **kw):
try: while True:
mastodon.status_post(**kw) try:
except MastodonIllegalArgumentError as e: mastodon.status_post(**kw)
if not 'is only available with feature set' in e.args[0]: except MastodonIllegalArgumentError as e:
raise if not 'is only available with feature set' in e.args[0]:
feature_set = e.args[0].rsplit(" ",1)[-1] raise
feature_set = e.args[0].rsplit(" ",1)[-1]
account, mastodon = get_usercontext(request, account, mastodon = get_usercontext(request,
feature_set=feature_set) feature_set=feature_set)
return status_post(account, request, mastodon, **kw) continue
except TypeError: except TypeError:
kw.pop("content_type") # not sure why, but the old code retried status_post without a
return status_post(account, request, mastodon, **kw) # content_type keyword, if there was a TypeError
kw.pop("content_type")
continue
else:
break
return account, mastodon return account, mastodon
@never_cache @never_cache
@br_login_required @br_login_required
def toot(request, mention=None): def toot(request, mention=None):
@ -881,15 +887,15 @@ def toot(request, mention=None):
), ),
) )
) )
if form.cleaned_data["visibility"] == "": if form.cleaned_data["visibility"] == "":
form.cleaned_data["visibility"] = request.session[ form.cleaned_data["visibility"] = request.session[
"active_user" "active_user"
].source.privacy ].source.privacy
try: try:
status_post( status_post(
account, mastodon, account, request, mastodon,
status=form.cleaned_data["status"], status=form.cleaned_data["status"],
visibility=form.cleaned_data["visibility"], visibility=form.cleaned_data["visibility"],
spoiler_text=form.cleaned_data["spoiler_text"], spoiler_text=form.cleaned_data["spoiler_text"],
media_ids=media_objects, media_ids=media_objects,
@ -913,8 +919,8 @@ def toot(request, mention=None):
"preferences": account.preferences, "preferences": account.preferences,
}, },
) )
else: else:
return result return result
return redirect(home) return redirect(home)
else: else:
return render( return render(