Skip to content

Commit 096d9be

Browse files
pixlwaveHywan
authored andcommitted
chore: Update Ruma and use the new UserProfileUpdate type and merge function.
1 parent b94d170 commit 096d9be

9 files changed

Lines changed: 34 additions & 76 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ rand = { version = "0.10.1", default-features = false, features = ["std", "std_r
7070
regex = { version = "1.12.2", default-features = false }
7171
reqwest = { version = "0.13.1", default-features = false }
7272
rmp-serde = { version = "1.3.0", default-features = false }
73-
ruma = { git = "https://github.com/ruma/ruma", rev = "b74d09e1a186c4eb87af581276fa26414ea1d7bb", features = [
73+
ruma = { git = "https://github.com/ruma/ruma", rev = "a494c58c541c63042b26380a9238a24bdda4d85c", features = [
7474
"client-api-c",
7575
"compat-unset-avatar",
7676
"compat-upload-signatures",

crates/matrix-sdk-base/src/sliding_sync.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ mod tests {
302302
},
303303
},
304304
mxc_uri, owned_event_id, owned_mxc_uri, owned_user_id,
305-
profile::UserProfile,
305+
profile::UserProfileUpdate,
306306
room_alias_id, room_id,
307307
serde::Raw,
308308
uint, user_id,
@@ -450,11 +450,11 @@ mod tests {
450450
let mut response = http::Response::new("0".to_owned());
451451
response.extensions.profiles.insert(
452452
alice.to_owned(),
453-
UserProfile::from_iter([("displayname".to_owned(), json!("Alice"))]),
453+
UserProfileUpdate::from_iter([("displayname".to_owned(), json!("Alice"))]),
454454
);
455455
response.extensions.profiles.insert(
456456
bob.to_owned(),
457-
UserProfile::from_iter([("displayname".to_owned(), json!("Bob"))]),
457+
UserProfileUpdate::from_iter([("displayname".to_owned(), json!("Bob"))]),
458458
);
459459

460460
// When the response is processed.
@@ -490,7 +490,7 @@ mod tests {
490490
let mut response = http::Response::new("1".to_owned());
491491
response.extensions.profiles.insert(
492492
alice.to_owned(),
493-
UserProfile::from_iter([("displayname".to_owned(), json!("Alice Updated"))]),
493+
UserProfileUpdate::from_iter([("displayname".to_owned(), json!("Alice Updated"))]),
494494
);
495495

496496
client

crates/matrix-sdk-base/src/store/integration_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use ruma::{
3737
},
3838
mxc_uri, owned_event_id, owned_mxc_uri,
3939
presence::PresenceState,
40-
profile::UserProfile,
40+
profile::UserProfileUpdate,
4141
push::Ruleset,
4242
room_id,
4343
room_version_rules::AuthorizationRules,
@@ -2159,7 +2159,7 @@ impl StateStoreIntegrationTests for DynStateStore {
21592159
let mut fields = BTreeMap::new();
21602160
fields.insert("displayname".to_owned(), json!("Alice"));
21612161
fields.insert("avatar_url".to_owned(), json!(null));
2162-
let update = UserProfile::from_iter(fields);
2162+
let update = UserProfileUpdate::from_iter(fields);
21632163

21642164
let mut changes = StateChanges::default();
21652165
changes.global_profiles.insert(user_id.to_owned(), update);
@@ -2173,7 +2173,7 @@ impl StateStoreIntegrationTests for DynStateStore {
21732173
let mut fields = BTreeMap::new();
21742174
fields.insert("displayname".to_owned(), json!(null));
21752175
fields.insert("avatar_url".to_owned(), json!("mxc://example.com/avatar"));
2176-
let update2 = UserProfile::from_iter(fields);
2176+
let update2 = UserProfileUpdate::from_iter(fields);
21772177

21782178
let mut changes = StateChanges::default();
21792179
changes.global_profiles.insert(user_id.to_owned(), update2);
@@ -2195,11 +2195,11 @@ impl StateStoreIntegrationTests for DynStateStore {
21952195
let mut changes = StateChanges::default();
21962196
changes.global_profiles.insert(
21972197
alice.to_owned(),
2198-
UserProfile::from_iter([("displayname".to_owned(), json!("Alice"))]),
2198+
UserProfileUpdate::from_iter([("displayname".to_owned(), json!("Alice"))]),
21992199
);
22002200
changes.global_profiles.insert(
22012201
bob.to_owned(),
2202-
UserProfile::from_iter([("displayname".to_owned(), json!("Bob"))]),
2202+
UserProfileUpdate::from_iter([("displayname".to_owned(), json!("Bob"))]),
22032203
);
22042204
self.save_changes(&changes).await?;
22052205

crates/matrix-sdk-base/src/store/memory_store.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -540,19 +540,9 @@ impl StateStore for MemoryStore {
540540
}
541541

542542
for (user_id, profile_update) in &changes.global_profiles {
543-
let merged = if let Some(existing_profile) = inner.global_profiles.get(user_id) {
544-
super::traits::merge_profile(existing_profile.clone(), profile_update.clone())
545-
} else {
546-
// TODO: Confirm if this is actually necessary. Related:
547-
// https://github.com/matrix-org/matrix-spec-proposals/pull/4262#discussion_r3466830101
548-
let map: BTreeMap<String, serde_json::Value> = profile_update
549-
.clone()
550-
.into_iter()
551-
.filter(|(_, value)| !value.is_null())
552-
.collect();
553-
UserProfile::from_iter(map)
554-
};
555-
inner.global_profiles.insert(user_id.clone(), merged);
543+
let mut profile = inner.global_profiles.get(user_id).cloned().unwrap_or_default();
544+
profile.merge(profile_update.clone());
545+
inner.global_profiles.insert(user_id.clone(), profile);
556546
}
557547

558548
debug!("Saved changes in {:?}", now.elapsed());

crates/matrix-sdk-base/src/store/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use ruma::{
6161
redaction::SyncRoomRedactionEvent,
6262
},
6363
},
64-
profile::UserProfile,
64+
profile::UserProfileUpdate,
6565
serde::Raw,
6666
};
6767
use serde::de::DeserializeOwned;
@@ -100,7 +100,7 @@ pub use self::{
100100
ComposerDraft, ComposerDraftType, DraftAttachment, DraftAttachmentContent, DraftThumbnail,
101101
DynStateStore, IncorrectMutexGuardError, IntoStateStore, SaveLockedStateStore, StateStore,
102102
StateStoreDataKey, StateStoreDataValue, StateStoreExt, SupportedVersionsResponse,
103-
ThreadSubscriptionCatchupToken, WellKnownResponse, merge_profile,
103+
ThreadSubscriptionCatchupToken, WellKnownResponse,
104104
},
105105
};
106106

@@ -590,7 +590,7 @@ pub struct StateChanges {
590590
///
591591
/// These follow the MSC4262 update pattern: fields with an explicit `null`
592592
/// value are removed, while fields that aren't present are left unchanged.
593-
pub global_profiles: BTreeMap<OwnedUserId, UserProfile>,
593+
pub global_profiles: BTreeMap<OwnedUserId, UserProfileUpdate>,
594594
}
595595

596596
impl StateChanges {

crates/matrix-sdk-base/src/store/traits.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,21 +2124,6 @@ where
21242124
}
21252125
}
21262126

2127-
// TODO: Use Ruma once the PR is merged there instead of this local helper (https://github.com/ruma/ruma/pull/2518)
2128-
/// Helper function to merge a profile update with an existing profile based on
2129-
/// the MSC
2130-
pub fn merge_profile(existing: UserProfile, update: UserProfile) -> UserProfile {
2131-
let mut map: BTreeMap<String, serde_json::Value> = existing.into_iter().collect();
2132-
for (key, value) in update {
2133-
if value.is_null() {
2134-
map.remove(&key);
2135-
} else {
2136-
map.insert(key, value);
2137-
}
2138-
}
2139-
UserProfile::from_iter(map)
2140-
}
2141-
21422127
/// Serialisable representation of get_supported_versions::Response.
21432128
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21442129
pub struct SupportedVersionsResponse {

crates/matrix-sdk-indexeddb/src/state_store/mod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,20 +1109,10 @@ impl_state_store!({
11091109
let existing: Option<UserProfile> =
11101110
store.get(&key).await?.map(|f| self.deserialize_value(&f)).transpose()?;
11111111

1112-
let merged = if let Some(existing_profile) = existing {
1113-
matrix_sdk_base::store::merge_profile(existing_profile, profile_update.clone())
1114-
} else {
1115-
// TODO: Confirm if this is actually necessary. Related:
1116-
// https://github.com/matrix-org/matrix-spec-proposals/pull/4262#discussion_r3466830101
1117-
let map: BTreeMap<String, serde_json::Value> = profile_update
1118-
.clone()
1119-
.into_iter()
1120-
.filter(|(_, value)| !value.is_null())
1121-
.collect();
1122-
UserProfile::from_iter(map)
1123-
};
1112+
let mut profile = existing.unwrap_or_default();
1113+
profile.merge(profile_update.clone());
11241114

1125-
store.put(&self.serialize_value(&merged)?).with_key(key).build()?;
1115+
store.put(&self.serialize_value(&profile)?).with_key(key).build()?;
11261116
}
11271117
}
11281118

crates/matrix-sdk-sqlite/src/state_store.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,20 +1615,13 @@ impl StateStore for SqliteStateStore {
16151615
let existing_data: Option<Vec<u8>> =
16161616
select_stmt.query_row([&user_id], |row| row.get(0)).optional()?;
16171617

1618-
let merged = if let Some(data) = existing_data {
1619-
let existing_profile: UserProfile = this.deserialize_json(&data)?;
1620-
matrix_sdk_base::store::merge_profile(existing_profile, profile_update)
1621-
} else {
1622-
// TODO: Confirm if this is actually necessary. Related:
1623-
// https://github.com/matrix-org/matrix-spec-proposals/pull/4262#discussion_r3466830101
1624-
let map: BTreeMap<String, serde_json::Value> = profile_update
1625-
.into_iter()
1626-
.filter(|(_, value)| !value.is_null())
1627-
.collect();
1628-
UserProfile::from_iter(map)
1629-
};
1618+
let mut profile: UserProfile = existing_data
1619+
.map(|data| this.deserialize_json(&data))
1620+
.transpose()?
1621+
.unwrap_or_default();
1622+
profile.merge(profile_update);
16301623

1631-
let serialized = this.serialize_json(&merged)?;
1624+
let serialized = this.serialize_json(&profile)?;
16321625
insert_stmt.execute((&user_id, serialized))?;
16331626
}
16341627
}

0 commit comments

Comments
 (0)