Skip to content

Commit 0cdec9d

Browse files
committed
refactor(threads): flatten the ThreadStatus enum
1 parent d180d49 commit 0cdec9d

File tree

10 files changed

+76
-93
lines changed

10 files changed

+76
-93
lines changed

bindings/matrix-sdk-ffi/src/room/mod.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,25 +1129,22 @@ impl Room {
11291129
pub async fn fetch_thread_subscription(
11301130
&self,
11311131
thread_root_event_id: String,
1132-
) -> Result<Option<ThreadStatus>, ClientError> {
1132+
) -> Result<Option<ThreadSubscription>, ClientError> {
11331133
let thread_root = EventId::parse(thread_root_event_id)?;
1134-
Ok(self.inner.fetch_thread_subscription(thread_root).await?.map(|sub| match sub {
1135-
matrix_sdk::room::ThreadStatus::Subscribed { automatic } => {
1136-
ThreadStatus::Subscribed { automatic }
1137-
}
1138-
}))
1134+
Ok(self
1135+
.inner
1136+
.fetch_thread_subscription(thread_root)
1137+
.await?
1138+
.map(|sub| ThreadSubscription { automatic: sub.automatic }))
11391139
}
11401140
}
11411141

1142-
/// Status of a thread subscription (MSC4306).
1143-
#[derive(uniffi::Enum)]
1144-
pub enum ThreadStatus {
1145-
/// The thread is subscribed to.
1146-
Subscribed {
1147-
/// Whether the thread subscription happened automatically (e.g. after a
1148-
/// mention) or if it was manually requested by the user.
1149-
automatic: bool,
1150-
},
1142+
/// A thread subscription (MSC4306).
1143+
#[derive(uniffi::Record)]
1144+
pub struct ThreadSubscription {
1145+
/// Whether the thread subscription happened automatically (e.g. after a
1146+
/// mention) or if it was manually requested by the user.
1147+
automatic: bool,
11511148
}
11521149

11531150
/// A listener for receiving new live location shares in a room.

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use crate::{
4848
deserialized_responses::MemberEvent,
4949
store::{
5050
ChildTransactionId, QueueWedgeError, Result, SerializableEventContent, StateStoreExt,
51-
ThreadStatus,
51+
ThreadSubscription,
5252
},
5353
};
5454

@@ -1790,40 +1790,40 @@ impl StateStoreIntegrationTests for DynStateStore {
17901790
self.upsert_thread_subscription(
17911791
room_id(),
17921792
first_thread,
1793-
ThreadStatus::Subscribed { automatic: true },
1793+
ThreadSubscription { automatic: true },
17941794
)
17951795
.await
17961796
.unwrap();
17971797

17981798
self.upsert_thread_subscription(
17991799
room_id(),
18001800
second_thread,
1801-
ThreadStatus::Subscribed { automatic: false },
1801+
ThreadSubscription { automatic: false },
18021802
)
18031803
.await
18041804
.unwrap();
18051805

18061806
// Now, reading the thread subscription returns the expected status.
18071807
let maybe_status = self.load_thread_subscription(room_id(), first_thread).await.unwrap();
1808-
assert_eq!(maybe_status, Some(ThreadStatus::Subscribed { automatic: true }));
1808+
assert_eq!(maybe_status, Some(ThreadSubscription { automatic: true }));
18091809
let maybe_status = self.load_thread_subscription(room_id(), second_thread).await.unwrap();
1810-
assert_eq!(maybe_status, Some(ThreadStatus::Subscribed { automatic: false }));
1810+
assert_eq!(maybe_status, Some(ThreadSubscription { automatic: false }));
18111811

18121812
// We can override the thread subscription status.
18131813
self.upsert_thread_subscription(
18141814
room_id(),
18151815
first_thread,
1816-
ThreadStatus::Subscribed { automatic: false },
1816+
ThreadSubscription { automatic: false },
18171817
)
18181818
.await
18191819
.unwrap();
18201820

18211821
// And it's correctly reflected.
18221822
let maybe_status = self.load_thread_subscription(room_id(), first_thread).await.unwrap();
1823-
assert_eq!(maybe_status, Some(ThreadStatus::Subscribed { automatic: false }));
1823+
assert_eq!(maybe_status, Some(ThreadSubscription { automatic: false }));
18241824
// And the second thread is still subscribed.
18251825
let maybe_status = self.load_thread_subscription(room_id(), second_thread).await.unwrap();
1826-
assert_eq!(maybe_status, Some(ThreadStatus::Subscribed { automatic: false }));
1826+
assert_eq!(maybe_status, Some(ThreadSubscription { automatic: false }));
18271827

18281828
// We can remove a thread subscription.
18291829
self.remove_thread_subscription(room_id(), second_thread).await.unwrap();
@@ -1833,7 +1833,7 @@ impl StateStoreIntegrationTests for DynStateStore {
18331833
assert_eq!(maybe_status, None);
18341834
// And the first thread is still subscribed.
18351835
let maybe_status = self.load_thread_subscription(room_id(), first_thread).await.unwrap();
1836-
assert_eq!(maybe_status, Some(ThreadStatus::Subscribed { automatic: false }));
1836+
assert_eq!(maybe_status, Some(ThreadSubscription { automatic: false }));
18371837

18381838
// Removing a thread subscription for an unknown thread is a no-op.
18391839
self.remove_thread_subscription(room_id(), second_thread).await.unwrap();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use super::{
4646
use crate::{
4747
MinimalRoomMemberEvent, RoomMemberships, StateStoreDataKey, StateStoreDataValue,
4848
deserialized_responses::{DisplayName, RawAnySyncOrStrippedState},
49-
store::{QueueWedgeError, ThreadStatus},
49+
store::{QueueWedgeError, ThreadSubscription},
5050
};
5151

5252
#[derive(Debug, Default)]
@@ -84,7 +84,7 @@ struct MemoryStoreInner {
8484
send_queue_events: BTreeMap<OwnedRoomId, Vec<QueuedRequest>>,
8585
dependent_send_queue_events: BTreeMap<OwnedRoomId, Vec<DependentQueuedRequest>>,
8686
seen_knock_requests: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, OwnedUserId>>,
87-
thread_subscriptions: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, ThreadStatus>>,
87+
thread_subscriptions: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, ThreadSubscription>>,
8888
}
8989

9090
/// In-memory, non-persistent implementation of the `StateStore`.
@@ -958,23 +958,23 @@ impl StateStore for MemoryStore {
958958
&self,
959959
room: &RoomId,
960960
thread_id: &EventId,
961-
status: ThreadStatus,
961+
subscription: ThreadSubscription,
962962
) -> Result<(), Self::Error> {
963963
self.inner
964964
.write()
965965
.unwrap()
966966
.thread_subscriptions
967967
.entry(room.to_owned())
968968
.or_default()
969-
.insert(thread_id.to_owned(), status);
969+
.insert(thread_id.to_owned(), subscription);
970970
Ok(())
971971
}
972972

973973
async fn load_thread_subscription(
974974
&self,
975975
room: &RoomId,
976976
thread_id: &EventId,
977-
) -> Result<Option<ThreadStatus>, Self::Error> {
977+
) -> Result<Option<ThreadSubscription>, Self::Error> {
978978
let inner = self.inner.read().unwrap();
979979
Ok(inner
980980
.thread_subscriptions

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -453,37 +453,26 @@ pub enum RoomLoadSettings {
453453
One(OwnedRoomId),
454454
}
455455

456-
/// Status of a thread subscription, as saved in the state store.
456+
/// A thread subscription, as saved in the state store.
457457
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
458-
pub enum ThreadStatus {
459-
/// The thread is subscribed to.
460-
Subscribed {
461-
/// Whether the subscription was made automatically by a client, not by
462-
/// manual user choice.
463-
automatic: bool,
464-
},
458+
pub struct ThreadSubscription {
459+
/// Whether the subscription was made automatically by a client, not by
460+
/// manual user choice.
461+
pub automatic: bool,
465462
}
466463

467-
impl ThreadStatus {
468-
/// Convert the current [`ThreadStatus`] into a string representation.
464+
impl ThreadSubscription {
465+
/// Convert the current [`ThreadSubscription`] into a string representation.
469466
pub fn as_str(&self) -> &'static str {
470-
match self {
471-
ThreadStatus::Subscribed { automatic } => {
472-
if *automatic {
473-
"automatic"
474-
} else {
475-
"manual"
476-
}
477-
}
478-
}
467+
if self.automatic { "automatic" } else { "manual" }
479468
}
480469

481-
/// Convert a string representation into a [`ThreadStatus`], if it is a
482-
/// valid one, or `None` otherwise.
470+
/// Convert a string representation into a [`ThreadSubscription`], if it is
471+
/// a valid one, or `None` otherwise.
483472
pub fn from_value(s: &str) -> Option<Self> {
484473
match s {
485-
"automatic" => Some(ThreadStatus::Subscribed { automatic: true }),
486-
"manual" => Some(ThreadStatus::Subscribed { automatic: false }),
474+
"automatic" => Some(Self { automatic: true }),
475+
"manual" => Some(Self { automatic: false }),
487476
_ => None,
488477
}
489478
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use crate::{
5555
deserialized_responses::{
5656
DisplayName, RawAnySyncOrStrippedState, RawMemberEvent, RawSyncOrStrippedState,
5757
},
58-
store::ThreadStatus,
58+
store::ThreadSubscription,
5959
};
6060

6161
/// An abstract state store trait that can be used to implement different stores
@@ -485,7 +485,7 @@ pub trait StateStore: AsyncTraitDeps {
485485
&self,
486486
room: &RoomId,
487487
thread_id: &EventId,
488-
status: ThreadStatus,
488+
subscription: ThreadSubscription,
489489
) -> Result<(), Self::Error>;
490490

491491
/// Remove a previous thread subscription for a given room and thread.
@@ -504,7 +504,7 @@ pub trait StateStore: AsyncTraitDeps {
504504
&self,
505505
room: &RoomId,
506506
thread_id: &EventId,
507-
) -> Result<Option<ThreadStatus>, Self::Error>;
507+
) -> Result<Option<ThreadSubscription>, Self::Error>;
508508
}
509509

510510
#[repr(transparent)]
@@ -804,16 +804,16 @@ impl<T: StateStore> StateStore for EraseStateStoreError<T> {
804804
&self,
805805
room: &RoomId,
806806
thread_id: &EventId,
807-
status: ThreadStatus,
807+
subscription: ThreadSubscription,
808808
) -> Result<(), Self::Error> {
809-
self.0.upsert_thread_subscription(room, thread_id, status).await.map_err(Into::into)
809+
self.0.upsert_thread_subscription(room, thread_id, subscription).await.map_err(Into::into)
810810
}
811811

812812
async fn load_thread_subscription(
813813
&self,
814814
room: &RoomId,
815815
thread_id: &EventId,
816-
) -> Result<Option<ThreadStatus>, Self::Error> {
816+
) -> Result<Option<ThreadSubscription>, Self::Error> {
817817
self.0.load_thread_subscription(room, thread_id).await.map_err(Into::into)
818818
}
819819

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use matrix_sdk_base::{
2727
store::{
2828
ChildTransactionId, ComposerDraft, DependentQueuedRequest, DependentQueuedRequestKind,
2929
QueuedRequest, QueuedRequestKind, RoomLoadSettings, SentRequestKey,
30-
SerializableEventContent, ServerInfo, StateChanges, StateStore, StoreError, ThreadStatus,
30+
SerializableEventContent, ServerInfo, StateChanges, StateStore, StoreError,
31+
ThreadSubscription,
3132
},
3233
MinimalRoomMemberEvent, RoomInfo, RoomMemberships, StateStoreDataKey, StateStoreDataValue,
3334
ROOM_VERSION_FALLBACK, ROOM_VERSION_RULES_FALLBACK,
@@ -1790,7 +1791,7 @@ impl_state_store!({
17901791
&self,
17911792
room: &RoomId,
17921793
thread_id: &EventId,
1793-
status: ThreadStatus,
1794+
subscription: ThreadSubscription,
17941795
) -> Result<()> {
17951796
let encoded_key = self.encode_key(keys::THREAD_SUBSCRIPTIONS, (room, thread_id));
17961797

@@ -1800,7 +1801,7 @@ impl_state_store!({
18001801
)?;
18011802
let obj = tx.object_store(keys::THREAD_SUBSCRIPTIONS)?;
18021803

1803-
let serialized_value = self.serialize_value(&status.as_str().to_owned());
1804+
let serialized_value = self.serialize_value(&subscription.as_str().to_owned());
18041805
obj.put_key_val(&encoded_key, &serialized_value?)?;
18051806

18061807
tx.await.into_result()?;
@@ -1812,7 +1813,7 @@ impl_state_store!({
18121813
&self,
18131814
room: &RoomId,
18141815
thread_id: &EventId,
1815-
) -> Result<Option<ThreadStatus>> {
1816+
) -> Result<Option<ThreadSubscription>> {
18161817
let encoded_key = self.encode_key(keys::THREAD_SUBSCRIPTIONS, (room, thread_id));
18171818

18181819
let js_value = self
@@ -1828,12 +1829,13 @@ impl_state_store!({
18281829
};
18291830

18301831
let status_string: String = self.deserialize_value(&js_value)?;
1831-
let status =
1832-
ThreadStatus::from_value(&status_string).ok_or_else(|| StoreError::InvalidData {
1832+
let status = ThreadSubscription::from_value(&status_string).ok_or_else(|| {
1833+
StoreError::InvalidData {
18331834
details: format!(
18341835
"invalid thread status for room {room} and thread {thread_id}: {status_string}"
18351836
),
1836-
})?;
1837+
}
1838+
})?;
18371839

18381840
Ok(Some(status))
18391841
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use matrix_sdk_base::{
1313
store::{
1414
migration_helpers::RoomInfoV1, ChildTransactionId, DependentQueuedRequest,
1515
DependentQueuedRequestKind, QueueWedgeError, QueuedRequest, QueuedRequestKind,
16-
RoomLoadSettings, SentRequestKey, ThreadStatus,
16+
RoomLoadSettings, SentRequestKey, ThreadSubscription,
1717
},
1818
MinimalRoomMemberEvent, RoomInfo, RoomMemberships, RoomState, StateChanges, StateStore,
1919
StateStoreDataKey, StateStoreDataValue, ROOM_VERSION_FALLBACK, ROOM_VERSION_RULES_FALLBACK,
@@ -2098,11 +2098,11 @@ impl StateStore for SqliteStateStore {
20982098
&self,
20992099
room_id: &RoomId,
21002100
thread_id: &EventId,
2101-
status: ThreadStatus,
2101+
subscription: ThreadSubscription,
21022102
) -> Result<(), Self::Error> {
21032103
let room_id = self.encode_key(keys::THREAD_SUBSCRIPTIONS, room_id);
21042104
let thread_id = self.encode_key(keys::THREAD_SUBSCRIPTIONS, thread_id);
2105-
let status = status.as_str();
2105+
let status = subscription.as_str();
21062106

21072107
self.acquire()
21082108
.await?
@@ -2121,7 +2121,7 @@ impl StateStore for SqliteStateStore {
21212121
&self,
21222122
room_id: &RoomId,
21232123
thread_id: &EventId,
2124-
) -> Result<Option<ThreadStatus>, Self::Error> {
2124+
) -> Result<Option<ThreadSubscription>, Self::Error> {
21252125
let room_id = self.encode_key(keys::THREAD_SUBSCRIPTIONS, room_id);
21262126
let thread_id = self.encode_key(keys::THREAD_SUBSCRIPTIONS, thread_id);
21272127

@@ -2136,7 +2136,7 @@ impl StateStore for SqliteStateStore {
21362136
.await
21372137
.optional()?
21382138
.map(|data| {
2139-
ThreadStatus::from_value(&data).ok_or_else(|| Error::InvalidData {
2139+
ThreadSubscription::from_value(&data).ok_or_else(|| Error::InvalidData {
21402140
details: format!("Invalid thread status: {data}"),
21412141
})
21422142
})

0 commit comments

Comments
 (0)