Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(search): Have channel search use macrodb to filter org id (#103)
Will Hutchinson19638d0fb5f0
diff --git a/rust/cloud-storage/comms_service_client/src/user_channels.rs b/rust/cloud-storage/comms_service_client/src/user_channels.rsindex 2bb7150d70..5d691fdd2e 100644--- a/rust/cloud-storage/comms_service_client/src/user_channels.rs+++ b/rust/cloud-storage/comms_service_client/src/user_channels.rs@@ -8,7 +8,7 @@ impl CommsServiceClient {&self,user_id: &str,user_org_id: Option<i32>,- ) -> Result<Vec<String>, ClientError> {+ ) -> Result<Vec<uuid::Uuid>, ClientError> {let url = if let Some(user_org_id) = user_org_id {format!("{}/internal/user_channels/{}?org_id={}",@@ -20,7 +20,7 @@ impl CommsServiceClient {let response = self.client.get(url).send().await.map_client_error().await?;- let result = response.json::<Vec<String>>().await.map_err(|e| {+ let result = response.json::<Vec<uuid::Uuid>>().await.map_err(|e| {ClientError::Generic(anyhow::anyhow!("unable to parse response from get_user_channel_ids: {}",e.to_string()diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-8b640c24a0a71ba83db60c1bd7a273aae232cbb8fab78cf029c66ec1e8576b61.json b/rust/cloud-storage/macro_db_client/.sqlx/query-8b640c24a0a71ba83db60c1bd7a273aae232cbb8fab78cf029c66ec1e8576b61.jsonnew file mode 100644index 0000000000..238e214685--- /dev/null+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-8b640c24a0a71ba83db60c1bd7a273aae232cbb8fab78cf029c66ec1e8576b61.json@@ -0,0 +1,23 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n c.id\n FROM\n comms_channels c\n WHERE\n c.id = ANY($1)\n AND c.channel_type = 'organization'\n AND c.org_id = $2\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ }+ ],+ "parameters": {+ "Left": [+ "UuidArray",+ "Int8"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "8b640c24a0a71ba83db60c1bd7a273aae232cbb8fab78cf029c66ec1e8576b61"+}diff --git a/rust/cloud-storage/macro_db_client/src/items/filter.rs b/rust/cloud-storage/macro_db_client/src/items/filter.rsindex 34c0628916..e25c093788 100644--- a/rust/cloud-storage/macro_db_client/src/items/filter.rs+++ b/rust/cloud-storage/macro_db_client/src/items/filter.rs@@ -5,33 +5,11 @@ use model::item::ShareableItemType;use crate::projects::get_project::get_sub_items::bulk_get_all_sub_project_ids;-/// Given a list of document ids and a list of file types, this will-/// return a subset list of items that are of the provided file types.-#[tracing::instrument(skip(db), err)]-pub async fn filter_documents_by_file_types(- db: &sqlx::PgPool,- documents: &[String],- file_types: &[String],-) -> anyhow::Result<Vec<String>> {- let documents = sqlx::query!(- r#"- SELECT- d.id- FROM- "Document" d- WHERE- d.id = ANY($1)- AND d."fileType" = ANY($2)- "#,- documents,- file_types,- )- .map(|row| row.id)- .fetch_all(db)- .await?;+mod channel;+mod document;- Ok(documents)-}+pub use channel::*;+pub use document::*;/// Given a list of item ids, the item type and a list of project_ids, this will/// return a subset list of items that are within the provided project ids and their sub-projects.diff --git a/rust/cloud-storage/macro_db_client/src/items/filter/channel.rs b/rust/cloud-storage/macro_db_client/src/items/filter/channel.rsnew file mode 100644index 0000000000..5c1addb9d6--- /dev/null+++ b/rust/cloud-storage/macro_db_client/src/items/filter/channel.rs@@ -0,0 +1,30 @@+//! This module contains db queries to filter out items by channel ids++/// Given a list of channel ids and an org id, this will+/// return all organization channel ids for the provided org id.+#[tracing::instrument(skip(db), err)]+pub async fn filter_channels_by_org_id(+ db: &sqlx::PgPool,+ channel_ids: &[uuid::Uuid],+ org_id: i64,+) -> anyhow::Result<Vec<uuid::Uuid>> {+ let channel_ids = sqlx::query!(+ r#"+ SELECT+ c.id+ FROM+ comms_channels c+ WHERE+ c.id = ANY($1)+ AND c.channel_type = 'organization'+ AND c.org_id = $2+ "#,+ channel_ids,+ org_id,+ )+ .map(|row| row.id)+ .fetch_all(db)+ .await?;++ Ok(channel_ids)+}diff --git a/rust/cloud-storage/macro_db_client/src/items/filter/document.rs b/rust/cloud-storage/macro_db_client/src/items/filter/document.rsnew file mode 100644index 0000000000..6442458ad2--- /dev/null+++ b/rust/cloud-storage/macro_db_client/src/items/filter/document.rs@@ -0,0 +1,30 @@+//! This module contains db queries to filter out a list of provided documents and only return those+//! matching the criteria++/// Given a list of document ids and a list of file types, this will+/// return a subset list of items that are of the provided file types.+#[tracing::instrument(skip(db), err)]+pub async fn filter_documents_by_file_types(+ db: &sqlx::PgPool,+ documents: &[String],+ file_types: &[String],+) -> anyhow::Result<Vec<String>> {+ let documents = sqlx::query!(+ r#"+ SELECT+ d.id+ FROM+ "Document" d+ WHERE+ d.id = ANY($1)+ AND d."fileType" = ANY($2)+ "#,+ documents,+ file_types,+ )+ .map(|row| row.id)+ .fetch_all(db)+ .await?;++ Ok(documents)+}diff --git a/rust/cloud-storage/opensearch_client/examples/channel_message_search_args.rs b/rust/cloud-storage/opensearch_client/examples/channel_message_search_args.rsindex 5c448fb688..ffd16c01c3 100644--- a/rust/cloud-storage/opensearch_client/examples/channel_message_search_args.rs+++ b/rust/cloud-storage/opensearch_client/examples/channel_message_search_args.rs@@ -8,7 +8,6 @@ fn main() {page: 1,page_size: 20,match_type: "exact".to_string(),- org_id: Some(42),thread_ids: vec!["thread1".to_string(), "thread2".to_string()],mentions: vec!["@alice".to_string(), "@bob".to_string()],..Default::default()diff --git a/rust/cloud-storage/opensearch_client/src/search/channels.rs b/rust/cloud-storage/opensearch_client/src/search/channels.rsindex ac9a990ea9..73c0a3daf2 100644--- a/rust/cloud-storage/opensearch_client/src/search/channels.rs+++ b/rust/cloud-storage/opensearch_client/src/search/channels.rs@@ -66,7 +66,6 @@ impl SearchQueryConfig for ChannelMessageSearchConfig {#[derive(Default)]struct ChannelMessageQueryBuilder {inner: SearchQueryBuilder<ChannelMessageSearchConfig>,- org_id: Option<i64>,thread_ids: Vec<String>,mentions: Vec<String>,channel_ids: Vec<String>,@@ -81,11 +80,6 @@ impl ChannelMessageQueryBuilder {}}- pub fn org_id(mut self, org_id: Option<i64>) -> Self {- self.org_id = org_id;- self- }-pub fn thread_ids(mut self, thread_ids: Vec<String>) -> Self {self.thread_ids = thread_ids;self@@ -124,11 +118,6 @@ impl ChannelMessageQueryBuilder {// CUSTOM ATTRIBUTES SECTION- // Add org_id to must clause if provided- if let Some(org_id) = self.org_id {- bool_query.must(QueryType::term("org_id", org_id));- }-// Add thread_ids to must clause if providedif !self.thread_ids.is_empty() {bool_query.must(QueryType::terms("thread_id", self.thread_ids));@@ -167,7 +156,6 @@ pub struct ChannelMessageSearchArgs {pub page: u32,pub page_size: u32,pub match_type: String,- pub org_id: Option<i64>,pub thread_ids: Vec<String>,pub mentions: Vec<String>,pub sender_ids: Vec<String>,@@ -190,7 +178,6 @@ impl ChannelMessageSearchArgs {.collapse(self.collapse).ids_only(self.ids_only).sender_ids(self.sender_ids)- .org_id(self.org_id).build_search_request()?.to_json())}diff --git a/rust/cloud-storage/opensearch_client/src/search/channels/test.rs b/rust/cloud-storage/opensearch_client/src/search/channels/test.rsindex b2ac6c229d..ceb68044c9 100644--- a/rust/cloud-storage/opensearch_client/src/search/channels/test.rs+++ b/rust/cloud-storage/opensearch_client/src/search/channels/test.rs@@ -11,7 +11,6 @@ fn test_build_search_request() -> anyhow::Result<()> {.search_on(SearchOn::Content).collapse(true).ids(vec!["id1".to_string(), "id2".to_string()])- .org_id(Some(123)).thread_ids(vec!["thread1".to_string(), "thread2".to_string()]).mentions(vec!["mention1".to_string(), "mention2".to_string()]).sender_ids(vec!["sender1".to_string(), "sender2".to_string()]);@@ -34,11 +33,6 @@ fn test_build_search_request() -> anyhow::Result<()> {"content": "test"}},- {- "term": {- "org_id": 123- }- },{"terms": {"thread_id": ["thread1", "thread2"]diff --git a/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rs b/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rsindex cad7b0c141..273fb4a53b 100644--- a/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rs+++ b/rust/cloud-storage/search_service/src/api/search/simple/simple_channel.rs@@ -94,36 +94,46 @@ pub(in crate::api::search) async fn search_channels(return Err(SearchError::NoQueryOrTermsProvided);};+ // Get all channel ids for the userlet channel_ids = ctx.comms_service_client.get_user_channel_ids(user_id, organization_id).await.map_err(|e| SearchError::InternalError(e.into()))?;+ // If the user has no channels, return an empty response+ if channel_ids.is_empty() {+ return Ok(vec![]);+ }+let filters = req.filters.unwrap_or_default();- let mut ids_only = false;- // if a request explicitly supplies channels, make sure they are ones- // the user has access to, and tell search to explictly search over these channels+ // filter through specific channel ids if providedlet channel_ids = if !filters.channel_ids.is_empty() {- ids_only = true;- let available_ids: HashSet<String> = channel_ids.into_iter().collect();- filters- .channel_ids+ let available_ids: HashSet<String> = filters.channel_ids.into_iter().collect();++ channel_ids.into_iter()- .filter(|id| available_ids.contains(id))+ .filter(|id| available_ids.contains(&id.to_string())).collect()} else {channel_ids};+ // filter through org_id if provided+ let channel_ids = if let Some(org_id) = filters.org_id {+ macro_db_client::items::filter::filter_channels_by_org_id(&ctx.db, &channel_ids, org_id)+ .await?+ } else {+ channel_ids+ };+let results = ctx.opensearch_client.search_channel_messages(ChannelMessageSearchArgs {terms,user_id: user_id.to_string(),- channel_ids,- org_id: filters.org_id,+ channel_ids: channel_ids.iter().map(|c| c.to_string()).collect(),thread_ids: filters.thread_ids,mentions: filters.mentions,page,@@ -132,7 +142,7 @@ pub(in crate::api::search) async fn search_channels(sender_ids: filters.sender_ids,search_on: req.search_on.into(),collapse: req.collapse.unwrap_or(false),- ids_only,+ ids_only: true, // For channel message search, we always want to search over the channel ids only}).await.map_err(SearchError::Search)?;