WIP: more mast parsing

This commit is contained in:
Jeffrey Goldberg 2023-11-26 16:25:00 -06:00
parent edeadabeb1
commit bfbd05a3e8
1 changed files with 17 additions and 3 deletions

View File

@ -1068,8 +1068,8 @@ class Connection(BaseModel):
value: str value: str
@staticmethod @staticmethod
def MastodonUname2Url(uname: str) ->Optional[HttpUrl]: def MastodonUname2Url(id: str) -> Optional[HttpUrl]:
"""From a Mastodon-style "user@domain.example" returns profile url.""" """From a Mastodon id "user@domain.example" returns profile url."""
# The closest thing to a formal spec of Mastodon usernames # The closest thing to a formal spec of Mastodon usernames
# where these regular expressions from a (reference?) # where these regular expressions from a (reference?)
@ -1077,10 +1077,24 @@ class Connection(BaseModel):
# #
# https://github.com/mastodon/mastodon/blob/852123867768e23410af5bd07ac0327bead0d9b2/app/models/account.rb#L68 # https://github.com/mastodon/mastodon/blob/852123867768e23410af5bd07ac0327bead0d9b2/app/models/account.rb#L68
# #
# USERNAME_RE = /[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?/i
# SERNAME_RE = /[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?/i # SERNAME_RE = /[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?/i
# MENTION_RE = %r{(?<![=/[:word:]])@((#{USERNAME_RE})(?:@[[:word:].-]+[[:word:]]+)?)}i # MENTION_RE = %r{(?<![=/[:word:]])@((#{USERNAME_RE})(?:@[[:word:].-]+[[:word:]]+)?)}i
# URL_PREFIX_RE = %r{\Ahttp(s?)://[^/]+} # URL_PREFIX_RE = %r{\Ahttp(s?)://[^/]+}
pattern = re.compile(r"""
@? # Optional @ prefix
(?P<uname>[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?) # username part
@ # separator
(?P<domain>[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?) # domain part
""", re.VERBOSE | re.IGNORECASE)
m = pattern.match(id)
uname = m.group("uname")
domain = m.group("domain")
url = HttpUrl(f'https://{domain}/@{uname}')
return url
@computed_field @computed_field
@cached_property @cached_property
@ -1094,7 +1108,7 @@ class Connection(BaseModel):
elif self.name == "Orcid": elif self.name == "Orcid":
url = f"https://orcid.org/{self.value}" url = f"https://orcid.org/{self.value}"
elif self.name == "Mastodon": elif self.name == "Mastodon":
url = MastodonUname2Url(self.value) url = self.MastodonUname2Url(self.value)
elif self.name == "email": elif self.name == "email":
url = f"mailto:{self.value}" url = f"mailto:{self.value}"
elif self.name == "website": elif self.name == "website":