Merge pull request #76 from cyisfor/handle_feature_set_errors

Handle feature set errors from Pleroma
This commit is contained in:
GCU Prosthetic Conscience 2020-07-08 12:37:02 -04:00 committed by GitHub
commit b1a2c7f57d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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,
@ -71,7 +72,7 @@ def get_session(domain):
return s return s
def get_usercontext(request): def get_usercontext(request, feature_set="mainline"):
if is_logged_in(request): if is_logged_in(request):
try: try:
client = Client.objects.get(api_base_id=request.session["active_instance"]) client = Client.objects.get(api_base_id=request.session["active_instance"])
@ -90,6 +91,7 @@ def get_usercontext(request):
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
) )
return user, mastodon return user, mastodon
else: else:
@ -816,6 +818,27 @@ def settings(request):
{"form": form, "account": account, "preferences": account.preferences}, {"form": form, "account": account, "preferences": account.preferences},
) )
def status_post(account, request, mastodon, **kw):
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)
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 @never_cache
@br_login_required @br_login_required
@ -872,26 +895,19 @@ 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:
try: status_post(
mastodon.status_post( 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,
content_type="text/markdown", content_type="text/markdown")
)
except TypeError:
mastodon.status_post(
status=form.cleaned_data["status"],
visibility=form.cleaned_data["visibility"],
spoiler_text=form.cleaned_data["spoiler_text"],
media_ids=media_objects,
)
except MastodonAPIError as error: except MastodonAPIError as error:
form.add_error( form.add_error(
"", "",
@ -985,8 +1001,8 @@ def redraft(request, id):
"active_user" "active_user"
].source.privacy ].source.privacy
try: try:
try: status_post(
mastodon.status_post( 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"],
@ -994,14 +1010,6 @@ def redraft(request, id):
in_reply_to_id=toot.in_reply_to_id, in_reply_to_id=toot.in_reply_to_id,
content_type="text/markdown", content_type="text/markdown",
) )
except TypeError:
mastodon.status_post(
status=form.cleaned_data["status"],
visibility=form.cleaned_data["visibility"],
spoiler_text=form.cleaned_data["spoiler_text"],
media_ids=media_objects,
in_reply_to_id=toot.in_reply_to_id,
)
mastodon.status_delete(id) mastodon.status_delete(id)
except MastodonAPIError as error: except MastodonAPIError as error:
form.add_error( form.add_error(
@ -1118,8 +1126,8 @@ def reply(request, id):
) )
) )
try: try:
try: status_post(
mastodon.status_post( 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"],
@ -1127,14 +1135,6 @@ def reply(request, id):
in_reply_to_id=id, in_reply_to_id=id,
content_type="text/markdown", content_type="text/markdown",
) )
except TypeError:
mastodon.status_post(
status=form.cleaned_data["status"],
visibility=form.cleaned_data["visibility"],
spoiler_text=form.cleaned_data["spoiler_text"],
media_ids=media_objects,
in_reply_to_id=id,
)
except MastodonAPIError as error: except MastodonAPIError as error:
form.add_error( form.add_error(
"", "",