Skip to content

Commit

Permalink
remove the builder
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink committed Dec 19, 2024
1 parent e4e2e87 commit 825b83f
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 111 deletions.
17 changes: 8 additions & 9 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,15 +1331,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,
kind: kind,
delivery_status: delivery_status,
limit: opts.limit,
direction: 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: 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
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
30 changes: 24 additions & 6 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,10 @@ pub(crate) mod tests {
amal_group.sync().await.expect("sync failed");

let amal_messages = amal_group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))
.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})
.unwrap()
.into_iter()
.collect::<Vec<StoredGroupMessage>>();
Expand Down Expand Up @@ -3295,7 +3298,10 @@ pub(crate) mod tests {
];

let messages = amal_group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))
.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})
.unwrap()
.into_iter()
.collect::<Vec<StoredGroupMessage>>();
Expand Down Expand Up @@ -3835,10 +3841,16 @@ pub(crate) mod tests {
bo_group.sync().await.unwrap();

let alix_messages = alix_group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))
.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})
.unwrap();
let bo_messages = bo_group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))
.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})
.unwrap();

assert_eq!(alix_messages.len(), 2);
Expand All @@ -3858,10 +3870,16 @@ pub(crate) mod tests {
bo_group.sync().await.unwrap();

let alix_messages = alix_group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))
.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})
.unwrap();
let bo_messages = bo_group
.find_messages(&MsgQueryArgs::default().kind(GroupMessageKind::Application))
.find_messages(&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
})
.unwrap();
assert_eq!(bo_messages.len(), 3);
assert_eq!(alix_messages.len(), 3); // Fails here, 2 != 3
Expand Down
4 changes: 2 additions & 2 deletions xmtp_mls/src/storage/encrypted_store/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ impl DbConnection {
let dm_id = String::from(members);

let query = dsl::groups
.order(dsl::created_at_ns.asc())
.filter(dsl::dm_id.eq(Some(dm_id)));
.filter(dsl::dm_id.eq(Some(dm_id)))
.order(dsl::last_message_ns.desc());

let groups: Vec<StoredGroup> = self.raw_query(|conn| query.load(conn))?;
if groups.len() > 1 {
Expand Down
114 changes: 41 additions & 73 deletions xmtp_mls/src/storage/encrypted_store/group_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,74 +116,16 @@ impl_store_or_ignore!(StoredGroupMessage, group_messages);

#[derive(Default)]
pub struct MsgQueryArgs {
sent_after_ns: Option<i64>,
sent_before_ns: Option<i64>,
kind: Option<GroupMessageKind>,
delivery_status: Option<DeliveryStatus>,
limit: Option<i64>,
direction: Option<SortDirection>,
}

impl MsgQueryArgs {
pub fn sent_after_ns(mut self, sent_after_ns: i64) -> Self {
self.sent_after_ns = Some(sent_after_ns);
self
}
pub fn maybe_sent_after_ns(mut self, sent_after_ns: Option<i64>) -> Self {
self.sent_after_ns = sent_after_ns;
self
}

pub fn sent_before_ns(mut self, sent_before_ns: i64) -> Self {
self.sent_before_ns = Some(sent_before_ns);
self
}
pub fn maybe_sent_before_ns(mut self, sent_before_ns: Option<i64>) -> Self {
self.sent_before_ns = sent_before_ns;
self
}

pub fn kind(mut self, kind: GroupMessageKind) -> Self {
self.kind = Some(kind);
self
}

pub fn maybe_kind(mut self, kind: Option<GroupMessageKind>) -> Self {
self.kind = kind;
self
}

pub fn direction(mut self, direction: SortDirection) -> Self {
self.direction = Some(direction);
self
}
pub fn maybe_direction(mut self, direction: Option<SortDirection>) -> Self {
self.direction = direction;
self
}

pub fn delivery_status(mut self, delivery_status: DeliveryStatus) -> Self {
self.delivery_status = Some(delivery_status);
self
}
pub fn maybe_delivery_status(mut self, delivery_status: Option<DeliveryStatus>) -> Self {
self.delivery_status = delivery_status;
self
}

pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn maybe_limit(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self
}
pub sent_after_ns: Option<i64>,
pub sent_before_ns: Option<i64>,
pub kind: Option<GroupMessageKind>,
pub delivery_status: Option<DeliveryStatus>,
pub limit: Option<i64>,
pub direction: Option<SortDirection>,
}

impl DbConnection {
/// Query for group messages
#[allow(clippy::too_many_arguments)]
pub fn get_group_messages(
&self,
group_id: &[u8],
Expand Down Expand Up @@ -397,21 +339,35 @@ pub(crate) mod tests {
let message = conn
.get_group_messages(
&group.id,
&MsgQueryArgs::default()
.sent_after_ns(1_000)
.sent_before_ns(100_000),
&MsgQueryArgs {
sent_after_ns: Some(1_000),
sent_before_ns: Some(100_000),
..Default::default()
},
)
.unwrap();
assert_eq!(message.len(), 1);
assert_eq!(message.first().unwrap().sent_at_ns, 10_000);

let messages = conn
.get_group_messages(&group.id, &MsgQueryArgs::default().sent_before_ns(100_000))
.get_group_messages(
&group.id,
&MsgQueryArgs {
sent_before_ns: Some(100_000),
..Default::default()
},
)
.unwrap();
assert_eq!(messages.len(), 2);

let messages = conn
.get_group_messages(&group.id, &MsgQueryArgs::default().sent_after_ns(10_000))
.get_group_messages(
&group.id,
&MsgQueryArgs {
sent_after_ns: Some(10_000),
..Default::default()
},
)
.unwrap();
assert_eq!(messages.len(), 2);
})
Expand Down Expand Up @@ -449,15 +405,21 @@ pub(crate) mod tests {
let application_messages = conn
.get_group_messages(
&group.id,
&MsgQueryArgs::default().kind(GroupMessageKind::Application),
&MsgQueryArgs {
kind: Some(GroupMessageKind::Application),
..Default::default()
},
)
.unwrap();
assert_eq!(application_messages.len(), 15);

let membership_changes = conn
.get_group_messages(
&group.id,
&MsgQueryArgs::default().kind(GroupMessageKind::MembershipChange),
&MsgQueryArgs {
kind: Some(GroupMessageKind::MembershipChange),
..Default::default()
},
)
.unwrap();
assert_eq!(membership_changes.len(), 15);
Expand All @@ -483,7 +445,10 @@ pub(crate) mod tests {
let messages_asc = conn
.get_group_messages(
&group.id,
&MsgQueryArgs::default().direction(SortDirection::Ascending),
&MsgQueryArgs {
direction: Some(SortDirection::Ascending),
..Default::default()
},
)
.unwrap();
assert_eq!(messages_asc.len(), 4);
Expand All @@ -495,7 +460,10 @@ pub(crate) mod tests {
let messages_desc = conn
.get_group_messages(
&group.id,
&MsgQueryArgs::default().direction(SortDirection::Descending),
&MsgQueryArgs {
direction: Some(SortDirection::Descending),
..Default::default()
},
)
.unwrap();
assert_eq!(messages_desc.len(), 4);
Expand Down

0 comments on commit 825b83f

Please sign in to comment.