Skip to content

Commit

Permalink
Return room tags in account data extension
Browse files Browse the repository at this point in the history
Fix #17694
  • Loading branch information
MadLittleMods committed Sep 12, 2024
1 parent 9b83fb7 commit 38036b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
34 changes: 32 additions & 2 deletions synapse/handlers/sliding_sync/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,16 @@ async def get_account_data_extension_response(
)

global_account_data_map = dict(all_global_account_data)

# TODO: This should take into account the `to_token`
global_account_data_map[
AccountDataTypes.PUSH_RULES
] = await self.push_rules_handler.push_rules_for_user(sync_config.user)

# Fetch room account data
account_data_by_room_map: Mapping[str, Mapping[str, JsonMapping]] = {}
#
# Mapping from room_id to mapping of `type` to `content` of room account data events.
account_data_by_room_map: Dict[str, Dict[str, JsonMapping]] = {}
relevant_room_ids = self.find_relevant_room_ids_for_extension(
requested_lists=account_data_request.lists,
requested_room_ids=account_data_request.rooms,
Expand All @@ -417,11 +420,38 @@ async def get_account_data_extension_response(
user_id, from_token.stream_token.account_data_key
)
)

# Add room tags
#
# TODO: This should take into account the `from_token` and `to_token`
tags_by_room = await self.store.get_updated_tags(
user_id, from_token.stream_token.account_data_key
)
for room_id, tags in tags_by_room.items():
account_data_by_room_map.setdefault(room_id, {})[
AccountDataTypes.TAG
] = {"tags": tags}
else:
# TODO: This should take into account the `to_token`
account_data_by_room_map = (
immutable_account_data_by_room_map = (
await self.store.get_room_account_data_for_user(user_id)
)
# We have to make a copy of the immutable data from the cache as we will
# be modifying it.
for (
room_id,
room_account_data_map,
) in immutable_account_data_by_room_map.items():
account_data_by_room_map[room_id] = dict(room_account_data_map)

# Add room tags
#
# TODO: This should take into account the `to_token`
tags_by_room = await self.store.get_tags_for_user(user_id)
for room_id, tags in tags_by_room.items():
account_data_by_room_map.setdefault(room_id, {})[
AccountDataTypes.TAG
] = {"tags": tags}

# Filter down to the relevant rooms
account_data_by_room_map = {
Expand Down
10 changes: 5 additions & 5 deletions synapse/storage/databases/main/account_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async def get_room_account_data_for_user(

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

txn.execute(sql, (user_id,))

by_room: Dict[str, Dict[str, JsonDict]] = {}
by_room: Dict[str, Dict[str, JsonMapping]] = {}
for room_id, account_data_type, content in txn:
room_data = by_room.setdefault(room_id, {})

Expand Down Expand Up @@ -429,7 +429,7 @@ def get_updated_global_account_data_for_user(

async def get_updated_room_account_data_for_user(
self, user_id: str, stream_id: int
) -> Dict[str, Dict[str, JsonDict]]:
) -> Dict[str, Dict[str, JsonMapping]]:
"""Get all the room account_data that's changed for a user.
Args:
Expand All @@ -442,14 +442,14 @@ async def get_updated_room_account_data_for_user(

def get_updated_room_account_data_for_user_txn(
txn: LoggingTransaction,
) -> Dict[str, Dict[str, JsonDict]]:
) -> Dict[str, Dict[str, JsonMapping]]:
sql = """
SELECT room_id, account_data_type, content FROM room_account_data
WHERE user_id = ? AND stream_id > ?
"""
txn.execute(sql, (user_id, stream_id))

account_data_by_room: Dict[str, Dict[str, JsonDict]] = {}
account_data_by_room: Dict[str, Dict[str, JsonMapping]] = {}
for row in txn:
room_account_data = account_data_by_room.setdefault(row[0], {})
room_account_data[row[1]] = db_to_json(row[2])
Expand Down

0 comments on commit 38036b2

Please sign in to comment.