diff --git a/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/down.sql b/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/down.sql index 22bd9bbd8..d55d28698 100644 --- a/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/down.sql +++ b/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/down.sql @@ -1,5 +1,3 @@ -ALTER TABLE groups DROP COLUMN dm_id; ALTER TABLE groups DROP COLUMN last_message_ns; -ALTER TABLE groups ADD COLUMN dm_inbox_id TEXT; DROP TRIGGER IF EXISTS msg_inserted; diff --git a/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/up.sql b/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/up.sql index 88de8b3cc..7a88cb3fa 100644 --- a/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/up.sql +++ b/xmtp_mls/migrations/2024-12-18-170645_add_dm_id/up.sql @@ -1,19 +1,4 @@ -ALTER TABLE groups ADD COLUMN dm_id TEXT; -ALTER TABLE groups ADD COLUMN last_message_ns BIGINT NOT NULL DEFAULT (created_at_ns); - --- Fill the dm_id column -UPDATE groups -SET dm_id = 'dm:' || - LOWER( - CASE - WHEN LOWER((SELECT inbox_id FROM identity)) < LOWER(dm_inbox_id) - THEN (SELECT inbox_id FROM identity) || ':' || dm_inbox_id - ELSE dm_inbox_id || ':' || (SELECT inbox_id FROM identity) - END - ) -WHERE dm_inbox_id IS NOT NULL; - -ALTER TABLE groups REMOVE COLUMN dm_inbox_id; +ALTER TABLE groups ADD COLUMN last_message_ns BIGINT NOT NULL DEFAULT (strftime('%s', 'now') * 1000000000); -- Create a trigger to auto-update group table on insert CREATE TRIGGER msg_iserted diff --git a/xmtp_mls/src/storage/encrypted_store/group.rs b/xmtp_mls/src/storage/encrypted_store/group.rs index fabb67b9b..7ccf2b97a 100644 --- a/xmtp_mls/src/storage/encrypted_store/group.rs +++ b/xmtp_mls/src/storage/encrypted_store/group.rs @@ -37,12 +37,12 @@ pub struct StoredGroup { pub added_by_inbox_id: String, /// The sequence id of the welcome message pub welcome_id: Option, + /// The inbox_id of the DM target + pub dm_inbox_id: Option, /// The last time the leaf node encryption key was rotated pub rotated_at_ns: i64, /// Enum, [`ConversationType`] signifies the group conversation type which extends to who can access it. pub conversation_type: ConversationType, - /// The inbox_id of the DM target - pub dm_id: Option, /// Timestamp of when the last message was sent for this group (updated automatically in a trigger) pub last_message_ns: i64, } @@ -59,7 +59,7 @@ impl StoredGroup { added_by_inbox_id: String, welcome_id: i64, conversation_type: ConversationType, - dm_id: Option, + dm_inbox_id: Option, ) -> Self { Self { id, @@ -70,7 +70,7 @@ impl StoredGroup { added_by_inbox_id, welcome_id: Some(welcome_id), rotated_at_ns: 0, - dm_id, + dm_inbox_id, last_message_ns: now_ns(), } } @@ -81,21 +81,21 @@ impl StoredGroup { created_at_ns: i64, membership_state: GroupMembershipState, added_by_inbox_id: String, - dm_id: Option, + dm_inbox_id: Option, ) -> Self { Self { id, created_at_ns, membership_state, installations_last_checked: 0, - conversation_type: match dm_id { + conversation_type: match dm_inbox_id { Some(_) => ConversationType::Dm, None => ConversationType::Group, }, added_by_inbox_id, welcome_id: None, rotated_at_ns: 0, - dm_id: dm_id, + dm_inbox_id, last_message_ns: now_ns(), } } @@ -116,7 +116,7 @@ impl StoredGroup { added_by_inbox_id: "".into(), welcome_id: None, rotated_at_ns: 0, - dm_id: None, + dm_inbox_id: None, last_message_ns: now_ns(), } } diff --git a/xmtp_mls/src/storage/encrypted_store/schema.rs b/xmtp_mls/src/storage/encrypted_store/schema.rs index 083aa7329..225cbbbe0 100644 --- a/xmtp_mls/src/storage/encrypted_store/schema.rs +++ b/xmtp_mls/src/storage/encrypted_store/schema.rs @@ -55,7 +55,6 @@ diesel::table! { dm_inbox_id -> Nullable, rotated_at_ns -> BigInt, conversation_type -> Integer, - dm_id -> Nullable, last_message_ns -> BigInt, } }