12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from unittest .mock import Mock
16
-
17
15
from twisted .test .proto_helpers import MemoryReactor
18
16
17
+ from synapse .rest import admin
18
+ from synapse .rest .client import login , room
19
19
from synapse .server import HomeServer
20
20
from synapse .storage .databases .main .event_push_actions import NotifCounts
21
21
from synapse .util import Clock
24
24
25
25
USER_ID = "@user:example.com"
26
26
27
- PlAIN_NOTIF = ["notify" , {"set_tweak" : "highlight" , "value" : False }]
28
- HIGHLIGHT = [
29
- "notify" ,
30
- {"set_tweak" : "sound" , "value" : "default" },
31
- {"set_tweak" : "highlight" },
32
- ]
33
-
34
27
35
28
class EventPushActionsStoreTestCase (HomeserverTestCase ):
29
+ servlets = [
30
+ admin .register_servlets ,
31
+ room .register_servlets ,
32
+ login .register_servlets ,
33
+ ]
34
+
36
35
def prepare (self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) -> None :
37
36
self .store = hs .get_datastores ().main
38
37
persist_events_store = hs .get_datastores ().persist_events
@@ -54,154 +53,118 @@ def test_get_unread_push_actions_for_user_in_range_for_email(self) -> None:
54
53
)
55
54
56
55
def test_count_aggregation (self ) -> None :
57
- room_id = "!foo:example.com"
58
- user_id = "@user1235:test"
56
+ # Create a user to receive notifications and send receipts.
57
+ user_id = self .register_user ("user1235" , "pass" )
58
+ token = self .login ("user1235" , "pass" )
59
+
60
+ # And another users to send events.
61
+ other_id = self .register_user ("other" , "pass" )
62
+ other_token = self .login ("other" , "pass" )
63
+
64
+ # Create a room and put both users in it.
65
+ room_id = self .helper .create_room_as (user_id , tok = token )
66
+ self .helper .join (room_id , other_id , tok = other_token )
59
67
60
- last_read_stream_ordering = [ 0 ]
68
+ last_event_id : str
61
69
62
- def _assert_counts (noitf_count : int , highlight_count : int ) -> None :
70
+ def _assert_counts (
71
+ noitf_count : int , unread_count : int , highlight_count : int
72
+ ) -> None :
63
73
counts = self .get_success (
64
74
self .store .db_pool .runInteraction (
65
- "" ,
66
- self .store ._get_unread_counts_by_pos_txn ,
75
+ "get-unread-counts " ,
76
+ self .store ._get_unread_counts_by_receipt_txn ,
67
77
room_id ,
68
78
user_id ,
69
- last_read_stream_ordering [0 ],
70
79
)
71
80
)
72
81
self .assertEqual (
73
82
counts ,
74
83
NotifCounts (
75
84
notify_count = noitf_count ,
76
- unread_count = 0 , # Unread counts are tested in the sync tests.
85
+ unread_count = unread_count ,
77
86
highlight_count = highlight_count ,
78
87
),
79
88
)
80
89
81
- def _inject_actions (stream : int , action : list ) -> None :
82
- event = Mock ()
83
- event .room_id = room_id
84
- event .event_id = f"$test{ stream } :example.com"
85
- event .internal_metadata .stream_ordering = stream
86
- event .internal_metadata .is_outlier .return_value = False
87
- event .depth = stream
88
-
89
- self .store ._events_stream_cache .entity_has_changed (room_id , stream )
90
-
91
- self .get_success (
92
- self .store .db_pool .simple_insert (
93
- table = "events" ,
94
- values = {
95
- "stream_ordering" : stream ,
96
- "topological_ordering" : stream ,
97
- "type" : "m.room.message" ,
98
- "room_id" : room_id ,
99
- "processed" : True ,
100
- "outlier" : False ,
101
- "event_id" : event .event_id ,
102
- },
103
- )
90
+ def _create_event (highlight : bool = False ) -> str :
91
+ result = self .helper .send_event (
92
+ room_id ,
93
+ type = "m.room.message" ,
94
+ content = {"msgtype" : "m.text" , "body" : user_id if highlight else "msg" },
95
+ tok = other_token ,
104
96
)
97
+ nonlocal last_event_id
98
+ last_event_id = result ["event_id" ]
99
+ return last_event_id
105
100
106
- self .get_success (
107
- self .store .add_push_actions_to_staging (
108
- event .event_id ,
109
- {user_id : action },
110
- False ,
111
- )
112
- )
113
- self .get_success (
114
- self .store .db_pool .runInteraction (
115
- "" ,
116
- self .persist_events_store ._set_push_actions_for_event_and_users_txn ,
117
- [(event , None )],
118
- [(event , None )],
119
- )
120
- )
121
-
122
- def _rotate (stream : int ) -> None :
123
- self .get_success (
124
- self .store .db_pool .runInteraction (
125
- "rotate-receipts" , self .store ._handle_new_receipts_for_notifs_txn
126
- )
127
- )
128
-
129
- self .get_success (
130
- self .store .db_pool .runInteraction (
131
- "rotate-notifs" , self .store ._rotate_notifs_before_txn , stream
132
- )
133
- )
134
-
135
- def _mark_read (stream : int , depth : int ) -> None :
136
- last_read_stream_ordering [0 ] = stream
101
+ def _rotate () -> None :
102
+ self .get_success (self .store ._rotate_notifs ())
137
103
104
+ def _mark_read (event_id : str ) -> None :
138
105
self .get_success (
139
106
self .store .insert_receipt (
140
107
room_id ,
141
108
"m.read" ,
142
109
user_id = user_id ,
143
- event_ids = [f"$test { stream } :example.com" ],
110
+ event_ids = [event_id ],
144
111
data = {},
145
112
)
146
113
)
147
114
148
- _assert_counts (0 , 0 )
149
- _inject_actions ( 1 , PlAIN_NOTIF )
150
- _assert_counts (1 , 0 )
151
- _rotate (1 )
152
- _assert_counts (1 , 0 )
115
+ _assert_counts (0 , 0 , 0 )
116
+ _create_event ( )
117
+ _assert_counts (1 , 1 , 0 )
118
+ _rotate ()
119
+ _assert_counts (1 , 1 , 0 )
153
120
154
- _inject_actions ( 3 , PlAIN_NOTIF )
155
- _assert_counts (2 , 0 )
156
- _rotate (3 )
157
- _assert_counts (2 , 0 )
121
+ event_id = _create_event ( )
122
+ _assert_counts (2 , 2 , 0 )
123
+ _rotate ()
124
+ _assert_counts (2 , 2 , 0 )
158
125
159
- _inject_actions ( 5 , PlAIN_NOTIF )
160
- _mark_read (3 , 3 )
161
- _assert_counts (1 , 0 )
126
+ _create_event ( )
127
+ _mark_read (event_id )
128
+ _assert_counts (1 , 1 , 0 )
162
129
163
- _mark_read (5 , 5 )
164
- _assert_counts (0 , 0 )
130
+ _mark_read (last_event_id )
131
+ _assert_counts (0 , 0 , 0 )
165
132
166
- _inject_actions (6 , PlAIN_NOTIF )
167
- _rotate (6 )
168
- _assert_counts (1 , 0 )
169
-
170
- self .get_success (
171
- self .store .db_pool .simple_delete (
172
- table = "event_push_actions" , keyvalues = {"1" : 1 }, desc = ""
173
- )
174
- )
133
+ _create_event ()
134
+ _rotate ()
135
+ _assert_counts (1 , 1 , 0 )
175
136
176
- _assert_counts (1 , 0 )
137
+ # Delete old event push actions, this should not affect the (summarised) count.
138
+ self .get_success (self .store ._remove_old_push_actions_that_have_rotated ())
139
+ _assert_counts (1 , 1 , 0 )
177
140
178
- _mark_read (6 , 6 )
179
- _assert_counts (0 , 0 )
141
+ _mark_read (last_event_id )
142
+ _assert_counts (0 , 0 , 0 )
180
143
181
- _inject_actions ( 8 , HIGHLIGHT )
182
- _assert_counts (1 , 1 )
183
- _rotate (8 )
184
- _assert_counts (1 , 1 )
144
+ event_id = _create_event ( True )
145
+ _assert_counts (1 , 1 , 1 )
146
+ _rotate ()
147
+ _assert_counts (1 , 1 , 1 )
185
148
186
149
# Check that adding another notification and rotating after highlight
187
150
# works.
188
- _inject_actions ( 10 , PlAIN_NOTIF )
189
- _rotate (10 )
190
- _assert_counts (2 , 1 )
151
+ _create_event ( )
152
+ _rotate ()
153
+ _assert_counts (2 , 2 , 1 )
191
154
192
155
# Check that sending read receipts at different points results in the
193
156
# right counts.
194
- _mark_read (8 , 8 )
195
- _assert_counts (1 , 0 )
196
- _mark_read (10 , 10 )
197
- _assert_counts (0 , 0 )
198
-
199
- _inject_actions ( 11 , HIGHLIGHT )
200
- _assert_counts (1 , 1 )
201
- _mark_read (11 , 11 )
202
- _assert_counts (0 , 0 )
203
- _rotate (11 )
204
- _assert_counts (0 , 0 )
157
+ _mark_read (event_id )
158
+ _assert_counts (1 , 1 , 0 )
159
+ _mark_read (last_event_id )
160
+ _assert_counts (0 , 0 , 0 )
161
+
162
+ _create_event ( True )
163
+ _assert_counts (1 , 1 , 1 )
164
+ _mark_read (last_event_id )
165
+ _assert_counts (0 , 0 , 0 )
166
+ _rotate ()
167
+ _assert_counts (0 , 0 , 0 )
205
168
206
169
def test_find_first_stream_ordering_after_ts (self ) -> None :
207
170
def add_event (so : int , ts : int ) -> None :
0 commit comments