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 40 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
32 changes: 18 additions & 14 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,19 @@ pub struct FfiListConversationsOptions {
pub created_before_ns: Option<i64>,
pub limit: Option<i64>,
pub consent_state: Option<FfiConsentState>,
pub include_duplicate_dms: bool,
}

impl From<FfiListConversationsOptions> for GroupQueryArgs {
fn from(opts: FfiListConversationsOptions) -> GroupQueryArgs {
GroupQueryArgs::default()
.maybe_created_before_ns(opts.created_before_ns)
.maybe_created_after_ns(opts.created_after_ns)
.maybe_limit(opts.limit)
.maybe_consent_state(opts.consent_state.map(Into::into))
GroupQueryArgs {
created_before_ns: opts.created_before_ns,
created_after_ns: opts.created_after_ns,
limit: opts.limit,
consent_state: opts.consent_state.map(Into::into),
include_duplicate_dms: opts.include_duplicate_dms,
..Default::default()
}
}
}

Expand Down Expand Up @@ -1351,15 +1355,15 @@ 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,
..Default::default()
})?
.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
2 changes: 1 addition & 1 deletion xmtp_mls/diesel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/storage/encrypted_store/schema.rs"
file = "src/storage/encrypted_store/schema_gen.rs"

[migrations_directory]
dir = "migrations"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DROP VIEW IF EXISTS conversation_list;
DROP VIEW IF EXISTS conversation_list;
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ SELECT
rm.authority_id
FROM
groups g
LEFT JOIN ranked_messages rm
ON g.id = rm.group_id AND rm.row_num = 1
ORDER BY COALESCE(rm.sent_at_ns, g.created_at_ns) DESC;
LEFT JOIN ranked_messages rm
ON g.id = rm.group_id AND rm.row_num = 1
ORDER BY COALESCE(rm.sent_at_ns, g.created_at_ns) DESC;
5 changes: 5 additions & 0 deletions xmtp_mls/migrations/2024-12-20-214747_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;
76 changes: 76 additions & 0 deletions xmtp_mls/migrations/2024-12-20-214747_add_dm_id/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
DROP VIEW IF EXISTS conversation_list;
ALTER TABLE groups ADD COLUMN dm_id TEXT;
ALTER TABLE groups ADD COLUMN last_message_ns BIGINT;

-- 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;

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;


CREATE VIEW conversation_list AS
WITH ranked_messages AS (
SELECT
gm.group_id,
gm.id AS message_id,
gm.decrypted_message_bytes,
gm.sent_at_ns,
gm.kind AS message_kind,
gm.sender_installation_id,
gm.sender_inbox_id,
gm.delivery_status,
gm.content_type,
gm.version_major,
gm.version_minor,
gm.authority_id,
ROW_NUMBER() OVER (PARTITION BY gm.group_id ORDER BY gm.sent_at_ns DESC) AS row_num
FROM
group_messages gm
WHERE
gm.kind = 1
)
SELECT
g.id AS id,
g.created_at_ns,
g.membership_state,
g.installations_last_checked,
g.added_by_inbox_id,
g.welcome_id,
g.dm_id,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to recreate the view to account for the column rename dm_inbox_id -> dm_id

g.rotated_at_ns,
g.conversation_type,
rm.message_id,
rm.decrypted_message_bytes,
rm.sent_at_ns,
rm.message_kind,
rm.sender_installation_id,
rm.sender_inbox_id,
rm.delivery_status,
rm.content_type,
rm.version_major,
rm.version_minor,
rm.authority_id
FROM
groups g
LEFT JOIN ranked_messages rm
ON g.id = rm.group_id AND rm.row_num = 1
ORDER BY COALESCE(rm.sent_at_ns, g.created_at_ns) DESC;
11 changes: 8 additions & 3 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use crate::groups::ConversationListItem;
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 @@ -637,8 +637,12 @@ where
target_inbox_id: String,
) -> Result<MlsGroup<Self>, ClientError> {
let conn = self.store().conn()?;

let group = conn
.find_dm_group(&target_inbox_id)?
.find_dm_group(&DmMembers {
member_one_inbox_id: self.inbox_id(),
member_two_inbox_id: &target_inbox_id,
})?
.ok_or(NotFound::DmByInbox(target_inbox_id))?;
Ok(MlsGroup::new(self.clone(), group.id, group.created_at_ns))
}
Expand Down Expand Up @@ -943,6 +947,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 @@ -496,7 +496,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 @@ -529,8 +532,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
Loading
Loading