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(channels): support latest_activity messages filter (#2736)

teoe5727e4f956d
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/channelMessageFilters.ts b/js/app/packages/service-clients/service-storage/generated/schemas/channelMessageFilters.ts
index 5c18e8f755..3b54f363fa 100644
--- a/js/app/packages/service-clients/service-storage/generated/schemas/channelMessageFilters.ts
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/channelMessageFilters.ts
@@ -4,11 +4,16 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/
+import type { ChannelMessageFiltersLastActivity } from './channelMessageFiltersLastActivity';
/**
* Filters for channel message queries.
*/
export interface ChannelMessageFilters {
+ /** When set, only return top-level messages that have activity after this
+timestamp. Activity means either the message itself was created after
+this time, or a thread reply was created after this time. */
+ last_activity?: ChannelMessageFiltersLastActivity;
/** When non-empty, only return messages with these IDs. */
message_ids?: string[];
}
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/channelMessageFiltersLastActivity.ts b/js/app/packages/service-clients/service-storage/generated/schemas/channelMessageFiltersLastActivity.ts
new file mode 100644
index 0000000000..0b368c1809
--- /dev/null
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/channelMessageFiltersLastActivity.ts
@@ -0,0 +1,13 @@
+/**
+ * Generated by orval v7.13.0 🍺
+ * Do not edit manually.
+ * document_storage_service
+ * OpenAPI spec version: 0.1.0
+ */
+
+/**
+ * When set, only return top-level messages that have activity after this
+timestamp. Activity means either the message itself was created after
+this time, or a thread reply was created after this time.
+ */
+export type ChannelMessageFiltersLastActivity = string | null;
diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts b/js/app/packages/service-clients/service-storage/generated/schemas/index.ts
index 710105f935..30862d5d82 100644
--- a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts
+++ b/js/app/packages/service-clients/service-storage/generated/schemas/index.ts
@@ -99,6 +99,7 @@ export * from './channelFiltersTeamId';
export * from './channelMessage';
export * from './channelMessageDeletedAt';
export * from './channelMessageFilters';
+export * from './channelMessageFiltersLastActivity';
export * from './channelMessageThreadId';
export * from './channelName';
export * from './channelOrgId';
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 f747d48262..8a22604297 100644
--- a/js/app/packages/service-clients/service-storage/generated/zod.ts
+++ b/js/app/packages/service-clients/service-storage/generated/zod.ts
@@ -1354,6 +1354,13 @@ export const postChannelMessagesQueryParams = zod.object({
export const postChannelMessagesBody = zod
.object({
+ last_activity: zod
+ .string()
+ .datetime({})
+ .nullish()
+ .describe(
+ 'When set, only return top-level messages that have activity after this\ntimestamp. Activity means either the message itself was created after\nthis time, or a thread reply was created after this time.'
+ ),
message_ids: zod
.array(zod.string().uuid())
.optional()
diff --git a/js/app/packages/service-clients/service-storage/openapi.json b/js/app/packages/service-clients/service-storage/openapi.json
index 77b82280d5..cb97f21c1f 100644
--- a/js/app/packages/service-clients/service-storage/openapi.json
+++ b/js/app/packages/service-clients/service-storage/openapi.json
@@ -6461,6 +6461,11 @@
"type": "object",
"description": "Filters for channel message queries.",
"properties": {
+ "last_activity": {
+ "type": ["string", "null"],
+ "format": "date-time",
+ "description": "When set, only return top-level messages that have activity after this\ntimestamp. Activity means either the message itself was created after\nthis time, or a thread reply was created after this time."
+ },
"message_ids": {
"type": "array",
"items": {
diff --git a/rust/cloud-storage/channels/.sqlx/query-18e88dcc063b70409ffbba9265b382056a22e0fceea057bd654a8d777fd8657d.json b/rust/cloud-storage/channels/.sqlx/query-857e3e97e49256afa01fd4e85422b8da87b62a601b26889c719cd8b2ec7de5c6.json
similarity index 74%
rename from rust/cloud-storage/channels/.sqlx/query-18e88dcc063b70409ffbba9265b382056a22e0fceea057bd654a8d777fd8657d.json
rename to rust/cloud-storage/channels/.sqlx/query-857e3e97e49256afa01fd4e85422b8da87b62a601b26889c719cd8b2ec7de5c6.json
index 72f2fc1c87..bbcf3ebde3 100644
--- a/rust/cloud-storage/channels/.sqlx/query-18e88dcc063b70409ffbba9265b382056a22e0fceea057bd654a8d777fd8657d.json
+++ b/rust/cloud-storage/channels/.sqlx/query-857e3e97e49256afa01fd4e85422b8da87b62a601b26889c719cd8b2ec7de5c6.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NOT NULL AND (m.created_at, m.id) > ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n ORDER BY m.created_at ASC, m.id ASC\n LIMIT $4\n ",
+ "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NOT NULL AND (m.created_at, m.id) > ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n AND ($6::timestamptz IS NULL OR (\n m.created_at > $6\n OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id\n AND r.deleted_at IS NULL\n AND r.created_at > $6\n )\n ))\n ORDER BY m.created_at ASC, m.id ASC\n LIMIT $4\n ",
"describe": {
"columns": [
{
@@ -50,7 +50,8 @@
"Timestamptz",
"Uuid",
"Int8",
- "UuidArray"
+ "UuidArray",
+ "Timestamptz"
]
},
"nullable": [
@@ -64,5 +65,5 @@
null
]
},
- "hash": "18e88dcc063b70409ffbba9265b382056a22e0fceea057bd654a8d777fd8657d"
+ "hash": "857e3e97e49256afa01fd4e85422b8da87b62a601b26889c719cd8b2ec7de5c6"
}
diff --git a/rust/cloud-storage/channels/.sqlx/query-bcb3ab4393f39aad3c3e76f634585b43fedb48da0081ea896bf39a7c18f261bd.json b/rust/cloud-storage/channels/.sqlx/query-ead66ab8a55df78540bbffc0a015014c2d067de3deb1f9b155cc87376277798f.json
similarity index 74%
rename from rust/cloud-storage/channels/.sqlx/query-bcb3ab4393f39aad3c3e76f634585b43fedb48da0081ea896bf39a7c18f261bd.json
rename to rust/cloud-storage/channels/.sqlx/query-ead66ab8a55df78540bbffc0a015014c2d067de3deb1f9b155cc87376277798f.json
index 6bf4125490..75eabe0696 100644
--- a/rust/cloud-storage/channels/.sqlx/query-bcb3ab4393f39aad3c3e76f634585b43fedb48da0081ea896bf39a7c18f261bd.json
+++ b/rust/cloud-storage/channels/.sqlx/query-ead66ab8a55df78540bbffc0a015014c2d067de3deb1f9b155cc87376277798f.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NULL OR (m.created_at, m.id) < ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n ORDER BY m.created_at DESC, m.id DESC\n LIMIT $4\n ",
+ "query": "\n SELECT\n m.id,\n m.channel_id,\n m.sender_id,\n m.content,\n m.created_at,\n m.updated_at,\n m.edited_at::timestamptz AS \"edited_at?\",\n m.deleted_at::timestamptz AS \"deleted_at?\"\n FROM comms_messages m\n WHERE m.channel_id = $1\n AND m.thread_id IS NULL\n AND (m.deleted_at IS NULL OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id AND r.deleted_at IS NULL\n ))\n AND ($2::timestamptz IS NULL OR (m.created_at, m.id) < ($2, $3))\n AND ($5::uuid[] IS NULL OR m.id = ANY($5))\n AND ($6::timestamptz IS NULL OR (\n m.created_at > $6\n OR EXISTS (\n SELECT 1 FROM comms_messages r\n WHERE r.thread_id = m.id\n AND r.deleted_at IS NULL\n AND r.created_at > $6\n )\n ))\n ORDER BY m.created_at DESC, m.id DESC\n LIMIT $4\n ",
"describe": {
"columns": [
{
@@ -50,7 +50,8 @@
"Timestamptz",
"Uuid",
"Int8",
- "UuidArray"
+ "UuidArray",
+ "Timestamptz"
]
},
"nullable": [
@@ -64,5 +65,5 @@
null
]
},
- "hash": "bcb3ab4393f39aad3c3e76f634585b43fedb48da0081ea896bf39a7c18f261bd"
+ "hash": "ead66ab8a55df78540bbffc0a015014c2d067de3deb1f9b155cc87376277798f"
}
diff --git a/rust/cloud-storage/channels/src/domain/models.rs b/rust/cloud-storage/channels/src/domain/models.rs
index d49db0004b..a7dd9fd3d5 100644
--- a/rust/cloud-storage/channels/src/domain/models.rs
+++ b/rust/cloud-storage/channels/src/domain/models.rs
@@ -19,6 +19,11 @@ pub struct ChannelMessageFilters {
/// When non-empty, only return messages with these IDs.
#[serde(default)]
pub message_ids: Vec<Uuid>,
+ /// When set, only return top-level messages that have activity after this
+ /// timestamp. Activity means either the message itself was created after
+ /// this time, or a thread reply was created after this time.
+ #[serde(default)]
+ pub last_activity: Option<DateTime<Utc>>,
}
/// Direction for cursor-based message pagination.
diff --git a/rust/cloud-storage/channels/src/inbound/axum_router/test.rs b/rust/cloud-storage/channels/src/inbound/axum_router/test.rs
index ccff754cea..63296ed20b 100644
--- a/rust/cloud-storage/channels/src/inbound/axum_router/test.rs
+++ b/rust/cloud-storage/channels/src/inbound/axum_router/test.rs
@@ -882,6 +882,7 @@ async fn post_messages_empty_body_uses_default_filters() {
let captured = svc.captured.lock().unwrap().clone().unwrap();
assert!(captured.message_ids.is_empty());
+ assert!(captured.last_activity.is_none());
}
#[tokio::test]
@@ -912,6 +913,39 @@ async fn post_messages_forwards_message_ids_filter() {
assert_eq!(captured.message_ids, vec![id_a, id_b]);
}
+#[tokio::test]
+async fn post_messages_forwards_last_activity_filter() {
+ let svc = CapturingService::new();
+ let router = channels_router(ChannelsRouterState::new(
+ svc.clone(),
+ TestAccessService::allow(),
+ ))
+ .layer(user_extension());
+
+ let channel_id = Uuid::new_v4();
+ let body = serde_json::json!({ "last_activity": "2024-06-01T12:00:00Z" }).to_string();
+
+ let request = Request::builder()
+ .method("POST")
+ .uri(format!("/{channel_id}/messages"))
+ .header("content-type", "application/json")
+ .body(axum::body::Body::from(body))
+ .unwrap();
+
+ let res = router.oneshot(request).await.unwrap();
+ assert_eq!(res.status(), StatusCode::OK);
+
+ let captured = svc.captured.lock().unwrap().clone().unwrap();
+ assert!(captured.last_activity.is_some());
+ let ts = captured.last_activity.unwrap();
+ assert_eq!(
+ ts,
+ chrono::DateTime::parse_from_rfc3339("2024-06-01T12:00:00Z")
+ .unwrap()
+ .with_timezone(&chrono::Utc)
+ );
+}
+
#[tokio::test]
async fn post_messages_rejects_oversized_filter_list() {
let router = channels_router(ChannelsRouterState::new(
diff --git a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs
index fdc1c88560..d0ef72b081 100644
--- a/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs
+++ b/rust/cloud-storage/channels/src/outbound/pg_channels_repo.rs
@@ -134,6 +134,7 @@ impl ChannelMessagesRepo for PgChannelMessagesRepo {
} else {
Some(&filters.message_ids)
};
+ let last_activity = filters.last_activity;
let (rows, has_more_newer) = match direction {
MessagePageDirection::Older => {
@@ -158,6 +159,15 @@ impl ChannelMessagesRepo for PgChannelMessagesRepo {
))
AND ($2::timestamptz IS NULL OR (m.created_at, m.id) < ($2, $3))
AND ($5::uuid[] IS NULL OR m.id = ANY($5))
+ AND ($6::timestamptz IS NULL OR (
+ m.created_at > $6
+ OR EXISTS (
+ SELECT 1 FROM comms_messages r
+ WHERE r.thread_id = m.id
+ AND r.deleted_at IS NULL
+ AND r.created_at > $6
+ )
+ ))
ORDER BY m.created_at DESC, m.id DESC
LIMIT $4
"#,
@@ -166,6 +176,7 @@ impl ChannelMessagesRepo for PgChannelMessagesRepo {
cursor_id,
limit_i64,
message_ids_filter as Option<&[Uuid]>,
+ last_activity,
)
.fetch_all(&self.pool)
.await?;
@@ -194,6 +205,15 @@ impl ChannelMessagesRepo for PgChannelMessagesRepo {
))
AND ($2::timestamptz IS NOT NULL AND (m.created_at, m.id) > ($2, $3))
AND ($5::uuid[] IS NULL OR m.id = ANY($5))
+ AND ($6::timestamptz IS NULL OR (
+ m.created_at > $6
+ OR EXISTS (
+ SELECT 1 FROM comms_messages r
+ WHERE r.thread_id = m.id
+ AND r.deleted_at IS NULL
+ AND r.created_at > $6
+ )
+ ))
ORDER BY m.created_at ASC, m.id ASC
LIMIT $4
"#,
@@ -202,6 +222,7 @@ impl ChannelMessagesRepo for PgChannelMessagesRepo {
cursor_id,
limit_i64 + 1,
message_ids_filter as Option<&[Uuid]>,
+ last_activity,
)
.fetch_all(&self.pool)
.await?;
diff --git a/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs b/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs
index 86bcb1c1de..c73909949d 100644
--- a/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs
+++ b/rust/cloud-storage/channels/src/outbound/pg_channels_repo/tests.rs
@@ -8,6 +8,7 @@ use uuid::Uuid;
const NO_FILTERS: ChannelMessageFilters = ChannelMessageFilters {
message_ids: Vec::new(),
+ last_activity: None,
};
const CH1: Uuid = Uuid::from_u128(0x00000000_0000_0000_0000_000000000c01);
@@ -246,6 +247,7 @@ async fn top_level_message_ids_filter_limits_to_subset(pool: Pool<Postgres>) ->
let repo = repo(pool);
let filters = ChannelMessageFilters {
message_ids: vec![MSG1, MSG3],
+ ..Default::default()
};
let result = repo
.get_top_level_messages(
@@ -700,3 +702,101 @@ async fn around_newest_message_has_no_after(pool: Pool<Postgres>) -> anyhow::Res
assert!(after.is_empty(), "nothing newer than MSG3");
Ok(())
}
+
+// -- last_activity filter -----------------------------------------------------
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn last_activity_filters_by_message_created_at(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let repo = repo(pool);
+ // msg3 created at 12:00 — only it was created after 11:30
+ let filters = ChannelMessageFilters {
+ last_activity: Some(
+ chrono::DateTime::parse_from_rfc3339("2024-01-01T11:30:00Z")
+ .unwrap()
+ .with_timezone(&chrono::Utc),
+ ),
+ ..Default::default()
+ };
+ let result = repo
+ .get_top_level_messages(
+ CH1,
+ &Query::Sort(CreatedAt, ()),
+ MessagePageDirection::Older,
+ 50,
+ &filters,
+ )
+ .await?;
+
+ let ids: Vec<Uuid> = result.rows.iter().map(|r| r.id).collect();
+ assert_eq!(ids, vec![MSG3]);
+ Ok(())
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn last_activity_includes_messages_with_recent_thread_replies(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let repo = repo(pool);
+ // msg1 created at 10:00 but has replies up to 10:04.
+ // msg2 (deleted) has reply at 11:01.
+ // msg3 created at 12:00.
+ // last_activity = 10:05 excludes msg1 (created 10:00, last reply 10:04),
+ // but includes msg2 (reply at 11:01) and msg3 (created 12:00).
+ let filters = ChannelMessageFilters {
+ last_activity: Some(
+ chrono::DateTime::parse_from_rfc3339("2024-01-01T10:05:00Z")
+ .unwrap()
+ .with_timezone(&chrono::Utc),
+ ),
+ ..Default::default()
+ };
+ let result = repo
+ .get_top_level_messages(
+ CH1,
+ &Query::Sort(CreatedAt, ()),
+ MessagePageDirection::Older,
+ 50,
+ &filters,
+ )
+ .await?;
+
+ let ids: Vec<Uuid> = result.rows.iter().map(|r| r.id).collect();
+ assert_eq!(ids, vec![MSG3, MSG2]);
+ Ok(())
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn last_activity_combined_with_message_ids(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let repo = repo(pool);
+ // Ask for msg1 and msg3, but with last_activity that excludes msg1
+ let filters = ChannelMessageFilters {
+ message_ids: vec![MSG1, MSG3],
+ last_activity: Some(
+ chrono::DateTime::parse_from_rfc3339("2024-01-01T11:00:00Z")
+ .unwrap()
+ .with_timezone(&chrono::Utc),
+ ),
+ };
+ let result = repo
+ .get_top_level_messages(
+ CH1,
+ &Query::Sort(CreatedAt, ()),
+ MessagePageDirection::Older,
+ 50,
+ &filters,
+ )
+ .await?;
+
+ let ids: Vec<Uuid> = result.rows.iter().map(|r| r.id).collect();
+ assert_eq!(ids, vec![MSG3]);
+ Ok(())
+}