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
1 changed files with 28 additions and 22 deletions

View File

@ -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,20 +811,25 @@ 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)
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)
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
@ -887,9 +893,9 @@ def toot(request, mention=None):
"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(