Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation Sitching #1432

Merged
merged 42 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5938aeb
migration
codabrink Dec 18, 2024
b250d02
Fill the column
codabrink Dec 18, 2024
334c066
wip
codabrink Dec 18, 2024
9b894b5
adjust column
codabrink Dec 18, 2024
7d1f8d9
adjust default
codabrink Dec 18, 2024
13031f8
efficient derivation
codabrink Dec 18, 2024
95639d9
use a trait
codabrink Dec 18, 2024
865506d
fix the migration
codabrink Dec 18, 2024
1438028
minor fixes
codabrink Dec 18, 2024
91388a7
update welcome
codabrink Dec 18, 2024
1873e99
use the concrete type
codabrink Dec 18, 2024
e4e2e87
cleanup
codabrink Dec 19, 2024
eb44a15
Merge branch 'main' into coda/stitching
codabrink Dec 19, 2024
825b83f
remove the builder
codabrink Dec 19, 2024
d4d3d30
Merge branch 'coda/stitching' of github.com:xmtp/libxmtp into coda/st…
codabrink Dec 19, 2024
037b8de
update the query
codabrink Dec 19, 2024
d155744
comment
codabrink Dec 19, 2024
0f5c726
subquery
codabrink Dec 19, 2024
be69a8d
need those inbox ids to be different
codabrink Dec 19, 2024
e7f933b
test
codabrink Dec 19, 2024
4c72ddc
cleanup
codabrink Dec 19, 2024
9cefbc5
cleanup
codabrink Dec 19, 2024
17f534e
lint
codabrink Dec 19, 2024
75c9f0d
Update xmtp_mls/migrations/2024-12-18-170645_add_dm_id/up.sql
codabrink Dec 20, 2024
3dfef1f
better test
codabrink Dec 20, 2024
fa4c810
test the timestamp
codabrink Dec 20, 2024
55644b8
cleanup
codabrink Dec 20, 2024
4959fbe
move comment
codabrink Dec 20, 2024
e820767
more cleanup
codabrink Dec 20, 2024
a287b35
test it
codabrink Dec 20, 2024
e70abb2
Merge remote-tracking branch 'origin/main' into coda/stitching
codabrink Dec 20, 2024
5477865
wasm
codabrink Dec 20, 2024
14693de
Merge remote-tracking branch 'origin/main' into coda/stitching
codabrink Dec 20, 2024
b234f27
test fix
codabrink Dec 20, 2024
918d9da
test fix
codabrink Dec 20, 2024
c8c6f5b
Merge branch 'main' into coda/stitching
codabrink Dec 20, 2024
3dbcd67
fix migration headaches
codabrink Dec 20, 2024
70ac2b4
fix
codabrink Dec 20, 2024
ab0acf1
lint
codabrink Dec 20, 2024
f375274
add option to include duplicate dms
codabrink Dec 20, 2024
9ed24e7
Merge remote-tracking branch 'origin/main' into coda/stitching
codabrink Dec 21, 2024
04ad4ed
lint
codabrink Dec 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,15 +1323,14 @@ impl FfiConversation {

let messages: Vec<FfiMessage> = self
.inner
.find_messages(
&MsgQueryArgs::default()
.maybe_sent_before_ns(opts.sent_before_ns)
.maybe_sent_after_ns(opts.sent_after_ns)
.maybe_kind(kind)
.maybe_delivery_status(delivery_status)
.maybe_limit(opts.limit)
.maybe_direction(direction),
)?
.find_messages(&MsgQueryArgs {
sent_before_ns: opts.sent_before_ns,
sent_after_ns: opts.sent_after_ns,
limit: opts.limit,
kind,
delivery_status,
direction,
})?
.into_iter()
.map(|msg| msg.into())
.collect();
Expand Down
7 changes: 5 additions & 2 deletions bindings_node/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ impl Conversation {
ConversationType::Dm => Some(XmtpGroupMessageKind::Application),
ConversationType::Sync => None,
};
let opts: MsgQueryArgs = opts.into();
let opts = MsgQueryArgs {
kind,
..opts.into()
};
let messages: Vec<Message> = group
.find_messages(&opts.maybe_kind(kind))
.find_messages(&opts)
.map_err(ErrorWrapper::from)?
.into_iter()
.map(|msg| msg.into())
Expand Down
14 changes: 8 additions & 6 deletions bindings_node/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ impl From<ListMessagesOptions> for MsgQueryArgs {
let delivery_status = opts.delivery_status.map(Into::into);
let direction = opts.direction.map(Into::into);

MsgQueryArgs::default()
.maybe_sent_before_ns(opts.sent_before_ns)
.maybe_sent_after_ns(opts.sent_after_ns)
.maybe_delivery_status(delivery_status)
.maybe_limit(opts.limit)
.maybe_direction(direction)
MsgQueryArgs {
sent_before_ns: opts.sent_before_ns,
sent_after_ns: opts.sent_after_ns,
delivery_status,
limit: opts.limit,
direction,
..Default::default()
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions bindings_wasm/src/conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,13 @@ impl Conversation {
ConversationType::Dm => Some(XmtpGroupMessageKind::Application),
ConversationType::Sync => None,
};
let opts: MsgQueryArgs = opts.into();

let opts = MsgQueryArgs {
kind,
..opts.into()
};
let messages: Vec<Message> = group
.find_messages(&opts.maybe_kind(kind))
.find_messages(&opts)
.map_err(|e| JsError::new(&format!("{e}")))?
.into_iter()
.map(Into::into)
Expand Down
14 changes: 8 additions & 6 deletions bindings_wasm/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ impl From<ListMessagesOptions> for MsgQueryArgs {
let delivery_status = opts.delivery_status.map(Into::into);
let direction = opts.direction.map(Into::into);

MsgQueryArgs::default()
.maybe_sent_before_ns(opts.sent_before_ns)
.maybe_sent_after_ns(opts.sent_after_ns)
.maybe_delivery_status(delivery_status)
.maybe_limit(opts.limit)
.maybe_direction(direction)
MsgQueryArgs {
sent_before_ns: opts.sent_before_ns,
sent_after_ns: opts.sent_after_ns,
delivery_status,
limit: opts.limit,
direction,
..Default::default()
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/cli/cli-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,10 @@ async fn main() -> color_eyre::eyre::Result<()> {
let group = client.get_sync_group(provider.conn_ref())?;
let group_id_str = hex::encode(group.group_id.clone());
group.sync().await?;
let messages = group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))?;
let messages = group.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})?;
info!(
group_id = group_id_str,
messages = messages.len(),
Expand Down
5 changes: 5 additions & 0 deletions xmtp_mls/migrations/2024-12-18-170645_add_dm_id/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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;
26 changes: 26 additions & 0 deletions xmtp_mls/migrations/2024-12-18-170645_add_dm_id/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ALTER TABLE groups ADD COLUMN dm_id TEXT;
ALTER TABLE groups ADD COLUMN last_message_ns BIGINT NOT NULL DEFAULT ((strftime('%s', 'now') * 1000000000) + (strftime('%f', 'now') * 1000000));
codabrink marked this conversation as resolved.
Show resolved Hide resolved

-- 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
)
nplasterer marked this conversation as resolved.
Show resolved Hide resolved
WHERE dm_inbox_id IS NOT NULL;

DROP INDEX IF EXISTS idx_dm_target;
ALTER TABLE groups DROP COLUMN dm_inbox_id;

-- Create a trigger to auto-update group table on insert
CREATE TRIGGER msg_inserted
AFTER INSERT ON group_messages
BEGIN
UPDATE groups
SET last_message_ns = (strftime('%s', 'now') * 1000000000) + (strftime('%f', 'now') * 1000000)
WHERE id = NEW.group_id;
END;
10 changes: 7 additions & 3 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use crate::groups::device_sync::WorkerHandle;
use crate::{
api::ApiClientWrapper,
groups::{
device_sync::preference_sync::UserPreferenceUpdate, group_permissions::PolicySet,
GroupError, GroupMetadataOptions, MlsGroup,
device_sync::preference_sync::UserPreferenceUpdate, group_metadata::DmMembers,
group_permissions::PolicySet, GroupError, GroupMetadataOptions, MlsGroup,
},
identity::{parse_credential, Identity, IdentityError},
identity_updates::{load_identity_updates, IdentityUpdateError},
Expand Down Expand Up @@ -638,7 +638,10 @@ where
target_inbox_id: String,
) -> Result<MlsGroup<Self>, ClientError> {
let conn = self.store().conn()?;
match conn.find_dm_group(&target_inbox_id)? {
match conn.find_dm_group(&DmMembers {
member_one_inbox_id: self.inbox_id(),
member_two_inbox_id: &target_inbox_id,
})? {
Some(dm_group) => Ok(MlsGroup::new(
self.clone(),
dm_group.id,
Expand Down Expand Up @@ -920,6 +923,7 @@ where
let query_args = GroupQueryArgs {
consent_state,
include_sync_groups: true,
include_duplicate_dms: true,
..GroupQueryArgs::default()
};
let groups = provider
Expand Down
11 changes: 8 additions & 3 deletions xmtp_mls/src/groups/device_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ where

let messages = provider.conn_ref().get_group_messages(
&sync_group.group_id,
&MsgQueryArgs::default().kind(GroupMessageKind::Application),
&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
},
)?;

for msg in messages.into_iter().rev() {
Expand Down Expand Up @@ -525,8 +528,10 @@ where
let sync_group = self.get_sync_group(provider.conn_ref())?;
sync_group.sync_with_conn(provider).await?;

let messages = sync_group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))?;
let messages = sync_group.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})?;

for msg in messages.into_iter().rev() {
let Ok(msg_content) =
Expand Down
3 changes: 1 addition & 2 deletions xmtp_mls/src/groups/device_sync/message_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ where
&self,
conn: &DbConnection,
) -> Result<Vec<Syncable>, DeviceSyncError> {
let groups =
conn.find_groups(GroupQueryArgs::default().conversation_type(ConversationType::Group))?;
let groups = conn.find_groups(GroupQueryArgs::default())?;

let mut all_messages = vec![];
for StoredGroup { id, .. } in groups.into_iter() {
Expand Down
71 changes: 61 additions & 10 deletions xmtp_mls/src/groups/group_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::fmt::Display;

use openmls::{extensions::Extensions, group::MlsGroup as OpenMlsGroup};
use prost::Message;
use thiserror::Error;

use xmtp_id::InboxId;
use xmtp_proto::xmtp::mls::message_contents::{
ConversationType as ConversationTypeProto, DmMembers as DmMembersProto,
GroupMetadataV1 as GroupMetadataProto, Inbox as InboxProto,
Expand Down Expand Up @@ -31,14 +34,14 @@ pub struct GroupMetadata {
pub conversation_type: ConversationType,
// TODO: Remove this once transition is completed
pub creator_inbox_id: String,
pub dm_members: Option<DmMembers>,
pub dm_members: Option<DmMembers<InboxId>>,
}

impl GroupMetadata {
pub fn new(
conversation_type: ConversationType,
creator_inbox_id: String,
dm_members: Option<DmMembers>,
dm_members: Option<DmMembers<InboxId>>,
) -> Self {
Self {
conversation_type,
Expand Down Expand Up @@ -130,25 +133,73 @@ impl TryFrom<i32> for ConversationType {
}

#[derive(Debug, Clone, PartialEq)]
pub struct DmMembers {
pub member_one_inbox_id: String,
pub member_two_inbox_id: String,
pub struct DmMembers<Id: AsRef<str>> {
pub member_one_inbox_id: Id,
pub member_two_inbox_id: Id,
}

impl From<DmMembers> for DmMembersProto {
fn from(value: DmMembers) -> Self {
impl<'a> DmMembers<String> {
pub fn as_ref(&'a self) -> DmMembers<&'a str> {
DmMembers {
member_one_inbox_id: &*self.member_one_inbox_id,
member_two_inbox_id: &*self.member_two_inbox_id,
}
}
}

impl<Id> From<DmMembers<Id>> for DmMembersProto
where
Id: AsRef<str>,
{
fn from(value: DmMembers<Id>) -> Self {
DmMembersProto {
dm_member_one: Some(InboxProto {
inbox_id: value.member_one_inbox_id.clone(),
inbox_id: value.member_one_inbox_id.as_ref().to_string(),
}),
dm_member_two: Some(InboxProto {
inbox_id: value.member_two_inbox_id.clone(),
inbox_id: value.member_two_inbox_id.as_ref().to_string(),
}),
}
}
}

impl TryFrom<DmMembersProto> for DmMembers {
impl<Id> From<&DmMembers<Id>> for String
where
Id: AsRef<str>,
{
fn from(members: &DmMembers<Id>) -> Self {
format!("{members}")
}
}

impl<Id> From<DmMembers<Id>> for String
where
Id: AsRef<str>,
{
fn from(members: DmMembers<Id>) -> Self {
format!("{members}")
}
}

impl<Id> Display for DmMembers<Id>
where
Id: AsRef<str>,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut inbox_ids = [
self.member_one_inbox_id.as_ref(),
self.member_two_inbox_id.as_ref(),
]
.into_iter()
.map(str::to_lowercase)
.collect::<Vec<_>>();
inbox_ids.sort();

write!(f, "dm:{}", inbox_ids.join(":"))
}
}

impl TryFrom<DmMembersProto> for DmMembers<InboxId> {
type Error = GroupMetadataError;

fn try_from(value: DmMembersProto) -> Result<Self, Self::Error> {
Expand Down
Loading