Skip to content

Commit

Permalink
Message History - group purpose (#553)
Browse files Browse the repository at this point in the history
* feat: adds sql migrations for group purpose

* feat:  adds groups Purpose
  • Loading branch information
tuddman authored Mar 18, 2024
1 parent 8b6a1fb commit b09e2c8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
9 changes: 9 additions & 0 deletions xmtp_mls/migrations/2024-03-15-152716_group_types/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- As SQLite does not support ALTER, we play this game of move, repopulate, drop. Here we recreate without the 'purpose' column.
BEGIN TRANSACTION;
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);
INSERT INTO backup_group SELECT id, created_at_ns, membership_state, installations_last_checked FROM groups;
DROP TABLE groups;
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);
INSERT INTO groups SELECT id, created_at_ns, membership_state, installations_last_checked FROM backup_group;
DROP TABLE backup_group;
COMMIT;
2 changes: 2 additions & 0 deletions xmtp_mls/migrations/2024-03-15-152716_group_types/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE groups
ADD COLUMN purpose INT NOT NULL DEFAULT 1
66 changes: 60 additions & 6 deletions xmtp_mls/src/storage/encrypted_store/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct StoredGroup {
pub membership_state: GroupMembershipState,
/// Track when the latest, most recent installations were checked
pub installations_last_checked: i64,
/// Enum: `conversation = 1, sync = 2`
pub purpose: Purpose,
}

impl_fetch!(StoredGroup, groups, Vec<u8>);
Expand All @@ -44,6 +46,7 @@ impl StoredGroup {
created_at_ns,
membership_state,
installations_last_checked: 0,
purpose: Purpose::Conversation,
}
}
}
Expand Down Expand Up @@ -74,6 +77,8 @@ impl DbConnection<'_> {
query = query.limit(limit);
}

query = query.filter(dsl::purpose.eq(Purpose::Conversation));

Ok(self.raw_query(|conn| query.load(conn))?)
}

Expand Down Expand Up @@ -172,6 +177,37 @@ where
}
}

#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsExpression, FromSqlRow)]
#[diesel(sql_type = Integer)]
pub enum Purpose {
Conversation = 1,
Sync = 2,
}

impl ToSql<Integer, Sqlite> for Purpose
where
i32: ToSql<Integer, Sqlite>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
out.set_value(*self as i32);
Ok(IsNull::No)
}
}

impl FromSql<Integer, Sqlite> for Purpose
where
i32: FromSql<Integer, Sqlite>,
{
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
match i32::from_sql(bytes)? {
1 => Ok(Purpose::Conversation),
2 => Ok(Purpose::Sync),
x => Err(format!("Unrecognized variant {}", x).into()),
}
}
}

#[cfg(test)]
pub(crate) mod tests {

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

/// Generate a test group
pub fn generate_group(state: Option<GroupMembershipState>) -> StoredGroup {
StoredGroup {
id: rand_vec(),
created_at_ns: now_ns(),
membership_state: state.unwrap_or(GroupMembershipState::Allowed),
installations_last_checked: 0,
}
let id = rand_vec();
let created_at_ns = now_ns();
let membership_state = state.unwrap_or(GroupMembershipState::Allowed);
StoredGroup::new(id, created_at_ns, membership_state)
}

#[test]
Expand Down Expand Up @@ -295,4 +329,24 @@ pub(crate) mod tests {
assert!(fetched_group.created_at_ns < fetched_group.installations_last_checked);
})
}

#[test]
fn test_new_group_has_correct_purpose() {
with_connection(|conn| {
let test_group = generate_group(None);

conn.raw_query(|raw_conn| {
diesel::insert_into(groups)
.values(test_group.clone())
.execute(raw_conn)
})
.unwrap();

let fetched_group: Result<Option<StoredGroup>, StorageError> =
conn.fetch(&test_group.id);
assert_ok!(fetched_group, Some(test_group));
let purpose = fetched_group.unwrap().unwrap().purpose;
assert_eq!(purpose, Purpose::Conversation);
})
}
}
1 change: 1 addition & 0 deletions xmtp_mls/src/storage/encrypted_store/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ diesel::table! {
created_at_ns -> BigInt,
membership_state -> Integer,
installations_last_checked -> BigInt,
purpose -> Integer,
}
}

Expand Down

0 comments on commit b09e2c8

Please sign in to comment.