Skip to content

Commit b09e2c8

Browse files
authored
Message History - group purpose (#553)
* feat: adds sql migrations for group purpose * feat: adds groups Purpose
1 parent 8b6a1fb commit b09e2c8

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- As SQLite does not support ALTER, we play this game of move, repopulate, drop. Here we recreate without the 'purpose' column.
2+
BEGIN TRANSACTION;
3+
CREATE TEMPORARY TABLE backup_group(id BLOB PRIMARY KEY NOT NULL, created_at_ns BIGINT NOT NULL, membership_state INT NOT NULL, installations_last_checked BIGINT NOT NULL);
4+
INSERT INTO backup_group SELECT id, created_at_ns, membership_state, installations_last_checked FROM groups;
5+
DROP TABLE groups;
6+
CREATE TABLE groups(id BLOB PRIMARY KEY NOT NULL, created_at_ns BIGINT NOT NULL, membership_state INT NOT NULL, installations_last_checked BIGINT NOT NULL);
7+
INSERT INTO groups SELECT id, created_at_ns, membership_state, installations_last_checked FROM backup_group;
8+
DROP TABLE backup_group;
9+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE groups
2+
ADD COLUMN purpose INT NOT NULL DEFAULT 1

xmtp_mls/src/storage/encrypted_store/group.rs

+60-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub struct StoredGroup {
3232
pub membership_state: GroupMembershipState,
3333
/// Track when the latest, most recent installations were checked
3434
pub installations_last_checked: i64,
35+
/// Enum: `conversation = 1, sync = 2`
36+
pub purpose: Purpose,
3537
}
3638

3739
impl_fetch!(StoredGroup, groups, Vec<u8>);
@@ -44,6 +46,7 @@ impl StoredGroup {
4446
created_at_ns,
4547
membership_state,
4648
installations_last_checked: 0,
49+
purpose: Purpose::Conversation,
4750
}
4851
}
4952
}
@@ -74,6 +77,8 @@ impl DbConnection<'_> {
7477
query = query.limit(limit);
7578
}
7679

80+
query = query.filter(dsl::purpose.eq(Purpose::Conversation));
81+
7782
Ok(self.raw_query(|conn| query.load(conn))?)
7883
}
7984

@@ -172,6 +177,37 @@ where
172177
}
173178
}
174179

180+
#[repr(i32)]
181+
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsExpression, FromSqlRow)]
182+
#[diesel(sql_type = Integer)]
183+
pub enum Purpose {
184+
Conversation = 1,
185+
Sync = 2,
186+
}
187+
188+
impl ToSql<Integer, Sqlite> for Purpose
189+
where
190+
i32: ToSql<Integer, Sqlite>,
191+
{
192+
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
193+
out.set_value(*self as i32);
194+
Ok(IsNull::No)
195+
}
196+
}
197+
198+
impl FromSql<Integer, Sqlite> for Purpose
199+
where
200+
i32: FromSql<Integer, Sqlite>,
201+
{
202+
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
203+
match i32::from_sql(bytes)? {
204+
1 => Ok(Purpose::Conversation),
205+
2 => Ok(Purpose::Sync),
206+
x => Err(format!("Unrecognized variant {}", x).into()),
207+
}
208+
}
209+
}
210+
175211
#[cfg(test)]
176212
pub(crate) mod tests {
177213

@@ -185,12 +221,10 @@ pub(crate) mod tests {
185221

186222
/// Generate a test group
187223
pub fn generate_group(state: Option<GroupMembershipState>) -> StoredGroup {
188-
StoredGroup {
189-
id: rand_vec(),
190-
created_at_ns: now_ns(),
191-
membership_state: state.unwrap_or(GroupMembershipState::Allowed),
192-
installations_last_checked: 0,
193-
}
224+
let id = rand_vec();
225+
let created_at_ns = now_ns();
226+
let membership_state = state.unwrap_or(GroupMembershipState::Allowed);
227+
StoredGroup::new(id, created_at_ns, membership_state)
194228
}
195229

196230
#[test]
@@ -295,4 +329,24 @@ pub(crate) mod tests {
295329
assert!(fetched_group.created_at_ns < fetched_group.installations_last_checked);
296330
})
297331
}
332+
333+
#[test]
334+
fn test_new_group_has_correct_purpose() {
335+
with_connection(|conn| {
336+
let test_group = generate_group(None);
337+
338+
conn.raw_query(|raw_conn| {
339+
diesel::insert_into(groups)
340+
.values(test_group.clone())
341+
.execute(raw_conn)
342+
})
343+
.unwrap();
344+
345+
let fetched_group: Result<Option<StoredGroup>, StorageError> =
346+
conn.fetch(&test_group.id);
347+
assert_ok!(fetched_group, Some(test_group));
348+
let purpose = fetched_group.unwrap().unwrap().purpose;
349+
assert_eq!(purpose, Purpose::Conversation);
350+
})
351+
}
298352
}

xmtp_mls/src/storage/encrypted_store/schema.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ diesel::table! {
3131
created_at_ns -> BigInt,
3232
membership_state -> Integer,
3333
installations_last_checked -> BigInt,
34+
purpose -> Integer,
3435
}
3536
}
3637

0 commit comments

Comments
 (0)