Skip to main content

tuwunel_service/migrations/
migrate_profile_keys.rs

1use std::sync::Arc;
2
3use ruma::{UserId, profile::ProfileFieldName};
4use tuwunel_core::{
5	Result, info,
6	result::NotFound,
7	utils::{
8		ReadyExt, TryReadyExt,
9		stream::{TryBroadbandExt, TryExpect},
10	},
11	warn,
12};
13use tuwunel_database::{Json, Map};
14
15use super::tally::{Outcome, Tally};
16use crate::{
17	Services,
18	integrity::useridprofilekey_value::{Finding, classify, scan},
19};
20
21/// Relocates the per-user displayname and avatar_url out of their dedicated
22/// columns into the unified useridprofilekey_value store keyed by MSC4133 field
23/// name, where the profile service now reads them.
24///
25/// The dedicated columns are left intact, so an older binary opening the same
26/// database still resolves. The relocation has no check, as those columns go
27/// stale, but the values already in the store are then repaired from theirs.
28pub(super) async fn migrate_profile_keys(services: &Services) -> Result {
29	let db = &services.db;
30	let cork = db.cork_and_sync();
31
32	let userid_displayname = db["userid_displayname"].clone();
33	let userid_avatarurl = db["userid_avatarurl"].clone();
34	let userid_blurhash = db["userid_blurhash"].clone();
35	let useridprofilekey_value = db["useridprofilekey_value"].clone();
36
37	warn!(
38		"Relocating displaynames, avatar_urls and blurhashes into the unified profile-key store"
39	);
40
41	let displaynames = userid_displayname
42		.stream()
43		.expect_ok()
44		.ready_fold(0_usize, |count, (user_id, displayname): (&UserId, &str)| {
45			let key = (user_id, ProfileFieldName::DisplayName.as_str());
46			let value = displayname.to_owned();
47
48			useridprofilekey_value.put(key, Json(value));
49
50			count.saturating_add(1)
51		})
52		.await;
53
54	let avatar_urls = userid_avatarurl
55		.stream()
56		.expect_ok()
57		.ready_fold(0_usize, |count, (user_id, avatar_url): (&UserId, &str)| {
58			let key = (user_id, ProfileFieldName::AvatarUrl.as_str());
59			let value = avatar_url.to_owned();
60
61			useridprofilekey_value.put(key, Json(value));
62
63			count.saturating_add(1)
64		})
65		.await;
66
67	let blurhashes = userid_blurhash
68		.stream()
69		.expect_ok()
70		.ready_fold(0_usize, |count, (user_id, blurhash): (&UserId, &str)| {
71			let key = (user_id, "xyz.amorgan.blurhash");
72			let value = blurhash.to_owned();
73
74			useridprofilekey_value.put(key, Json(value));
75
76			count.saturating_add(1)
77		})
78		.await;
79
80	// Each finding names its own row, so the repairs cannot interfere.
81	let tally = scan(services)
82		.broad_and_then(async |finding| repair(&useridprofilekey_value, &finding).await)
83		.ready_try_fold(Tally::default(), |tally, outcome| Ok(tally.record(outcome)))
84		.await?;
85
86	drop(cork);
87	info!(
88		%displaynames,
89		%avatar_urls,
90		%blurhashes,
91		fixed_strings = %tally.repaired,
92		removed = %tally.removed,
93		stale = %tally.stale,
94		"Relocated profile keys into useridprofilekey_value"
95	);
96
97	useridprofilekey_value.sort()
98}
99
100/// Re-encodes or removes the value a finding names, as it stands now.
101///
102/// The row is read again and classified as the check does, so the text written is
103/// the current text. A row removed since the scan, or no longer needing repair, is
104/// counted stale.
105#[tracing::instrument(level = "debug", skip_all)]
106pub(super) async fn repair(
107	useridprofilekey_value: &Arc<Map>,
108	finding: &Finding,
109) -> Result<Outcome> {
110	let (Finding::Bare { key, .. } | Finding::Opaque { key, .. }) = finding;
111	let current = useridprofilekey_value.get(key).await.optional()?;
112	let outcome = match current
113		.as_deref()
114		.and_then(|value| classify(key, value))
115	{
116		| None => Outcome::Stale,
117		| Some(Finding::Opaque { key, value }) => {
118			warn!(?key, ?value, "Non-UTF8 data in profile value");
119			useridprofilekey_value.remove(&key);
120
121			Outcome::Removed
122		},
123		| Some(Finding::Bare { key, text }) => {
124			useridprofilekey_value.raw_put(&key, Json(text.as_str()));
125
126			Outcome::Repaired
127		},
128	};
129
130	Ok(outcome)
131}