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

Commit

Permalink
Split get_account_data_for_user into separate global & room methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Feb 3, 2023
1 parent 480e0a1 commit 238f9af
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
5 changes: 2 additions & 3 deletions synapse/handlers/initial_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ async def _snapshot_all_rooms(

tags_by_room = await self.store.get_tags_for_user(user_id)

account_data, account_data_by_room = await self.store.get_account_data_for_user(
user_id
)
account_data = await self.store.get_global_account_data_for_user(user_id)
account_data_by_room = await self.store.get_room_account_data_for_user(user_id)

public_room_ids = await self.store.get_public_room_ids()

Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ async def copy_room_tags_and_direct_to_room(
user_id: The user's ID.
"""
# Retrieve user account data for predecessor room
user_account_data, _ = await self.store.get_account_data_for_user(user_id)
user_account_data = await self.store.get_global_account_data_for_user(user_id)

# Copy direct message state if applicable
direct_rooms = user_account_data.get(AccountDataTypes.DIRECT, {})
Expand Down
10 changes: 6 additions & 4 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,10 +1762,12 @@ async def _generate_sync_entry_for_account_data(
)
else:
# TODO Do not fetch room account data if it will be unused.
(
global_account_data,
account_data_by_room,
) = await self.store.get_account_data_for_user(sync_config.user.to_string())
global_account_data = await self.store.get_global_account_data_for_user(
sync_config.user.to_string()
)
account_data_by_room = await self.store.get_room_account_data_for_user(
sync_config.user.to_string()
)

global_account_data["m.push_rules"] = await self.push_rules_for_user(
sync_config.user
Expand Down
3 changes: 2 additions & 1 deletion synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,8 @@ async def on_GET(
if not await self._store.get_user_by_id(user_id):
raise NotFoundError("User not found")

global_data, by_room_data = await self._store.get_account_data_for_user(user_id)
global_data = await self._store.get_global_account_data_for_user(user_id)
by_room_data = await self._store.get_room_account_data_for_user(user_id)
return HTTPStatus.OK, {
"account_data": {
"global": global_data,
Expand Down
58 changes: 43 additions & 15 deletions synapse/storage/databases/main/account_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ def get_max_account_data_stream_id(self) -> int:
return self._account_data_id_gen.get_current_token()

@cached()
async def get_account_data_for_user(
async def get_global_account_data_for_user(
self, user_id: str
) -> Tuple[Dict[str, JsonDict], Dict[str, Dict[str, JsonDict]]]:
) -> Dict[str, JsonDict]:
"""
Get all the client account_data for a user.
Expand All @@ -133,14 +133,14 @@ async def get_account_data_for_user(
Args:
user_id: The user to get the account_data for.
Returns:
A 2-tuple of a dict of global account_data and a dict mapping from
room_id string to per room account_data dicts.
The global account_data.
"""

def get_account_data_for_user_txn(
def get_global_account_data_for_user(
txn: LoggingTransaction,
) -> Tuple[Dict[str, JsonDict], Dict[str, Dict[str, JsonDict]]]:
) -> Dict[str, JsonDict]:
# The 'content != '{}' condition below prevents us from using
# `simple_select_list_txn` here, as it doesn't support conditions
# other than 'equals'.
Expand All @@ -158,10 +158,34 @@ def get_account_data_for_user_txn(
txn.execute(sql, (user_id,))
rows = self.db_pool.cursor_to_dict(txn)

global_account_data = {
return {
row["account_data_type"]: db_to_json(row["content"]) for row in rows
}

return await self.db_pool.runInteraction(
"get_global_account_data_for_user", get_global_account_data_for_user
)

@cached()
async def get_room_account_data_for_user(
self, user_id: str
) -> Dict[str, Dict[str, JsonDict]]:
"""
Get all the client account_data for a user.
If experimental MSC3391 support is enabled, any entries with an empty
content body are excluded; as this means they have been deleted.
Args:
user_id: The user to get the account_data for.
Returns:
A dict mapping from room_id string to per room account_data dicts.
"""

def get_room_account_data_for_user_txn(
txn: LoggingTransaction,
) -> Dict[str, Dict[str, JsonDict]]:
# The 'content != '{}' condition below prevents us from using
# `simple_select_list_txn` here, as it doesn't support conditions
# other than 'equals'.
Expand All @@ -185,10 +209,10 @@ def get_account_data_for_user_txn(

room_data[row["account_data_type"]] = db_to_json(row["content"])

return global_account_data, by_room
return by_room

return await self.db_pool.runInteraction(
"get_account_data_for_user", get_account_data_for_user_txn
"get_room_account_data_for_user_txn", get_room_account_data_for_user_txn
)

@cached(num_args=2, max_entries=5000, tree=True)
Expand Down Expand Up @@ -470,7 +494,8 @@ def process_replication_rows(
self.get_global_account_data_by_type_for_user.invalidate(
(row.user_id, row.data_type)
)
self.get_account_data_for_user.invalidate((row.user_id,))
self.get_global_account_data_for_user.invalidate((row.user_id,))
self.get_room_account_data_for_user.invalidate((row.user_id,))
self.get_account_data_for_room.invalidate((row.user_id, row.room_id))
self.get_account_data_for_room_and_type.invalidate(
(row.user_id, row.room_id, row.data_type)
Expand Down Expand Up @@ -518,7 +543,7 @@ async def add_account_data_to_room(
)

self._account_data_stream_cache.entity_has_changed(user_id, next_id)
self.get_account_data_for_user.invalidate((user_id,))
self.get_room_account_data_for_user.invalidate((user_id,))
self.get_account_data_for_room.invalidate((user_id, room_id))
self.get_account_data_for_room_and_type.prefill(
(user_id, room_id, account_data_type), content
Expand Down Expand Up @@ -584,7 +609,7 @@ def _remove_account_data_for_room_txn(
return None

self._account_data_stream_cache.entity_has_changed(user_id, next_id)
self.get_account_data_for_user.invalidate((user_id,))
self.get_room_account_data_for_user.invalidate((user_id,))
self.get_account_data_for_room.invalidate((user_id, room_id))
self.get_account_data_for_room_and_type.prefill(
(user_id, room_id, account_data_type), {}
Expand Down Expand Up @@ -619,7 +644,7 @@ async def add_account_data_for_user(
)

self._account_data_stream_cache.entity_has_changed(user_id, next_id)
self.get_account_data_for_user.invalidate((user_id,))
self.get_global_account_data_for_user.invalidate((user_id,))
self.get_global_account_data_by_type_for_user.invalidate(
(user_id, account_data_type)
)
Expand Down Expand Up @@ -787,7 +812,7 @@ def _remove_account_data_for_user_txn(
return None

self._account_data_stream_cache.entity_has_changed(user_id, next_id)
self.get_account_data_for_user.invalidate((user_id,))
self.get_global_account_data_for_user.invalidate((user_id,))
self.get_global_account_data_by_type_for_user.prefill(
(user_id, account_data_type), {}
)
Expand Down Expand Up @@ -848,7 +873,10 @@ def _purge_account_data_for_user_txn(
txn, self.get_account_data_for_room_and_type, (user_id,)
)
self._invalidate_cache_and_stream(
txn, self.get_account_data_for_user, (user_id,)
txn, self.get_global_account_data_for_user, (user_id,)
)
self._invalidate_cache_and_stream(
txn, self.get_room_account_data_for_user, (user_id,)
)
self._invalidate_cache_and_stream(
txn, self.get_global_account_data_by_type_for_user, (user_id,)
Expand Down

0 comments on commit 238f9af

Please sign in to comment.