Skip to main content

tuwunel_service/pusher/
mod.rs

1//! Users' pushers and the notifications they deliver.
2//!
3//! The service stores pushers, evaluates push rules for each appended event,
4//! keeps notification counts, and delivers to push gateways and by email.
5
6mod append;
7mod badge;
8mod email;
9mod notification;
10mod redact;
11mod request;
12mod send;
13mod suppressed;
14#[cfg(test)]
15mod tests;
16
17use std::{
18	collections::BTreeMap,
19	sync::{Arc, LazyLock},
20};
21
22use async_trait::async_trait;
23use futures::{Stream, StreamExt, TryFutureExt, future::join};
24use ruma::{
25	DeviceId, OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
26	api::client::push::{Pusher, PusherKind, set_pusher::v3::PusherAction},
27	events::{
28		AnySyncTimelineEvent, GlobalAccountDataEventType, push_rules::PushRulesEvent,
29		room::power_levels::RoomPowerLevels,
30	},
31	push::{Action, FlattenedJson, PushConditionPowerLevelsCtx, PushConditionRoomCtx, Ruleset},
32	serde::Raw,
33	uint,
34};
35use serde::Deserialize;
36use tracing::Level;
37use tuwunel_core::{
38	Err, Result, err, implement,
39	utils::{
40		BoolExt, MutexMap,
41		future::TryExtExt,
42		result::ErrLog,
43		stream::{BroadbandExt, IterStream, ReadyExt, TryIgnore, WidebandExt},
44	},
45};
46use tuwunel_database::{Database, Deserialized, Ignore, Interfix, Map};
47use tuwunel_matrix::Event;
48use url::Url;
49
50use self::badge::SentBadges;
51pub use self::{
52	append::Notified,
53	email::Outcome as EmailOutcome,
54	suppressed::{SuppressedPushes, SuppressedRooms},
55};
56
57/// The events an event relates to, keyed by relation type, for MSC3664.
58type RelatedEvents = BTreeMap<String, FlattenedJson>;
59
60/// Guards one user's counter in one room across a read-modify-write.
61///
62/// It holds one entry per (room, user) with an increment (append), decrement
63/// (redaction) or reset (receipt, local send) in flight.
64type CountMutex = MutexMap<(OwnedRoomId, OwnedUserId), ()>;
65
66/// The inputs for evaluating one user's push rules against one event.
67pub struct Evaluate<'a, 'b> {
68	/// The user whose rules are evaluated, and who is notified.
69	pub user: &'b UserId,
70	/// The user's ruleset, which outlives the borrowed actions returned.
71	pub ruleset: &'a Ruleset,
72	/// The room's power levels, without which some conditions never match.
73	pub power_levels: Option<&'b RoomPowerLevels>,
74	/// The event being evaluated, in the format the conditions flatten.
75	pub pdu: &'b Raw<AnySyncTimelineEvent>,
76	/// The room the event was sent to.
77	pub room_id: &'b RoomId,
78	/// The events it relates to, or `None` where they are not resolved at all.
79	pub related_events: Option<&'b Arc<RelatedEvents>>,
80}
81
82/// MSC3664 matches a reply as if it carried a relation of this type.
83const IN_REPLY_TO: &str = "m.in_reply_to";
84
85/// Shared by every event that resolves no relations, which is every event
86/// while MSC3664 evaluation is disabled.
87static NO_RELATED_EVENTS: LazyLock<Arc<RelatedEvents>> = LazyLock::new(Arc::default);
88
89#[derive(Deserialize)]
90struct ExtractRelatesTo {
91	#[serde(rename = "m.relates_to")]
92	relates_to: RelatesTo,
93}
94
95#[derive(Deserialize)]
96struct RelatesTo {
97	rel_type: Option<String>,
98
99	event_id: Option<OwnedEventId>,
100
101	#[serde(rename = "m.in_reply_to")]
102	in_reply_to: Option<InReplyTo>,
103}
104
105#[derive(Deserialize)]
106struct InReplyTo {
107	event_id: OwnedEventId,
108}
109
110/// Stores pushers and delivers the notifications they ask for.
111///
112/// HTTP pushers are pushed through the sending queue, and email pushers are
113/// mailed by this service's worker.
114pub struct Service {
115	services: Arc<crate::services::OnceServices>,
116	notification_increment_mutex: CountMutex,
117	highlight_increment_mutex: CountMutex,
118	db: Data,
119	suppressed: suppressed::SuppressedQueue,
120	sent_badges: SentBadges,
121	email_wakes: email::Wakes,
122	email_locks: email::Locks,
123}
124
125struct Data {
126	db: Arc<Database>,
127	senderkey_emailcursor: Arc<Map>,
128	senderkey_pusher: Arc<Map>,
129	senderkeyroom_emailthrottle: Arc<Map>,
130	pushkey_deviceid: Arc<Map>,
131	useridcount_notification: Arc<Map>,
132	userroomid_highlightcount: Arc<Map>,
133	userroomid_notificationcount: Arc<Map>,
134	roomuserid_lastnotificationread: Arc<Map>,
135}
136
137#[async_trait]
138impl crate::Service for Service {
139	fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
140		Ok(Arc::new(Self {
141			services: args.services.clone(),
142			notification_increment_mutex: MutexMap::new(),
143			highlight_increment_mutex: MutexMap::new(),
144			db: Data {
145				db: args.db.clone(),
146				senderkey_emailcursor: args.db["senderkey_emailcursor"].clone(),
147				senderkey_pusher: args.db["senderkey_pusher"].clone(),
148				senderkeyroom_emailthrottle: args.db["senderkeyroom_emailthrottle"].clone(),
149				pushkey_deviceid: args.db["pushkey_deviceid"].clone(),
150				useridcount_notification: args.db["useridcount_notification"].clone(),
151				userroomid_highlightcount: args.db["userroomid_highlightcount"].clone(),
152				userroomid_notificationcount: args.db["userroomid_notificationcount"].clone(),
153				roomuserid_lastnotificationread: args.db["roomuserid_lastnotificationread"]
154					.clone(),
155			},
156			suppressed: suppressed::SuppressedQueue::default(),
157			sent_badges: SentBadges::default(),
158			email_wakes: email::Wakes::default(),
159			email_locks: MutexMap::new(),
160		}))
161	}
162
163	async fn worker(self: Arc<Self>) -> Result {
164		if self.db.db.is_read_only() {
165			return Ok(()); // Mailed notifications could not be recorded and would be mailed again.
166		}
167
168		self.email_worker().await;
169
170		Ok(())
171	}
172
173	async fn interrupt(&self) { self.close_email_wakes(); }
174
175	fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
176}
177
178/// Stores or deletes one of the user's pushers, as `/pushers/set` asks.
179///
180/// Storing refuses an HTTP pusher whose URL is invalid or forbidden. It
181/// refuses an email pusher when the server has no SMTP, or when the address is
182/// malformed or not verified on the user's account.
183#[implement(Service)]
184pub async fn set_pusher(
185	&self,
186	sender: &UserId,
187	sender_device: &DeviceId,
188	pusher: &PusherAction,
189) -> Result {
190	match pusher {
191		| PusherAction::Delete(ids) =>
192			self.set_pusher_delete(sender, ids.pushkey.as_str())
193				.await,
194		| PusherAction::Post(data) =>
195			self.set_pusher_post(sender, sender_device, pusher, &data.pusher)
196				.await?,
197	}
198
199	Ok(())
200}
201
202#[implement(Service)]
203async fn set_pusher_delete(&self, sender: &UserId, pushkey: &str) {
204	self.delete_pusher(sender, pushkey).await;
205}
206
207#[implement(Service)]
208async fn set_pusher_post(
209	&self,
210	sender: &UserId,
211	sender_device: &DeviceId,
212	action: &PusherAction,
213	pusher: &Pusher,
214) -> Result {
215	let pushkey = pusher.ids.pushkey.as_str();
216
217	if pushkey.len() > 512 {
218		return Err!(Request(InvalidParam("Push key length cannot be greater than 512 bytes.")));
219	}
220
221	if pusher.ids.app_id.as_str().len() > 64 {
222		return Err!(Request(InvalidParam("App ID length cannot be greater than 64 bytes.")));
223	}
224
225	if let PusherKind::Http(http) = &pusher.kind {
226		let url = &http.url;
227		let url = Url::parse(&http.url).map_err(|e| {
228			err!(Request(InvalidParam(warn!(%url, "HTTP pusher URL is not a valid URL: {e}"))))
229		})?;
230
231		self.check_http_pusher_url(&url)?;
232	}
233
234	if matches!(pusher.kind, PusherKind::Email(_)) {
235		self.check_email_pusher(sender, pushkey).await?;
236	}
237
238	self.put_pusher(sender, pushkey, action).await;
239
240	self.db
241		.pushkey_deviceid
242		.insert(pushkey, sender_device);
243
244	self.forget_sent_badge(sender, pushkey);
245
246	Ok(())
247}
248
249#[implement(Service)]
250fn check_http_pusher_url(&self, url: &Url) -> Result {
251	if ["http", "https"]
252		.iter()
253		.all(|&scheme| !scheme.eq_ignore_ascii_case(url.scheme()))
254	{
255		return Err!(Request(InvalidParam(
256			warn!(%url, "HTTP pusher URL is not a valid HTTP/HTTPS URL")
257		)));
258	}
259
260	if self.services.client.proxy.resolver_alias(url) {
261		return Err!(Request(InvalidParam(
262			warn!(%url, "HTTP pusher URL is a forbidden proxy endpoint")
263		)));
264	}
265
266	if !self.services.client.valid_cidr_range_url(url) {
267		return Err!(Request(InvalidParam(
268			warn!(%url, "HTTP pusher URL is a forbidden remote address")
269		)));
270	}
271
272	Ok(())
273}
274
275/// Refuses an email pusher this server cannot or may not deliver.
276///
277/// Mail needs SMTP configured and the address verified on the sender's own
278/// account. Without the binding check any user could direct notification mail
279/// at any address.
280#[implement(Service)]
281async fn check_email_pusher(&self, sender: &UserId, address: &str) -> Result {
282	if !self.services.sendmail.is_enabled() {
283		return Err!(Request(InvalidParam(
284			"Email notifications are not available on this server."
285		)));
286	}
287
288	self.services.sendmail.check_address(address)?;
289	self.services
290		.threepid
291		.address_bound_to(sender, address)
292		.await?
293		.into_option()
294		.ok_or_else(|| {
295			err!(Request(ThreepidNotFound("Email address is not bound to your account.")))
296		})
297}
298
299/// Deletes one of the user's pushers, with its email cursor and throttles.
300///
301/// The email lock keeps the worker from writing them back.
302#[implement(Service)]
303pub async fn delete_pusher(&self, sender: &UserId, pushkey: &str) {
304	let _lock = self.email_lock(sender, pushkey).await;
305
306	self.remove_pusher(sender, pushkey).await;
307}
308
309/// Deletes a pusher and everything kept for it.
310///
311/// Callers hold the pusher's email lock.
312#[implement(Service)]
313async fn remove_pusher(&self, sender: &UserId, pushkey: &str) {
314	let key = (sender, pushkey);
315
316	self.db.senderkey_pusher.del(key);
317	self.db.pushkey_deviceid.remove(pushkey);
318	self.clear_suppressed_pushkey(sender, pushkey);
319	self.forget_sent_badge(sender, pushkey);
320
321	// Last: a cancelled removal leaves only email rows, cleared at boot or by a new email pusher.
322	self.forget_email_state(sender, pushkey).await;
323
324	self.services
325		.sending
326		.cleanup_events(None, Some(sender), Some(pushkey))
327		.await
328		.ok();
329}
330
331#[implement(Service)]
332pub async fn get_device_pushkeys(&self, sender: &UserId, device_id: &DeviceId) -> Vec<String> {
333	self.get_pushkeys(sender)
334		.map(ToOwned::to_owned)
335		.broad_filter_map(async |pushkey| {
336			self.get_pusher_device(&pushkey)
337				.await
338				.ok()
339				.as_ref()
340				.is_some_and(|pusher_device| pusher_device == device_id)
341				.then_some(pushkey)
342		})
343		.collect()
344		.await
345}
346
347#[implement(Service)]
348pub async fn get_pusher_device(&self, pushkey: &str) -> Result<OwnedDeviceId> {
349	self.db
350		.pushkey_deviceid
351		.get(pushkey)
352		.await
353		.deserialized()
354}
355
356#[implement(Service)]
357pub async fn get_pusher(&self, sender: &UserId, pushkey: &str) -> Result<Pusher> {
358	let senderkey = (sender, pushkey);
359	self.db
360		.senderkey_pusher
361		.qry(&senderkey)
362		.await
363		.deserialized()
364}
365
366#[implement(Service)]
367pub async fn get_pushers(&self, sender: &UserId) -> Vec<Pusher> {
368	let prefix = (sender, Interfix);
369	self.db
370		.senderkey_pusher
371		.stream_prefix(&prefix)
372		.ignore_err()
373		.map(|(_, pusher): (Ignore, Pusher)| pusher)
374		.collect()
375		.await
376}
377
378#[implement(Service)]
379pub fn get_pushkeys<'a>(&'a self, sender: &'a UserId) -> impl Stream<Item = &str> + Send + 'a {
380	let prefix = (sender, Interfix);
381	self.db
382		.senderkey_pusher
383		.keys_prefix(&prefix)
384		.ignore_err()
385		.map(|(_, pushkey): (Ignore, &str)| pushkey)
386}
387
388/// The push ruleset a user's notifications are evaluated against.
389///
390/// A user without stored rules gets the server default ruleset.
391#[implement(Service)]
392#[tracing::instrument(skip(self), level = "debug")]
393pub async fn ruleset(&self, user_id: &UserId) -> Ruleset {
394	self.services
395		.account_data
396		.get_global(user_id, GlobalAccountDataEventType::PushRules)
397		.await
398		.log_err(Level::TRACE)
399		.map_or_else(|_| Ruleset::server_default(user_id), |ev: PushRulesEvent| ev.content.global)
400}
401
402#[implement(Service)]
403#[tracing::instrument(level = "debug", skip_all)]
404pub fn get_notifications<'a>(
405	&'a self,
406	sender: &'a UserId,
407	from: Option<u64>,
408) -> impl Stream<Item = (u64, Notified)> + Send + 'a {
409	let from = from
410		.map(|from| from.saturating_sub(1))
411		.unwrap_or(u64::MAX);
412
413	self.db
414		.useridcount_notification
415		.rev_stream_from(&(sender, from))
416		.ignore_err()
417		.map(|item: ((&UserId, u64), _)| (item.0, item.1))
418		.ready_take_while(move |((user_id, _count), _)| sender == *user_id)
419		.map(|((_, count), notified)| (count, notified))
420}
421
422#[implement(Service)]
423#[tracing::instrument(level = "debug", skip_all)]
424pub async fn get_actions<'a>(
425	&self,
426	Evaluate {
427		user,
428		ruleset,
429		power_levels,
430		pdu,
431		room_id,
432		related_events,
433	}: Evaluate<'a, '_>,
434) -> &'a [Action] {
435	let user_display_name = self
436		.services
437		.profile
438		.displayname(user)
439		.unwrap_or_else(|_| user.localpart().to_owned());
440
441	let room_joined_count = self
442		.services
443		.state_cache
444		.room_joined_count(room_id)
445		.map_ok(TryInto::try_into)
446		.map_ok(|res| res.unwrap_or_else(|_| uint!(1)))
447		.unwrap_or_default();
448
449	let (room_joined_count, user_display_name) = join(room_joined_count, user_display_name).await;
450
451	let power_levels = power_levels.map(|power_levels| PushConditionPowerLevelsCtx {
452		users: power_levels.users.clone(),
453		users_default: power_levels.users_default,
454		notifications: power_levels.notifications.clone(),
455		rules: power_levels.rules.clone(),
456	});
457
458	let ctx = PushConditionRoomCtx::new(
459		room_id.to_owned(),
460		room_joined_count,
461		user.to_owned(),
462		user_display_name,
463	);
464
465	let ctx = match related_events {
466		| Some(related_events) => ctx.with_related_events(related_events.clone()),
467		| None => ctx,
468	};
469
470	let ctx = match power_levels {
471		| Some(pl) => ctx.with_power_levels(pl),
472		| None => ctx,
473	};
474
475	ruleset.get_actions(pdu, &ctx).await
476}
477
478/// Resolve the events an event relates to, for the MSC3664 push condition.
479///
480/// `None` while `push.msc3664_related_event_match` is disabled, which leaves every
481/// MSC3664 condition unable to match. The relations do not vary by recipient,
482/// so the append path resolves them once for the whole room; the push-notice
483/// path resolves them per notice, since it holds one event at a time.
484#[implement(Service)]
485#[tracing::instrument(level = "debug", skip_all)]
486pub async fn related_events<E: Event>(&self, event: &E) -> Option<Arc<RelatedEvents>> {
487	let config = &self.services.server.config;
488
489	config
490		.push
491		.msc3664_related_event_match
492		.then_async(async || {
493			let Ok(ExtractRelatesTo { relates_to }) = event.get_content() else {
494				return Some(NO_RELATED_EVENTS.clone());
495			};
496
497			let reply = relates_to
498				.in_reply_to
499				.map(|reply| (IN_REPLY_TO.to_owned(), reply.event_id));
500
501			let related = relates_to
502				.rel_type
503				.zip(relates_to.event_id)
504				.into_iter()
505				.chain(reply)
506				.stream()
507				.wide_filter_map(async |(rel_type, event_id)| {
508					let related = self
509						.services
510						.timeline
511						.get_pdu(&event_id)
512						.await
513						.ok()
514						// A relation crossing rooms is not one the sender could have made, and
515						// following it would expose an event the recipient may not be in a room for.
516						.filter(|related| related.room_id() == event.room_id())?;
517
518					let related: Raw<AnySyncTimelineEvent> = related.to_format();
519
520					Some((rel_type, FlattenedJson::from_raw(&related)))
521				})
522				.collect()
523				.await;
524
525			Some(Arc::new(related))
526		})
527		.await
528		.flatten()
529}