Skip to main content

tuwunel_service/profile/
mod.rs

1mod remote;
2#[cfg(test)]
3mod tests;
4
5use std::{borrow::Cow, collections::BTreeMap, sync::Arc};
6
7use futures::{
8	Stream, StreamExt, TryStreamExt,
9	future::{Either, join},
10	stream::empty,
11};
12use ruma::{
13	CanonicalJsonObject, CanonicalJsonValue, MxcUri, OwnedMxcUri, OwnedRoomId, OwnedUserId,
14	RoomId, UserId,
15	api::federation::query::get_profile_information,
16	events::{
17		StateEventType,
18		room::member::{MembershipState, RoomMemberEventContent},
19	},
20	profile::{ProfileFieldName, ProfileFieldValue},
21};
22use serde::Deserialize;
23use serde_json::Value;
24use tuwunel_core::{
25	Err, Result, err, implement,
26	smallvec::SmallVec,
27	utils::{
28		BoolExt, MutexMap, MutexMapGuard, ReadyExt,
29		future::TryExtExt,
30		result::NotFound,
31		stream::{IterStream, TryIgnore, TryReadyExt, automatic_width},
32	},
33	warn,
34};
35use tuwunel_database::{
36	Deserialized, Ignore, Interfix, Json, KeyVal, Map, Txn, deserialize_from_slice, serialize_key,
37};
38use tuwunel_matrix::{PduBuilder, pdu::Content};
39
40type ProfileLock = MutexMapGuard<OwnedUserId, ()>;
41
42pub struct Service {
43	mutex: MutexMap<OwnedUserId, ()>,
44	services: Arc<crate::services::OnceServices>,
45	profilechangeid_userid: Arc<Map>,
46	useridprofilekey_value: Arc<Map>,
47}
48
49impl crate::Service for Service {
50	fn build(args: &crate::Args<'_>) -> Result<Arc<Self>> {
51		Ok(Arc::new(Self {
52			mutex: MutexMap::new(),
53			services: args.services.clone(),
54			profilechangeid_userid: args.db["profilechangeid_userid"].clone(),
55			useridprofilekey_value: args.db["useridprofilekey_value"].clone(),
56		}))
57	}
58
59	fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
60}
61
62/// One logged profile write: the user whose profile was written, and the name
63/// of a field the write logged.
64///
65/// Both members borrow the database cursor that produced them, so a consumer
66/// retaining either past the cursor's next advance must own it first.
67pub type ProfileChange<'a> = (&'a UserId, &'a str);
68
69/// A row of the profile change log: the field's name rides the key so that a
70/// write covering several fields needs one count, and the value names the user
71/// for the rows keyed by room.
72type ChangeKeyVal<'a> = KeyVal<'a, (&'a str, u64, &'a str), &'a UserId>;
73
74/// One field of a profile write: the name and the value to store, or `None`
75/// to clear it.
76pub type ProfileValue = (ProfileFieldName, Option<Value>);
77
78// A single write names one field; a combined write names both.
79type ChangedWrites<'a> = SmallVec<[&'a ProfileValue; 2]>;
80
81/// The field names one write actually changes, almost always just the one the
82/// caller named.
83type ChangedFields<'a> = SmallVec<[&'a str; 1]>;
84
85/// The field names a profile holds when it is cleared.
86///
87/// A profile commonly holds only the two canonical fields, which sizes the
88/// inline budget.
89type ClearedFields = SmallVec<[ProfileFieldName; 2]>;
90
91/// The stored profile as it would read after candidate fields are written.
92///
93/// Candidate values overlay retained fields before whole-profile admission.
94type ProspectiveProfile<'a> = BTreeMap<ProfileFieldName, Cow<'a, Value>>;
95
96/// MSC4426 maximum `m.status` text length, in bytes.
97const MAX_STATUS_TEXT_LENGTH: usize = 256;
98
99/// MSC4426 maximum `m.status` emoji length, in bytes.
100const MAX_STATUS_EMOJI_LENGTH: usize = 32;
101
102/// Maximum local display name length, in characters.
103///
104/// A longer name is refused with `M_BAD_JSON`.
105pub const MAX_DISPLAYNAME_LENGTH: usize = 256;
106
107const MAX_AVATAR_URL_LENGTH: usize = 1000;
108
109/// Per-update policy for fanning a global profile change out to each of
110/// the user's joined rooms as a fresh `m.room.member` event. Mirrors the
111/// MSC4466 `propagate_to` axis.
112#[derive(Copy, Clone, Debug, Eq, PartialEq)]
113pub enum Propagation {
114	/// Send a member event to every joined room.
115	All,
116
117	/// Send a member event only to rooms whose current per-room value
118	/// matches the user's prior global value; rooms with a per-room
119	/// override (e.g. set via `/myroomnick`) are skipped.
120	Unchanged,
121
122	/// Send no member events; update the global profile only.
123	None,
124}
125
126#[implement(Service)]
127pub async fn update_all_rooms(
128	&self,
129	user_id: &UserId,
130	profile_values: &[(ProfileFieldName, Option<Value>)],
131	propagation: Propagation,
132) {
133	if matches!(propagation, Propagation::None) {
134		return;
135	}
136
137	if !profile_values.iter().any(|(name, _)| {
138		matches!(name, ProfileFieldName::DisplayName | ProfileFieldName::AvatarUrl)
139	}) {
140		return;
141	}
142
143	// Suspended senders may not emit member events; OIDC, SSO, and MAS profile
144	// updates reach here without passing any suspension-blocked route.
145	if self.services.users.is_suspended(user_id).await {
146		return;
147	}
148
149	let (current_displayname, current_avatar_url) =
150		if matches!(propagation, Propagation::Unchanged) {
151			join(self.displayname(user_id).ok(), self.avatar_url(user_id).ok()).await
152		} else {
153			(None, None)
154		};
155
156	let rooms: Vec<OwnedRoomId> = self
157		.services
158		.state_cache
159		.rooms_joined(user_id)
160		.map(Into::into)
161		.collect()
162		.await;
163
164	rooms
165		.iter()
166		.stream()
167		.for_each_concurrent(automatic_width(), async |room_id| {
168			if let Err(e) = self
169				.update_room(
170					user_id,
171					room_id,
172					profile_values,
173					propagation,
174					current_displayname.as_deref(),
175					current_avatar_url.as_deref(),
176				)
177				.await
178			{
179				warn!(
180					%user_id,
181					%room_id,
182					%e,
183					"Failed to update room profile",
184				);
185			}
186		})
187		.await;
188}
189
190#[implement(Service)]
191async fn update_room(
192	&self,
193	user_id: &UserId,
194	room_id: &RoomId,
195	profile_values: &[ProfileValue],
196	propagation: Propagation,
197	current_displayname: Option<&str>,
198	current_avatar_url: Option<&MxcUri>,
199) -> Result {
200	let unchanged = match propagation {
201		| Propagation::All => false,
202		| Propagation::Unchanged => true,
203		| Propagation::None => return Ok(()),
204	};
205
206	// Held from the read so a membership change landing between the two
207	// cannot be overwritten by the append.
208	let state_lock = self.services.state.mutex.lock(room_id).await;
209
210	let pdu = self
211		.services
212		.state_accessor
213		.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())
214		.await?;
215
216	let member: RoomMemberEventContent = pdu.content.deserialize_as_unchecked()?;
217
218	if !matches!(member.membership, MembershipState::Join) {
219		return Ok(());
220	}
221
222	let writes = changed_writes(
223		&member,
224		profile_values,
225		unchanged,
226		current_displayname,
227		current_avatar_url,
228	);
229
230	if writes.is_empty() {
231		return Ok(());
232	}
233
234	// Copy the previous content raw so its unrelated keys survive (MSC4466).
235	let overlaid = overlay_member_content(pdu.content.deserialize()?, &writes)?;
236	let content = Content::new(&overlaid)?;
237
238	// Some write values do not survive the typed round trip, so the result is checked.
239	let checked: RoomMemberEventContent = content.deserialize_as_unchecked()?;
240
241	BoolExt::ok_or_else(overlay_holds(&checked, &writes), || {
242		err!(Request(Unknown("The overlaid member event does not hold the profile write")))
243	})?;
244
245	let builder = PduBuilder {
246		event_type: StateEventType::RoomMember.into(),
247		content,
248		state_key: Some(user_id.as_str().into()),
249		..PduBuilder::default()
250	};
251
252	self.services
253		.timeline
254		.build_and_append_pdu(builder, user_id, room_id, &state_lock)
255		.await?;
256
257	Ok(())
258}
259
260/// Returns the profile writes that change the member event.
261///
262/// Compares against the typed previous content, so a no-op write emits nothing.
263/// With `unchanged`, a field is written only where the room still holds the
264/// current global value, so a room override is left alone.
265fn changed_writes<'a>(
266	member: &RoomMemberEventContent,
267	profile_values: &'a [ProfileValue],
268	unchanged: bool,
269	current_displayname: Option<&str>,
270	current_avatar_url: Option<&MxcUri>,
271) -> ChangedWrites<'a> {
272	profile_values
273		.iter()
274		.filter(|write| {
275			!unchanged || holds_current(member, write, current_displayname, current_avatar_url)
276		})
277		.filter(|write| !member_holds(member, write))
278		.collect()
279}
280
281fn holds_current(
282	member: &RoomMemberEventContent,
283	(name, _): &ProfileValue,
284	current_displayname: Option<&str>,
285	current_avatar_url: Option<&MxcUri>,
286) -> bool {
287	let current = match name {
288		| ProfileFieldName::DisplayName => current_displayname,
289		| _ => current_avatar_url.map(MxcUri::as_str),
290	};
291
292	field_matches(member, name, current).unwrap_or(false)
293}
294
295fn member_holds(member: &RoomMemberEventContent, (name, value): &ProfileValue) -> bool {
296	let written = value.as_ref().and_then(Value::as_str);
297
298	field_matches(member, name, written).unwrap_or(true)
299}
300
301/// Whether the member's `name` field reads as `value`.
302///
303/// Returns `None` for a field other than the display name or the avatar URL,
304/// which the member event does not carry.
305fn field_matches(
306	member: &RoomMemberEventContent,
307	name: &ProfileFieldName,
308	value: Option<&str>,
309) -> Option<bool> {
310	match name {
311		| ProfileFieldName::DisplayName => Some(member.displayname.as_deref() == value),
312		| ProfileFieldName::AvatarUrl => Some(avatar_matches(member, value)),
313		| _ => None,
314	}
315}
316
317/// Whether the member's avatar reads as `url`.
318///
319/// `compat-empty-string-null` reads a written `""` back as `None`, so an empty
320/// `url` matches an absent avatar.
321fn avatar_matches(member: &RoomMemberEventContent, url: Option<&str>) -> bool {
322	member.avatar_url.as_deref().map(MxcUri::as_str) == url.filter(|url| !url.is_empty())
323}
324
325/// Overlays the profile writes onto the previous member content.
326///
327/// MSC4466 copies every other key unchanged, and forbids copying `reason` and
328/// `join_authorised_via_users_server`. A key the write clears is removed.
329fn overlay_member_content(
330	mut content: CanonicalJsonObject,
331	writes: &[&ProfileValue],
332) -> Result<CanonicalJsonObject> {
333	content.remove("reason");
334	content.remove("join_authorised_via_users_server");
335
336	writes
337		.iter()
338		.try_for_each(|write| write_field(&mut content, write))
339		.map(|()| content)
340}
341
342fn write_field(content: &mut CanonicalJsonObject, (name, value): &ProfileValue) -> Result {
343	match value {
344		| Some(value) =>
345			content.insert(name.as_str().into(), CanonicalJsonValue::deserialize(value)?),
346		| None => content.remove(name.as_str()),
347	};
348
349	Ok(())
350}
351
352fn overlay_holds(member: &RoomMemberEventContent, writes: &[&ProfileValue]) -> bool {
353	matches!(member.membership, MembershipState::Join)
354		&& writes
355			.iter()
356			.all(|write| member_holds(member, write))
357}
358
359/// Sets a new displayname or removes it if displayname is None. You still
360/// need to notify all rooms of this change.
361#[implement(Service)]
362pub async fn set_displayname(
363	&self,
364	user_id: &UserId,
365	displayname: Option<&str>,
366	propagation: Option<Propagation>,
367) -> Result {
368	self.set_profile_keys(
369		user_id,
370		&[(
371			ProfileFieldName::DisplayName,
372			displayname.map(|displayname| {
373				serde_json::to_value(displayname).expect("displayname serialization cannot fail")
374			}),
375		)],
376		propagation,
377	)
378	.await
379}
380
381/// Returns the displayname of a user on this homeserver.
382#[implement(Service)]
383pub async fn displayname(&self, user_id: &UserId) -> Result<String> {
384	self.profile_key(user_id, &ProfileFieldName::DisplayName)
385		.await
386}
387
388/// Sets a new avatar_url or removes it if avatar_url is None.
389#[implement(Service)]
390pub async fn set_avatar_url(
391	&self,
392	user_id: &UserId,
393	avatar_url: Option<&MxcUri>,
394	propagation: Option<Propagation>,
395) -> Result {
396	self.set_profile_keys(
397		user_id,
398		&[(
399			ProfileFieldName::AvatarUrl,
400			avatar_url.map(|avatar_url| {
401				serde_json::to_value(avatar_url).expect("avatar url serialization cannot fail")
402			}),
403		)],
404		propagation,
405	)
406	.await
407}
408
409/// Get the `avatar_url` of a user.
410#[implement(Service)]
411pub async fn avatar_url(&self, user_id: &UserId) -> Result<OwnedMxcUri> {
412	self.profile_key(user_id, &ProfileFieldName::AvatarUrl)
413		.await
414}
415
416/// Sets a new timezone or removes it if timezone is None.
417#[implement(Service)]
418pub async fn set_timezone(
419	&self,
420	user_id: &UserId,
421	timezone: Option<&str>,
422	propagation: Option<Propagation>,
423) -> Result {
424	self.set_profile_keys(
425		user_id,
426		&[(
427			ProfileFieldName::TimeZone,
428			timezone.map(|timezone| {
429				serde_json::to_value(timezone).expect("timezone serialization cannot fail")
430			}),
431		)],
432		propagation,
433	)
434	.await
435}
436
437/// Get the timezone of a user.
438#[implement(Service)]
439pub async fn timezone(&self, user_id: &UserId) -> Result<String> {
440	self.profile_key(user_id, &ProfileFieldName::TimeZone)
441		.await
442}
443
444/// Streams every stored profile field.
445///
446/// A field whose stored value no longer parses is dropped; `try_all_profile_keys`
447/// surfaces it instead.
448#[implement(Service)]
449pub fn all_profile_keys(&self, user_id: &UserId) -> impl Stream<Item = ProfileFieldValue> + Send {
450	self.try_all_profile_keys(user_id).ignore_err()
451}
452
453/// Streams every stored profile field, surfacing storage and decoding errors.
454///
455/// A stored value that no longer parses as a profile field is an error item
456/// rather than a dropped one, so a caller validating the whole profile can
457/// refuse to proceed on it.
458#[implement(Service)]
459pub fn try_all_profile_keys(
460	&self,
461	user_id: &UserId,
462) -> impl Stream<Item = Result<ProfileFieldValue>> + Send {
463	let prefix = (user_id, Interfix);
464
465	self.useridprofilekey_value
466		.stream_prefix(&prefix)
467		.ready_and_then(move |((_, key), Json(val)): ((Ignore, &str), Json<Value>)| {
468			ProfileFieldValue::new(key, val).map_err(|_| {
469				err!(Database(error!(%user_id, %key, "Invalid json in database profile value")))
470			})
471		})
472}
473
474/// Streams the names of the fields a user's profile holds.
475///
476/// The name rides the key, so a caller after names alone skips deserializing
477/// every value the way `all_profile_keys` must.
478#[implement(Service)]
479pub fn profile_field_names(
480	&self,
481	user_id: &UserId,
482) -> impl Stream<Item = ProfileFieldName> + Send {
483	self.try_profile_field_names(user_id).ignore_err()
484}
485
486/// Enumerates current profile field names, preserving storage and decoding errors.
487///
488/// Sync bases must finish this inventory before acknowledging profile delivery.
489#[implement(Service)]
490pub fn try_profile_field_names(
491	&self,
492	user_id: &UserId,
493) -> impl Stream<Item = Result<ProfileFieldName>> + Send {
494	let prefix = (user_id, Interfix);
495
496	self.useridprofilekey_value
497		.keys_prefix(&prefix)
498		.map_ok(|(_, name): (Ignore, &str)| name.into())
499}
500
501/// Clears every stored profile field and propagates canonical removals.
502///
503/// Member events are rewritten only for a local user, before the removals and
504/// their change rows commit together under the profile lock. A preparation
505/// error leaves storage unchanged but does not undo emitted member events.
506#[implement(Service)]
507pub async fn clear_profile_keys(&self, user_id: &UserId) -> Result {
508	let _profile_lock = self.mutex.lock(user_id).await;
509
510	let prefix = (user_id, Interfix);
511	let fields: ClearedFields = self
512		.useridprofilekey_value
513		.keys_prefix(&prefix)
514		.map_ok(|(_, field): (Ignore, &str)| field.into())
515		.try_collect()
516		.await?;
517
518	if self.services.globals.user_is_local(user_id) {
519		self.update_all_rooms(
520			user_id,
521			&[(ProfileFieldName::DisplayName, None), (ProfileFieldName::AvatarUrl, None)],
522			Propagation::All,
523		)
524		.await;
525	}
526
527	let txn = fields
528		.iter()
529		.fold(self.services.db.txn(), |mut txn, field| {
530			txn.del(&self.useridprofilekey_value, (user_id, field.as_str()));
531			txn
532		});
533
534	let rooms = || {
535		self.services
536			.state_cache
537			.rooms_joined_checked(user_id)
538	};
539
540	self.publish_update(user_id, &fields, &fields, txn, rooms)
541		.await
542}
543
544/// Sets profile field values, removing a field whose value is `None`.
545///
546/// Member events are rewritten first, then the stored fields and their change
547/// rows commit together under the profile lock. A preparation error leaves
548/// storage unchanged but does not undo emitted member events.
549#[implement(Service)]
550pub async fn set_profile_keys(
551	&self,
552	user_id: &UserId,
553	profile_values: &[(ProfileFieldName, Option<Value>)],
554	propagation: Option<Propagation>,
555) -> Result {
556	let profile_lock = self.mutex.lock(user_id).await;
557
558	self.set_profile_keys_locked(&profile_lock, user_id, profile_values, propagation)
559		.await
560}
561
562/// Sets profile field values under a profile lock the caller already holds.
563///
564/// A caller that must read the stored profile and write it back without an
565/// interleaved writer takes the lock once and passes it here.
566#[implement(Service)]
567async fn set_profile_keys_locked(
568	&self,
569	_profile_lock: &ProfileLock,
570	user_id: &UserId,
571	profile_values: &[(ProfileFieldName, Option<Value>)],
572	propagation: Option<Propagation>,
573) -> Result {
574	let local = self.services.globals.user_is_local(user_id);
575
576	let changed = self
577		.changed_fields(user_id, profile_values)
578		.await?;
579
580	if local {
581		self.check_changed_keys(user_id, profile_values, &changed)
582			.await?;
583	}
584
585	let propagation = propagation.unwrap_or(
586		if self
587			.services
588			.config
589			.profile
590			.preserve_room_overrides
591		{
592			Propagation::Unchanged
593		} else {
594			Propagation::All
595		},
596	);
597
598	if !matches!(propagation, Propagation::None) && local {
599		self.update_all_rooms(user_id, profile_values, propagation)
600			.await;
601	}
602
603	let txn = profile_values
604		.iter()
605		.fold(self.services.db.txn(), |mut txn, (name, value)| {
606			let key = (user_id, name.as_str());
607
608			if let Some(value) = value {
609				txn.put(&self.useridprofilekey_value, key, Json(value));
610			} else {
611				txn.del(&self.useridprofilekey_value, key);
612			}
613
614			txn
615		});
616
617	let rooms = || {
618		self.services
619			.state_cache
620			.rooms_joined_checked(user_id)
621	};
622
623	// A local write restating a stored value still reaches the writer's devices.
624	let logged = profile_values
625		.iter()
626		.map(|(name, _)| name.as_str())
627		.filter(|name| local || changed.contains(name));
628
629	self.publish_update(user_id, logged, &changed, txn, rooms)
630		.await
631}
632
633/// Pairs a profile field with the string a write stores in it.
634///
635/// A `None` value clears the field. `set_profile_keys` and
636/// `set_profile_keys_after` each take a slice of these pairs.
637#[must_use]
638pub fn string_field(name: ProfileFieldName, value: Option<&str>) -> ProfileValue {
639	(name, value.map(Value::from))
640}
641
642/// Checks local profile values, runs `before_write`, then writes the values,
643/// all under the user's profile lock.
644///
645/// A caller that changes the account a profile belongs to passes that change
646/// as `before_write`, so a refused value returns before the account changes
647/// and the values checked are the values written. `before_write` must not
648/// write this user's profile, whose lock it runs under.
649#[implement(Service)]
650pub async fn set_profile_keys_after<F>(
651	&self,
652	user_id: &UserId,
653	profile_values: &[ProfileValue],
654	propagation: Option<Propagation>,
655	before_write: F,
656) -> Result
657where
658	F: Future<Output = Result>,
659{
660	let profile_lock = self.mutex.lock(user_id).await;
661	let changed = self
662		.changed_fields(user_id, profile_values)
663		.await?;
664
665	self.check_changed_keys(user_id, profile_values, &changed)
666		.await?;
667
668	before_write.await?;
669
670	if profile_values.is_empty() {
671		return Ok(());
672	}
673
674	self.set_profile_keys_locked(&profile_lock, user_id, profile_values, propagation)
675		.await
676}
677
678// Only a change is checked, so a stored value now refused can still be restated.
679#[implement(Service)]
680async fn check_changed_keys(
681	&self,
682	user_id: &UserId,
683	profile_values: &[ProfileValue],
684	changed: &ChangedFields<'_>,
685) -> Result {
686	let is_changed = |name: &ProfileFieldName| changed.contains(&name.as_str());
687
688	profile_values
689		.iter()
690		.try_for_each(|(name, value)| {
691			check_profile_key(name.as_str())?;
692			value
693				.as_ref()
694				.filter(|_| is_changed(name))
695				.map_or(Ok(()), |value| check_profile_value(name.as_str(), value))
696		})?;
697
698	let grows = profile_values
699		.iter()
700		.any(|(name, value)| value.is_some() && is_changed(name));
701
702	if !grows {
703		return Ok(());
704	}
705
706	self.enforce_profile_size(user_id, profile_values)
707		.await
708}
709
710/// Names the fields whose stored value the write would actually change.
711///
712/// Only these are logged for the user's rooms, or at all for a remote user. A
713/// write restoring what is already stored is news to nobody else, and the
714/// on-demand remote refresh reissues every field on every lookup of a remote
715/// profile, so logging those would multiply the log by the request rate rather
716/// than the change rate.
717#[implement(Service)]
718async fn changed_fields<'a>(
719	&self,
720	user_id: &UserId,
721	profile_values: &'a [(ProfileFieldName, Option<Value>)],
722) -> Result<ChangedFields<'a>> {
723	profile_values
724		.iter()
725		.try_stream()
726		.try_filter_map(async |(name, value)| {
727			let stored = self.profile_key(user_id, name).await.optional()?;
728
729			let changed = stored
730				.as_ref()
731				.ne(&value.as_ref())
732				.then_some(name.as_str());
733
734			Ok(changed)
735		})
736		.try_collect()
737		.await
738}
739
740/// Commits staged profile fields together with their change rows.
741///
742/// Every logged field gets a row under the user's own prefix; every changed
743/// field also gets one under each room they are joined to. The row names the
744/// field and not only the user because a removal is otherwise unreportable: a
745/// reader re-reading the live profile cannot tell a cleared field from one
746/// that was never set. A room scan error discards the whole batch, so no row
747/// for that count is ever visible.
748#[implement(Service)]
749#[tracing::instrument(
750	name = "profile_update",
751	level = "debug",
752	skip_all,
753	fields(
754		%user_id,
755	),
756)]
757async fn publish_update<'a, L, T, S>(
758	&self,
759	user_id: &UserId,
760	logged: L,
761	changed: &[T],
762	txn: Txn,
763	rooms: impl FnOnce() -> S + Send,
764) -> Result
765where
766	L: IntoIterator<Item: AsRef<str>> + Clone,
767	T: AsRef<str> + Sync,
768	S: Stream<Item = Result<&'a RoomId>> + Send,
769{
770	if logged.clone().into_iter().next().is_none() {
771		txn.execute();
772		return Ok(());
773	}
774
775	let count = self.services.globals.next_count();
776	let txn = self.stage_update(txn, user_id.as_str(), user_id, *count, logged);
777	let txn = if changed.is_empty() {
778		txn
779	} else {
780		rooms()
781			.ready_try_fold(txn, |txn, room_id| {
782				Ok(self.stage_update(txn, room_id.as_str(), user_id, *count, changed))
783			})
784			.await?
785	};
786
787	txn.execute();
788
789	Ok(())
790}
791
792#[implement(Service)]
793fn stage_update<I>(&self, txn: Txn, scope: &str, user_id: &UserId, count: u64, fields: I) -> Txn
794where
795	I: IntoIterator<Item: AsRef<str>>,
796{
797	fields.into_iter().fold(txn, |mut txn, name| {
798		txn.put_raw(&self.profilechangeid_userid, (scope, count, name.as_ref()), user_id);
799		txn
800	})
801}
802
803/// Streams the profile fields the user wrote themselves, restatements included.
804///
805/// The range is half-open on the low side, so a caller passes the sync token
806/// it already delivered. An absent `to` leaves the walk unbounded above.
807/// Storage and decoding failures are logged and skipped.
808#[implement(Service)]
809#[inline]
810pub fn profile_changed<'a>(
811	&'a self,
812	user_id: &'a UserId,
813	from: u64,
814	to: Option<u64>,
815) -> impl Stream<Item = ProfileChange<'a>> + Send + 'a {
816	self.try_profile_changed(user_id, from, to)
817		.inspect_err(|error| warn!(%error, "Profile change log row failed to read"))
818		.ignore_err()
819}
820
821/// Streams the user's logged profile fields, surfacing read failures.
822///
823/// The range excludes `from` and includes `to`, when provided. Fields and user
824/// IDs borrow the cursor and must be consumed or owned before it advances.
825#[implement(Service)]
826#[inline]
827pub fn try_profile_changed<'a>(
828	&'a self,
829	user_id: &'a UserId,
830	from: u64,
831	to: Option<u64>,
832) -> impl Stream<Item = Result<ProfileChange<'a>>> + Send + 'a {
833	self.profile_changed_user_or_room(user_id.as_str(), from, to)
834}
835
836/// Streams the profile fields any member of the room changed.
837///
838/// The range works as it does for a single user. A member appears once per
839/// field they changed, however many of the caller's rooms they share.
840/// Storage and decoding failures are logged and skipped.
841#[implement(Service)]
842#[inline]
843pub fn room_profile_changed<'a>(
844	&'a self,
845	room_id: &'a RoomId,
846	from: u64,
847	to: Option<u64>,
848) -> impl Stream<Item = ProfileChange<'a>> + Send + 'a {
849	self.try_room_profile_changed(room_id, from, to)
850		.inspect_err(|error| warn!(%error, "Profile change log row failed to read"))
851		.ignore_err()
852}
853
854/// Streams a room's logged profile fields, surfacing read failures.
855///
856/// The range excludes `from` and includes `to`, when provided. Fields and user
857/// IDs borrow the cursor and must be consumed or owned before it advances.
858#[implement(Service)]
859#[inline]
860pub fn try_room_profile_changed<'a>(
861	&'a self,
862	room_id: &'a RoomId,
863	from: u64,
864	to: Option<u64>,
865) -> impl Stream<Item = Result<ProfileChange<'a>>> + Send + 'a {
866	self.profile_changed_user_or_room(room_id.as_str(), from, to)
867}
868
869#[implement(Service)]
870fn profile_changed_user_or_room<'a>(
871	&'a self,
872	user_or_room_id: &'a str,
873	from: u64,
874	to: Option<u64>,
875) -> impl Stream<Item = Result<ProfileChange<'a>>> + Send + 'a {
876	let to = to.unwrap_or(u64::MAX);
877
878	if from >= to {
879		return Either::Left(empty());
880	}
881
882	let start = (user_or_room_id, from.saturating_add(1));
883	let prefix = serialize_key((user_or_room_id, Interfix)).expect("profile scope prefix");
884	let end = to
885		.checked_add(1)
886		.map(|count| serialize_key((user_or_room_id, count)).expect("profile range end"));
887
888	// Bound raw keys before decoding so unrelated corrupt rows cannot fail this range.
889	let changes = self
890		.profilechangeid_userid
891		.stream_from_raw(&start)
892		.ready_try_take_while(move |(key, _)| {
893			Ok(key.starts_with(prefix.as_slice())
894				&& end
895					.as_ref()
896					.is_none_or(|end| *key < end.as_slice()))
897		})
898		.ready_and_then(|(key, value)| {
899			let ((_, _, field), user_id): ChangeKeyVal<'_> =
900				(deserialize_from_slice(key)?, deserialize_from_slice(value)?);
901
902			Ok((user_id, field))
903		});
904
905	Either::Right(changes)
906}
907
908/// Gets a specific user profile key
909#[implement(Service)]
910pub async fn profile_key<T>(&self, user_id: &UserId, profile_key: &ProfileFieldName) -> Result<T>
911where
912	T: for<'de> Deserialize<'de> + Send,
913{
914	let key = (user_id, profile_key);
915	let Json(value) = self
916		.useridprofilekey_value
917		.qry(&key)
918		.await
919		.map_err(|error| {
920			if error.is_not_found() {
921				err!(Request(NotFound("The requested profile key does not exist.")))
922			} else {
923				error
924			}
925		})?
926		.deserialized()
927		.map_err(|_| err!(Database("Cannot deserialize database profile value")))?;
928
929	Ok(value)
930}
931
932/// Fill membership content with the user's display name and avatar.
933///
934/// Profile lookup failures clear the corresponding fields. All other content
935/// fields are preserved.
936#[implement(Service)]
937pub async fn fill_content(
938	&self,
939	user_id: &UserId,
940	mut content: RoomMemberEventContent,
941) -> RoomMemberEventContent {
942	let displayname = self.displayname(user_id).ok();
943	let avatar_url = self.avatar_url(user_id).ok();
944
945	let (displayname, avatar_url) = join(displayname, avatar_url).await;
946
947	content.displayname = displayname;
948	content.avatar_url = avatar_url;
949
950	content
951}
952
953#[implement(Service)]
954pub async fn fetch_remote_profile(&self, user_id: &UserId) -> Result {
955	assert!(
956		!self.services.globals.user_is_local(user_id),
957		"fetch remote profile called with a local user"
958	);
959
960	if let Ok(response) = self
961		.services
962		.federation
963		.execute(user_id.server_name(), get_profile_information::v1::Request {
964			user_id: user_id.to_owned(),
965			field: None,
966		})
967		.await
968	{
969		self.merge_profile(user_id, response).await?;
970	}
971
972	Ok(())
973}
974
975/// MSC4133 maximum total profile size (64 KiB), measured over the JSON of the
976/// full profile including displayname and avatar_url.
977pub(super) const MAX_PROFILE_SIZE: usize = 65_536;
978
979/// Reject a prospective profile write exceeding the MSC4133 64 KiB cap.
980///
981/// The candidate value replaces the stored field for the size calculation. A
982/// stored field that no longer parses is logged and left out of the total, so
983/// it cannot block writes to the other fields; removals skip this check.
984#[implement(Service)]
985async fn enforce_profile_size(
986	&self,
987	user_id: &UserId,
988	profile_values: &[ProfileValue],
989) -> Result {
990	// The write lands whole, so all its replacements and removals apply before measuring.
991	let written = |name: &ProfileFieldName| {
992		profile_values
993			.iter()
994			.any(|(field, _)| field == name)
995	};
996
997	let replacements = profile_values
998		.iter()
999		.filter_map(|(name, value)| Some((name.clone(), Cow::Borrowed(value.as_ref()?))))
1000		.stream();
1001
1002	let profile: ProspectiveProfile<'_> = self
1003		.try_all_profile_keys(user_id)
1004		.ready_filter_map(|field| {
1005			field
1006				.inspect_err(|e| warn!(%user_id, %e, "Skipping unreadable profile field"))
1007				.ok()
1008		})
1009		.ready_filter_map(|field| {
1010			let name = field.field_name();
1011			(!written(&name)).then(|| (name, Cow::Owned(field.value().into_owned())))
1012		})
1013		.chain(replacements)
1014		.collect()
1015		.await;
1016
1017	let profile_size = serde_json::to_vec(&profile).map_or(0, |buf| buf.len());
1018
1019	if profile_size > MAX_PROFILE_SIZE {
1020		return Err!(Request(ProfileTooLarge(
1021			"Profile would exceed the maximum size of 64 KiB."
1022		)));
1023	}
1024
1025	Ok(())
1026}
1027
1028/// MSC4133 maximum profile field-name length, in bytes.
1029const MAX_KEY_LENGTH: usize = 255;
1030
1031/// Checks raw sync selectors before normalization or connection mutation.
1032///
1033/// Duplicate selectors count toward admission, and names are checked by byte length.
1034#[implement(Service)]
1035pub fn check_requested_fields(&self, fields: &[ProfileFieldName]) -> Result {
1036	if fields.len()
1037		> self
1038			.services
1039			.config
1040			.profile
1041			.max_fields_per_request
1042	{
1043		return Err!(Request(InvalidParam("Too many selected profile fields.")));
1044	}
1045
1046	if fields
1047		.iter()
1048		.any(|name| name.as_str().len() > MAX_KEY_LENGTH)
1049	{
1050		return Err!(Request(KeyTooLarge("Profile key names cannot be longer than 255 bytes.")));
1051	}
1052
1053	Ok(())
1054}
1055
1056/// Validates a client-supplied `m.room.member` content from any route that accepts one.
1057///
1058/// `json_len` is the canonical length. `displayname` and `avatar_url` are passed
1059/// only when the write stores them, so a value the room already holds is not
1060/// bounded again. The content is refused over `max_bytes`, the display name over
1061/// `MAX_DISPLAYNAME_LENGTH` characters, and the avatar URL over
1062/// `MAX_AVATAR_URL_LENGTH` bytes or when it is not a valid `mxc://` URI, all with
1063/// `M_BAD_JSON`.
1064pub fn check_member_content(
1065	max_bytes: usize,
1066	json_len: usize,
1067	displayname: Option<&str>,
1068	avatar_url: Option<&str>,
1069) -> Result {
1070	check_member_size(max_bytes, json_len)?;
1071	check_member_displayname(displayname)?;
1072	check_member_avatar_url(avatar_url)
1073}
1074
1075/// Bounds the canonical size of a client-supplied `m.room.member` content, in bytes.
1076///
1077/// Every route measures the canonical form, the one the stored event carries, so
1078/// the cap means the same thing wherever a member event enters.
1079pub fn check_member_size(max_bytes: usize, json_len: usize) -> Result {
1080	check_field_length(json_len, max_bytes, "Membership event content", "bytes")
1081}
1082
1083/// Bounds a member event's display name, in characters.
1084///
1085/// An absent name passes. A present one is refused over
1086/// `MAX_DISPLAYNAME_LENGTH` characters.
1087pub fn check_member_displayname(displayname: Option<&str>) -> Result {
1088	displayname.map_or(Ok(()), check_displayname_length)
1089}
1090
1091/// Validates a member event's avatar URL.
1092///
1093/// An absent URL passes. A present one must be a valid `mxc://` URI within the byte cap.
1094pub fn check_member_avatar_url(avatar_url: Option<&str>) -> Result {
1095	avatar_url.map_or(Ok(()), check_avatar_mxc)
1096}
1097
1098/// Validate a profile field name against the Common Namespaced Identifier
1099/// Grammar: a lowercase-leading identifier over `[a-z0-9_.-]`, matching the
1100/// reference homeserver. Length is bounded separately by `MAX_KEY_LENGTH`.
1101fn check_profile_key(name: &str) -> Result {
1102	if name.len() > MAX_KEY_LENGTH {
1103		return Err!(Request(KeyTooLarge("Profile key names cannot be longer than 255 bytes.")));
1104	}
1105
1106	let ok = name
1107		.bytes()
1108		.next()
1109		.is_some_and(|b| b.is_ascii_lowercase())
1110		&& name.bytes().all(|b| {
1111			b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'_' | b'.' | b'-')
1112		});
1113
1114	if !ok {
1115		return Err!(Request(BadJson(
1116			"Profile key names must follow the Common Namespaced Identifier Grammar."
1117		)));
1118	}
1119
1120	Ok(())
1121}
1122
1123/// Validate a profile field value against the schema of the proposal naming
1124/// the field.
1125///
1126/// MSC4133 reserves no schema of its own, so a field this does not recognize
1127/// carries any JSON the size cap admits. A stored `null` clears the field for
1128/// readers without removing it, and is accepted for every field name.
1129fn check_profile_value(name: &str, value: &Value) -> Result {
1130	if value.is_null() {
1131		return Ok(());
1132	}
1133
1134	match name {
1135		| "m.status" | "org.matrix.msc4426.status" => check_status(value),
1136		| "m.call" | "org.matrix.msc4426.call" => check_call(value),
1137		| "m.tz" | "us.cloke.msc4175.tz" => check_timezone(value),
1138		| "displayname" => check_displayname(value),
1139		| "avatar_url" => check_avatar_url(value),
1140		| _ => Ok(()),
1141	}
1142}
1143
1144/// Validate an MSC4426 status against its two required fields and their byte
1145/// budgets.
1146///
1147/// Both `text` and `emoji` are required, so a partial object is rejected
1148/// rather than stored for clients to render half of. The emoji budget counts
1149/// bytes and never graphemes, which the proposal calls out because grapheme
1150/// definitions keep growing.
1151fn check_status(value: &Value) -> Result {
1152	let (Some(text), Some(emoji)) = (
1153		value.get("text").and_then(Value::as_str),
1154		value.get("emoji").and_then(Value::as_str),
1155	) else {
1156		return Err!(Request(BadJson("Status requires a text and an emoji string.")));
1157	};
1158
1159	check_length(text.len(), MAX_STATUS_TEXT_LENGTH, "Status text", "bytes")?;
1160	check_length(emoji.len(), MAX_STATUS_EMOJI_LENGTH, "Status emoji", "bytes")
1161}
1162
1163/// Bound one profile value by its length budget.
1164///
1165/// MSC4426 mandates both the `M_TOO_LARGE` errcode and a 400, while the
1166/// kind-derived table promotes that errcode to 413, so this is one of the few
1167/// call sites naming its own status.
1168fn check_length(len: usize, max: usize, field: &str, unit: &str) -> Result {
1169	len.le(&max).then_some(()).ok_or_else(|| {
1170		err!(RequestStatus(
1171			BAD_REQUEST,
1172			TooLarge("{field} cannot be longer than {max} {unit}.")
1173		))
1174	})
1175}
1176
1177/// Bound a display name or avatar URL by its length budget.
1178///
1179/// These limits answer `M_BAD_JSON` at 400, while `check_length` keeps
1180/// `M_TOO_LARGE` for the status text and emoji.
1181fn check_field_length(len: usize, max: usize, field: &str, unit: &str) -> Result {
1182	len.le(&max)
1183		.then_some(())
1184		.ok_or_else(|| err!(Request(BadJson("{field} cannot be longer than {max} {unit}."))))
1185}
1186
1187/// Validate an MSC4426 call indicator.
1188///
1189/// Every field is optional, so an empty object is the valid "in a call, joined
1190/// at an unstated time" value the proposal's own example uses.
1191fn check_call(value: &Value) -> Result {
1192	let Some(call) = value.as_object() else {
1193		return Err!(Request(BadJson("Call must be an object.")));
1194	};
1195
1196	call.get("call_joined_ts")
1197		.is_none_or(Value::is_number)
1198		.then_some(())
1199		.ok_or_else(|| err!(Request(BadJson("Call join timestamp must be a number."))))
1200}
1201
1202/// Maximum `m.tz` length, in bytes.
1203///
1204/// The longest name the database ships is 32 bytes, so this is headroom for
1205/// later additions rather than a limit any real zone approaches.
1206const MAX_TIMEZONE_LENGTH: usize = 64;
1207
1208/// Maximum number of `/`-separated components in an `m.tz` name.
1209///
1210/// The deepest names the database ships are three deep, in the
1211/// `America/Argentina/Buenos_Aires` shape.
1212const MAX_TIMEZONE_COMPONENTS: usize = 3;
1213
1214/// Validate an MSC4175 time zone against the shape of an IANA Time Zone
1215/// Database name.
1216///
1217/// Membership in a bundled copy of the database is deliberately not tested:
1218/// the proposal's rationale for a loose check is that clients and servers
1219/// carry different database versions, so a browser one release ahead of ours
1220/// would have a newly added zone rejected. Shape alone still refuses offsets,
1221/// platform display names, and anything else that could never name a zone.
1222fn check_timezone(value: &Value) -> Result {
1223	let Some(name) = value.as_str() else {
1224		return Err!(Request(InvalidParam("Time zone must be a string.")));
1225	};
1226
1227	is_timezone_name(name)
1228		.then_some(())
1229		.ok_or_else(|| {
1230			err!(Request(InvalidParam(
1231				"Time zone must be a name from the IANA Time Zone Database."
1232			)))
1233		})
1234}
1235
1236/// Test a string against the tzfile naming rules, narrowed to what every name
1237/// the database ships actually uses.
1238///
1239/// A name is one to three `/`-separated components. Draining the remainder
1240/// afterwards is what rejects a fourth, leaving the whole test one pass over
1241/// the string.
1242fn is_timezone_name(name: &str) -> bool {
1243	let mut components = name.split('/');
1244
1245	name.len().le(&MAX_TIMEZONE_LENGTH)
1246		&& components
1247			.by_ref()
1248			.take(MAX_TIMEZONE_COMPONENTS)
1249			.all(is_timezone_component)
1250		&& components.next().is_none()
1251}
1252
1253/// Test one `/`-separated component of an `m.tz` name.
1254///
1255/// Components hold ASCII alphanumerics, `_`, `+`, and `-`. Each begins with a
1256/// letter, which is what refuses a bare numeric offset.
1257fn is_timezone_component(component: &str) -> bool {
1258	component
1259		.bytes()
1260		.next()
1261		.is_some_and(|byte| byte.is_ascii_alphabetic())
1262		&& component
1263			.bytes()
1264			.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'+' | b'-'))
1265}
1266
1267/// Bound a display name by its character budget.
1268///
1269/// Characters are Unicode scalar values, so a name in a multi-byte script is
1270/// not penalized against one in ASCII.
1271fn check_displayname(value: &Value) -> Result {
1272	let Some(name) = value.as_str() else {
1273		return Err!(Request(BadJson("Display name must be a string.")));
1274	};
1275
1276	check_displayname_length(name)
1277}
1278
1279fn check_displayname_length(name: &str) -> Result {
1280	let len = name
1281		.chars()
1282		.take(MAX_DISPLAYNAME_LENGTH + 1)
1283		.count();
1284
1285	check_field_length(len, MAX_DISPLAYNAME_LENGTH, "Display name", "characters")
1286}
1287
1288// As in Synapse's default, the profile takes any avatar string within the cap.
1289fn check_avatar_url(value: &Value) -> Result {
1290	let Some(url) = value.as_str() else {
1291		return Err!(Request(BadJson("Avatar URL must be a string.")));
1292	};
1293
1294	check_avatar_url_length(url)
1295}
1296
1297fn check_avatar_mxc(url: &str) -> Result {
1298	check_avatar_url_length(url)?;
1299
1300	// An empty value clears the avatar, so it is not a URI.
1301	BoolExt::ok_or_else(url.is_empty() || <&MxcUri>::from(url).is_valid(), || {
1302		err!(Request(BadJson("Avatar URL must be a valid mxc:// URI.")))
1303	})
1304}
1305
1306fn check_avatar_url_length(url: &str) -> Result {
1307	check_field_length(url.len(), MAX_AVATAR_URL_LENGTH, "Avatar URL", "bytes")
1308}