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
{
"db_name": "PostgreSQL",
"query": "\n WITH requested AS (\n SELECT entity_id, entity_type\n FROM unnest($2::text[], $3::text[]) AS entities(entity_id, entity_type)\n ),\n -- Scope uses all active notifications, not just the requested states: a\n -- seen reply still assigns an unseen root send to its thread row.\n -- Build scope evidence once per batch, never rescan history per send.\n channel_scopes AS MATERIALIZED (\n SELECT DISTINCT n.event_item_id AS channel_id,\n CASE WHEN n.notification_event_type = 'channel_mention'\n THEN COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n END AS mentioned_message_id,\n COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') AS thread_id\n FROM requested\n JOIN user_notification un\n ON un.user_id = $1 AND un.deleted_at IS NULL\n AND un.event_item_type = 'channel' AND un.event_item_id = requested.entity_id\n AND un.state = ANY($9::notification_state[])\n AND un.notification_event_type IN ('channel_mention', 'channel_message_reply')\n JOIN notification n ON n.id = un.notification_id\n WHERE $8::bool AND requested.entity_type = 'channel'\n )\n SELECT requested.entity_id AS \"requested_id!\",\n requested.entity_type AS \"requested_type!\",\n matched.notification_id AS \"notification_id!\",\n matched.event_item_id AS \"event_item_id!\",\n matched.event_item_type AS \"event_item_type!\",\n matched.notification_event_type AS \"notification_event_type!\",\n matched.sent AS \"sent!\",\n matched.state AS \"state!: NotificationState\",\n matched.created_at AS \"created_at!\",\n matched.viewed_at,\n matched.notification_metadata,\n matched.sender_id\n FROM requested\n CROSS JOIN LATERAL (\n SELECT un.notification_id, n.event_item_id, n.event_item_type,\n n.notification_event_type, un.sent, un.state,\n un.created_at::timestamptz AS created_at,\n un.seen_at::timestamptz AS viewed_at,\n n.metadata AS notification_metadata, n.sender_id\n FROM (\n SELECT viewer_match.*\n FROM (\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.deleted_at IS NULL\n AND un.state = ANY($4::notification_state[])\n AND un.event_item_type = requested.entity_type\n AND un.event_item_id = requested.entity_id\n UNION\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM notification n\n CROSS JOIN LATERAL (\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.notification_id = n.id\n AND un.deleted_at IS NULL AND un.state = ANY($4::notification_state[])\n -- Probe the viewer's row per notification: a plain join\n -- can scan the viewer's whole history for each entity.\n OFFSET 0\n ) un\n WHERE n.secondary_event_item_type = requested.entity_type\n AND n.secondary_event_item_id = requested.entity_id\n UNION\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM notification n\n CROSS JOIN LATERAL (\n SELECT un.notification_id, un.sent, un.state, un.created_at, un.seen_at\n FROM user_notification un\n WHERE un.user_id = $1 AND un.notification_id = n.id\n AND un.deleted_at IS NULL AND un.state = ANY($4::notification_state[])\n OFFSET 0\n ) un\n WHERE requested.entity_type = 'channel_message'\n AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id', '') = requested.entity_id\n ) viewer_match\n ORDER BY viewer_match.created_at DESC, viewer_match.notification_id DESC\n -- With no filter on the notification row, the newest matches\n -- are the result, so limit before reading notification rows.\n LIMIT CASE WHEN cardinality($5::text[]) = 0 AND NOT $7::bool AND NOT $8::bool THEN $6::bigint END\n ) un\n JOIN notification n ON n.id = un.notification_id\n WHERE (cardinality($5::text[]) = 0 OR n.notification_event_type = ANY($5))\n AND (NOT $7::bool OR (\n n.notification_event_type IN ('channel_message_send', 'channel_mention', 'document_mention', 'channel_access_requested')\n AND COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') IS NULL\n ))\n AND (NOT $8::bool OR requested.entity_type <> 'channel' OR (\n n.notification_event_type IN ('document_mention', 'channel_access_requested')\n OR (n.notification_event_type IN ('channel_message_send', 'channel_message_reply') AND (\n -- Mention stacks consume the mention, not its duplicate send/reply.\n EXISTS (\n SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id\n AND scope.mentioned_message_id = COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n )\n OR (n.notification_event_type = 'channel_message_send' AND NOT EXISTS (\n SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id AND scope.thread_id <> ''\n AND scope.thread_id = COALESCE(n.metadata->>'messageId', n.metadata->>'message_id')\n ))\n OR (n.notification_event_type = 'channel_message_reply'\n AND NULLIF(COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id'), '') IS NULL)\n ))\n OR (n.notification_event_type = 'channel_mention' AND (\n COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') = ''\n OR (COALESCE(n.metadata->>'threadId', n.metadata->>'thread_id') IS NULL\n AND COALESCE(n.metadata->>'messageId', n.metadata->>'message_id') = ''\n AND EXISTS (SELECT 1 FROM channel_scopes scope\n WHERE scope.channel_id = n.event_item_id AND scope.thread_id = ''))\n ))\n ))\n ORDER BY un.created_at DESC, un.notification_id DESC\n LIMIT $6\n ) matched\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "requested_id!",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "requested_type!",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "notification_id!",
"type_info": "Uuid"
},
{
"ordinal": 3,
"name": "event_item_id!",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "event_item_type!",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "notification_event_type!",
"type_info": "Varchar"
},
{
"ordinal": 6,
"name": "sent!",
"type_info": "Bool"
},
{
"ordinal": 7,
"name": "state!: NotificationState",
"type_info": {
"Custom": {
"name": "notification_state",
"kind": {
"Enum": [
"unseen",
"seen",
"done"
]
}
}
}
},
{
"ordinal": 8,
"name": "created_at!",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "viewed_at",
"type_info": "Timestamptz"
},
{
"ordinal": 10,
"name": "notification_metadata",
"type_info": "Jsonb"
},
{
"ordinal": 11,
"name": "sender_id",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Text",
"TextArray",
"TextArray",
{
"Custom": {
"name": "notification_state[]",
"kind": {
"Array": {
"Custom": {
"name": "notification_state",
"kind": {
"Enum": [
"unseen",
"seen",
"done"
]
}
}
}
}
}
},
"TextArray",
"Int8",
"Bool",
"Bool",
{
"Custom": {
"name": "notification_state[]",
"kind": {
"Array": {
"Custom": {
"name": "notification_state",
"kind": {
"Enum": [
"unseen",
"seen",
"done"
]
}
}
}
}
}
}
]
},
"nullable": [
null,
null,
null,
false,
false,
false,
null,
null,
null,
null,
false,
true
]
},
"hash": "cbbd68ce3bec22c342aedf075ea13a39e236e3aeb0c8d85049fe5516a4ddeed3"
}