From dd19608b66594570d9fd731ba860e40711b701a3 Mon Sep 17 00:00:00 2001 From: Cy Date: Mon, 1 Jun 2020 00:21:38 +0000 Subject: [PATCH] Mastodon.py raises an error when session is expired Sometimes Brutaldon thinks it has a valid session key, but the upstream Fediverse server does not, and Brutaldon needs to handle that. Brutal probably should un-set its own internal logged-in state when that happens, but for now it just returns the same thing as if you weren't logged into Brutaldon, making you log in again, making Brutaldon get a new session token from upstream. --- brutaldon/views.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/brutaldon/views.py b/brutaldon/views.py index 5249078..0ffe216 100644 --- a/brutaldon/views.py +++ b/brutaldon/views.py @@ -133,7 +133,7 @@ def br_login_required(function=None, home_url=None, redirect_field_name=None): def _dec(view_func): def _view(request, *args, **kwargs): - if not is_logged_in(request): + def not_logged_in(): url = None if redirect_field_name and redirect_field_name in request.REQUEST: url = request.REQUEST[redirect_field_name] @@ -142,8 +142,14 @@ def br_login_required(function=None, home_url=None, redirect_field_name=None): if not url: url = "/" return HttpResponseRedirect(url) + if not is_logged_in(request): + return not_logged_in() else: - return view_func(request, *args, **kwargs) + try: + return view_func(request, *args, **kwargs) + except MastodonAPIError: + # mastodon must have expired our session + return not_logged_in() _view.__name__ = view_func.__name__ _view.__dict__ = view_func.__dict__