45
45
from synapse .storage .databases .main .cache import CacheInvalidationWorkerStore
46
46
from synapse .storage .types import Cursor
47
47
from synapse .storage .util .id_generators import IdGenerator
48
- from synapse .types import JsonDict , ThirdPartyInstanceID
48
+ from synapse .types import JsonDict , RetentionPolicy , ThirdPartyInstanceID
49
49
from synapse .util import json_encoder
50
50
from synapse .util .caches .descriptors import cached
51
51
from synapse .util .stringutils import MXC_REGEX
@@ -699,20 +699,28 @@ def delete_ratelimit_txn(txn: LoggingTransaction) -> None:
699
699
await self .db_pool .runInteraction ("delete_ratelimit" , delete_ratelimit_txn )
700
700
701
701
@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 :
703
703
"""Get the retention policy for a given room.
704
704
705
705
If no retention policy has been found for this room, returns a policy defined
706
706
by the configured default policy (which has None as both the 'min_lifetime' and
707
707
the 'max_lifetime' if no default policy has been defined in the server's
708
708
configuration).
709
709
710
+ If support for retention policies is disabled, a policy with a 'min_lifetime' and
711
+ 'max_lifetime' of None is returned.
712
+
710
713
Args:
711
714
room_id: The ID of the room to get the retention policy of.
712
715
713
716
Returns:
714
717
A dict containing "min_lifetime" and "max_lifetime" for this room.
715
718
"""
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 ()
716
724
717
725
def get_retention_policy_for_room_txn (
718
726
txn : LoggingTransaction ,
@@ -736,10 +744,10 @@ def get_retention_policy_for_room_txn(
736
744
# If we don't know this room ID, ret will be None, in this case return the default
737
745
# policy.
738
746
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
+ )
743
751
744
752
min_lifetime = ret [0 ]["min_lifetime" ]
745
753
max_lifetime = ret [0 ]["max_lifetime" ]
@@ -754,10 +762,10 @@ def get_retention_policy_for_room_txn(
754
762
if max_lifetime is None :
755
763
max_lifetime = self .config .retention .retention_default_max_lifetime
756
764
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
+ )
761
769
762
770
async def get_media_mxcs_in_room (self , room_id : str ) -> Tuple [List [str ], List [str ]]:
763
771
"""Retrieves all the local and remote media MXC URIs in a given room
@@ -994,7 +1002,7 @@ def _quarantine_media_txn(
994
1002
995
1003
async def get_rooms_for_retention_period_in_range (
996
1004
self , min_ms : Optional [int ], max_ms : Optional [int ], include_null : bool = False
997
- ) -> Dict [str , Dict [ str , Optional [ int ]] ]:
1005
+ ) -> Dict [str , RetentionPolicy ]:
998
1006
"""Retrieves all of the rooms within the given retention range.
999
1007
1000
1008
Optionally includes the rooms which don't have a retention policy.
@@ -1016,7 +1024,7 @@ async def get_rooms_for_retention_period_in_range(
1016
1024
1017
1025
def get_rooms_for_retention_period_in_range_txn (
1018
1026
txn : LoggingTransaction ,
1019
- ) -> Dict [str , Dict [ str , Optional [ int ]] ]:
1027
+ ) -> Dict [str , RetentionPolicy ]:
1020
1028
range_conditions = []
1021
1029
args = []
1022
1030
@@ -1047,10 +1055,10 @@ def get_rooms_for_retention_period_in_range_txn(
1047
1055
rooms_dict = {}
1048
1056
1049
1057
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
+ )
1054
1062
1055
1063
if include_null :
1056
1064
# 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(
1065
1073
# policy in its state), add it with a null policy.
1066
1074
for row in rows :
1067
1075
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 ()
1072
1077
1073
1078
return rooms_dict
1074
1079
0 commit comments