Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add get_userinfo_by_id method to ModuleApi #9581

Merged
merged 17 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.d/9581.feature
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add get_userinfo_by_id method to ModuleApi.
Add `get_userinfo_by_id` method to ModuleApi. `UserInfo` is a new type returned from the registration store. This also deprecates the `get_user_by_id` method on the registration store.
jaywink marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 22 additions & 30 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def __init__(

@cached()
async def get_user_by_id(self, user_id: str) -> Optional[Dict[str, Any]]:
"""Deprecated: use get_userinfo_by_id instead"""
return await self.db_pool.simple_select_one(
table="users",
keyvalues={"name": user_id},
Expand All @@ -166,40 +167,31 @@ async def get_user_by_id(self, user_id: str) -> Optional[Dict[str, Any]]:
desc="get_user_by_id",
)

@cached()
async def get_userinfo_by_id(self, user_id: str) -> Optional[UserInfo]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please can you give this a docstring?

Also, please can you stick a comment or docstring on get_user_by_id to say it's deprecated and replaced by get_userinfo_by_id?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably best deprecate _store.get_user_by_id in a follow-up pr, I'm happy to do that?

Oh I just read this. I'd roll it into this PR, since it's simple enough.

Unless you were talking about actually replacing uses of get_user_by_id with get_userinfo_by_id? Which would be amazing if you've got time, and yes a separate PR would be best.

user_data = await self.db_pool.simple_select_one(
table="users",
keyvalues={"name": user_id},
retcols=[
"name",
"password_hash",
"is_guest",
"admin",
"consent_version",
"consent_server_notice_sent",
"appservice_id",
"creation_ts",
"user_type",
"deactivated",
"shadow_banned",
],
allow_none=True,
desc="get_userinfo_by_id",
)
"""Get a UserInfo object for a user by user ID.

Note! Currently uses the cache of `get_user_by_id`. Once that deprecated method is removed,
this method should be cached.

Args:
user_id: The user to fetch user info for.
Returns:
`UserInfo` object if user found, otherwise `None`.
"""
user_data = await self.get_user_by_id(user_id)
if not user_data:
return None
return UserInfo(
appservice_id=user_data.get("appservice_id"),
consent_server_notice_sent=user_data.get("consent_server_notice_sent"),
consent_version=user_data.get("consent_version"),
creation_ts=user_data.get("creation_ts", 0),
is_admin=bool(user_data.get("admin")),
is_deactivated=bool(user_data.get("deactivated")),
is_guest=bool(user_data.get("is_guest")),
is_shadow_banned=bool(user_data.get("shadow_banned")),
user_id=UserID.from_string(str(user_data.get("name"))),
user_type=user_data.get("user_type"),
appservice_id=user_data["appservice_id"],
consent_server_notice_sent=user_data["consent_server_notice_sent"],
consent_version=user_data["consent_version"],
creation_ts=user_data["creation_ts"],
is_admin=bool(user_data["admin"]),
is_deactivated=bool(user_data["deactivated"]),
is_guest=bool(user_data["is_guest"]),
is_shadow_banned=bool(user_data["shadow_banned"]),
user_id=UserID.from_string(user_data["name"]),
user_type=user_data["user_type"],
)

async def is_trial_user(self, user_id: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion synapse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ def get_verify_key_from_cross_signing_key(key_info):

@attr.s(auto_attribs=True, frozen=True, slots=True)
class UserInfo:
"""Represents an object with information about a user.
"""Holds information about a user. Result of get_userinfo_by_id.

Attributes:
user_id: ID of the user.
Expand Down