4545from synapse .storage .databases .main .cache import CacheInvalidationWorkerStore
4646from synapse .storage .types import Cursor
4747from synapse .storage .util .id_generators import IdGenerator
48- from synapse .types import JsonDict , ThirdPartyInstanceID
48+ from synapse .types import JsonDict , RetentionPolicy , ThirdPartyInstanceID
4949from synapse .util import json_encoder
5050from synapse .util .caches .descriptors import cached
5151from synapse .util .stringutils import MXC_REGEX
@@ -699,20 +699,28 @@ def delete_ratelimit_txn(txn: LoggingTransaction) -> None:
699699 await self .db_pool .runInteraction ("delete_ratelimit" , delete_ratelimit_txn )
700700
701701 @cached ()
702- async def get_retention_policy_for_room (self , room_id : str ) -> Dict [ str , int ] :
702+ async def get_retention_policy_for_room (self , room_id : str ) -> RetentionPolicy :
703703 """Get the retention policy for a given room.
704704
705705 If no retention policy has been found for this room, returns a policy defined
706706 by the configured default policy (which has None as both the 'min_lifetime' and
707707 the 'max_lifetime' if no default policy has been defined in the server's
708708 configuration).
709709
710+ If support for retention policies is disabled, a policy with a 'min_lifetime' and
711+ 'max_lifetime' of None is returned.
712+
710713 Args:
711714 room_id: The ID of the room to get the retention policy of.
712715
713716 Returns:
714717 A dict containing "min_lifetime" and "max_lifetime" for this room.
715718 """
719+ # If the room retention feature is disabled, return a policy with no minimum nor
720+ # maximum. This prevents incorrectly filtering out events when sending to
721+ # the client.
722+ if not self .config .retention .retention_enabled :
723+ return RetentionPolicy ()
716724
717725 def get_retention_policy_for_room_txn (
718726 txn : LoggingTransaction ,
@@ -736,10 +744,10 @@ def get_retention_policy_for_room_txn(
736744 # If we don't know this room ID, ret will be None, in this case return the default
737745 # policy.
738746 if not ret :
739- return {
740- " min_lifetime" : self .config .retention .retention_default_min_lifetime ,
741- " max_lifetime" : self .config .retention .retention_default_max_lifetime ,
742- }
747+ return RetentionPolicy (
748+ min_lifetime = self .config .retention .retention_default_min_lifetime ,
749+ max_lifetime = self .config .retention .retention_default_max_lifetime ,
750+ )
743751
744752 min_lifetime = ret [0 ]["min_lifetime" ]
745753 max_lifetime = ret [0 ]["max_lifetime" ]
@@ -754,10 +762,10 @@ def get_retention_policy_for_room_txn(
754762 if max_lifetime is None :
755763 max_lifetime = self .config .retention .retention_default_max_lifetime
756764
757- return {
758- " min_lifetime" : min_lifetime ,
759- " max_lifetime" : max_lifetime ,
760- }
765+ return RetentionPolicy (
766+ min_lifetime = min_lifetime ,
767+ max_lifetime = max_lifetime ,
768+ )
761769
762770 async def get_media_mxcs_in_room (self , room_id : str ) -> Tuple [List [str ], List [str ]]:
763771 """Retrieves all the local and remote media MXC URIs in a given room
@@ -994,7 +1002,7 @@ def _quarantine_media_txn(
9941002
9951003 async def get_rooms_for_retention_period_in_range (
9961004 self , min_ms : Optional [int ], max_ms : Optional [int ], include_null : bool = False
997- ) -> Dict [str , Dict [ str , Optional [ int ]] ]:
1005+ ) -> Dict [str , RetentionPolicy ]:
9981006 """Retrieves all of the rooms within the given retention range.
9991007
10001008 Optionally includes the rooms which don't have a retention policy.
@@ -1016,7 +1024,7 @@ async def get_rooms_for_retention_period_in_range(
10161024
10171025 def get_rooms_for_retention_period_in_range_txn (
10181026 txn : LoggingTransaction ,
1019- ) -> Dict [str , Dict [ str , Optional [ int ]] ]:
1027+ ) -> Dict [str , RetentionPolicy ]:
10201028 range_conditions = []
10211029 args = []
10221030
@@ -1047,10 +1055,10 @@ def get_rooms_for_retention_period_in_range_txn(
10471055 rooms_dict = {}
10481056
10491057 for row in rows :
1050- rooms_dict [row ["room_id" ]] = {
1051- " min_lifetime" : row ["min_lifetime" ],
1052- " max_lifetime" : row ["max_lifetime" ],
1053- }
1058+ rooms_dict [row ["room_id" ]] = RetentionPolicy (
1059+ min_lifetime = row ["min_lifetime" ],
1060+ max_lifetime = row ["max_lifetime" ],
1061+ )
10541062
10551063 if include_null :
10561064 # If required, do a second query that retrieves all of the rooms we know
@@ -1065,10 +1073,7 @@ def get_rooms_for_retention_period_in_range_txn(
10651073 # policy in its state), add it with a null policy.
10661074 for row in rows :
10671075 if row ["room_id" ] not in rooms_dict :
1068- rooms_dict [row ["room_id" ]] = {
1069- "min_lifetime" : None ,
1070- "max_lifetime" : None ,
1071- }
1076+ rooms_dict [row ["room_id" ]] = RetentionPolicy ()
10721077
10731078 return rooms_dict
10741079
0 commit comments