mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-16 13:08:03 +00:00
Some fixes for normalizing Bluesky handle + better error catching
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user