Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(soup): add channel_type filters for channels in soup (#1492)

teoff34b3114a8c
diff --git a/js/app/packages/service-clients/service-search/generated/models/channelFilters.ts b/js/app/packages/service-clients/service-search/generated/models/channelFilters.ts
index 3edebc554d..568807a18f 100644
--- a/js/app/packages/service-clients/service-search/generated/models/channelFilters.ts
+++ b/js/app/packages/service-clients/service-search/generated/models/channelFilters.ts
@@ -13,6 +13,8 @@ import type { ChannelFiltersOrgId } from './channelFiltersOrgId';
export interface ChannelFilters {
/** Channel IDs to search within. Examples: ['general']. Empty to search all accessible channels. */
channel_ids?: string[];
+ /** Channel types to filter by. Examples: ['public'], ['direct_message', 'private']. Empty to search all channel types. */
+ channel_types?: string[];
/** Filter by channel importance. None to ignore, true to pass through (no clause), false to short-circuit and return nothing. */
importance?: ChannelFiltersImportance;
/** Channel user mentions to search for. Examples: ['@username']. Empty if not filtering by mentions. */
diff --git a/js/app/packages/service-clients/service-search/openapi.json b/js/app/packages/service-clients/service-search/openapi.json
index aa63d79161..32f66fdd15 100644
--- a/js/app/packages/service-clients/service-search/openapi.json
+++ b/js/app/packages/service-clients/service-search/openapi.json
@@ -184,6 +184,13 @@
},
"description": "Channel IDs to search within. Examples: ['general']. Empty to search all accessible channels."
},
+ "channel_types": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "description": "Channel types to filter by. Examples: ['public'], ['direct_message', 'private']. Empty to search all channel types."
+ },
"importance": {
"type": ["boolean", "null"],
"description": "Filter by channel importance. None to ignore, true to pass through (no clause), false to short-circuit and return nothing."
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/channelFilters.ts b/js/app/packages/service-clients/service-storage/generated/schemas/channelFilters.ts
index 7ce749b6a7..4abc4bce21 100644
--- a/js/app/packages/service-clients/service-storage/generated/schemas/channelFilters.ts
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/channelFilters.ts
@@ -13,6 +13,8 @@ import type { ChannelFiltersOrgId } from './channelFiltersOrgId';
export interface ChannelFilters {
/** Channel IDs to search within. Examples: ['general']. Empty to search all accessible channels. */
channel_ids?: string[];
+ /** Channel types to filter by. Examples: ['public'], ['direct_message', 'private']. Empty to search all channel types. */
+ channel_types?: string[];
/** Filter by channel importance. None to ignore, true to pass through (no clause), false to short-circuit and return nothing. */
importance?: ChannelFiltersImportance;
/** Channel user mentions to search for. Examples: ['@username']. Empty if not filtering by mentions. */
diff --git a/js/app/packages/service-clients/service-storage/generated/zod.ts b/js/app/packages/service-clients/service-storage/generated/zod.ts
index 4ddc29ac02..db43a2ec70 100644
--- a/js/app/packages/service-clients/service-storage/generated/zod.ts
+++ b/js/app/packages/service-clients/service-storage/generated/zod.ts
@@ -3677,6 +3677,12 @@ export const postItemsSoupBody = zod
.describe(
"Channel IDs to search within. Examples: ['general']. Empty to search all accessible channels."
),
+ channel_types: zod
+ .array(zod.string())
+ .optional()
+ .describe(
+ "Channel types to filter by. Examples: ['public'], ['direct_message', 'private']. Empty to search all channel types."
+ ),
importance: zod
.boolean()
.nullish()
diff --git a/js/app/packages/service-clients/service-storage/openapi.json b/js/app/packages/service-clients/service-storage/openapi.json
index cd4a340660..a9b1c3feed 100644
--- a/js/app/packages/service-clients/service-storage/openapi.json
+++ b/js/app/packages/service-clients/service-storage/openapi.json
@@ -4681,6 +4681,13 @@
},
"description": "Channel IDs to search within. Examples: ['general']. Empty to search all accessible channels."
},
+ "channel_types": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "description": "Channel types to filter by. Examples: ['public'], ['direct_message', 'private']. Empty to search all channel types."
+ },
"importance": {
"type": ["boolean", "null"],
"description": "Filter by channel importance. None to ignore, true to pass through (no clause), false to short-circuit and return nothing."
diff --git a/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic.rs b/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic.rs
index c4d633abfb..e015e6f55b 100644
--- a/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic.rs
+++ b/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic.rs
@@ -92,6 +92,9 @@ fn build_channel_filter(ast: Option<&Expr<ChannelLiteral>>) -> String {
filter_ast::ExprFrame::Literal(ChannelLiteral::OrganizationId(org_id)) => {
format!("c.org_id = {org_id}")
}
+ filter_ast::ExprFrame::Literal(ChannelLiteral::ChannelType(ct)) => {
+ format!("c.channel_type = '{ct}'")
+ }
// These filters don't apply at the channel level, they're for messages
// So we return an empty string which will be filtered out
filter_ast::ExprFrame::Literal(ChannelLiteral::ThreadId(_)) => String::new(),
diff --git a/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic/tests.rs b/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic/tests.rs
index dbd5ba5f18..508c47d9f5 100644
--- a/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic/tests.rs
+++ b/rust/cloud-storage/comms/src/outbound/postgres/comms_repo/dynamic/tests.rs
@@ -232,6 +232,78 @@ async fn test_get_user_channels_dynamic_filter_by_importance(pool: Pool<sqlx::Po
}
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("channels"))
+)]
+async fn test_get_user_channels_dynamic_filter_by_channel_type(pool: Pool<sqlx::Postgres>) {
+ use filter_ast::ExpandFrame;
+ use item_filters::ChannelFilters;
+
+ let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+
+ // Filter for organization channels only
+ let channel_filters = ChannelFilters {
+ channel_types: vec!["organization".to_string()],
+ ..Default::default()
+ };
+
+ let filter_ast = ChannelFilters::expand_ast(channel_filters)
+ .unwrap()
+ .map(std::sync::Arc::new);
+
+ let params = GetChannelsRequest {
+ macro_id: user_id.into_owned(),
+ limit: Some(20),
+ query: Query::Sort(SimpleSortMethod::UpdatedAt, filter_ast),
+ }
+ .into_params();
+
+ let channels = get_user_channels_dynamic(&pool, &params).await.unwrap();
+
+ // Channels A and B are organization type
+ assert_eq!(channels.len(), 2, "Should return 2 organization channels");
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("channels"))
+)]
+async fn test_get_user_channels_dynamic_filter_by_multiple_channel_types(
+ pool: Pool<sqlx::Postgres>,
+) {
+ use filter_ast::ExpandFrame;
+ use item_filters::ChannelFilters;
+
+ let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+
+ // Filter for public and direct_message channels
+ let channel_filters = ChannelFilters {
+ channel_types: vec!["public".to_string(), "direct_message".to_string()],
+ ..Default::default()
+ };
+
+ let filter_ast = ChannelFilters::expand_ast(channel_filters)
+ .unwrap()
+ .map(std::sync::Arc::new);
+
+ let params = GetChannelsRequest {
+ macro_id: user_id.into_owned(),
+ limit: Some(20),
+ query: Query::Sort(SimpleSortMethod::UpdatedAt, filter_ast),
+ }
+ .into_params();
+
+ let channels = get_user_channels_dynamic(&pool, &params).await.unwrap();
+
+ // Channel C is public, Channel D is direct_message
+ assert_eq!(
+ channels.len(),
+ 2,
+ "Should return 2 channels (public + direct_message)"
+ );
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("channels"))
diff --git a/rust/cloud-storage/item_filters/src/ast.rs b/rust/cloud-storage/item_filters/src/ast.rs
index 2dd0a5390d..a41032d028 100644
--- a/rust/cloud-storage/item_filters/src/ast.rs
+++ b/rust/cloud-storage/item_filters/src/ast.rs
@@ -4,7 +4,7 @@
use crate::{
ChannelFilters, ChatFilters, DocumentFilters, EmailFilters, EntityFilters, ProjectFilters,
ast::{
- channel::ChannelLiteral,
+ channel::{ChannelLiteral, ChannelTypeFilter},
chat::{ChatLiteral, ChatRole},
email::EmailLiteral,
project::ProjectLiteral,
@@ -49,6 +49,9 @@ pub enum ExpandErr {
/// unknown chat type
#[error(transparent)]
ChatRoleErr(#[from] UnknownValue<ChatRole>),
+ /// unknown channel type
+ #[error(transparent)]
+ ChannelTypeErr(#[from] UnknownValue<ChannelTypeFilter>),
/// invalid uuid
#[error("Invalid uuid string: {0}")]
Uuid(#[from] uuid::Error),
diff --git a/rust/cloud-storage/item_filters/src/ast/channel.rs b/rust/cloud-storage/item_filters/src/ast/channel.rs
index 6f7b7b4530..94c61b1faf 100644
--- a/rust/cloud-storage/item_filters/src/ast/channel.rs
+++ b/rust/cloud-storage/item_filters/src/ast/channel.rs
@@ -1,9 +1,43 @@
use filter_ast::{ExpandFrame, Expr, FoldTree, TryExpandNode};
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
use serde::{Deserialize, Serialize};
+use strum::Display;
use uuid::Uuid;
-use crate::{ChannelFilters, ast::ExpandErr};
+use crate::{
+ ChannelFilters,
+ ast::{ExpandErr, ParseFromStr, UnknownValue},
+};
+
+/// the possible channel types
+#[derive(Debug, Serialize, Deserialize, Clone, Copy, Display)]
+#[strum(serialize_all = "snake_case")]
+#[serde(rename_all = "snake_case")]
+pub enum ChannelTypeFilter {
+ /// a public channel
+ Public,
+ /// an organization channel
+ Organization,
+ /// a private channel
+ Private,
+ /// a direct message channel
+ DirectMessage,
+}
+
+impl ParseFromStr for ChannelTypeFilter {
+ fn parse_from_str<T: AsRef<str>>(s: T) -> Result<Self, UnknownValue<Self>> {
+ match s.as_ref() {
+ "public" => Ok(Self::Public),
+ "organization" => Ok(Self::Organization),
+ "private" => Ok(Self::Private),
+ "direct_message" => Ok(Self::DirectMessage),
+ _ => Err(UnknownValue(
+ s.as_ref().to_string(),
+ std::marker::PhantomData,
+ )),
+ }
+ }
+}
/// the possible literal values in a channel filter ast
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -18,6 +52,8 @@ pub enum ChannelLiteral {
ChannelId(Uuid),
/// the message comes from some sender x
Sender(MacroUserIdStr<'static>),
+ /// the channel type to filter by
+ ChannelType(ChannelTypeFilter),
/// this node value filters by channel importance. false short-circuits to match nothing.
Importance(bool),
}
@@ -34,6 +70,7 @@ impl ExpandFrame<ChannelLiteral> for ChannelFilters {
org_id,
channel_ids,
sender_ids,
+ channel_types,
importance,
} = filter_request;
@@ -61,6 +98,11 @@ impl ExpandFrame<ChannelLiteral> for ChannelFilters {
.map(|s| MacroUserIdStr::parse_from_str(s).map(CowLike::into_owned))
.try_expand(|r| r.map(ChannelLiteral::Sender), Expr::or)?;
+ let channel_type_nodes = channel_types
+ .iter()
+ .map(ChannelTypeFilter::parse_from_str)
+ .try_expand(|r| r.map(ChannelLiteral::ChannelType), Expr::or)?;
+
let importance_node = importance.map(|imp| Expr::Literal(ChannelLiteral::Importance(imp)));
Ok([
@@ -69,6 +111,7 @@ impl ExpandFrame<ChannelLiteral> for ChannelFilters {
organizations,
channel_ids,
sender_ids,
+ channel_type_nodes,
importance_node,
]
.into_iter()
diff --git a/rust/cloud-storage/item_filters/src/ast/tests.rs b/rust/cloud-storage/item_filters/src/ast/tests.rs
index 535e09928b..df77eefc38 100644
--- a/rust/cloud-storage/item_filters/src/ast/tests.rs
+++ b/rust/cloud-storage/item_filters/src/ast/tests.rs
@@ -251,3 +251,82 @@ fn it_expands_email_thread_ids_with_sender() {
// Should be AND of sender and thread_id
assert!(json.get("&").is_some());
}
+
+#[test]
+fn it_expands_channel_types() {
+ let f = EntityFilters {
+ channel_filters: crate::ChannelFilters {
+ channel_types: vec!["public".to_string()],
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+
+ let ast = Arc::into_inner(
+ EntityFilterAst::new_from_filters(f)
+ .unwrap()
+ .unwrap()
+ .channel_filter
+ .unwrap(),
+ )
+ .unwrap();
+
+ let json = serde_json::to_value(ast).unwrap();
+ let exp = json!({
+ "l": {
+ "ChannelType": "public"
+ }
+ });
+
+ assert_eq!(json, exp);
+}
+
+#[test]
+fn it_expands_multiple_channel_types() {
+ let f = EntityFilters {
+ channel_filters: crate::ChannelFilters {
+ channel_types: vec!["public".to_string(), "direct_message".to_string()],
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+
+ let ast = Arc::into_inner(
+ EntityFilterAst::new_from_filters(f)
+ .unwrap()
+ .unwrap()
+ .channel_filter
+ .unwrap(),
+ )
+ .unwrap();
+
+ let json = serde_json::to_value(ast).unwrap();
+ // Should be OR of two channel types
+ assert!(json.get("|").is_some());
+}
+
+#[test]
+fn it_expands_channel_type_with_channel_id() {
+ let channel_id = Uuid::new_v4();
+ let f = EntityFilters {
+ channel_filters: crate::ChannelFilters {
+ channel_ids: vec![channel_id.to_string()],
+ channel_types: vec!["private".to_string()],
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+
+ let ast = Arc::into_inner(
+ EntityFilterAst::new_from_filters(f)
+ .unwrap()
+ .unwrap()
+ .channel_filter
+ .unwrap(),
+ )
+ .unwrap();
+
+ let json = serde_json::to_value(ast).unwrap();
+ // Should be AND of channel_id and channel_type
+ assert!(json.get("&").is_some());
+}
diff --git a/rust/cloud-storage/item_filters/src/lib.rs b/rust/cloud-storage/item_filters/src/lib.rs
index 9938ccb600..16d765b98e 100644
--- a/rust/cloud-storage/item_filters/src/lib.rs
+++ b/rust/cloud-storage/item_filters/src/lib.rs
@@ -172,6 +172,10 @@ pub struct ChannelFilters {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub sender_ids: Vec<String>,
+ /// Channel types to filter by. Examples: ['public'], ['direct_message', 'private']. Empty to search all channel types.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub channel_types: Vec<String>,
+
/// Filter by channel importance. None to ignore, true to pass through (no clause), false to short-circuit and return nothing.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub importance: Option<bool>,
@@ -185,6 +189,7 @@ impl IsEmpty for ChannelFilters {
org_id,
channel_ids,
sender_ids,
+ channel_types,
importance,
} = self;
thread_ids.is_empty()
@@ -192,6 +197,7 @@ impl IsEmpty for ChannelFilters {
&& org_id.is_none()
&& channel_ids.is_empty()
&& sender_ids.is_empty()
+ && channel_types.is_empty()
&& importance.is_none()
}
}