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

Commit dd927b2

Browse files
authored
Merge pull request #5388 from matrix-org/erikj/fix_email_push
Fix email notifications for unnamed rooms with multiple people
2 parents 414d2ca + a099926 commit dd927b2

File tree

5 files changed

+124
-30
lines changed

5 files changed

+124
-30
lines changed

changelog.d/5388.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix email notifications for unnamed rooms with multiple people.

synapse/push/emailpusher.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ def _start_processing(self):
114114

115115
run_as_background_process("emailpush.process", self._process)
116116

117+
def _pause_processing(self):
118+
"""Used by tests to temporarily pause processing of events.
119+
120+
Asserts that its not currently processing.
121+
"""
122+
assert not self._is_processing
123+
self._is_processing = True
124+
125+
def _resume_processing(self):
126+
"""Used by tests to resume processing of events after pausing.
127+
"""
128+
assert self._is_processing
129+
self._is_processing = False
130+
self._start_processing()
131+
117132
@defer.inlineCallbacks
118133
def _process(self):
119134
# we should never get here if we are already processing
@@ -215,6 +230,10 @@ def _unsafe_process(self):
215230

216231
@defer.inlineCallbacks
217232
def save_last_stream_ordering_and_success(self, last_stream_ordering):
233+
if last_stream_ordering is None:
234+
# This happens if we haven't yet processed anything
235+
return
236+
218237
self.last_stream_ordering = last_stream_ordering
219238
yield self.store.update_pusher_last_stream_ordering_and_success(
220239
self.app_id, self.email, self.user_id,

synapse/push/presentable_names.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ def calculate_room_name(store, room_state_ids, user_id, fallback_to_members=True
162162

163163

164164
def descriptor_from_member_events(member_events):
165+
"""Get a description of the room based on the member events.
166+
167+
Args:
168+
member_events (Iterable[FrozenEvent])
169+
170+
Returns:
171+
str
172+
"""
173+
174+
member_events = list(member_events)
175+
165176
if len(member_events) == 0:
166177
return "nobody"
167178
elif len(member_events) == 1:

synapse/push/pusherpool.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def start(self):
6060
def add_pusher(self, user_id, access_token, kind, app_id,
6161
app_display_name, device_display_name, pushkey, lang, data,
6262
profile_tag=""):
63+
"""Creates a new pusher and adds it to the pool
64+
65+
Returns:
66+
Deferred[EmailPusher|HttpPusher]
67+
"""
6368
time_now_msec = self.clock.time_msec()
6469

6570
# we try to create the pusher just to validate the config: it
@@ -103,7 +108,9 @@ def add_pusher(self, user_id, access_token, kind, app_id,
103108
last_stream_ordering=last_stream_ordering,
104109
profile_tag=profile_tag,
105110
)
106-
yield self.start_pusher_by_id(app_id, pushkey, user_id)
111+
pusher = yield self.start_pusher_by_id(app_id, pushkey, user_id)
112+
113+
defer.returnValue(pusher)
107114

108115
@defer.inlineCallbacks
109116
def remove_pushers_by_app_id_and_pushkey_not_user(self, app_id, pushkey,
@@ -184,21 +191,28 @@ def on_new_receipts(self, min_stream_id, max_stream_id, affected_room_ids):
184191

185192
@defer.inlineCallbacks
186193
def start_pusher_by_id(self, app_id, pushkey, user_id):
187-
"""Look up the details for the given pusher, and start it"""
194+
"""Look up the details for the given pusher, and start it
195+
196+
Returns:
197+
Deferred[EmailPusher|HttpPusher|None]: The pusher started, if any
198+
"""
188199
if not self._should_start_pushers:
189200
return
190201

191202
resultlist = yield self.store.get_pushers_by_app_id_and_pushkey(
192203
app_id, pushkey
193204
)
194205

195-
p = None
206+
pusher_dict = None
196207
for r in resultlist:
197208
if r['user_name'] == user_id:
198-
p = r
209+
pusher_dict = r
199210

200-
if p:
201-
yield self._start_pusher(p)
211+
pusher = None
212+
if pusher_dict:
213+
pusher = yield self._start_pusher(pusher_dict)
214+
215+
defer.returnValue(pusher)
202216

203217
@defer.inlineCallbacks
204218
def _start_pushers(self):
@@ -224,7 +238,7 @@ def _start_pusher(self, pusherdict):
224238
pusherdict (dict):
225239
226240
Returns:
227-
None
241+
Deferred[EmailPusher|HttpPusher]
228242
"""
229243
try:
230244
p = self.pusher_factory.create_pusher(pusherdict)
@@ -270,6 +284,8 @@ def _start_pusher(self, pusherdict):
270284

271285
p.on_started(have_notifs)
272286

287+
defer.returnValue(p)
288+
273289
@defer.inlineCallbacks
274290
def remove_pusher(self, app_id, pushkey, user_id):
275291
appid_pushkey = "%s:%s" % (app_id, pushkey)

tests/push/test_email.py

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import os
1717

18+
import attr
1819
import pkg_resources
1920

2021
from twisted.internet.defer import Deferred
@@ -25,6 +26,13 @@
2526
from tests.unittest import HomeserverTestCase
2627

2728

29+
@attr.s
30+
class _User(object):
31+
"Helper wrapper for user ID and access token"
32+
id = attr.ib()
33+
token = attr.ib()
34+
35+
2836
class EmailPusherTests(HomeserverTestCase):
2937

3038
servlets = [
@@ -71,25 +79,32 @@ def sendmail(*args, **kwargs):
7179

7280
return hs
7381

74-
def test_sends_email(self):
75-
82+
def prepare(self, reactor, clock, hs):
7683
# Register the user who gets notified
77-
user_id = self.register_user("user", "pass")
78-
access_token = self.login("user", "pass")
79-
80-
# Register the user who sends the message
81-
other_user_id = self.register_user("otheruser", "pass")
82-
other_access_token = self.login("otheruser", "pass")
84+
self.user_id = self.register_user("user", "pass")
85+
self.access_token = self.login("user", "pass")
86+
87+
# Register other users
88+
self.others = [
89+
_User(
90+
id=self.register_user("otheruser1", "pass"),
91+
token=self.login("otheruser1", "pass"),
92+
),
93+
_User(
94+
id=self.register_user("otheruser2", "pass"),
95+
token=self.login("otheruser2", "pass"),
96+
),
97+
]
8398

8499
# Register the pusher
85100
user_tuple = self.get_success(
86-
self.hs.get_datastore().get_user_by_access_token(access_token)
101+
self.hs.get_datastore().get_user_by_access_token(self.access_token)
87102
)
88103
token_id = user_tuple["token_id"]
89104

90-
self.get_success(
105+
self.pusher = self.get_success(
91106
self.hs.get_pusherpool().add_pusher(
92-
user_id=user_id,
107+
user_id=self.user_id,
93108
access_token=token_id,
94109
kind="email",
95110
app_id="m.email",
@@ -101,22 +116,54 @@ def test_sends_email(self):
101116
)
102117
)
103118

104-
# Create a room
105-
room = self.helper.create_room_as(user_id, tok=access_token)
119+
def test_simple_sends_email(self):
120+
# Create a simple room with two users
121+
room = self.helper.create_room_as(self.user_id, tok=self.access_token)
122+
self.helper.invite(
123+
room=room, src=self.user_id, tok=self.access_token, targ=self.others[0].id,
124+
)
125+
self.helper.join(room=room, user=self.others[0].id, tok=self.others[0].token)
106126

107-
# Invite the other person
108-
self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
127+
# The other user sends some messages
128+
self.helper.send(room, body="Hi!", tok=self.others[0].token)
129+
self.helper.send(room, body="There!", tok=self.others[0].token)
109130

110-
# The other user joins
111-
self.helper.join(room=room, user=other_user_id, tok=other_access_token)
131+
# We should get emailed about that message
132+
self._check_for_mail()
112133

113-
# The other user sends some messages
114-
self.helper.send(room, body="Hi!", tok=other_access_token)
115-
self.helper.send(room, body="There!", tok=other_access_token)
134+
def test_multiple_members_email(self):
135+
# We want to test multiple notifications, so we pause processing of push
136+
# while we send messages.
137+
self.pusher._pause_processing()
138+
139+
# Create a simple room with multiple other users
140+
room = self.helper.create_room_as(self.user_id, tok=self.access_token)
141+
142+
for other in self.others:
143+
self.helper.invite(
144+
room=room, src=self.user_id, tok=self.access_token, targ=other.id,
145+
)
146+
self.helper.join(room=room, user=other.id, tok=other.token)
147+
148+
# The other users send some messages
149+
self.helper.send(room, body="Hi!", tok=self.others[0].token)
150+
self.helper.send(room, body="There!", tok=self.others[1].token)
151+
self.helper.send(room, body="There!", tok=self.others[1].token)
152+
153+
# Nothing should have happened yet, as we're paused.
154+
assert not self.email_attempts
155+
156+
self.pusher._resume_processing()
157+
158+
# We should get emailed about those messages
159+
self._check_for_mail()
160+
161+
def _check_for_mail(self):
162+
"Check that the user receives an email notification"
116163

117164
# Get the stream ordering before it gets sent
118165
pushers = self.get_success(
119-
self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
166+
self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
120167
)
121168
self.assertEqual(len(pushers), 1)
122169
last_stream_ordering = pushers[0]["last_stream_ordering"]
@@ -126,7 +173,7 @@ def test_sends_email(self):
126173

127174
# It hasn't succeeded yet, so the stream ordering shouldn't have moved
128175
pushers = self.get_success(
129-
self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
176+
self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
130177
)
131178
self.assertEqual(len(pushers), 1)
132179
self.assertEqual(last_stream_ordering, pushers[0]["last_stream_ordering"])
@@ -143,7 +190,7 @@ def test_sends_email(self):
143190

144191
# The stream ordering has increased
145192
pushers = self.get_success(
146-
self.hs.get_datastore().get_pushers_by(dict(user_name=user_id))
193+
self.hs.get_datastore().get_pushers_by(dict(user_name=self.user_id))
147194
)
148195
self.assertEqual(len(pushers), 1)
149196
self.assertTrue(pushers[0]["last_stream_ordering"] > last_stream_ordering)

0 commit comments

Comments
 (0)