Skip to content

Commit

Permalink
Refactor: extract Spond._match_person() from repeated code in `Spon…
Browse files Browse the repository at this point in the history
…d.get_person()`
  • Loading branch information
elliot-100 committed Oct 29, 2024
1 parent a0665ab commit a11db8b
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,24 @@ async def get_person(self, user: str) -> JSONDict:
await self.get_groups()
for group in self.groups:
for member in group["members"]:
if (
member["id"] == user
or ("email" in member and member["email"]) == user
or member["firstName"] + " " + member["lastName"] == user
or ("profile" in member and member["profile"]["id"] == user)
):
if self._match_person(member, user):
return member
if "guardians" in member:
for guardian in member["guardians"]:
if (
guardian["id"] == user
or ("email" in guardian and guardian["email"]) == user
or guardian["firstName"] + " " + guardian["lastName"]
== user
or (
"profile" in guardian
and guardian["profile"]["id"] == user
)
):
if self._match_person(guardian, user):
return guardian
errmsg = f"No person matched with identifier '{user}'."
raise KeyError(errmsg)

@staticmethod
def _match_person(person: JSONDict, match_str: str) -> bool:
return (
person["id"] == match_str
or ("email" in person and person["email"]) == match_str
or person["firstName"] + " " + person["lastName"] == match_str
or ("profile" in person and person["profile"]["id"] == match_str)
)

@_SpondBase.require_authentication
async def get_messages(self, max_chats: int = 100) -> list[JSONDict] | None:
"""
Expand Down

0 comments on commit a11db8b

Please sign in to comment.