Skip to content

Commit

Permalink
Add new find_messages method (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas authored Nov 13, 2023
1 parent 3294846 commit 15626cb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
8 changes: 5 additions & 3 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,17 @@ where
pub fn find_groups(
&self,
allowed_states: Option<Vec<GroupMembershipState>>,
created_at_ns_gt: Option<i64>,
created_after_ns: Option<i64>,
created_before_ns: Option<i64>,
limit: Option<i64>,
) -> Result<Vec<MlsGroup<ApiClient>>, ClientError> {
Ok(self
.store
.find_groups(
&mut self.store.conn()?,
allowed_states,
created_at_ns_gt,
created_after_ns,
created_before_ns,
limit,
)?
.into_iter()
Expand Down Expand Up @@ -279,7 +281,7 @@ mod tests {
let group_1 = client.create_group().unwrap();
let group_2 = client.create_group().unwrap();

let groups = client.find_groups(None, None, None).unwrap();
let groups = client.find_groups(None, None, None, None).unwrap();
assert_eq!(groups.len(), 2);
assert_eq!(groups[0].group_id, group_1.group_id);
assert_eq!(groups[1].group_id, group_2.group_id);
Expand Down
21 changes: 21 additions & 0 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
storage::{
group::{GroupMembershipState, StoredGroup},
group_intent::{IntentKind, IntentState, NewGroupIntent, StoredGroupIntent},
group_message::{GroupMessageKind, StoredGroupMessage},
DbConnection, StorageError,
},
utils::{hash::sha256, time::now_ns, topic::get_group_topic},
Expand Down Expand Up @@ -111,6 +112,26 @@ where
Ok(Self::new(client, group_id, stored_group.created_at_ns))
}

pub fn find_messages(
&self,
kind: Option<GroupMessageKind>,
sent_before_ns: Option<i64>,
sent_after_ns: Option<i64>,
limit: Option<i64>,
) -> Result<Vec<StoredGroupMessage>, GroupError> {
let mut conn = self.client.store.conn()?;
let messages = self.client.store.get_group_messages(
&mut conn,
&self.group_id,
sent_after_ns,
sent_before_ns,
kind,
limit,
)?;

Ok(messages)
}

pub async fn send_message(&self, message: &[u8]) -> Result<(), GroupError> {
let mut conn = self.client.store.conn()?;
let intent_data: Vec<u8> = SendMessageIntentData::new(message.to_vec()).into();
Expand Down
28 changes: 22 additions & 6 deletions xmtp_mls/src/storage/encrypted_store/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl EncryptedMessageStore {
&self,
conn: &mut DbConnection,
allowed_states: Option<Vec<GroupMembershipState>>,
created_at_ns_gt: Option<i64>,
created_after_ns: Option<i64>,
created_before_ns: Option<i64>,
limit: Option<i64>,
) -> Result<Vec<StoredGroup>, StorageError> {
let mut query = dsl::groups.order(dsl::created_at_ns.asc()).into_boxed();
Expand All @@ -59,8 +60,12 @@ impl EncryptedMessageStore {
query = query.filter(dsl::membership_state.eq_any(allowed_states));
}

if let Some(created_at_ns_gt) = created_at_ns_gt {
query = query.filter(dsl::created_at_ns.gt(created_at_ns_gt));
if let Some(created_after_ns) = created_after_ns {
query = query.filter(dsl::created_at_ns.gt(created_after_ns));
}

if let Some(created_before_ns) = created_before_ns {
query = query.filter(dsl::created_at_ns.lt(created_before_ns));
}

if let Some(limit) = limit {
Expand Down Expand Up @@ -195,7 +200,9 @@ pub(crate) mod tests {
let test_group_2 = generate_group(Some(GroupMembershipState::Allowed));
test_group_2.store(&mut conn).unwrap();

let all_results = store.find_groups(&mut conn, None, None, None).unwrap();
let all_results = store
.find_groups(&mut conn, None, None, None, None)
.unwrap();
assert_eq!(all_results.len(), 2);

let pending_results = store
Expand All @@ -204,18 +211,27 @@ pub(crate) mod tests {
Some(vec![GroupMembershipState::Pending]),
None,
None,
None,
)
.unwrap();
assert_eq!(pending_results[0].id, test_group_1.id);
assert_eq!(pending_results.len(), 1);

// Offset and limit
let results_with_limit = store.find_groups(&mut conn, None, None, Some(1)).unwrap();
let results_with_limit = store
.find_groups(&mut conn, None, None, None, Some(1))
.unwrap();
assert_eq!(results_with_limit.len(), 1);
assert_eq!(results_with_limit[0].id, test_group_1.id);

let results_with_created_at_ns_after = store
.find_groups(&mut conn, None, Some(test_group_1.created_at_ns), Some(1))
.find_groups(
&mut conn,
None,
Some(test_group_1.created_at_ns),
None,
Some(1),
)
.unwrap();
assert_eq!(results_with_created_at_ns_after.len(), 1);
assert_eq!(results_with_created_at_ns_after[0].id, test_group_2.id);
Expand Down
25 changes: 17 additions & 8 deletions xmtp_mls/src/storage/encrypted_store/group_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,33 @@ impl EncryptedMessageStore {
&self,
conn: &mut DbConnection,
group_id: GroupId,
sent_after: Option<i64>,
sent_before: Option<i64>,
sent_after_ns: Option<i64>,
sent_before_ns: Option<i64>,
kind: Option<GroupMessageKind>,
limit: Option<i64>,
) -> Result<Vec<StoredGroupMessage>, StorageError> {
use super::schema::group_messages::dsl;

let mut query = dsl::group_messages
.filter(dsl::group_id.eq(group_id.as_ref()))
.into_boxed();

if let Some(sent_after) = sent_after {
if let Some(sent_after) = sent_after_ns {
query = query.filter(dsl::sent_at_ns.gt(sent_after));
}

if let Some(sent_before) = sent_before {
if let Some(sent_before) = sent_before_ns {
query = query.filter(dsl::sent_at_ns.lt(sent_before));
}

if let Some(kind) = kind {
query = query.filter(dsl::kind.eq(kind));
}

if let Some(limit) = limit {
query = query.limit(limit);
}

Ok(query.load::<StoredGroupMessage>(conn)?)
}

Expand Down Expand Up @@ -194,7 +200,7 @@ mod tests {
assert_eq!(count, 50);

let messages = store
.get_group_messages(&mut conn, &group.id, None, None, None)
.get_group_messages(&mut conn, &group.id, None, None, None, None)
.unwrap();
assert_eq!(messages.len(), 50);
})
Expand All @@ -214,18 +220,18 @@ mod tests {
];
assert_ok!(messages.store(&mut conn));
let message = store
.get_group_messages(&mut conn, &group.id, Some(1_000), Some(100_000), None)
.get_group_messages(&mut conn, &group.id, Some(1_000), Some(100_000), None, None)
.unwrap();
assert_eq!(message.len(), 1);
assert_eq!(message.first().unwrap().sent_at_ns, 10_000);

let messages = store
.get_group_messages(&mut conn, &group.id, None, Some(100_000), None)
.get_group_messages(&mut conn, &group.id, None, Some(100_000), None, None)
.unwrap();
assert_eq!(messages.len(), 2);

let messages = store
.get_group_messages(&mut conn, &group.id, Some(10_000), None, None)
.get_group_messages(&mut conn, &group.id, Some(10_000), None, None, None)
.unwrap();
assert_eq!(messages.len(), 2);
})
Expand Down Expand Up @@ -274,6 +280,7 @@ mod tests {
None,
None,
Some(GroupMessageKind::Application),
None,
)
.unwrap();
assert_eq!(application_messages.len(), 10);
Expand All @@ -285,6 +292,7 @@ mod tests {
None,
None,
Some(GroupMessageKind::MemberAdded),
None,
)
.unwrap();
assert_eq!(member_removed.len(), 10);
Expand All @@ -296,6 +304,7 @@ mod tests {
None,
None,
Some(GroupMessageKind::MemberRemoved),
None,
)
.unwrap();
assert_eq!(member_added.len(), 10);
Expand Down

0 comments on commit 15626cb

Please sign in to comment.