tuwunel_service/sending/sender/dispatch/push/
suppressed.rs1use futures::StreamExt;
2use ruma::{
3 OwnedRoomId, OwnedUserId, RoomId, UserId, api::client::push::Pusher, presence::PresenceState,
4 push::Ruleset,
5};
6use tuwunel_core::{
7 debug, extract_variant, implement, trace,
8 utils::{
9 IterStream, ReadyExt,
10 stream::{BroadbandExt, WidebandExt},
11 },
12 warn,
13};
14use tuwunel_matrix::{Event, Pdu};
15
16use super::PUSH_WIDTH;
17use crate::{
18 pusher::SuppressedRooms,
19 rooms::timeline::RawPduId,
20 sending::{SendingEvent, Service},
21};
22
23struct Flush<'a> {
27 user_id: &'a UserId,
28 pushkey: &'a str,
29 pusher: &'a Pusher,
30 ruleset: &'a Ruleset,
31 reason: &'static str,
32}
33
34const ACTIVE_PRESENCE_AGE_MS: u64 = 65_000;
36const ACTIVE_SYNC_GAP_MS: u64 = 32_000;
37
38#[implement(Service)]
43pub fn schedule_flush_suppressed_for_pushkey(
44 &self,
45 user_id: OwnedUserId,
46 pushkey: String,
47 reason: &'static str,
48) {
49 let sending = self.services.sending.clone();
50
51 self.spawn_flush(async move {
52 sending
53 .flush_suppressed_for_pushkey(&user_id, &pushkey, reason)
54 .await;
55 });
56}
57
58#[implement(Service)]
63pub fn schedule_flush_suppressed_for_user(&self, user_id: OwnedUserId, reason: &'static str) {
64 let sending = self.services.sending.clone();
65
66 self.spawn_flush(async move {
67 sending
68 .flush_suppressed_for_user(&user_id, reason)
69 .await;
70 });
71}
72
73#[implement(Service)]
74async fn flush_suppressed_for_pushkey(
75 &self,
76 user_id: &UserId,
77 pushkey: &str,
78 reason: &'static str,
79) {
80 let suppressed = self
81 .services
82 .pusher
83 .take_suppressed_for_pushkey(user_id, pushkey);
84
85 if suppressed.is_empty() {
86 return;
87 }
88
89 let Ok(pusher) = self
90 .services
91 .pusher
92 .get_pusher(user_id, pushkey)
93 .await
94 .inspect_err(|error| {
95 warn!(?user_id, pushkey, ?error, "Missing pusher for suppressed flush");
96 })
97 else {
98 return;
99 };
100
101 let ruleset = self.services.pusher.ruleset(user_id).await;
102 let flush = Flush {
103 user_id,
104 pushkey,
105 pusher: &pusher,
106 ruleset: &ruleset,
107 reason,
108 };
109
110 self.flush_suppressed_rooms(&flush, suppressed)
111 .await;
112}
113
114#[implement(Service)]
115async fn flush_suppressed_for_user(&self, user_id: &UserId, reason: &'static str) {
116 let suppressed = self
117 .services
118 .pusher
119 .take_suppressed_for_user(user_id);
120
121 if suppressed.is_empty() {
122 return;
123 }
124
125 let ruleset = self.services.pusher.ruleset(user_id).await;
126
127 for (pushkey, rooms) in suppressed {
128 let Ok(pusher) = self
129 .services
130 .pusher
131 .get_pusher(user_id, &pushkey)
132 .await
133 .inspect_err(|error| {
134 warn!(?user_id, pushkey, ?error, "Missing pusher for suppressed flush");
135 })
136 else {
137 continue;
138 };
139
140 let flush = Flush {
141 user_id,
142 pushkey: &pushkey,
143 pusher: &pusher,
144 ruleset: &ruleset,
145 reason,
146 };
147
148 self.flush_suppressed_rooms(&flush, rooms).await;
149 }
150}
151
152#[implement(Service)]
153async fn flush_suppressed_rooms(&self, flush: &Flush<'_>, rooms: SuppressedRooms) {
154 if rooms.is_empty() {
155 return;
156 }
157
158 let Flush { user_id, pushkey, reason, .. } = *flush;
159
160 debug!(?user_id, pushkey, rooms = rooms.len(), reason, "Flushing suppressed pushes");
161 let sent = rooms
162 .into_iter()
163 .stream()
164 .then(|(room_id, pdu_ids)| self.flush_suppressed_room(flush, room_id, pdu_ids))
165 .ready_fold(0, usize::saturating_add)
166 .await;
167
168 debug!(?user_id, pushkey, sent, "Flushed suppressed push notifications");
169}
170
171#[implement(Service)]
172async fn flush_suppressed_room(
173 &self,
174 flush: &Flush<'_>,
175 room_id: OwnedRoomId,
176 pdu_ids: Vec<RawPduId>,
177) -> usize {
178 let unread = self
179 .services
180 .pusher
181 .notification_count(flush.user_id, &room_id)
182 .await;
183
184 if unread == 0 {
185 trace!(user_id = ?flush.user_id, ?room_id, "Skipping suppressed push flush: no unread");
186 return 0;
187 }
188
189 pdu_ids
190 .into_iter()
191 .stream()
192 .wide_filter_map(async |pdu_id| {
193 self.suppressable_pdu(flush.user_id, &pdu_id)
194 .await
195 .map(|pdu| (pdu_id, pdu))
196 })
197 .broadn_then(Some(PUSH_WIDTH), async |(pdu_id, pdu)| {
198 self.flush_suppressed_pdu(flush, &room_id, pdu_id, &pdu)
199 .await
200 })
201 .ready_filter(|&sent| sent)
202 .count()
203 .await
204}
205
206#[implement(Service)]
207async fn flush_suppressed_pdu(
208 &self,
209 flush: &Flush<'_>,
210 room_id: &RoomId,
211 pdu_id: RawPduId,
212 pdu: &Pdu,
213) -> bool {
214 let Flush { user_id, pushkey, pusher, ruleset, .. } = *flush;
215
216 let Err(error) = self
217 .services
218 .pusher
219 .send_push_notice(user_id, pusher, ruleset, pdu)
220 .await
221 else {
222 return true;
223 };
224
225 let requeued = self
226 .services
227 .pusher
228 .queue_suppressed_push(user_id, pushkey, room_id, pdu_id);
229
230 warn!(
231 ?user_id,
232 ?room_id,
233 ?error,
234 requeued,
235 "Failed to send suppressed push notification"
236 );
237
238 false
239}
240
241#[implement(Service)]
242pub(super) async fn enqueue_suppressed_push_events(
243 &self,
244 user_id: &UserId,
245 pushkey: &str,
246 events: &[SendingEvent],
247) -> usize {
248 events
249 .iter()
250 .stream()
251 .ready_filter_map(|event| extract_variant!(event, SendingEvent::Pdu))
252 .wide_filter_map(async |pdu_id| {
253 self.suppressable_pdu(user_id, pdu_id)
254 .await
255 .map(|pdu| (pdu_id, pdu))
256 })
257 .ready_fold(0, |queued: usize, (pdu_id, pdu)| {
258 let accepted = self.services.pusher.queue_suppressed_push(
259 user_id,
260 pushkey,
261 pdu.room_id(),
262 *pdu_id,
263 );
264
265 queued.saturating_add(usize::from(accepted))
266 })
267 .await
268}
269
270#[implement(Service)]
274async fn suppressable_pdu(&self, user_id: &UserId, pdu_id: &RawPduId) -> Option<Pdu> {
275 let Ok(pdu) = self
276 .services
277 .timeline
278 .get_pdu_from_id(pdu_id)
279 .await
280 else {
281 debug!(?user_id, ?pdu_id, "Suppressed PDU is missing");
282 return None;
283 };
284
285 if pdu.is_redacted() {
286 trace!(?user_id, ?pdu_id, "Suppressed PDU is redacted");
287 return None;
288 }
289
290 Some(pdu)
291}
292
293#[implement(Service)]
299pub(super) async fn pushing_suppressed(&self, user_id: &UserId) -> bool {
300 if !self.services.config.push.suppress_when_active {
301 debug!(?user_id, "push not suppressed: push.suppress_when_active disabled");
302 return false;
303 }
304
305 let Ok(presence) = self.services.presence.get_presence(user_id).await else {
306 debug!(?user_id, "push not suppressed: presence unavailable");
307 return false;
308 };
309
310 if presence.content.presence != PresenceState::Online {
311 debug!(
312 ?user_id,
313 presence = ?presence.content.presence,
314 "push not suppressed: presence not online"
315 );
316
317 return false;
318 }
319
320 let presence_age_ms = presence
321 .content
322 .last_active_ago
323 .map(u64::from)
324 .unwrap_or(u64::MAX);
325
326 if presence_age_ms >= ACTIVE_PRESENCE_AGE_MS {
327 debug!(?user_id, presence_age_ms, "push not suppressed: presence too old");
328 return false;
329 }
330
331 let sync_gap_ms = self
332 .services
333 .presence
334 .last_sync_gap_ms(user_id)
335 .await;
336
337 match sync_gap_ms {
338 | Some(gap) if gap < ACTIVE_SYNC_GAP_MS => {
339 debug!(
340 ?user_id,
341 presence_age_ms,
342 sync_gap_ms = gap,
343 "suppressing push: active heuristic"
344 );
345
346 true
347 },
348 | Some(gap) => {
349 debug!(
350 ?user_id,
351 presence_age_ms,
352 sync_gap_ms = gap,
353 "push not suppressed: sync gap too large"
354 );
355
356 false
357 },
358 | None => {
359 debug!(?user_id, presence_age_ms, "push not suppressed: no recent sync");
360
361 false
362 },
363 }
364}