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 brutaldon.models import Client, Account, Preference, Theme
from mastodon import ( from mastodon import (
Mastodon, Mastodon,
MastodonIllegalArgumentError,
AttribAccessDict, AttribAccessDict,
MastodonError, MastodonError,
MastodonAPIError, MastodonAPIError,
@ -83,7 +84,7 @@ 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,
@ -810,6 +811,7 @@ def settings(request):
) )
def status_post(account, request, mastodon, **kw): def status_post(account, request, mastodon, **kw):
while True:
try: try:
mastodon.status_post(**kw) mastodon.status_post(**kw)
except MastodonIllegalArgumentError as e: except MastodonIllegalArgumentError as e:
@ -820,10 +822,14 @@ def status_post(account, request, mastodon, **kw):
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:
# not sure why, but the old code retried status_post without a
# content_type keyword, if there was a TypeError
kw.pop("content_type") kw.pop("content_type")
return status_post(account, request, mastodon, **kw) continue
else:
break
return account, mastodon return account, mastodon
@never_cache @never_cache
@ -888,7 +894,7 @@ def toot(request, mention=None):
].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"],