From 488206f15b5bb9186218b1f089f37d293b513def Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Sat, 8 Feb 2025 15:07:35 +0100 Subject: [PATCH] Some fixes for normalizing Bluesky handle + better error catching --- ivatar/ivataraccount/models.py | 3 +++ ivatar/ivataraccount/views.py | 16 ++++++++++++++-- ivatar/utils.py | 20 +++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/ivatar/ivataraccount/models.py b/ivatar/ivataraccount/models.py index 25453b1..ec66459 100644 --- a/ivatar/ivataraccount/models.py +++ b/ivatar/ivataraccount/models.py @@ -348,7 +348,9 @@ class ConfirmedEmail(BaseAccountModel): """ Helper method to set Bluesky handle """ + bs = Bluesky() + handle = bs.normalize_handle(handle) avatar = bs.get_profile(handle) if not avatar: raise Exception("Invalid Bluesky handle") @@ -502,6 +504,7 @@ class ConfirmedOpenId(BaseAccountModel): Helper method to set Bluesky handle """ bs = Bluesky() + handle = bs.normalize_handle(handle) avatar = bs.get_profile(handle) if not avatar: raise Exception("Invalid Bluesky handle") diff --git a/ivatar/ivataraccount/views.py b/ivatar/ivataraccount/views.py index d7add09..55d15c1 100644 --- a/ivatar/ivataraccount/views.py +++ b/ivatar/ivataraccount/views.py @@ -428,8 +428,20 @@ class AssignBlueskyHandleToOpenIdView(SuccessMessageMixin, TemplateView): messages.error( request, _("Handle '%s' not found: %s" % (bluesky_handle, e)) ) - return HttpResponseRedirect(reverse_lazy("profile")) - openid.set_bluesky_handle(bluesky_handle) + return HttpResponseRedirect( + reverse_lazy( + "assign_photo_openid", kwargs={"openid_id": int(kwargs["open_id"])} + ) + ) + try: + openid.set_bluesky_handle(bluesky_handle) + except Exception as e: + messages.error(request, _("Error: %s" % (e))) + return HttpResponseRedirect( + reverse_lazy( + "assign_photo_openid", kwargs={"openid_id": int(kwargs["open_id"])} + ) + ) openid.photo = None openid.save() diff --git a/ivatar/utils.py b/ivatar/utils.py index 1e935be..4acac81 100644 --- a/ivatar/utils.py +++ b/ivatar/utils.py @@ -61,10 +61,25 @@ class Bluesky: auth_response.raise_for_status() self.session = auth_response.json() - def get_profile(self, handle: str): + def normalize_handle(self, handle: str) -> str: + """ + Return the normalized handle for given handle + """ + # Normalize Bluesky handle in case someone enters an '@' at the beginning + if handle.startswith("@"): + handle = handle[1:] + # Remove trailing spaces or spaces at the beginning + while handle.startswith(" "): + handle = handle[1:] + while handle.endswith(" "): + handle = handle[:-1] + return handle + + def get_profile(self, handle: str) -> str: if not self.session: self.login() profile_response = None + try: profile_response = requests.get( f"{self.service}/xrpc/app.bsky.actor.getProfile", @@ -81,6 +96,9 @@ class Bluesky: return profile_response.json() def get_avatar(self, handle: str): + """ + Get avatar URL for a handle + """ profile = self.get_profile(handle) return profile["avatar"] if profile else None