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.

refactor(external-chat): extract the generic domain from whatsapp_linked (#8962)

Wolf Mermelsteinfb47bd91758c
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index ec2f9302e9..5afd299331 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -5196,6 +5196,7 @@
"crates/entity_registry/entity_registry",
"crates/entity_registry/entity_registry_db_utils",
"crates/entity_registry/shared_entity_registry",
+ "crates/external_chat",
"crates/favorites",
"crates/fig_engine",
"crates/filter_ast",
@@ -6760,6 +6761,45 @@
"crates/remote_env_var",
"crates/workspace-hack"
],
+ "external_chat": [
+ "crates/agent",
+ "crates/ai_toolset",
+ "crates/ai_usage",
+ "crates/attachment",
+ "crates/bot_id",
+ "crates/bot_token",
+ "crates/cowlike",
+ "crates/database_env_vars",
+ "crates/document_sub_type",
+ "crates/entity_access",
+ "crates/entity_access_db_utils",
+ "crates/external_chat",
+ "crates/genai_telemetry",
+ "crates/harness_id",
+ "crates/harness_token",
+ "crates/macro_auth",
+ "crates/macro_authorization",
+ "crates/macro_entrypoint",
+ "crates/macro_env",
+ "crates/macro_env_var",
+ "crates/macro_user_id",
+ "crates/macro_uuid",
+ "crates/model",
+ "crates/model-entity",
+ "crates/model-error-response",
+ "crates/model_file_type",
+ "crates/model_owner",
+ "crates/model_user",
+ "crates/models_bulk_upload",
+ "crates/models_entity_access_management",
+ "crates/models_pagination",
+ "crates/models_permissions",
+ "crates/non_empty",
+ "crates/prompt",
+ "crates/remote_env_var",
+ "crates/share_permission_db_utils",
+ "crates/workspace-hack"
+ ],
"favorites": [
"crates/agent",
"crates/ai_toolset",
@@ -14129,6 +14169,7 @@
"crates/entity_registry/entity_registry",
"crates/entity_registry/entity_registry_db_utils",
"crates/entity_registry/shared_entity_registry",
+ "crates/external_chat",
"crates/genai_telemetry",
"crates/harness_id",
"crates/harness_token",
@@ -14189,6 +14230,7 @@
"crates/entity_registry/entity_registry",
"crates/entity_registry/entity_registry_db_utils",
"crates/entity_registry/shared_entity_registry",
+ "crates/external_chat",
"crates/genai_telemetry",
"crates/harness_id",
"crates/harness_token",
@@ -14231,6 +14273,7 @@
"crates/entity_registry/entity_registry",
"crates/entity_registry/entity_registry_db_utils",
"crates/entity_registry/shared_entity_registry",
+ "crates/external_chat",
"crates/genai_telemetry",
"crates/harness_id",
"crates/harness_token",
diff --git a/.sqlx/query-95d004bbf9780aefaaf6c658804cf8e00dc455559d9f00dda44c84c9d9dec7fe.json b/.sqlx/query-035fe2a061ac76943164a8d577be44f62adf3708358c79be2fb3f2985eabebc9.json
similarity index 70%
rename from .sqlx/query-95d004bbf9780aefaaf6c658804cf8e00dc455559d9f00dda44c84c9d9dec7fe.json
rename to .sqlx/query-035fe2a061ac76943164a8d577be44f62adf3708358c79be2fb3f2985eabebc9.json
index 2a59136202..cce9994e97 100644
--- a/.sqlx/query-95d004bbf9780aefaaf6c658804cf8e00dc455559d9f00dda44c84c9d9dec7fe.json
+++ b/.sqlx/query-035fe2a061ac76943164a8d577be44f62adf3708358c79be2fb3f2985eabebc9.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT m.id,m.person_id,p.user_id,COALESCE(p.display_name,'WhatsApp participant') AS \"name!\",\n m.body,m.reply_to,m.source_at,m.ingested_at,m.revised_at,m.deleted_at,m.kind,m.rich,m.mentions,m.forwarded,m.view_once,\n (SELECT state FROM whatsapp_send s WHERE s.message_id = m.id AND s.user_id = $2 ORDER BY s.created_at DESC LIMIT 1) AS delivery\n FROM external_chat_message m JOIN external_chat_person p ON p.id = m.person_id\n WHERE m.chat_id = $1 AND m.message_id IS NOT NULL\n AND ($3::uuid IS NULL OR (m.source_at,m.id) < (SELECT source_at,id FROM external_chat_message WHERE id = $3 AND chat_id = $1 AND message_id IS NOT NULL))\n AND ($5::uuid IS NULL OR m.id = $5)\n ORDER BY m.source_at DESC,m.id DESC LIMIT $4",
+ "query": "SELECT m.id,m.person_id,p.user_id,COALESCE(p.display_name,'WhatsApp participant') AS \"name!\",\n m.body,m.reply_to,m.source_at,m.ingested_at,m.revised_at,m.deleted_at,m.kind,m.rich,m.mentions,m.forwarded,m.view_once\n FROM external_chat_message m JOIN external_chat_person p ON p.id = m.person_id\n WHERE m.chat_id = $1 AND m.message_id IS NOT NULL\n AND ($2::uuid IS NULL OR (m.source_at,m.id) < (SELECT source_at,id FROM external_chat_message WHERE id = $2 AND chat_id = $1 AND message_id IS NOT NULL))\n AND ($4::uuid IS NULL OR m.id = $4)\n ORDER BY m.source_at DESC,m.id DESC LIMIT $3",
"describe": {
"columns": [
{
@@ -77,17 +77,11 @@
"ordinal": 14,
"name": "view_once",
"type_info": "Bool"
- },
- {
- "ordinal": 15,
- "name": "delivery",
- "type_info": "Text"
}
],
"parameters": {
"Left": [
"Uuid",
- "Text",
"Uuid",
"Int8",
"Uuid"
@@ -108,9 +102,8 @@
true,
false,
false,
- false,
- null
+ false
]
},
- "hash": "95d004bbf9780aefaaf6c658804cf8e00dc455559d9f00dda44c84c9d9dec7fe"
+ "hash": "035fe2a061ac76943164a8d577be44f62adf3708358c79be2fb3f2985eabebc9"
}
diff --git a/.sqlx/query-7016d22d76448dd0ba6ea0ad71b28b4882cbd6c866f8442bb4f677b99a40c28d.json b/.sqlx/query-13f77af5dad557c08f09bd71d74135985c749820a96c4ae2af02f6db9bed9541.json
similarity index 58%
rename from .sqlx/query-7016d22d76448dd0ba6ea0ad71b28b4882cbd6c866f8442bb4f677b99a40c28d.json
rename to .sqlx/query-13f77af5dad557c08f09bd71d74135985c749820a96c4ae2af02f6db9bed9541.json
index e7dd5f8f56..e9a75074ae 100644
--- a/.sqlx/query-7016d22d76448dd0ba6ea0ad71b28b4882cbd6c866f8442bb4f677b99a40c28d.json
+++ b/.sqlx/query-13f77af5dad557c08f09bd71d74135985c749820a96c4ae2af02f6db9bed9541.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "WITH sources AS MATERIALIZED (\n SELECT array_append(ARRAY(SELECT team_id::text FROM team_user WHERE user_id = $1)\n || ARRAY(SELECT channel_id::text FROM comms_channel_participants WHERE user_id = $1 AND left_at IS NULL), $1) AS ids),\n candidates AS MATERIALIZED (\n SELECT $2::uuid AS id WHERE $2::uuid IS NOT NULL\n UNION\n SELECT ea.entity_id FROM entity_access ea, sources s\n WHERE $2::uuid IS NULL AND ea.entity_type = 'external_chat' AND ea.source_id = ANY(s.ids)\n UNION\n SELECT rs.chat_id FROM external_chat_read_state rs WHERE $2::uuid IS NULL AND rs.user_id = $1)\n SELECT g.id,g.provider,g.name,g.kind,g.user_id,g.remote_id,g.state,g.adopted_at,g.backfill_state,g.backfill_imported,\n (SELECT count(*) FROM (SELECT 1 FROM external_chat_message m WHERE m.chat_id=g.id AND m.message_id IS NOT NULL\n AND m.deleted_at IS NULL AND m.origin<>'history' AND m.kind<>'system'\n AND NOT EXISTS(SELECT 1 FROM external_chat_person p WHERE p.id=m.person_id AND p.user_id=$1)\n AND (r.message_id IS NULL OR (m.source_at,m.id)>(r.read_at,r.message_id)) LIMIT $3) unread) AS \"unread_count!\",\n (SELECT max(source_at) FROM external_chat_message WHERE chat_id=g.id AND message_id IS NOT NULL) AS last_message_at,\n roster.metadata AS \"metadata?\",\n seen.community_jid AS \"community_jid?\",seen.community_announcements AS \"community_announcements?\",parent.name AS \"community_name?\"\n FROM candidates c JOIN external_chat g ON g.id=c.id\n LEFT JOIN external_chat_read_state r ON r.chat_id=g.id AND r.user_id=$1\n LEFT JOIN whatsapp_linked_account a ON a.user_id=$1\n LEFT JOIN whatsapp_roster roster ON roster.account_id=a.id AND roster.group_jid=g.remote_id\n LEFT JOIN LATERAL (SELECT r.metadata->>'communityJid' AS community_jid,COALESCE((r.metadata->>'communityAnnouncements')::boolean,false) AS community_announcements\n FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id\n WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=g.remote_id\n ORDER BY r.observed_at DESC LIMIT 1) seen ON true\n LEFT JOIN LATERAL (SELECT r.metadata->>'name' AS name\n FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id\n WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=seen.community_jid\n ORDER BY r.observed_at DESC LIMIT 1) parent ON true\n -- On the candidate, not the chat: Postgres would otherwise check every chat.\n WHERE ($2::uuid IS NOT NULL OR external_chat_can_view(c.id,$1))\n ORDER BY g.adopted_at DESC LIMIT 2000",
+ "query": "WITH sources AS MATERIALIZED (\n SELECT array_append(ARRAY(SELECT team_id::text FROM team_user WHERE user_id = $1)\n || ARRAY(SELECT channel_id::text FROM comms_channel_participants WHERE user_id = $1 AND left_at IS NULL), $1) AS ids),\n candidates AS MATERIALIZED (\n SELECT $2::uuid AS id WHERE $2::uuid IS NOT NULL\n UNION\n SELECT ea.entity_id FROM entity_access ea, sources s\n WHERE $2::uuid IS NULL AND ea.entity_type = 'external_chat' AND ea.source_id = ANY(s.ids)\n UNION\n SELECT rs.chat_id FROM external_chat_read_state rs WHERE $2::uuid IS NULL AND rs.user_id = $1)\n SELECT g.id,g.provider,g.name,g.kind,g.user_id,g.remote_id,g.state,g.adopted_at,g.backfill_state,g.backfill_imported,\n (SELECT count(*) FROM (SELECT 1 FROM external_chat_message m WHERE m.chat_id=g.id AND m.message_id IS NOT NULL\n AND m.deleted_at IS NULL AND m.origin<>'history' AND m.kind<>'system'\n AND NOT EXISTS(SELECT 1 FROM external_chat_person p WHERE p.id=m.person_id AND p.user_id=$1)\n AND (r.message_id IS NULL OR (m.source_at,m.id)>(r.read_at,r.message_id)) LIMIT $3) unread) AS \"unread_count!\",\n (SELECT max(source_at) FROM external_chat_message WHERE chat_id=g.id AND message_id IS NOT NULL) AS last_message_at\n FROM candidates c JOIN external_chat g ON g.id=c.id\n LEFT JOIN external_chat_read_state r ON r.chat_id=g.id AND r.user_id=$1\n -- On the candidate, not the chat: Postgres would otherwise check every chat.\n WHERE ($2::uuid IS NOT NULL OR external_chat_can_view(c.id,$1))\n ORDER BY g.adopted_at DESC LIMIT 2000",
"describe": {
"columns": [
{
@@ -62,26 +62,6 @@
"ordinal": 11,
"name": "last_message_at",
"type_info": "Timestamptz"
- },
- {
- "ordinal": 12,
- "name": "metadata?",
- "type_info": "Jsonb"
- },
- {
- "ordinal": 13,
- "name": "community_jid?",
- "type_info": "Text"
- },
- {
- "ordinal": 14,
- "name": "community_announcements?",
- "type_info": "Bool"
- },
- {
- "ordinal": 15,
- "name": "community_name?",
- "type_info": "Text"
}
],
"parameters": {
@@ -103,12 +83,8 @@
true,
false,
null,
- null,
- false,
- null,
- null,
null
]
},
- "hash": "7016d22d76448dd0ba6ea0ad71b28b4882cbd6c866f8442bb4f677b99a40c28d"
+ "hash": "13f77af5dad557c08f09bd71d74135985c749820a96c4ae2af02f6db9bed9541"
}
diff --git a/.sqlx/query-225dc781edd770a96e44bd623cd019b1a873121b93b9f980a594c239f6d82135.json b/.sqlx/query-225dc781edd770a96e44bd623cd019b1a873121b93b9f980a594c239f6d82135.json
new file mode 100644
index 0000000000..9d82277ac0
--- /dev/null
+++ b/.sqlx/query-225dc781edd770a96e44bd623cd019b1a873121b93b9f980a594c239f6d82135.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT DISTINCT ON (message_id) message_id AS \"message_id!\", state FROM whatsapp_send\n WHERE chat_id = $1 AND user_id = $2 AND message_id = ANY($3)\n ORDER BY message_id, created_at DESC",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "message_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "state",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ true,
+ false
+ ]
+ },
+ "hash": "225dc781edd770a96e44bd623cd019b1a873121b93b9f980a594c239f6d82135"
+}
diff --git a/.sqlx/query-4e66e885331ad08e1977016dd24ab23d021ad4cfa2de1b68b83224c79d89d50d.json b/.sqlx/query-4e66e885331ad08e1977016dd24ab23d021ad4cfa2de1b68b83224c79d89d50d.json
new file mode 100644
index 0000000000..5dcc752e8a
--- /dev/null
+++ b/.sqlx/query-4e66e885331ad08e1977016dd24ab23d021ad4cfa2de1b68b83224c79d89d50d.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT metadata FROM whatsapp_roster WHERE account_id = $1 AND group_jid = ANY($2)",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "metadata",
+ "type_info": "Jsonb"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "4e66e885331ad08e1977016dd24ab23d021ad4cfa2de1b68b83224c79d89d50d"
+}
diff --git a/.sqlx/query-6b66ed3efaeda51173bc28fe34d97b89f543c646da80691d5bdf9544bd2ca8aa.json b/.sqlx/query-6b66ed3efaeda51173bc28fe34d97b89f543c646da80691d5bdf9544bd2ca8aa.json
new file mode 100644
index 0000000000..0ec304f6a6
--- /dev/null
+++ b/.sqlx/query-6b66ed3efaeda51173bc28fe34d97b89f543c646da80691d5bdf9544bd2ca8aa.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT g.id AS \"id!\", seen.community_jid AS \"community_jid?\",\n seen.community_announcements AS \"community_announcements?\", parent.name AS \"community_name?\"\n FROM external_chat g\n LEFT JOIN LATERAL (SELECT r.metadata->>'communityJid' AS community_jid,COALESCE((r.metadata->>'communityAnnouncements')::boolean,false) AS community_announcements\n FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id\n WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=g.remote_id\n ORDER BY r.observed_at DESC LIMIT 1) seen ON true\n LEFT JOIN LATERAL (SELECT r.metadata->>'name' AS name\n FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id\n WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=seen.community_jid\n ORDER BY r.observed_at DESC LIMIT 1) parent ON true\n WHERE g.id = ANY($1)",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "community_jid?",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "community_announcements?",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 3,
+ "name": "community_name?",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ true,
+ true
+ ]
+ },
+ "hash": "6b66ed3efaeda51173bc28fe34d97b89f543c646da80691d5bdf9544bd2ca8aa"
+}
diff --git a/Cargo.lock b/Cargo.lock
index 643024872d..b3af08cd62 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6506,6 +6506,7 @@ dependencies = [
"entity_mutation",
"entity_registry",
"entity_registry_db_utils",
+ "external_chat",
"favorites",
"flate2",
"foreign_entity",
@@ -7874,6 +7875,38 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af9673d8203fcb076b19dfd17e38b3d4ae9f44959416ea532ce72415a6020365"
+[[package]]
+name = "external_chat"
+version = "0.1.0"
+dependencies = [
+ "async-trait",
+ "axum",
+ "chrono",
+ "entity_access",
+ "entity_access_db_utils",
+ "image",
+ "macro_authorization",
+ "macro_user_id",
+ "model-entity",
+ "models_permissions",
+ "nom 8.0.0",
+ "opentelemetry",
+ "rootcause",
+ "serde",
+ "serde_json",
+ "sha2 0.10.9",
+ "share_permission_db_utils",
+ "sqlx",
+ "strum 0.27.2",
+ "thiserror 2.0.18",
+ "tokio",
+ "tracing",
+ "tracing-opentelemetry",
+ "utoipa",
+ "uuid",
+ "workspace-hack",
+]
+
[[package]]
name = "fallible-iterator"
version = "0.2.0"
@@ -21694,6 +21727,7 @@ dependencies = [
"chrono",
"entity_access",
"entity_access_db_utils",
+ "external_chat",
"macro_db_migrator",
"macro_user_id",
"sqlx",
@@ -21749,6 +21783,7 @@ dependencies = [
"entity_access",
"entity_access_db_utils",
"entity_registry_db_utils",
+ "external_chat",
"harness_id",
"image",
"macro_authorization",
@@ -21759,7 +21794,6 @@ dependencies = [
"model_user",
"models_permissions",
"nom 8.0.0",
- "opentelemetry",
"rootcause",
"serde",
"serde_json",
@@ -21767,11 +21801,9 @@ dependencies = [
"share_permission_db_utils",
"sqlx",
"strum 0.27.2",
- "thiserror 2.0.18",
"tokio",
"tower 0.5.3",
"tracing",
- "tracing-opentelemetry",
"utoipa",
"uuid",
"workspace-hack",
@@ -21787,6 +21819,7 @@ dependencies = [
"chrono",
"database_env_vars",
"doppler-config",
+ "external_chat",
"futures",
"macro_aws_config",
"macro_config",
diff --git a/Cargo.toml b/Cargo.toml
index 9c1a24efe7..788625eff0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -134,6 +134,7 @@ members = [
"crates/webhook_signature",
"crates/whatsapp_e2e",
"crates/whatsapp_fake_server",
+ "crates/external_chat",
"crates/whatsapp_linked",
"crates/whatsapp_store_pg",
"crates/whatsmeow",
diff --git a/crates/external_chat/AGENTS.md b/crates/external_chat/AGENTS.md
new file mode 100644
index 0000000000..d9888623e1
--- /dev/null
+++ b/crates/external_chat/AGENTS.md
@@ -0,0 +1,58 @@
+# external_chat
+
+## What it is
+
+External chats: a chat from another messaging service (WhatsApp is the one
+provider) added to Macro as an independently shared `ExternalChat` entity.
+This crate is the provider-neutral part: list and detail, timeline, read
+state, sharing, deliberate sends and uploads, attachment import into static
+files, notification effects and the `/external-chats` routes, over the
+`external_chat*` tables. A provider (`crates/whatsapp_linked`) implements the
+`ChatProvider` port; DSS composes `ExternalChatService<WhatsAppProvider>`.
+
+## Start here
+
+- `src/domain/ports.rs`: `ChatProvider` (accounts, who can send, members,
+ names, communities, adopt/detach, the provider's send outbox) and the
+ repository, publisher, file store and rate limiter ports.
+- `src/domain/service.rs` + `service/timeline.rs`: the use cases. The
+ `ReplyBlock` decision (`reply_block`) combines `SendAuthority`, the
+ provider account and its `SendingFacts`.
+- `src/domain/timeline.rs` + `timeline/view.rs`: DTOs and how stored rich
+ content, mentions and votes become names and tallies (`People` comes from
+ the provider). `src/domain/rich.rs`: stored `RichContent`.
+- `src/domain/effects.rs`, `media.rs`: notification outbox and attachment
+ import drained by DSS. `src/signals.rs`: channels DSS listens on.
+- `src/outbound/pg.rs`: the adapter, plus public transaction helpers
+ (`notify`, `notify_change`, `lock_chat_in`, `fail_attachment_in`, ...) a
+ provider's adapter uses when it writes the generic tables.
+
+## Boundaries
+
+- Depends on: `entity_access` (receipts), `entity_access_db_utils`,
+ `share_permission_db_utils`.
+- Used by: `whatsapp_linked` (implements the port), DSS, the session service
+ (signals, models) and `whatsapp_e2e`. Never depends on a provider.
+
+## Checks
+
+- `cargo test -p external_chat --all-features`; the end-to-end Postgres tests
+ of these reads need the WhatsApp ingest and live in `whatsapp_linked`
+ (`cargo test -p whatsapp_linked --all-features`, `SQLX_OFFLINE` unset).
+- Check `--no-default-features --features ports` and `inbound` independently.
+
+## Gotchas
+
+- No provider tables (`whatsapp_*`) in this crate's SQL: anything a provider
+ stores is read through `ChatProvider`.
+- HTTP error JSON, codes and some messages still name WhatsApp; they are API
+ and change only with the web app.
+- View access is `external_chat_access_level` (see
+ `docs/architecture/external-chats.md`); bot view receipts read but never
+ borrow a person's account, sends or unread cursor (`view_scope`).
+
+## Related
+
+- Docs: `docs/architecture/external-chats.md`,
+ `docs/architecture/whatsapp-linked-devices.md` · Rules:
+ `.agents/rules/observability.md`
diff --git a/crates/external_chat/CLAUDE.md b/crates/external_chat/CLAUDE.md
new file mode 120000
index 0000000000..47dc3e3d86
--- /dev/null
+++ b/crates/external_chat/CLAUDE.md
@@ -0,0 +1 @@
+AGENTS.md
\ No newline at end of file
diff --git a/crates/external_chat/Cargo.toml b/crates/external_chat/Cargo.toml
new file mode 100644
index 0000000000..f7f0606632
--- /dev/null
+++ b/crates/external_chat/Cargo.toml
@@ -0,0 +1,71 @@
+[package]
+edition = "2024"
+name = "external_chat"
+publish = false
+version = "0.1.0"
+
+[features]
+default = []
+# Port traits and the domain services built on them.
+ports = [
+ "dep:async-trait",
+ "dep:entity_access",
+ "dep:image",
+ "dep:macro_user_id",
+ "dep:nom",
+ "dep:rootcause",
+ "dep:sha2",
+ "entity_access/ports",
+]
+# Postgres adapters.
+outbound = [
+ "dep:entity_access",
+ "dep:entity_access_db_utils",
+ "dep:share_permission_db_utils",
+ "dep:opentelemetry",
+ "dep:sqlx",
+ "dep:tracing-opentelemetry",
+ "entity_access/ports",
+ "entity_access/outbound",
+ "ports",
+]
+# DSS HTTP routes.
+inbound = [
+ "dep:axum",
+ "dep:entity_access",
+ "dep:macro_authorization",
+ "entity_access/inbound",
+ "ports",
+ "schema",
+]
+schema = ["dep:utoipa"]
+
+[dependencies]
+async-trait = { workspace = true, optional = true }
+axum = { workspace = true, features = ["multipart"], optional = true }
+chrono = { workspace = true }
+entity_access = { path = "../entity_access", default-features = false, optional = true }
+entity_access_db_utils = { path = "../entity_access_db_utils", optional = true }
+image = { workspace = true, optional = true }
+macro_authorization = { path = "../macro_authorization", default-features = false, features = ["axum"], optional = true }
+macro_user_id = { path = "../macro_user_id", optional = true }
+model-entity = { path = "../model-entity" }
+models_permissions = { path = "../models_permissions" }
+nom = { workspace = true, optional = true }
+opentelemetry = { workspace = true, features = ["trace"], optional = true }
+rootcause = { workspace = true, optional = true }
+serde = { workspace = true }
+serde_json = { workspace = true }
+sha2 = { workspace = true, optional = true }
+share_permission_db_utils = { path = "../share_permission_db_utils", optional = true }
+sqlx = { workspace = true, optional = true }
+strum = { workspace = true }
+thiserror = { workspace = true }
+tracing = { workspace = true }
+tracing-opentelemetry = { workspace = true, optional = true }
+utoipa = { workspace = true, optional = true }
+uuid = { workspace = true }
+workspace-hack = { version = "0.1", path = "../workspace-hack" }
+
+[dev-dependencies]
+tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
diff --git a/crates/external_chat/src/domain.rs b/crates/external_chat/src/domain.rs
new file mode 100644
index 0000000000..c9497338da
--- /dev/null
+++ b/crates/external_chat/src/domain.rs
@@ -0,0 +1,23 @@
+/// Added chats, messages, sends and attachments shared with the web app.
+pub mod models;
+/// Polls, locations, contact cards, events and system lines.
+pub mod rich;
+
+/// Native notification outbox.
+#[cfg(feature = "ports")]
+pub mod effects;
+/// Attachment import into static files.
+#[cfg(feature = "ports")]
+pub mod media;
+/// Port traits, including the provider port.
+#[cfg(feature = "ports")]
+pub mod ports;
+/// DSS use cases.
+#[cfg(feature = "ports")]
+pub mod service;
+/// Pixel size and length of files sent from Macro.
+#[cfg(feature = "ports")]
+pub mod shape;
+/// Timeline, read state and sharing contracts.
+#[cfg(feature = "ports")]
+pub mod timeline;
diff --git a/crates/whatsapp_linked/src/domain/effects.rs b/crates/external_chat/src/domain/effects.rs
similarity index 88%
rename from crates/whatsapp_linked/src/domain/effects.rs
rename to crates/external_chat/src/domain/effects.rs
index d0779d0ba6..2855423a6f 100644
--- a/crates/whatsapp_linked/src/domain/effects.rs
+++ b/crates/external_chat/src/domain/effects.rs
@@ -8,12 +8,12 @@ use std::sync::Arc;
#[derive(Clone)]
pub struct EffectsService {
repository: Arc<dyn EffectsRepository>,
- publisher: Arc<dyn GroupPublisher>,
+ publisher: Arc<dyn ChatPublisher>,
}
impl EffectsService {
- /// Wire the outbox and the group publisher.
- pub fn new(repository: Arc<dyn EffectsRepository>, publisher: Arc<dyn GroupPublisher>) -> Self {
+ /// Wire the outbox and the chat publisher.
+ pub fn new(repository: Arc<dyn EffectsRepository>, publisher: Arc<dyn ChatPublisher>) -> Self {
Self {
repository,
publisher,
@@ -28,7 +28,7 @@ impl EffectsService {
err,
fields(due, published)
)]
- pub async fn publish_pending(&self) -> LinkedResult<usize> {
+ pub async fn publish_pending(&self) -> ExternalChatResult<usize> {
let effects = self.repository.pending_effects(50).await?;
tracing::Span::current().record("due", effects.len());
let mut published = 0;
@@ -52,7 +52,7 @@ impl EffectsService {
message_id = %effect.message_id,
code = error.current_context().code(),
error = %error,
- "WhatsApp group notification will retry"
+ "External chat notification will retry"
);
self.repository.defer_effect(effect.message_id).await?;
}
diff --git a/crates/whatsapp_linked/src/domain/media.rs b/crates/external_chat/src/domain/media.rs
similarity index 90%
rename from crates/whatsapp_linked/src/domain/media.rs
rename to crates/external_chat/src/domain/media.rs
index 8fe07e3d7a..e228d86545 100644
--- a/crates/whatsapp_linked/src/domain/media.rs
+++ b/crates/external_chat/src/domain/media.rs
@@ -28,7 +28,7 @@ impl MediaService {
err,
fields(due, imported)
)]
- pub async fn import_pending(&self) -> LinkedResult<usize> {
+ pub async fn import_pending(&self) -> ExternalChatResult<usize> {
let due = self.repository.due_imports(8).await?;
tracing::Span::current().record("due", due.len());
let mut imported = 0;
@@ -69,7 +69,7 @@ impl MediaService {
err,
fields(due, deleted)
)]
- pub async fn delete_released(&self) -> LinkedResult<usize> {
+ pub async fn delete_released(&self) -> ExternalChatResult<usize> {
let due = self.repository.due_file_deletions(25).await?;
tracing::Span::current().record("due", due.len());
let mut deleted = vec![];
@@ -89,9 +89,8 @@ impl MediaService {
Ok(deleted.len())
}
- /// Delete staged uploads that were never sent, old participant receipts
- /// and old consumed pairings.
- pub async fn expire_retained(&self) -> LinkedResult<u64> {
- self.repository.expire_retained().await
+ /// Delete staged uploads that were never sent.
+ pub async fn expire_uploads(&self) -> ExternalChatResult<u64> {
+ self.repository.expire_uploads().await
}
}
diff --git a/crates/external_chat/src/domain/models.rs b/crates/external_chat/src/domain/models.rs
new file mode 100644
index 0000000000..d4e20944fe
--- /dev/null
+++ b/crates/external_chat/src/domain/models.rs
@@ -0,0 +1,621 @@
+//! Added external chats, their messages and deliberate sends, as DSS and the
+//! web app see them, whatever the provider.
+
+use chrono::{DateTime, Utc};
+pub use model_entity::{ExternalChatKind, ExternalChatProvider};
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+/// Explicit request to add a chat; the provider account is selected
+/// server-side. The chat's name always follows the provider.
+#[derive(Debug, Clone, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatAdd))]
+pub struct AddChat {
+ /// The service the chat lives in.
+ pub provider: ExternalChatProvider,
+ /// The chat's id at the provider, as its chat list names it. WhatsApp: a
+ /// group the requester's account has joined, or a direct chat's other
+ /// person's JID.
+ pub remote_id: String,
+ /// Group or direct chat; must match what `remote_id` is.
+ pub kind: ExternalChatKind,
+}
+
+/// Lifecycle of an added chat.
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+pub enum ChatState {
+ /// Observed and sendable.
+ Active,
+ /// Locally detached; history stays readable.
+ Detached,
+}
+
+/// One remote chat added as a person's external chat entity.
+#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
+pub struct ChatRef {
+ /// The external chat entity id, independent of any observing account.
+ pub id: Uuid,
+ /// The service the chat lives in.
+ pub provider: ExternalChatProvider,
+ /// Remote chat identity, never its display name. WhatsApp: the group's
+ /// JID, or a direct chat's other person.
+ pub remote_id: String,
+ /// Group or direct chat.
+ pub kind: ExternalChatKind,
+ /// The person who added the chat, its owner.
+ pub user_id: String,
+ /// Lifecycle.
+ pub state: ChatState,
+ /// History boundary: earlier live messages are not imported.
+ pub adopted_at: DateTime<Utc>,
+}
+
+/// The WhatsApp community a group belongs to.
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatCommunity))]
+pub struct CommunityReference {
+ /// The community's WhatsApp JID.
+ pub id: String,
+ /// The community's name.
+ pub name: String,
+ /// This group is the community's announcement group.
+ pub announcements: bool,
+}
+
+/// Shown when the community itself is not in the account's group list.
+const UNKNOWN_COMMUNITY_NAME: &str = "WhatsApp community";
+
+impl CommunityReference {
+ /// The community `id`, named after its own metadata's `name` when known.
+ pub fn named(id: String, name: Option<&str>, announcements: bool) -> Self {
+ let name = name
+ .map(str::trim)
+ .filter(|name| !name.is_empty())
+ .unwrap_or(UNKNOWN_COMMUNITY_NAME)
+ .to_owned();
+ Self {
+ id,
+ name,
+ announcements,
+ }
+ }
+}
+
+/// Why a chat can't be added to Macro; the app words it from the code.
+#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq, thiserror::Error)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = WhatsAppCannotAdd))]
+pub enum CannotAdd {
+ /// The account's WhatsApp left the group or was removed.
+ #[error("your WhatsApp is not in this group any more")]
+ NotInGroup,
+ /// A community itself has no messages; its groups can be added.
+ #[error("a community has no messages of its own; add one of its groups")]
+ Community,
+ /// The chat with yourself.
+ #[error("your chat with yourself can't be added to Macro")]
+ OwnChat,
+ /// A direct chat that is not in the account's chat list.
+ #[error("this chat is not in your WhatsApp chat list")]
+ NotInChatList,
+}
+
+/// Where importing a group's earlier messages stands.
+#[derive(
+ Debug,
+ Clone,
+ Copy,
+ Serialize,
+ Deserialize,
+ PartialEq,
+ Eq,
+ strum::IntoStaticStr,
+ strum::EnumString,
+)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatBackfillState))]
+#[strum(serialize_all = "snake_case")]
+pub enum BackfillState {
+ /// Nothing to start from: the owner's account knows no message in this
+ /// chat yet (paired before WhatsApp sent its chat list, and quiet since).
+ /// Becomes `pending` with the chat's first message.
+ WaitingForMessage,
+ /// About to start: the owner's account has not picked it up yet.
+ Pending,
+ /// Asking the owner's phone for earlier messages.
+ Running,
+ /// The owner's phone did not answer; retried when it is back.
+ WaitingForPhone,
+ /// Everything the phone had (within the limits) was imported.
+ Done,
+ /// Stopped at the limits ([`BACKFILL_MESSAGE_LIMIT`] messages or
+ /// [`BACKFILL_MAX_AGE_DAYS`] days); older messages stay in WhatsApp.
+ Partial,
+}
+
+impl BackfillState {
+ /// Stable storage spelling.
+ pub fn as_str(self) -> &'static str {
+ self.into()
+ }
+
+ /// Parse the storage spelling.
+ pub fn parse(value: &str) -> Option<Self> {
+ value.parse().ok()
+ }
+}
+
+/// Earlier messages imported after adding a chat.
+#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatBackfill))]
+pub struct Backfill {
+ /// Progress.
+ pub state: BackfillState,
+ /// Messages received from the phone so far (including ones already shown).
+ pub imported: u32,
+}
+
+/// Most earlier messages imported when a group is added.
+pub const BACKFILL_MESSAGE_LIMIT: u32 = 500;
+/// Earlier messages older than this many days before adding are not imported.
+pub const BACKFILL_MAX_AGE_DAYS: i64 = 90;
+
+/// Deliberate operations on an existing message.
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatMutation))]
+pub enum Mutation {
+ /// Add, replace or remove the caller's reaction.
+ Reaction,
+ /// Edit the caller's own recent message.
+ Edit,
+ /// Delete the caller's own recent message for everyone.
+ Delete,
+}
+
+/// Content of a deliberate send.
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(tag = "type", rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatReplyContent))]
+pub enum ReplyContent {
+ /// Plain text.
+ Text {
+ /// UTF-8 text, at most [`TEXT_LIMIT_BYTES`] bytes.
+ text: String,
+ /// Group participants the text @-mentions, by their WhatsApp id
+ /// (`jid` of a roster participant); the text must contain
+ /// `@<digits before the @ of the id>` for each.
+ #[serde(default)]
+ mentions: Vec<String>,
+ },
+ /// One attachment staged through the attachments route.
+ Media {
+ /// The staged upload, imported as the message's static file.
+ #[serde(rename = "uploadId")]
+ upload_id: Uuid,
+ /// Optional caption; audio has none.
+ caption: Option<String>,
+ },
+ /// An operation on an existing message in this chat.
+ Mutation {
+ /// Existing message, resolved to its provider source by the server.
+ #[serde(rename = "messageId")]
+ message_id: Uuid,
+ /// Explicit operation.
+ operation: Mutation,
+ /// Replacement text or emoji; absent for delete, empty to remove a reaction.
+ text: Option<String>,
+ },
+}
+
+/// Explicit send action from a person. Only deliberate sends reach WhatsApp.
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatSendReply))]
+pub struct SendReply {
+ /// Stable client intent key. A changed payload with this key is a conflict.
+ pub idempotency_key: Uuid,
+ /// Content.
+ pub message: ReplyContent,
+ /// Message being quoted, within this chat.
+ #[serde(default)]
+ pub reply_to: Option<Uuid>,
+}
+
+/// Observed send state. `unknown` means the request may have reached WhatsApp;
+/// it is never sent again automatically.
+#[derive(
+ Debug,
+ Clone,
+ Copy,
+ Serialize,
+ Deserialize,
+ PartialEq,
+ Eq,
+ strum::IntoStaticStr,
+ strum::EnumString,
+)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatDeliveryState))]
+#[strum(serialize_all = "snake_case")]
+pub enum DeliveryState {
+ /// Durable intent, not yet claimed.
+ Pending,
+ /// Claimed by the account's session; may already be dispatching.
+ Sending,
+ /// The outcome of a dispatched request is not known.
+ Unknown,
+ /// WhatsApp accepted the message; this does not establish delivery.
+ Accepted,
+ /// Rejected, expired or no longer authorized.
+ Failed,
+}
+
+impl DeliveryState {
+ /// Stable storage spelling.
+ pub fn as_str(self) -> &'static str {
+ self.into()
+ }
+
+ /// Parse the storage spelling.
+ pub fn parse(value: &str) -> Option<Self> {
+ value.parse().ok()
+ }
+}
+
+/// Durable result of a deliberate send.
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatSendProgress))]
+pub struct SendProgress {
+ /// Stable intent id.
+ pub id: Uuid,
+ /// Person who submitted it.
+ pub user_id: String,
+ /// External chat.
+ pub chat_id: Uuid,
+ /// The original request, so a failed draft can be recovered.
+ pub payload: SendReply,
+ /// Current state.
+ pub state: DeliveryState,
+ /// The chat's message, once the provider accepted it.
+ pub message_id: Option<Uuid>,
+ /// Sanitized failure code.
+ pub error_code: Option<String>,
+ /// Intent creation time.
+ pub created_at: DateTime<Utc>,
+}
+
+/// Attachment categories WhatsApp distinguishes.
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatMediaKind))]
+pub enum MediaKind {
+ /// JPEG, PNG or WebP.
+ Image,
+ /// MP3, Ogg, AAC or MP4 audio. Ogg/Opus is sent as a voice note.
+ Audio,
+ /// MP4 video.
+ Video,
+ /// PDF or plain text.
+ Document,
+}
+
+/// Every MIME type Macro sends to WhatsApp, with its category.
+pub const SENDABLE_MEDIA: [(&str, MediaKind); 10] = [
+ ("image/jpeg", MediaKind::Image),
+ ("image/png", MediaKind::Image),
+ ("image/webp", MediaKind::Image),
+ ("audio/mpeg", MediaKind::Audio),
+ ("audio/ogg", MediaKind::Audio),
+ ("audio/aac", MediaKind::Audio),
+ ("audio/mp4", MediaKind::Audio),
+ ("video/mp4", MediaKind::Video),
+ ("application/pdf", MediaKind::Document),
+ ("text/plain", MediaKind::Document),
+];
+
+impl MediaKind {
+ /// Category of a supported MIME type; anything else is refused.
+ pub fn of(mime: &str) -> Option<Self> {
+ SENDABLE_MEDIA
+ .iter()
+ .find(|(sendable, _)| *sendable == mime)
+ .map(|(_, kind)| *kind)
+ }
+}
+
+/// How WhatsApp shows an attachment, when not as a plain file of its kind.
+#[derive(
+ Debug,
+ Clone,
+ Copy,
+ Serialize,
+ Deserialize,
+ PartialEq,
+ Eq,
+ strum::IntoStaticStr,
+ strum::EnumString,
+)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatMediaPresentation))]
+#[strum(serialize_all = "snake_case")]
+pub enum MediaPresentation {
+ /// A sticker: a small image (WebP, possibly animated), no caption.
+ Sticker,
+ /// A short video WhatsApp plays as a looping, muted GIF.
+ Gif,
+ /// A voice note recorded in WhatsApp (push to talk).
+ VoiceNote,
+}
+
+impl MediaPresentation {
+ /// Stable storage spelling.
+ pub fn as_str(self) -> &'static str {
+ self.into()
+ }
+
+ /// Parse the storage spelling.
+ pub fn parse(value: &str) -> Option<Self> {
+ value.parse().ok()
+ }
+}
+
+/// Largest attachment imported or sent, in bytes.
+pub const MEDIA_LIMIT_BYTES: usize = 32 * 1024 * 1024;
+
+/// A staged attachment, sent only by a later deliberate send.
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatUploadedAttachment))]
+pub struct UploadedAttachment {
+ /// Use as `upload_id` in a media send.
+ pub upload_id: Uuid,
+ /// Display filename.
+ pub filename: String,
+ /// Category.
+ pub kind: MediaKind,
+}
+
+/// Where an attachment of a message stands.
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatAttachmentState))]
+pub enum AttachmentState {
+ /// Downloading or importing.
+ Loading,
+ /// Imported; `file_id` is set.
+ Ready,
+ /// Download or import failed for good.
+ Unavailable,
+ /// A file type Macro does not import.
+ Unsupported,
+}
+
+/// One attachment of a message.
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatAttachment))]
+pub struct Attachment {
+ /// Position within the message.
+ pub ordinal: i16,
+ /// Current state.
+ pub state: AttachmentState,
+ /// Display filename.
+ pub file_name: String,
+ /// Declared MIME type.
+ pub mime: String,
+ /// Declared size in bytes.
+ pub size: u64,
+ /// The static file holding it, once ready.
+ pub file_id: Option<Uuid>,
+ /// Pixel width of an image, sticker or video, when WhatsApp declared it.
+ pub width: Option<u32>,
+ /// Pixel height, when declared.
+ pub height: Option<u32>,
+ /// Length of audio or video in whole seconds, when declared.
+ pub duration_seconds: Option<u32>,
+ /// Sticker, GIF or voice note; `null` for an ordinary file of its kind.
+ pub presentation: Option<MediaPresentation>,
+}
+
+/// The attachments of one message.
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatMessageAttachments))]
+pub struct MessageAttachments {
+ /// The message.
+ pub message_id: Uuid,
+ /// Its attachments in order; deleted ones are omitted.
+ pub attachments: Vec<Attachment>,
+}
+
+/// Most messages or send ids one attachments or delivery request names.
+pub const BATCH_LIMIT: usize = 100;
+
+/// Most messages in one timeline page, and the page size when none is asked.
+pub const PAGE_LIMIT: i64 = 100;
+/// Timeline page size when the caller names none.
+pub const DEFAULT_PAGE: i64 = 50;
+
+/// Maximum UTF-8 bytes in one text send.
+pub const TEXT_LIMIT_BYTES: usize = 16 * 1024;
+/// Edits and deletes are offered for this long after the original send.
+pub const MUTATION_WINDOW_SECONDS: i64 = 10 * 60;
+
+/// Operations implemented for a chat, independent of current permission.
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatCapabilities))]
+pub struct Capabilities {
+ /// Maximum UTF-8 bytes per text send.
+ pub text_limit_bytes: u32,
+ /// Supported message operations.
+ pub mutations: Vec<Mutation>,
+ /// Edits and deletes are offered this long after sending.
+ pub mutation_window_seconds: u32,
+ /// Attachments per send.
+ pub max_attachments: u8,
+ /// Largest attachment in bytes.
+ pub attachment_limit_bytes: u32,
+ /// Supported attachment MIME types.
+ pub attachment_types: Vec<String>,
+}
+
+impl Default for Capabilities {
+ fn default() -> Self {
+ Self {
+ text_limit_bytes: TEXT_LIMIT_BYTES as u32,
+ mutations: vec![Mutation::Reaction, Mutation::Edit, Mutation::Delete],
+ mutation_window_seconds: MUTATION_WINDOW_SECONDS as u32,
+ max_attachments: 1,
+ attachment_limit_bytes: MEDIA_LIMIT_BYTES as u32,
+ attachment_types: SENDABLE_MEDIA
+ .iter()
+ .map(|(mime, _)| (*mime).to_owned())
+ .collect(),
+ }
+ }
+}
+
+/// Why the viewer cannot send now.
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "schema", schema(as = ExternalChatReplyBlock))]
+pub enum ReplyBlock {
+ /// The viewer has not connected their own account at the provider.
+ NoAccount,
+ /// The viewer's provider account is not connected.
+ Disconnected,
+ /// The viewer has not added this chat from their own account.
+ NotAdded,
+ /// The viewer is not in the chat, or may not post in it now.
+ NotAllowed,
+ /// The chat is detached.
+ Unavailable,
+}
+
+/// Who is sending, as authenticated by the invoking path.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum SendAuthority {
+ /// A person acting in the app.
+ Human,
+ /// The person's verified API credential; sends only from their own account.
+ UserApiToken,
+ /// Acting for the person without being them: a session carrying an agent
+ /// attribution. Bots, harnesses and internal callers never reach these use
+ /// cases (`UserOnly` rejects them). Never sends to WhatsApp.
+ Automation,
+}
+
+/// Sanitized failures safe to show in the native UI.
+#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
+pub enum ExternalChatError {
+ /// Invalid input with actionable detail.
+ #[error("{0}")]
+ Invalid(&'static str),
+ /// The chat can't be added to Macro.
+ #[error("{0}")]
+ CannotAdd(CannotAdd),
+ /// Absent or outside the caller's authorized scope.
+ #[error("not found")]
+ NotFound,
+ /// The caller lacks authority for this operation.
+ #[error("you do not have permission for this WhatsApp operation")]
+ Forbidden,
+ /// Conflicts with an existing identity, transaction or idempotency key.
+ #[error("this conflicts with an existing WhatsApp connection or request")]
+ Conflict,
+ /// The account is not connected.
+ #[error("connect your WhatsApp account first")]
+ Disconnected,
+ /// The intent expired before it was attempted.
+ #[error("send_expired")]
+ SendExpired,
+ /// The session-service lease epoch is stale; the replica must stop the account.
+ #[error("lease_fenced")]
+ Fenced,
+ /// The person's deliberate send request budget is exhausted.
+ #[error("wait one second before sending another WhatsApp request")]
+ RateLimited,
+ /// Retryable infrastructure failure.
+ #[error("WhatsApp is temporarily unavailable; retry shortly")]
+ Unavailable,
+ /// The account has too many events waiting to be stored; the session
+ /// service leaves the event unacknowledged and WhatsApp redelivers it.
+ #[error("too many WhatsApp events are waiting for this account")]
+ Backlogged,
+ /// Stored data broke an invariant; retrying does not help. The report
+ /// carries the cause.
+ #[error("something went wrong with this WhatsApp chat")]
+ Internal,
+}
+
+impl ExternalChatError {
+ /// Stable code for logs, spans and clients.
+ pub const fn code(&self) -> &'static str {
+ match self {
+ Self::Invalid(_) => "invalid_input",
+ Self::CannotAdd(CannotAdd::NotInGroup) => "cannot_add_not_in_group",
+ Self::CannotAdd(CannotAdd::Community) => "cannot_add_community",
+ Self::CannotAdd(CannotAdd::OwnChat) => "cannot_add_own_chat",
+ Self::CannotAdd(CannotAdd::NotInChatList) => "cannot_add_not_in_chat_list",
+ Self::NotFound => "not_found",
+ Self::Forbidden => "forbidden",
+ Self::Conflict => "conflict",
+ Self::Disconnected => "disconnected",
+ Self::SendExpired => "send_expired",
+ Self::Fenced => "lease_fenced",
+ Self::RateLimited => "rate_limited",
+ Self::Unavailable => "temporarily_unavailable",
+ Self::Backlogged => "backlogged",
+ Self::Internal => "internal_error",
+ }
+ }
+}
+
+/// Pixel size and length of an attachment, where known.
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
+pub struct MediaShape {
+ /// Width in pixels (images and videos).
+ pub width: Option<u32>,
+ /// Height in pixels.
+ pub height: Option<u32>,
+ /// Length in whole seconds (audio and video).
+ pub duration_seconds: Option<u32>,
+}
+
+/// One attachment of one source message.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct AttachmentRef {
+ /// Chat the message belongs to.
+ pub chat_id: Uuid,
+ /// Verified author.
+ pub person_id: Uuid,
+ /// The provider's message id.
+ pub source_id: String,
+ /// Position within the message.
+ pub ordinal: i16,
+}
diff --git a/crates/external_chat/src/domain/ports.rs b/crates/external_chat/src/domain/ports.rs
new file mode 100644
index 0000000000..da8da158e8
--- /dev/null
+++ b/crates/external_chat/src/domain/ports.rs
@@ -0,0 +1,347 @@
+//! Capabilities the external chat use cases need. The Postgres adapter
+//! implements the repositories, a provider crate implements [`ChatProvider`],
+//! and DSS supplies the notification publisher, file store and rate limiter.
+//!
+//! Repository methods that change state are atomic and re-check, under lock, the
+//! facts their contract names. Methods that signal another process do it with
+//! `NOTIFY` inside the same transaction, so a signal is never sent for a change
+//! that did not commit.
+
+use super::{models::*, timeline::view::People};
+use async_trait::async_trait;
+use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+/// Result of every port and service call.
+pub type ExternalChatResult<T> = Result<T, rootcause::Report<ExternalChatError>>;
+
+/// A person's account at a provider, as the generic use cases need it.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct AccountRef {
+ /// The provider account.
+ pub id: Uuid,
+ /// The Macro user who linked it.
+ pub user_id: String,
+ /// Its identity at the provider (WhatsApp: the verified JID), once known.
+ pub identity: Option<String>,
+ /// Observation consent is active; only then can the person send.
+ pub connected: bool,
+ /// Consent generation the other facts were read at; a send recorded for
+ /// it fails once the person reconnects or disconnects.
+ pub generation: i64,
+}
+
+/// The facts that decide whether a person can send in a chat now.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SendingFacts {
+ /// The account's membership has consent at its current generation.
+ pub observing: bool,
+ /// The account is in the chat.
+ pub remote_member: bool,
+ /// The account may post under current chat settings.
+ pub can_post: bool,
+}
+
+/// What a mutation request points at.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct MutationTarget {
+ /// Source time of the original message.
+ pub source_at: DateTime<Utc>,
+ /// The original was sent from this verified identity.
+ pub sent_by: Vec<String>,
+ /// Already deleted.
+ pub deleted: bool,
+}
+
+/// An earlier send found by its intent key.
+#[derive(Debug, Clone)]
+pub struct StoredSend {
+ /// SHA-256 of its canonical request; a retry must match it.
+ pub request_sha256: [u8; 32],
+ /// Where it got to.
+ pub progress: SendProgress,
+}
+
+/// A validated intent, authorized by the domain.
+pub struct SendIntent<'a> {
+ /// Sending person.
+ pub user: &'a MacroUserIdStr<'a>,
+ /// Their account at its current generation.
+ pub account: &'a AccountRef,
+ /// Chat.
+ pub chat: &'a ChatRef,
+ /// Request.
+ pub request: &'a SendReply,
+ /// SHA-256 of the canonical request.
+ pub digest: [u8; 32],
+}
+
+/// Who is in a chat, as its observers last read it.
+#[derive(Debug, Clone)]
+pub struct Members {
+ /// The members.
+ pub members: super::timeline::ExternalChatMembers,
+ /// The provider's disappearing-messages timer in seconds while it is on.
+ pub disappearing_seconds: Option<u32>,
+}
+
+/// One provider of external chats (WhatsApp). The generic use cases decide
+/// access, validation and what to show; the provider owns its accounts,
+/// membership and the outbox that sends.
+#[async_trait]
+pub trait ChatProvider: Send + Sync + 'static {
+ /// Operations and limits of a chat.
+ fn capabilities(&self, chat: &ChatRef) -> Capabilities;
+ /// Check the people a text @-mentions: each a person's id at the
+ /// provider, named in the text the way the provider renders it.
+ fn validate_mentions(&self, text: &str, mentions: &[String]) -> ExternalChatResult<()>;
+ /// The person's own account at the provider, in any state.
+ async fn account(&self, user: &MacroUserIdStr<'_>) -> ExternalChatResult<Option<AccountRef>>;
+ /// Whether `account` can send in each of `chats` now, in order.
+ async fn sending_facts(
+ &self,
+ account: &AccountRef,
+ chats: &[ChatRef],
+ ) -> ExternalChatResult<Vec<SendingFacts>>;
+ /// The community each of `chats` belongs to, in order.
+ async fn communities(
+ &self,
+ chats: &[ChatRef],
+ ) -> ExternalChatResult<Vec<Option<CommunityReference>>>;
+ /// Who is in the chat; `None` when no observer has read it.
+ async fn members(&self, chat: &ChatRef) -> ExternalChatResult<Option<Members>>;
+ /// Names for the provider ids (mentions, system lines) of a chat.
+ async fn people(&self, chat: &ChatRef, ids: &[String]) -> ExternalChatResult<People>;
+ /// Ask the chat's observers and the viewer's own account to read the
+ /// chat again; the provider rate limits refreshes.
+ async fn request_refresh(&self, chat: &ChatRef, viewer: &str) -> ExternalChatResult<()>;
+ /// Validate and add one of the person's provider chats as their own
+ /// external chat, with their account's membership; adding it again
+ /// returns the existing one. Signals the provider.
+ async fn adopt(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ request: &AddChat,
+ ) -> ExternalChatResult<ChatRef>;
+ /// Detach locally: end observing consents and fail undispatched sends.
+ /// History stays. Signals the observers and `external_chat_changed`.
+ async fn detach(&self, chat: &ChatRef) -> ExternalChatResult<()>;
+ /// The source of a message in this chat, for quotes and mutations.
+ async fn mutation_target(
+ &self,
+ chat: &ChatRef,
+ message: Uuid,
+ ) -> ExternalChatResult<Option<MutationTarget>>;
+ /// The person's earlier send with this intent key in the chat.
+ async fn stored_send(
+ &self,
+ chat_id: Uuid,
+ user: &MacroUserIdStr<'_>,
+ idempotency_key: Uuid,
+ ) -> ExternalChatResult<Option<StoredSend>>;
+ /// Record the intent and assign the chat sequence. Replays with the same
+ /// key return the stored progress; a different payload is a `Conflict`.
+ /// Inserts only while the account is connected at its generation, the
+ /// chat is active and the membership still observes. Signals the sender.
+ async fn enqueue_send(&self, intent: SendIntent<'_>) -> ExternalChatResult<SendProgress>;
+ /// The person's sends in a chat that are not yet accepted, newest first.
+ async fn open_sends(
+ &self,
+ chat_id: Uuid,
+ user: &MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<Vec<SendProgress>>;
+ /// The person's sends for these chat messages or intent ids.
+ async fn sends_for(
+ &self,
+ chat_id: Uuid,
+ user: &MacroUserIdStr<'_>,
+ ids: &[Uuid],
+ ) -> ExternalChatResult<Vec<SendProgress>>;
+ /// The state of the viewer's newest send of each of these messages.
+ async fn delivery_states(
+ &self,
+ chat_id: Uuid,
+ viewer: &str,
+ messages: &[Uuid],
+ ) -> ExternalChatResult<Vec<(Uuid, DeliveryState)>>;
+}
+
+/// Shared request budget for deliberate sends, keyed by authenticated person.
+#[async_trait]
+pub trait SendRateLimiter: Send + Sync {
+ /// Consume one request across all chats and service replicas. Requests
+ /// rejected later still count; infrastructure failure must fail closed.
+ async fn check(&self, user: &MacroUserIdStr<'_>) -> ExternalChatResult<()>;
+}
+
+/// A validated attachment upload.
+pub struct StagedUpload<'a> {
+ /// Uploader, who will own the static file.
+ pub user: &'a MacroUserIdStr<'a>,
+ /// Chat it may be sent to.
+ pub chat_id: Uuid,
+ /// Display filename.
+ pub filename: &'a str,
+ /// Supported MIME type.
+ pub mime: &'a str,
+ /// Bytes, at most [`MEDIA_LIMIT_BYTES`].
+ pub bytes: &'a [u8],
+ /// SHA-256 of `bytes`.
+ pub sha256: [u8; 32],
+ /// Pixel size and length read from the file.
+ pub shape: MediaShape,
+}
+
+/// Added external chat entities and their staged uploads.
+#[async_trait]
+pub trait ChatRepository: Send + Sync {
+ /// The chat behind an external chat entity.
+ async fn chat(&self, chat_id: Uuid) -> ExternalChatResult<Option<ChatRef>>;
+ /// Attachments of these messages of the chat.
+ async fn message_attachments(
+ &self,
+ chat: &ChatRef,
+ messages: &[Uuid],
+ ) -> ExternalChatResult<Vec<MessageAttachments>>;
+ /// Stage an attachment for a later send by the same person in the same chat.
+ async fn stage_upload(&self, upload: StagedUpload<'_>) -> ExternalChatResult<Uuid>;
+ /// MIME type of the person's unsent staged upload in this chat.
+ async fn staged_upload_mime(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ chat_id: Uuid,
+ upload: Uuid,
+ ) -> ExternalChatResult<Option<String>>;
+}
+
+/// Who wrote a message.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Origin {
+ /// Someone at the provider: a person's phone or app.
+ Remote,
+ /// A deliberate send from Macro.
+ Macro,
+ /// Earlier messages imported after adding the chat.
+ History,
+}
+
+/// A chat message revision that still needs generic notifications.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ChatEffect {
+ /// External chat entity.
+ pub chat_id: Uuid,
+ /// Chat message.
+ pub message_id: Uuid,
+ /// Revision to publish.
+ pub revision: i64,
+ /// Nothing was published for this message before.
+ pub first_publication: bool,
+ /// Who wrote it.
+ pub origin: Origin,
+ /// Verified Macro user behind a phone-origin message, never notified of it.
+ pub associated_user: Option<String>,
+ /// A system line (added, left, renamed…), never a notification.
+ pub system: bool,
+}
+
+impl ChatEffect {
+ /// Only a newly received live message creates a notification.
+ pub fn should_notify(&self) -> bool {
+ self.first_publication && self.origin != Origin::History && !self.system
+ }
+}
+
+/// The notification outbox.
+#[async_trait]
+pub trait EffectsRepository: Send + Sync {
+ /// Due revisions, oldest first.
+ async fn pending_effects(&self, limit: i64) -> ExternalChatResult<Vec<ChatEffect>>;
+ /// Mark exactly this revision published; later revisions stay due.
+ async fn published(&self, message: Uuid, revision: i64) -> ExternalChatResult<()>;
+ /// Retry this message later without blocking others.
+ async fn defer_effect(&self, message: Uuid) -> ExternalChatResult<()>;
+}
+
+/// What a new message's notification names.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct MessageNotice {
+ /// The service the chat lives in.
+ pub provider: ExternalChatProvider,
+ /// The chat's name.
+ pub chat_name: String,
+ /// The message was deleted, or edited before it was announced.
+ pub superseded: bool,
+}
+
+/// Who a change or a notification reaches, beyond ordinary entity grants.
+#[async_trait]
+pub trait AudienceRepository: Send + Sync {
+ /// People who can still view the chat through link or team sharing; they
+ /// become known once they read it.
+ async fn link_viewers(&self, chat: Uuid) -> ExternalChatResult<Vec<String>>;
+ /// The facts a message's notification needs; `None` if it is gone.
+ async fn message_notice(
+ &self,
+ chat: Uuid,
+ message: Uuid,
+ ) -> ExternalChatResult<Option<MessageNotice>>;
+}
+
+/// Generic notifications for current chat viewers.
+#[async_trait]
+pub trait ChatPublisher: Send + Sync {
+ /// Publish one message revision to its current viewers.
+ async fn publish(&self, effect: &ChatEffect) -> ExternalChatResult<()>;
+}
+
+/// Downloaded or uploaded bytes waiting to become a static file.
+#[derive(Clone)]
+pub struct FileImport {
+ /// Which attachment.
+ pub attachment: AttachmentRef,
+ /// The staged bytes' id (the upload id of a file sent from Macro).
+ pub blob_id: Uuid,
+ /// The chat the attachment belongs to.
+ pub chat_id: Uuid,
+ /// Owner of the static file: the uploader, or the chat's owner.
+ pub owner: String,
+ /// Display filename.
+ pub filename: String,
+ /// MIME type.
+ pub mime: String,
+ /// Earlier failed imports.
+ pub attempts: i32,
+}
+
+/// The import queue between blobs and static files, and the static files no
+/// attachment references any more.
+#[async_trait]
+pub trait MediaRepository: Send + Sync {
+ /// Imports whose retry time has come, claimed, without their bytes.
+ async fn due_imports(&self, limit: i64) -> ExternalChatResult<Vec<FileImport>>;
+ /// One claimed import's plaintext bytes; `None` once the blob is gone.
+ async fn import_bytes(&self, import: &FileImport) -> ExternalChatResult<Option<Vec<u8>>>;
+ /// `file_id` holds the bytes: attach it to the message, drop the blob and
+ /// queue publication. A file the attachment no longer takes (deleted, or
+ /// imported meanwhile) is queued for deletion instead.
+ async fn import_stored(&self, import: &FileImport, file_id: Uuid) -> ExternalChatResult<()>;
+ /// Retry later, or give up and show a notice on the message.
+ async fn import_failed(&self, import: &FileImport, permanent: bool) -> ExternalChatResult<()>;
+ /// Delete staged uploads nobody sent.
+ async fn expire_uploads(&self) -> ExternalChatResult<u64>;
+ /// Static files no attachment references any more, claimed for deletion.
+ async fn due_file_deletions(&self, limit: i64) -> ExternalChatResult<Vec<Uuid>>;
+ /// The static file service no longer holds these files.
+ async fn files_deleted(&self, files: &[Uuid]) -> ExternalChatResult<()>;
+}
+
+/// Static file storage, the same as channel media's.
+#[async_trait]
+pub trait FileStore: Send + Sync {
+ /// Store the bytes as a new static file owned by `import.owner`; returns
+ /// its id.
+ async fn store(&self, import: &FileImport, bytes: Vec<u8>) -> ExternalChatResult<Uuid>;
+ /// Delete one static file; one that is already gone counts as deleted.
+ async fn delete(&self, file_id: Uuid) -> ExternalChatResult<()>;
+}
diff --git a/crates/whatsapp_linked/src/domain/rich.rs b/crates/external_chat/src/domain/rich.rs
similarity index 100%
rename from crates/whatsapp_linked/src/domain/rich.rs
rename to crates/external_chat/src/domain/rich.rs
diff --git a/crates/whatsapp_linked/src/domain/rich/test.rs b/crates/external_chat/src/domain/rich/test.rs
similarity index 100%
rename from crates/whatsapp_linked/src/domain/rich/test.rs
rename to crates/external_chat/src/domain/rich/test.rs
diff --git a/crates/external_chat/src/domain/service.rs b/crates/external_chat/src/domain/service.rs
new file mode 100644
index 0000000000..665128a606
--- /dev/null
+++ b/crates/external_chat/src/domain/service.rs
@@ -0,0 +1,453 @@
+//! Adding, detaching and deliberate sends of external chats, as served by DSS.
+
+use super::{models::*, ports::*, timeline::*};
+use chrono::{DateTime, Utc};
+use entity_access::domain::models::{EntityAccessReceipt, EntityType, RequiredPermission};
+use macro_user_id::user_id::MacroUserIdStr;
+use sha2::{Digest, Sha256};
+use std::sync::Arc;
+use uuid::Uuid;
+
+#[cfg(test)]
+mod test;
+mod timeline;
+
+/// DSS use cases for external chats of one provider.
+pub struct ExternalChatService<P> {
+ provider: Arc<P>,
+ chats: Arc<dyn ChatRepository>,
+ timeline: Arc<dyn TimelineRepository>,
+ send_rate_limiter: Arc<dyn SendRateLimiter>,
+}
+
+impl<P> Clone for ExternalChatService<P> {
+ fn clone(&self) -> Self {
+ Self {
+ provider: self.provider.clone(),
+ chats: self.chats.clone(),
+ timeline: self.timeline.clone(),
+ send_rate_limiter: self.send_rate_limiter.clone(),
+ }
+ }
+}
+
+impl<P: ChatProvider> ExternalChatService<P> {
+ /// Wire adapters at the composition root.
+ pub fn new<R>(
+ provider: Arc<P>,
+ repository: Arc<R>,
+ send_rate_limiter: Arc<dyn SendRateLimiter>,
+ ) -> Self
+ where
+ R: ChatRepository + TimelineRepository + 'static,
+ {
+ Self {
+ provider,
+ chats: repository.clone(),
+ timeline: repository,
+ send_rate_limiter,
+ }
+ }
+
+ /// Add an existing chat of the person's provider account, such as a
+ /// WhatsApp group they joined, as their own external chat; never creates
+ /// or joins anything at the provider. Adding the same chat twice returns
+ /// the existing one.
+ pub async fn add(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ request: &AddChat,
+ authority: SendAuthority,
+ ) -> ExternalChatResult<ChatRef> {
+ human(authority)?;
+ self.provider.adopt(user, request).await
+ }
+
+ /// Detach locally; nothing changes at the provider. Only the person who
+ /// added the chat, in their own session, stops syncing it.
+ #[tracing::instrument(name = "whatsapp.linked.detach", skip_all, err, fields(chat_id))]
+ pub async fn detach<T: RequiredPermission>(
+ &self,
+ access: &EntityAccessReceipt<T>,
+ authority: SendAuthority,
+ ) -> ExternalChatResult<()> {
+ human(authority)?;
+ let (user, chat_id) = receipt_scope(access)?;
+ tracing::Span::current().record("chat_id", tracing::field::display(chat_id));
+ let chat = self
+ .chats
+ .chat(chat_id)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ if chat.user_id != user.as_ref() {
+ return Err(ExternalChatError::Forbidden.into());
+ }
+ self.provider.detach(&chat).await
+ }
+
+ async fn reply_block(
+ &self,
+ account: Option<&AccountRef>,
+ chat: &ChatRef,
+ ) -> ExternalChatResult<Option<ReplyBlock>> {
+ let facts = match account {
+ Some(account) if account.connected => self
+ .provider
+ .sending_facts(account, std::slice::from_ref(chat))
+ .await?
+ .pop(),
+ _ => None,
+ };
+ Ok(reply_block(account, chat, facts.as_ref()))
+ }
+
+ /// Record a deliberate send for the person's own account. The provider
+ /// dispatches it; uncertain outcomes are never resent.
+ #[tracing::instrument(
+ name = "whatsapp.linked.send",
+ skip_all,
+ err,
+ fields(chat_id, send_id, kind)
+ )]
+ pub async fn send_reply<T: RequiredPermission>(
+ &self,
+ access: &EntityAccessReceipt<T>,
+ request: &SendReply,
+ authority: SendAuthority,
+ ) -> ExternalChatResult<SendProgress> {
+ sender(authority)?;
+ let (user, chat_id) = receipt_scope(access)?;
+ tracing::Span::current().record("chat_id", tracing::field::display(chat_id));
+ validate_request(request, |text, mentions| {
+ self.provider.validate_mentions(text, mentions)
+ })?;
+ let digest: [u8; 32] = Sha256::digest(
+ serde_json::to_vec(request)
+ .map_err(|_| ExternalChatError::Invalid("invalid request"))?,
+ )
+ .into();
+ // A retry with the same intent key returns the original send, whatever
+ // happened since; it never sends twice.
+ if let Some(StoredSend {
+ request_sha256,
+ progress,
+ }) = self
+ .provider
+ .stored_send(chat_id, user, request.idempotency_key)
+ .await?
+ {
+ if request_sha256 != digest {
+ return Err(ExternalChatError::Conflict.into());
+ }
+ tracing::Span::current().record("send_id", tracing::field::display(progress.id));
+ return Ok(progress);
+ }
+ self.send_rate_limiter.check(user).await?;
+ let chat = self
+ .chats
+ .chat(chat_id)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ let account = self.provider.account(user).await?;
+ if let Some(block) = self.reply_block(account.as_ref(), &chat).await? {
+ return Err(refusal(block).into());
+ }
+ let account = account.ok_or(ExternalChatError::Disconnected)?;
+ let identity = account
+ .identity
+ .as_deref()
+ .ok_or(ExternalChatError::Disconnected)?;
+ if let Some(quoted) = request.reply_to {
+ self.provider
+ .mutation_target(&chat, quoted)
+ .await?
+ .filter(|target| !target.deleted)
+ .ok_or(ExternalChatError::NotFound)?;
+ }
+ if let ReplyContent::Media { upload_id, caption } = &request.message {
+ let mime = self
+ .chats
+ .staged_upload_mime(user, chat_id, *upload_id)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ if MediaKind::of(&mime) == Some(MediaKind::Audio) && caption.is_some() {
+ return Err(ExternalChatError::Invalid(
+ "send an audio caption as a separate message",
+ )
+ .into());
+ }
+ }
+ if let ReplyContent::Mutation {
+ message_id,
+ operation,
+ ..
+ } = &request.message
+ {
+ tracing::Span::current().record("kind", tracing::field::debug(operation));
+ let target = self
+ .provider
+ .mutation_target(&chat, *message_id)
+ .await?
+ .filter(|target| !target.deleted)
+ .ok_or(ExternalChatError::NotFound)?;
+ authorize_mutation(*operation, &target, identity, Utc::now())?;
+ }
+ let progress = self
+ .provider
+ .enqueue_send(SendIntent {
+ user,
+ account: &account,
+ chat: &chat,
+ request,
+ digest,
+ })
+ .await?;
+ tracing::Span::current().record("send_id", tracing::field::display(progress.id));
+ Ok(progress)
+ }
+
+ /// Stage one attachment for a later deliberate send in this chat. Nothing
+ /// is sent until a media reply names it.
+ #[tracing::instrument(name = "whatsapp.linked.upload", skip_all, err, fields(chat_id, size = bytes.len()))]
+ pub async fn upload_attachment<T: RequiredPermission>(
+ &self,
+ access: &EntityAccessReceipt<T>,
+ filename: &str,
+ mime: &str,
+ bytes: &[u8],
+ authority: SendAuthority,
+ ) -> ExternalChatResult<UploadedAttachment> {
+ sender(authority)?;
+ let (user, chat_id) = receipt_scope(access)?;
+ tracing::Span::current().record("chat_id", tracing::field::display(chat_id));
+ let kind = MediaKind::of(mime).ok_or(ExternalChatError::Invalid(
+ "this file type cannot be sent to WhatsApp",
+ ))?;
+ let filename = filename.trim();
+ if bytes.is_empty()
+ || bytes.len() > MEDIA_LIMIT_BYTES
+ || filename.is_empty()
+ || filename.len() > 255
+ || filename
+ .chars()
+ .any(|c| c.is_control() || c == '/' || c == '\\')
+ {
+ return Err(ExternalChatError::Invalid(
+ "an attachment needs a filename and must be non-empty and within the size limit",
+ )
+ .into());
+ }
+ let chat = self
+ .chats
+ .chat(chat_id)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ let account = self.provider.account(user).await?;
+ if let Some(block) = self.reply_block(account.as_ref(), &chat).await? {
+ return Err(refusal(block).into());
+ }
+ let upload_id = self
+ .chats
+ .stage_upload(StagedUpload {
+ user,
+ chat_id,
+ filename,
+ mime,
+ bytes,
+ sha256: Sha256::digest(bytes).into(),
+ shape: super::shape::measure(kind, mime, bytes),
+ })
+ .await?;
+ Ok(UploadedAttachment {
+ upload_id,
+ filename: filename.to_owned(),
+ kind,
+ })
+ }
+
+ /// Attachment states of messages the viewer can read. The message body
+ /// keeps a text fallback for readers that don't know about them.
+ pub async fn message_attachments<T: RequiredPermission>(
+ &self,
+ access: &EntityAccessReceipt<T>,
+ messages: &[Uuid],
+ ) -> ExternalChatResult<Vec<MessageAttachments>> {
+ let (_, chat_id) = view_scope(access)?;
+ if messages.len() > BATCH_LIMIT {
+ return Err(ExternalChatError::Invalid("too many messages in one request").into());
+ }
+ let chat = self
+ .chats
+ .chat(chat_id)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ self.chats.message_attachments(&chat, messages).await
+ }
+
+ /// The viewer's sends for these intents or chat messages.
+ pub async fn delivery<T: RequiredPermission>(
+ &self,
+ access: &EntityAccessReceipt<T>,
+ ids: &[Uuid],
+ ) -> ExternalChatResult<Vec<SendProgress>> {
+ let (user, chat_id) = receipt_scope(access)?;
+ if ids.len() > BATCH_LIMIT {
+ return Err(ExternalChatError::Invalid("too many messages in one request").into());
+ }
+ self.provider.sends_for(chat_id, user, ids).await
+ }
+}
+
+fn human(authority: SendAuthority) -> ExternalChatResult<()> {
+ match authority {
+ SendAuthority::Human => Ok(()),
+ SendAuthority::UserApiToken | SendAuthority::Automation => {
+ Err(ExternalChatError::Forbidden.into())
+ }
+ }
+}
+
+fn sender(authority: SendAuthority) -> ExternalChatResult<()> {
+ match authority {
+ SendAuthority::Human | SendAuthority::UserApiToken => Ok(()),
+ SendAuthority::Automation => Err(ExternalChatError::Forbidden.into()),
+ }
+}
+
+/// The external chat a receipt names.
+fn receipt_chat<T: RequiredPermission>(
+ access: &EntityAccessReceipt<T>,
+) -> ExternalChatResult<Uuid> {
+ let entity = access.entity();
+ if entity.entity_type != EntityType::ExternalChat {
+ return Err(ExternalChatError::Forbidden.into());
+ }
+ Ok(Uuid::parse_str(&entity.entity_id).map_err(|_| ExternalChatError::NotFound)?)
+}
+
+/// The person and chat of a receipt held by a person.
+fn receipt_scope<T: RequiredPermission>(
+ access: &EntityAccessReceipt<T>,
+) -> ExternalChatResult<(&MacroUserIdStr<'static>, Uuid)> {
+ let user = access
+ .get_authenticated_user()
+ .map_err(|_| ExternalChatError::Forbidden)?;
+ Ok((user, receipt_chat(access)?))
+}
+
+/// Read receipts may represent a bot, but never borrow its acting user's
+/// provider identity, send records or unread state.
+fn view_scope<T: RequiredPermission>(
+ access: &EntityAccessReceipt<T>,
+) -> ExternalChatResult<(&str, Uuid)> {
+ let principal = match access.auth() {
+ entity_access::domain::models::EntityAccessAuth::Authenticated(user) => user.as_ref(),
+ entity_access::domain::models::EntityAccessAuth::Bot(bot) => bot.bot_id_str().as_ref(),
+ _ => return Err(ExternalChatError::Forbidden.into()),
+ };
+ Ok((principal, receipt_chat(access)?))
+}
+
+/// The error a send or upload refused for `block` returns.
+fn refusal(block: ReplyBlock) -> ExternalChatError {
+ match block {
+ ReplyBlock::NoAccount | ReplyBlock::Disconnected => ExternalChatError::Disconnected,
+ ReplyBlock::NotAdded | ReplyBlock::NotAllowed | ReplyBlock::Unavailable => {
+ ExternalChatError::Forbidden
+ }
+ }
+}
+
+/// Why the person cannot send now, from the current facts.
+pub(crate) fn reply_block(
+ account: Option<&AccountRef>,
+ chat: &ChatRef,
+ facts: Option<&SendingFacts>,
+) -> Option<ReplyBlock> {
+ let Some(account) = account else {
+ return Some(ReplyBlock::NoAccount);
+ };
+ if !account.connected {
+ return Some(ReplyBlock::Disconnected);
+ }
+ if chat.state != ChatState::Active {
+ return Some(ReplyBlock::Unavailable);
+ }
+ let Some(facts) = facts.filter(|facts| facts.observing) else {
+ return Some(ReplyBlock::NotAdded);
+ };
+ if !facts.remote_member || !facts.can_post {
+ return Some(ReplyBlock::NotAllowed);
+ }
+ None
+}
+
+fn validate_text(text: &str) -> ExternalChatResult<()> {
+ if text.trim().is_empty() || text.len() > TEXT_LIMIT_BYTES {
+ return Err(ExternalChatError::Invalid(
+ "the message is empty or longer than the text limit",
+ )
+ .into());
+ }
+ Ok(())
+}
+
+/// Check a request before anything is stored; the provider checks mentions.
+pub(crate) fn validate_request(
+ request: &SendReply,
+ validate_mentions: impl FnOnce(&str, &[String]) -> ExternalChatResult<()>,
+) -> ExternalChatResult<()> {
+ if request.idempotency_key.is_nil() {
+ return Err(ExternalChatError::Invalid("a stable send intent key is required").into());
+ }
+ match &request.message {
+ ReplyContent::Text { text, mentions } => {
+ validate_text(text)?;
+ validate_mentions(text, mentions)
+ }
+ ReplyContent::Media { caption: None, .. } => Ok(()),
+ ReplyContent::Media {
+ caption: Some(caption),
+ ..
+ } => validate_text(caption),
+ ReplyContent::Mutation {
+ operation: Mutation::Edit,
+ text: Some(text),
+ ..
+ } => validate_text(text),
+ ReplyContent::Mutation {
+ operation: Mutation::Reaction,
+ text: Some(emoji),
+ ..
+ } if emoji.chars().count() <= 32 && !emoji.chars().any(char::is_control) => Ok(()),
+ ReplyContent::Mutation {
+ operation: Mutation::Delete,
+ text: None,
+ ..
+ } => Ok(()),
+ ReplyContent::Mutation { .. } => {
+ Err(ExternalChatError::Invalid("invalid message operation").into())
+ }
+ }
+}
+
+/// Edits and deletes are limited to the person's own recent messages.
+pub(crate) fn authorize_mutation(
+ operation: Mutation,
+ target: &MutationTarget,
+ identity: &str,
+ now: DateTime<Utc>,
+) -> ExternalChatResult<()> {
+ if operation == Mutation::Reaction {
+ return Ok(());
+ }
+ if !target.sent_by.iter().any(|id| id == identity) {
+ return Err(ExternalChatError::Forbidden.into());
+ }
+ if now - target.source_at > chrono::Duration::seconds(MUTATION_WINDOW_SECONDS) {
+ return Err(
+ ExternalChatError::Invalid("the time to change this message has passed").into(),
+ );
+ }
+ Ok(())
+}
diff --git a/crates/external_chat/src/domain/service/test.rs b/crates/external_chat/src/domain/service/test.rs
new file mode 100644
index 0000000000..acc5f4776e
--- /dev/null
+++ b/crates/external_chat/src/domain/service/test.rs
@@ -0,0 +1,176 @@
+use super::*;
+use chrono::Duration;
+
+fn account(connected: bool) -> AccountRef {
+ AccountRef {
+ id: Uuid::from_u128(1),
+ user_id: "macro|[email protected]".into(),
+ identity: Some("[email protected]".into()),
+ connected,
+ generation: 3,
+ }
+}
+
+fn chat(state: ChatState) -> ChatRef {
+ ChatRef {
+ kind: ExternalChatKind::Group,
+ id: Uuid::from_u128(2),
+ provider: ExternalChatProvider::WhatsApp,
+ remote_id: "[email protected]".into(),
+ user_id: "macro|[email protected]".into(),
+ state,
+ adopted_at: Utc::now(),
+ }
+}
+
+fn facts() -> SendingFacts {
+ SendingFacts {
+ observing: true,
+ remote_member: true,
+ can_post: true,
+ }
+}
+
+fn no_mentions(_: &str, mentions: &[String]) -> ExternalChatResult<()> {
+ assert!(mentions.is_empty());
+ Ok(())
+}
+
+#[test]
+fn reply_block_names_the_first_missing_fact() {
+ let active = chat(ChatState::Active);
+ let connected = account(true);
+ assert_eq!(reply_block(Some(&connected), &active, Some(&facts())), None);
+ assert_eq!(
+ reply_block(None, &active, None),
+ Some(ReplyBlock::NoAccount)
+ );
+ assert_eq!(
+ reply_block(Some(&account(false)), &active, None),
+ Some(ReplyBlock::Disconnected)
+ );
+ assert_eq!(
+ reply_block(Some(&connected), &chat(ChatState::Detached), Some(&facts())),
+ Some(ReplyBlock::Unavailable)
+ );
+ assert_eq!(
+ reply_block(Some(&connected), &active, None),
+ Some(ReplyBlock::NotAdded)
+ );
+ let mut announce = facts();
+ announce.can_post = false;
+ assert_eq!(
+ reply_block(Some(&connected), &active, Some(&announce)),
+ Some(ReplyBlock::NotAllowed)
+ );
+}
+
+#[test]
+fn requests_are_bounded_and_operations_carry_the_right_text() {
+ let key = Uuid::from_u128(9);
+ let message = Uuid::from_u128(10);
+ let valid = [
+ ReplyContent::Text {
+ text: "hi".into(),
+ mentions: vec![],
+ },
+ ReplyContent::Mutation {
+ message_id: message,
+ operation: Mutation::Reaction,
+ text: Some(String::new()),
+ },
+ ReplyContent::Mutation {
+ message_id: message,
+ operation: Mutation::Edit,
+ text: Some("fixed".into()),
+ },
+ ReplyContent::Mutation {
+ message_id: message,
+ operation: Mutation::Delete,
+ text: None,
+ },
+ ];
+ for content in valid {
+ validate_request(
+ &SendReply {
+ idempotency_key: key,
+ message: content,
+ reply_to: None,
+ },
+ no_mentions,
+ )
+ .unwrap();
+ }
+ let invalid = [
+ ReplyContent::Text {
+ text: " ".into(),
+ mentions: vec![],
+ },
+ ReplyContent::Text {
+ text: "x".repeat(TEXT_LIMIT_BYTES + 1),
+ mentions: vec![],
+ },
+ ReplyContent::Mutation {
+ message_id: message,
+ operation: Mutation::Delete,
+ text: Some("not allowed".into()),
+ },
+ ReplyContent::Mutation {
+ message_id: message,
+ operation: Mutation::Edit,
+ text: None,
+ },
+ ];
+ for content in invalid {
+ assert!(
+ validate_request(
+ &SendReply {
+ idempotency_key: key,
+ message: content,
+ reply_to: None,
+ },
+ no_mentions
+ )
+ .is_err()
+ );
+ }
+ assert!(
+ validate_request(
+ &SendReply {
+ idempotency_key: Uuid::nil(),
+ message: ReplyContent::Text {
+ text: "hi".into(),
+ mentions: vec![]
+ },
+ reply_to: None,
+ },
+ no_mentions
+ )
+ .is_err()
+ );
+}
+
+#[test]
+fn only_the_author_changes_a_recent_message() {
+ let now = Utc::now();
+ let mine = MutationTarget {
+ source_at: now - Duration::minutes(1),
+ sent_by: vec!["[email protected]".into(), "42@lid".into()],
+ deleted: false,
+ };
+ let identity = "[email protected]";
+ authorize_mutation(Mutation::Edit, &mine, identity, now).unwrap();
+ authorize_mutation(Mutation::Delete, &mine, identity, now).unwrap();
+ assert!(matches!(
+ authorize_mutation(Mutation::Edit, &mine, "[email protected]", now)
+ .unwrap_err()
+ .current_context(),
+ ExternalChatError::Forbidden
+ ));
+ authorize_mutation(Mutation::Reaction, &mine, "[email protected]", now).unwrap();
+ let old = MutationTarget {
+ source_at: now - Duration::minutes(11),
+ ..mine
+ };
+ assert!(authorize_mutation(Mutation::Edit, &old, identity, now).is_err());
+}
diff --git a/crates/whatsapp_linked/src/domain/service/timeline.rs b/crates/external_chat/src/domain/service/timeline.rs
similarity index 50%
rename from crates/whatsapp_linked/src/domain/service/timeline.rs
rename to crates/external_chat/src/domain/service/timeline.rs
index f22af3e749..6f8b19b7f3 100644
--- a/crates/whatsapp_linked/src/domain/service/timeline.rs
+++ b/crates/external_chat/src/domain/service/timeline.rs
@@ -1,5 +1,6 @@
-//! Authorized standalone timeline operations.
+//! Authorized timeline, read state and sharing operations.
use super::*;
+use crate::domain::rich::RichContent;
use entity_access::domain::models::{AccessLevel, EntityPermission};
use macro_user_id::user_id::MacroUserIdStr;
use models_permissions::share_permission::{
@@ -7,62 +8,79 @@ use models_permissions::share_permission::{
channel_share_permission::{UpdateChannelSharePermission, UpdateOperation},
team_share::{TeamShareLevel, TeamShareRequest, authorize_team_share},
};
-impl LinkedService {
- /// List all shared groups, including for viewers with no WhatsApp account.
+impl<P: ChatProvider> ExternalChatService<P> {
+ /// List all shared chats, including for viewers with no provider account.
#[tracing::instrument(name = "whatsapp.linked.list", skip_all, err, fields(count))]
- pub async fn adopted_groups(
+ pub async fn list(
&self,
user: &MacroUserIdStr<'_>,
- ) -> LinkedResult<Vec<ExternalChatSummary>> {
- let records = self.timeline.visible_groups(user).await?;
- let account = self.accounts.owned_account(user).await?;
+ ) -> ExternalChatResult<Vec<ExternalChatSummary>> {
+ let records = self.timeline.visible_chats(user).await?;
+ let account = self.provider.account(user).await?;
tracing::Span::current().record("count", records.len());
+ let chats: Vec<ChatRef> = records.iter().map(|record| record.chat.clone()).collect();
+ let sending = match &account {
+ Some(account) if account.connected && !chats.is_empty() => {
+ self.provider.sending_facts(account, &chats).await?
+ }
+ _ => vec![],
+ };
+ let communities = if chats.is_empty() {
+ vec![]
+ } else {
+ self.provider.communities(&chats).await?
+ };
+ let mut sending = sending.into_iter();
Ok(records
.into_iter()
- .map(|record| {
- let facts = account
- .as_ref()
- .map(|account| sending_facts(account, &record.binding, record.roster));
- let reason = reply_block(account.as_ref(), &record.binding, facts.as_ref());
- record.facts.summary(reason)
+ .zip(communities)
+ .map(|(record, community)| {
+ let facts = sending.next();
+ let reason = reply_block(account.as_ref(), &record.chat, facts.as_ref());
+ record.facts.summary(reason, community)
})
.collect())
}
- /// Authorized group detail, standard sharing and own sending identity.
+ /// Authorized chat detail, standard sharing and own sending identity.
#[tracing::instrument(name = "whatsapp.linked.detail", skip_all, err, fields(chat_id))]
- pub async fn group_detail<T: RequiredPermission>(
+ pub async fn detail<T: RequiredPermission>(
&self,
access: &EntityAccessReceipt<T>,
- ) -> LinkedResult<ExternalChatDetail> {
+ ) -> ExternalChatResult<ExternalChatDetail> {
let (user, id) = view_scope(access)?;
tracing::Span::current().record("chat_id", tracing::field::display(id));
- let binding = self
- .bindings
- .binding(id)
+ let chat = self
+ .chats
+ .chat(id)
.await?
- .ok_or(LinkedError::NotFound)?;
- // A bot viewer reads, but has no WhatsApp account of its own.
+ .ok_or(ExternalChatError::NotFound)?;
+ // A bot viewer reads, but has no provider account of its own.
let account = match access.get_authenticated_user() {
- Ok(person) => self.accounts.owned_account(person).await?,
+ Ok(person) => self.provider.account(person).await?,
Err(_) => None,
};
- let facts = self.timeline.group_facts(id, user).await?;
- let observers = self.groups.group_accounts(id, user).await?;
- self.groups.request_groups_refresh(&observers).await?;
- let reason = self.reply_block(account.as_ref(), &binding).await?;
+ let facts = self.timeline.chat_facts(id, user).await?;
+ self.provider.request_refresh(&chat, user).await?;
+ let reason = self.reply_block(account.as_ref(), &chat).await?;
let access_level = match access.entity_permission() {
EntityPermission::AccessLevel { access_level } => *access_level,
- _ => return Err(LinkedError::Forbidden.into()),
+ _ => return Err(ExternalChatError::Forbidden.into()),
};
- let roster = self.groups.binding_roster(&binding).await?;
+ let community = self
+ .provider
+ .communities(std::slice::from_ref(&chat))
+ .await?
+ .pop()
+ .flatten();
+ let members = self.provider.members(&chat).await?;
Ok(ExternalChatDetail {
- chat: facts.summary(reason),
+ chat: facts.summary(reason, community),
share_permission: self.timeline.permissions(id).await?,
access_level,
- disappearing_seconds: roster.as_ref().and_then(|r| r.disappearing_seconds),
- members: roster.as_ref().map(ExternalChatMembers::from),
- sending_identity: account.and_then(|a| a.identity_jid),
- limits: Capabilities::default(),
+ disappearing_seconds: members.as_ref().and_then(|m| m.disappearing_seconds),
+ members: members.map(|m| m.members),
+ sending_identity: account.and_then(|a| a.identity),
+ limits: self.provider.capabilities(&chat),
})
}
/// Read the complete timeline through view access, without remote
@@ -72,25 +90,77 @@ impl LinkedService {
access: &EntityAccessReceipt<T>,
before: Option<Uuid>,
limit: Option<i64>,
- ) -> LinkedResult<ExternalChatMessages> {
+ ) -> ExternalChatResult<ExternalChatMessages> {
let (user, id) = view_scope(access)?;
let limit = limit.unwrap_or(DEFAULT_PAGE);
if !(1..=PAGE_LIMIT).contains(&limit) {
- return Err(LinkedError::Invalid("the page size is out of range").into());
+ return Err(ExternalChatError::Invalid("the page size is out of range").into());
}
- self.timeline.messages(id, user, before, limit).await
+ let chat = self
+ .chats
+ .chat(id)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ let page = self.timeline.messages(&chat, user, before, limit).await?;
+ Ok(ExternalChatMessages {
+ messages: self.message_views(&chat, user, page.messages).await?,
+ next_cursor: page.next_cursor,
+ })
}
/// One message through view access, as the timeline shows it.
pub async fn message<T: RequiredPermission>(
&self,
access: &EntityAccessReceipt<T>,
message: Uuid,
- ) -> LinkedResult<ExternalChatMessage> {
+ ) -> ExternalChatResult<ExternalChatMessage> {
let (user, id) = view_scope(access)?;
- self.timeline
- .message(id, user, message)
+ let chat = self
+ .chats
+ .chat(id)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ let stored = self
+ .timeline
+ .message(&chat, user, message)
+ .await?
+ .ok_or(ExternalChatError::NotFound)?;
+ self.message_views(&chat, user, vec![stored])
.await?
- .ok_or_else(|| LinkedError::NotFound.into())
+ .pop()
+ .ok_or_else(|| ExternalChatError::NotFound.into())
+ }
+ /// Stored messages as `viewer` sees them: the people they name and the
+ /// viewer's own delivery states.
+ async fn message_views(
+ &self,
+ chat: &ChatRef,
+ viewer: &str,
+ messages: Vec<StoredMessage>,
+ ) -> ExternalChatResult<Vec<ExternalChatMessage>> {
+ let mut named = vec![];
+ for message in &messages {
+ if let Some(RichContent::System(line)) = &message.rich {
+ named.extend(line.participants.iter().cloned());
+ }
+ named.extend(message.mentions.iter().cloned());
+ }
+ let people = self.provider.people(chat, &named).await?;
+ let ids: Vec<Uuid> = messages.iter().map(|message| message.id).collect();
+ let deliveries = if ids.is_empty() {
+ vec![]
+ } else {
+ self.provider.delivery_states(chat.id, viewer, &ids).await?
+ };
+ Ok(messages
+ .into_iter()
+ .map(|message| {
+ let delivery = deliveries
+ .iter()
+ .find(|(id, _)| *id == message.id)
+ .map(|(_, state)| *state);
+ view::message_view(message, delivery, viewer, &people)
+ })
+ .collect())
}
/// Advance the caller's own read cursor monotonically.
#[tracing::instrument(name = "whatsapp.linked.read", skip_all, err, fields(chat_id, message_id = %message))]
@@ -98,7 +168,7 @@ impl LinkedService {
&self,
access: &EntityAccessReceipt<T>,
message: Uuid,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let (user, id) = receipt_scope(access)?;
tracing::Span::current().record("chat_id", tracing::field::display(id));
self.timeline.mark_read(id, user, message).await
@@ -107,15 +177,15 @@ impl LinkedService {
pub async fn open_sends<T: RequiredPermission>(
&self,
access: &EntityAccessReceipt<T>,
- ) -> LinkedResult<Vec<SendProgress>> {
+ ) -> ExternalChatResult<Vec<SendProgress>> {
let (user, id) = receipt_scope(access)?;
- self.sends.open_sends(id, user).await
+ self.provider.open_sends(id, user).await
}
/// Read standard permission settings.
pub async fn permissions<T: RequiredPermission>(
&self,
access: &EntityAccessReceipt<T>,
- ) -> LinkedResult<SharePermissionV2> {
+ ) -> ExternalChatResult<SharePermissionV2> {
let (_, id) = view_scope(access)?;
self.timeline.permissions(id).await
}
@@ -128,20 +198,20 @@ impl LinkedService {
access: &EntityAccessReceipt<T>,
update: &UpdateSharePermissionRequestV2,
authority: SendAuthority,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
human(authority)?;
let (user, id) = receipt_scope(access)?;
tracing::Span::current().record("chat_id", tracing::field::display(id));
let facts = self.timeline.share_facts(id).await?;
if !facts.owner.is_user(user) {
- return Err(LinkedError::Forbidden.into());
+ return Err(ExternalChatError::Forbidden.into());
}
if update
.link_share_access_level
.flatten()
.is_some_and(|level| level != AccessLevel::View)
{
- return Err(LinkedError::Invalid("WhatsApp chats are shared read-only").into());
+ return Err(ExternalChatError::Invalid("WhatsApp chats are shared read-only").into());
}
validate_channel_grants(
update
@@ -158,7 +228,7 @@ impl LinkedService {
},
TeamShareLevel::View,
)
- .map_err(|_| LinkedError::Forbidden)?;
+ .map_err(|_| ExternalChatError::Forbidden)?;
self.timeline
.update_permissions(id, user, update, command.as_ref())
.await
@@ -167,7 +237,7 @@ impl LinkedService {
/// Channel grants name each channel once and are read-only; a removal needs no
/// level.
-fn validate_channel_grants(grants: &[UpdateChannelSharePermission]) -> LinkedResult<()> {
+fn validate_channel_grants(grants: &[UpdateChannelSharePermission]) -> ExternalChatResult<()> {
let mut channels = std::collections::HashSet::new();
for grant in grants {
let read_only = match grant.operation {
@@ -180,9 +250,10 @@ fn validate_channel_grants(grants: &[UpdateChannelSharePermission]) -> LinkedRes
|| Uuid::parse_str(&grant.channel_id).is_err()
|| !read_only
{
- return Err(
- LinkedError::Invalid("WhatsApp chats are shared read-only with a channel").into(),
- );
+ return Err(ExternalChatError::Invalid(
+ "WhatsApp chats are shared read-only with a channel",
+ )
+ .into());
}
}
Ok(())
diff --git a/crates/whatsapp_linked/src/domain/shape.rs b/crates/external_chat/src/domain/shape.rs
similarity index 99%
rename from crates/whatsapp_linked/src/domain/shape.rs
rename to crates/external_chat/src/domain/shape.rs
index 34cae79679..275b147dee 100644
--- a/crates/whatsapp_linked/src/domain/shape.rs
+++ b/crates/external_chat/src/domain/shape.rs
@@ -1,8 +1,8 @@
//! Pixel size and length of a file someone sends from Macro, read from its
//! headers so the timeline and recipients' phones can reserve space for it.
-use super::events::MediaShape;
use super::models::MediaKind;
+use super::models::MediaShape;
use nom::IResult;
use nom::bytes::complete::take;
use nom::number::complete::{be_i32, be_u8, be_u24, be_u32, be_u64};
diff --git a/crates/whatsapp_linked/src/domain/shape/test.rs b/crates/external_chat/src/domain/shape/test.rs
similarity index 100%
rename from crates/whatsapp_linked/src/domain/shape/test.rs
rename to crates/external_chat/src/domain/shape/test.rs
diff --git a/crates/whatsapp_linked/src/domain/timeline.rs b/crates/external_chat/src/domain/timeline.rs
similarity index 81%
rename from crates/whatsapp_linked/src/domain/timeline.rs
rename to crates/external_chat/src/domain/timeline.rs
index 2d82267e61..40f1588ebf 100644
--- a/crates/whatsapp_linked/src/domain/timeline.rs
+++ b/crates/external_chat/src/domain/timeline.rs
@@ -1,5 +1,5 @@
//! External chat timeline and sharing contracts.
-use super::{models::*, ports::LinkedResult};
+use super::{models::*, ports::ExternalChatResult, rich::RichContent};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use macro_user_id::user_id::MacroUserIdStr;
@@ -93,26 +93,6 @@ pub struct ExternalChatParticipant {
pub admin: bool,
}
-impl From<&GroupInfo> for ExternalChatMembers {
- fn from(info: &GroupInfo) -> Self {
- Self {
- name: info.name.clone(),
- participants: info
- .participants
- .iter()
- .map(|participant| ExternalChatParticipant {
- jid: participant.identity.jid.clone(),
- alternate_jid: participant.identity.alternate_jid.clone(),
- display_name: participant.identity.display_name.clone(),
- admin: participant.admin,
- })
- .collect(),
- announce: info.announce,
- observed_at: info.observed_at,
- }
- }
-}
-
/// Message sender from protocol-verified identity evidence.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
@@ -310,7 +290,7 @@ pub const UNREAD_COUNT_CAP: i64 = 100;
/// What the store knows about one chat for one viewer; the domain turns it
/// into an [`ExternalChatSummary`] for both the list and the detail.
#[derive(Debug, Clone)]
-pub struct GroupFacts {
+pub struct ChatFacts {
/// Entity identifier.
pub id: Uuid,
/// The service the chat lives in.
@@ -327,24 +307,15 @@ pub struct GroupFacts {
pub last_message_at: Option<DateTime<Utc>>,
/// History import progress.
pub backfill: Option<Backfill>,
- /// The community the newest roster any observer reported names.
- pub community: Option<CommunityFacts>,
}
-/// A chat's community as its observers' rosters report it.
-#[derive(Debug, Clone)]
-pub struct CommunityFacts {
- /// The community's JID.
- pub jid: String,
- /// The community's own name, when an observer has its metadata.
- pub name: Option<String>,
- /// The chat is the community's announcement group.
- pub announcements: bool,
-}
-
-impl GroupFacts {
+impl ChatFacts {
/// The chat as the list and the detail show it.
- pub fn summary(self, send_reason: Option<ReplyBlock>) -> ExternalChatSummary {
+ pub fn summary(
+ self,
+ send_reason: Option<ReplyBlock>,
+ community: Option<CommunityReference>,
+ ) -> ExternalChatSummary {
ExternalChatSummary {
id: self.id,
provider: self.provider,
@@ -356,72 +327,115 @@ impl GroupFacts {
can_send: send_reason.is_none(),
send_reason,
backfill: self.backfill,
- community: self.community.map(|community| {
- CommunityReference::named(
- community.jid,
- community.name.as_deref(),
- community.announcements,
- )
- }),
+ community,
}
}
}
-/// Batched facts for one visible entity and the caller's own remote roster.
-pub struct VisibleGroup {
+/// One chat a viewer can see.
+pub struct VisibleChat {
/// Shared entity facts.
- pub facts: GroupFacts,
- /// Lifecycle and remote group identity.
- pub binding: Binding,
- /// Caller-account roster, absent when not connected or not a remote member.
- pub roster: Option<GroupInfo>,
+ pub facts: ChatFacts,
+ /// Lifecycle and remote chat identity.
+ pub chat: ChatRef,
}
-/// Group metadata persistence. Object safe because DSS supplies one shared adapter.
+/// One stored message of a timeline page, before the provider names the
+/// people in it and the viewer's delivery state is added.
+#[derive(Debug, Clone)]
+pub struct StoredMessage {
+ /// Stable message id.
+ pub id: Uuid,
+ /// Sender.
+ pub sender: ExternalChatSender,
+ /// Plain text; empty once deleted.
+ pub body: String,
+ /// Message quoted by this one.
+ pub reply_to: Option<Uuid>,
+ /// Timestamp assigned by the provider.
+ pub sent_at: DateTime<Utc>,
+ /// Timestamp first received by Macro.
+ pub received_at: DateTime<Utc>,
+ /// Last edit timestamp.
+ pub edited_at: Option<DateTime<Utc>>,
+ /// Deleted for everyone.
+ pub deleted: bool,
+ /// Reactions grouped by emoji.
+ pub reactions: Vec<ExternalChatReaction>,
+ /// Imported or pending attachments.
+ pub attachments: Vec<Attachment>,
+ /// A message, or a system line.
+ pub kind: ExternalChatMessageKind,
+ /// Forwarded from another chat.
+ pub forwarded: bool,
+ /// Sent as view-once.
+ pub view_once: bool,
+ /// Provider ids of the people the body @-mentions; empty once deleted.
+ pub mentions: Vec<String>,
+ /// Structured content; `None` once deleted.
+ pub rich: Option<RichContent>,
+ /// Current votes when it is a poll.
+ pub votes: Vec<view::StoredVote>,
+}
+
+/// Newest-first stored timeline page; `before` is the last returned message id.
+#[derive(Debug, Clone)]
+pub struct StoredPage {
+ /// Messages in descending sent_at, id order.
+ pub messages: Vec<StoredMessage>,
+ /// Cursor for older messages.
+ pub next_cursor: Option<Uuid>,
+}
+
+/// Chat, message and read-state persistence. Object safe because DSS supplies
+/// one shared adapter.
#[async_trait]
pub trait TimelineRepository: Send + Sync {
/// Candidate list already filtered by generic sharing.
- async fn visible_groups(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<Vec<VisibleGroup>>;
- /// Facts for an authorized group, for a viewer principal (a person or a
+ async fn visible_chats(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<Vec<VisibleChat>>;
+ /// Facts for an authorized chat, for a viewer principal (a person or a
/// bot, whose id is not a Macro user id).
- async fn group_facts(&self, group: Uuid, viewer: &str) -> LinkedResult<GroupFacts>;
+ async fn chat_facts(&self, chat: Uuid, viewer: &str) -> ExternalChatResult<ChatFacts>;
/// Bounded, stable pagination for a viewer principal.
async fn messages(
&self,
- group: Uuid,
+ chat: &ChatRef,
viewer: &str,
before: Option<Uuid>,
limit: i64,
- ) -> LinkedResult<ExternalChatMessages>;
+ ) -> ExternalChatResult<StoredPage>;
/// One message of the timeline, shaped like a page entry.
async fn message(
&self,
- group: Uuid,
+ chat: &ChatRef,
viewer: &str,
message: Uuid,
- ) -> LinkedResult<Option<ExternalChatMessage>>;
- /// Monotone viewer read cursor, scoped to this group.
+ ) -> ExternalChatResult<Option<StoredMessage>>;
+ /// Monotone viewer read cursor, scoped to this chat.
async fn mark_read(
&self,
- group: Uuid,
+ chat: Uuid,
user: &MacroUserIdStr<'_>,
message: Uuid,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// Standard permission object.
- async fn permissions(&self, group: Uuid) -> LinkedResult<SharePermissionV2>;
+ async fn permissions(&self, chat: Uuid) -> ExternalChatResult<SharePermissionV2>;
/// Guarded canonical team-sharing facts for domain authorization.
async fn share_facts(
&self,
- group: Uuid,
- ) -> LinkedResult<models_permissions::share_permission::team_share::TeamShareFacts>;
+ chat: Uuid,
+ ) -> ExternalChatResult<models_permissions::share_permission::team_share::TeamShareFacts>;
/// Actual-owner update, including canonical team-share policy.
async fn update_permissions(
&self,
- group: Uuid,
+ chat: Uuid,
user: &MacroUserIdStr<'_>,
update: &UpdateSharePermissionRequestV2,
command: Option<
&models_permissions::share_permission::team_share::AuthorizedTeamShareCommand,
>,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
}
diff --git a/crates/whatsapp_linked/src/domain/timeline/view.rs b/crates/external_chat/src/domain/timeline/view.rs
similarity index 65%
rename from crates/whatsapp_linked/src/domain/timeline/view.rs
rename to crates/external_chat/src/domain/timeline/view.rs
index b6ef41bba9..242eb5af16 100644
--- a/crates/whatsapp_linked/src/domain/timeline/view.rs
+++ b/crates/external_chat/src/domain/timeline/view.rs
@@ -1,4 +1,4 @@
-//! How stored rich content, mentions and votes read in the timeline: WhatsApp
+//! How stored rich content, mentions and votes read in the timeline: provider
//! ids become people's names, votes become tallies.
use super::*;
@@ -8,29 +8,44 @@ use std::collections::HashMap;
#[cfg(test)]
mod test;
-/// Who WhatsApp ids are, as far as the group knows: people seen in it (with
+/// Who provider ids are, as far as the chat knows: people seen in it (with
/// their verified Macro user) and the roster's names.
-#[derive(Debug, Default)]
+#[derive(Debug)]
pub struct People {
- by_jid: HashMap<String, ExternalChatPersonRef>,
+ by_id: HashMap<String, ExternalChatPersonRef>,
+ unnamed: fn(&str) -> Option<String>,
+}
+
+impl Default for People {
+ fn default() -> Self {
+ Self::new(|_| None)
+ }
}
impl People {
+ /// Nobody known yet; `unnamed` names an id nobody named (WhatsApp: its
+ /// phone number).
+ pub fn new(unnamed: fn(&str) -> Option<String>) -> Self {
+ Self {
+ by_id: HashMap::new(),
+ unnamed,
+ }
+ }
+
/// Learn a person's name; the first name given for an id wins, so pass
/// the better sources first.
- pub fn learn(&mut self, jid: &str, person: ExternalChatPersonRef) {
- self.by_jid.entry(jid.to_owned()).or_insert(person);
+ pub fn learn(&mut self, id: &str, person: ExternalChatPersonRef) {
+ self.by_id.entry(id.to_owned()).or_insert(person);
}
- /// The person behind an id: their name when known, else their phone
- /// number, else a generic name.
- pub fn get(&self, jid: &str) -> ExternalChatPersonRef {
- if let Some(person) = self.by_jid.get(jid) {
+ /// The person behind an id: their name when known, else what `unnamed`
+ /// calls them, else a generic name.
+ pub fn get(&self, id: &str) -> ExternalChatPersonRef {
+ if let Some(person) = self.by_id.get(id) {
return person.clone();
}
ExternalChatPersonRef {
- name: crate::domain::jid::phone_number(jid)
- .unwrap_or_else(|| UNKNOWN_PERSON.to_owned()),
+ name: (self.unnamed)(id).unwrap_or_else(|| UNKNOWN_PERSON.to_owned()),
user_id: None,
}
}
@@ -139,3 +154,37 @@ pub fn rich_view(
RichContent::PollVote(_) => return None,
})
}
+
+/// A stored message as the timeline shows it to `viewer`.
+pub fn message_view(
+ message: StoredMessage,
+ delivery_state: Option<DeliveryState>,
+ viewer: &str,
+ people: &People,
+) -> ExternalChatMessage {
+ let sender = ExternalChatPersonRef {
+ name: message.sender.name.clone(),
+ user_id: message.sender.user_id.clone(),
+ };
+ let votes: Vec<&StoredVote> = message.votes.iter().collect();
+ ExternalChatMessage {
+ id: message.id,
+ mentions: mentions_view(&message.mentions, people),
+ rich: message
+ .rich
+ .and_then(|content| rich_view(content, &sender, &votes, viewer, people)),
+ sender: message.sender,
+ body: message.body,
+ reply_to: message.reply_to,
+ sent_at: message.sent_at,
+ received_at: message.received_at,
+ edited_at: message.edited_at,
+ deleted: message.deleted,
+ reactions: message.reactions,
+ attachments: message.attachments,
+ delivery_state,
+ kind: message.kind,
+ forwarded: message.forwarded,
+ view_once: message.view_once,
+ }
+}
diff --git a/crates/whatsapp_linked/src/domain/timeline/view/test.rs b/crates/external_chat/src/domain/timeline/view/test.rs
similarity index 94%
rename from crates/whatsapp_linked/src/domain/timeline/view/test.rs
rename to crates/external_chat/src/domain/timeline/view/test.rs
index ff50802f13..6df3d6e547 100644
--- a/crates/whatsapp_linked/src/domain/timeline/view/test.rs
+++ b/crates/external_chat/src/domain/timeline/view/test.rs
@@ -1,6 +1,12 @@
use super::*;
use crate::domain::rich::{Call, CallOutcome, Poll, PollOption, SystemAction};
+/// How a provider names an id nobody named: here, a phone number.
+fn phone_number(id: &str) -> Option<String> {
+ id.strip_suffix("@s.whatsapp.net")
+ .map(|number| format!("+{number}"))
+}
+
fn person(name: &str, user: Option<&str>) -> ExternalChatPersonRef {
ExternalChatPersonRef {
name: name.to_owned(),
@@ -10,7 +16,7 @@ fn person(name: &str, user: Option<&str>) -> ExternalChatPersonRef {
#[test]
fn mentions_name_people_seen_in_the_group_else_their_number() {
- let mut people = People::default();
+ let mut people = People::new(phone_number);
people.learn(
person("Ana", Some("macro|[email protected]")),
@@ -109,7 +115,7 @@ fn a_poll_tallies_current_votes_per_option_and_marks_the_viewers() {
#[test]
fn a_system_line_names_who_did_it_and_to_whom() {
- let mut people = People::default();
+ let mut people = People::new(phone_number);
people.learn("[email protected]", person("Ben", None));
let added = RichContent::System(SystemLine {
action: SystemAction::ParticipantsAdded,
diff --git a/crates/external_chat/src/inbound.rs b/crates/external_chat/src/inbound.rs
new file mode 100644
index 0000000000..e3b9e0803b
--- /dev/null
+++ b/crates/external_chat/src/inbound.rs
@@ -0,0 +1,2 @@
+/// Axum routes served by DSS.
+pub mod router;
diff --git a/crates/external_chat/src/inbound/router.rs b/crates/external_chat/src/inbound/router.rs
new file mode 100644
index 0000000000..52a375acc8
--- /dev/null
+++ b/crates/external_chat/src/inbound/router.rs
@@ -0,0 +1,487 @@
+//! Thin HTTP adapter: authenticated people and external chat receipts flow
+//! to [`ExternalChatService`]. DSS mounts [`external_chats_router`] at
+//! `/external-chats` (any provider's added chats); each provider mounts its
+//! own routes (WhatsApp: `/whatsapp`), sharing [`ApiError`], [`authority`]
+//! and [`no_store`]. The OpenAPI tag keeps the routes' original module name,
+//! which the generated clients group by.
+
+use crate::domain::{models::*, ports::ChatProvider, service::ExternalChatService, timeline::*};
+use axum::extract::Query;
+use axum::{
+ Json, Router,
+ extract::{DefaultBodyLimit, FromRef, Multipart, Path, State},
+ http::{HeaderValue, StatusCode, header},
+ response::{IntoResponse, Response},
+ routing::{get, post},
+};
+use entity_access::domain::models::{OwnerAccessLevel, ViewAccessLevel};
+use entity_access::{
+ domain::ports::EntityAccessService, inbound::axum_extractors::ExternalChatAccessLevelExtractor,
+};
+use macro_authorization::{
+ CredentialOrigin, MacroAuthorizationExtractor, MacroAuthorizationService,
+ MacroAuthorizationState, MacroUserAuthentication, UserOnly,
+};
+use models_permissions::share_permission::{SharePermissionV2, UpdateSharePermissionRequestV2};
+use serde::Deserialize;
+use std::sync::Arc;
+use uuid::Uuid;
+
+/// Capabilities supplied by DSS.
+pub struct ExternalChatRouterState<P, Eas, Auth> {
+ /// Use cases.
+ pub service: Arc<ExternalChatService<P>>,
+ /// Standard chat receipt provider.
+ pub entity_access_service: Arc<Eas>,
+ /// Standard authentication provider.
+ pub authorization_state: MacroAuthorizationState<Auth>,
+}
+
+impl<P, Eas, Auth> Clone for ExternalChatRouterState<P, Eas, Auth> {
+ fn clone(&self) -> Self {
+ Self {
+ service: self.service.clone(),
+ entity_access_service: self.entity_access_service.clone(),
+ authorization_state: self.authorization_state.clone(),
+ }
+ }
+}
+
+impl<P, Eas, Auth> FromRef<ExternalChatRouterState<P, Eas, Auth>> for Arc<Eas> {
+ fn from_ref(state: &ExternalChatRouterState<P, Eas, Auth>) -> Self {
+ state.entity_access_service.clone()
+ }
+}
+
+impl<P, Eas, Auth> FromRef<ExternalChatRouterState<P, Eas, Auth>>
+ for MacroAuthorizationState<Auth>
+{
+ fn from_ref(state: &ExternalChatRouterState<P, Eas, Auth>) -> Self {
+ state.authorization_state.clone()
+ }
+}
+
+/// Domain errors as `{code, message}` JSON.
+pub struct ApiError(ExternalChatError);
+
+impl From<rootcause::Report<ExternalChatError>> for ApiError {
+ fn from(error: rootcause::Report<ExternalChatError>) -> Self {
+ if matches!(
+ error.current_context(),
+ ExternalChatError::Unavailable
+ | ExternalChatError::Fenced
+ | ExternalChatError::Internal
+ ) {
+ tracing::error!(error = ?error, "WhatsApp linked request failed");
+ }
+ Self(error.current_context().clone())
+ }
+}
+
+impl IntoResponse for ApiError {
+ fn into_response(self) -> Response {
+ let status = match self.0 {
+ ExternalChatError::Invalid(_) | ExternalChatError::CannotAdd(_) => {
+ StatusCode::BAD_REQUEST
+ }
+ ExternalChatError::Forbidden => StatusCode::FORBIDDEN,
+ ExternalChatError::RateLimited => StatusCode::TOO_MANY_REQUESTS,
+ ExternalChatError::NotFound => StatusCode::NOT_FOUND,
+ ExternalChatError::Conflict
+ | ExternalChatError::Disconnected
+ | ExternalChatError::SendExpired => StatusCode::CONFLICT,
+ ExternalChatError::Unavailable | ExternalChatError::Backlogged => {
+ StatusCode::SERVICE_UNAVAILABLE
+ }
+ ExternalChatError::Fenced | ExternalChatError::Internal => {
+ StatusCode::INTERNAL_SERVER_ERROR
+ }
+ };
+ let mut response = (
+ status,
+ Json(serde_json::json!({ "code": self.0.code(), "message": self.0.to_string() })),
+ )
+ .into_response();
+ if matches!(self.0, ExternalChatError::RateLimited) {
+ response
+ .headers_mut()
+ .insert(header::RETRY_AFTER, HeaderValue::from_static("1"));
+ }
+ response
+ }
+}
+
+/// Who is acting, from how the caller authenticated.
+pub fn authority(user: &MacroUserAuthentication) -> SendAuthority {
+ match user.origin {
+ CredentialOrigin::MacroApiJwt | CredentialOrigin::UserApiKey => SendAuthority::UserApiToken,
+ CredentialOrigin::Session if user.agent_id.is_none() => SendAuthority::Human,
+ CredentialOrigin::Session
+ | CredentialOrigin::Bot
+ | CredentialOrigin::Harness
+ | CredentialOrigin::Internal => SendAuthority::Automation,
+ }
+}
+
+/// Pairing codes and send drafts are never cached.
+pub fn no_store<S: Clone + Send + Sync + 'static>(router: Router<S>) -> Router<S> {
+ router.layer(axum::middleware::map_response(
+ |mut response: Response| async move {
+ let headers = response.headers_mut();
+ headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
+ headers.insert(
+ header::REFERRER_POLICY,
+ HeaderValue::from_static("no-referrer"),
+ );
+ response
+ },
+ ))
+}
+
+/// Added chats of any provider, under `/external-chats`.
+pub fn external_chats_router<P, Eas, Auth, S>(
+ state: ExternalChatRouterState<P, Eas, Auth>,
+) -> Router<S>
+where
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+ S: Send + Sync + 'static,
+{
+ no_store(
+ Router::new()
+ .route("/", get(list::<P, Eas, Auth>).post(add::<P, Eas, Auth>))
+ .route(
+ "/{chat_id}",
+ get(chat::<P, Eas, Auth>).delete(remove::<P, Eas, Auth>),
+ )
+ .route("/{chat_id}/messages", get(messages::<P, Eas, Auth>))
+ .route(
+ "/{chat_id}/messages/{message_id}",
+ get(message::<P, Eas, Auth>),
+ )
+ .route(
+ "/{chat_id}/messages/attachments",
+ post(message_attachments::<P, Eas, Auth>),
+ )
+ .route("/{chat_id}/read", post(mark_read::<P, Eas, Auth>))
+ .route(
+ "/{chat_id}/permissions",
+ get(permissions::<P, Eas, Auth>).patch(update_permissions::<P, Eas, Auth>),
+ )
+ .route(
+ "/{chat_id}/sends",
+ get(open_sends::<P, Eas, Auth>).post(send::<P, Eas, Auth>),
+ )
+ .route(
+ "/{chat_id}/attachments",
+ post(upload::<P, Eas, Auth>)
+ .layer(DefaultBodyLimit::max(MEDIA_LIMIT_BYTES + 64 * 1024)),
+ )
+ .route("/{chat_id}/delivery", post(delivery::<P, Eas, Auth>)),
+ )
+ .with_state(state)
+}
+
+/// List external chats the caller can view, including without a provider account.
+#[utoipa::path(get, tag = "whatsapp_linked::inbound::router", path = "/external-chats", operation_id = "external_chats", responses((status = 200, body = Vec<ExternalChatSummary>)))]
+pub async fn list<P: ChatProvider, Eas, Auth: MacroAuthorizationService>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ auth: MacroAuthorizationExtractor<Auth, UserOnly>,
+) -> Result<Json<Vec<ExternalChatSummary>>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .list(&auth.authorization.macro_user_id)
+ .await?,
+ ))
+}
+
+/// Add one of the caller's provider chats as their own external chat.
+#[utoipa::path(post, tag = "whatsapp_linked::inbound::router", path = "/external-chats", operation_id = "external_chat_add", request_body = AddChat, responses((status = 200, body = AddedChat)))]
+pub async fn add<P: ChatProvider, Eas, Auth: MacroAuthorizationService>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ auth: MacroAuthorizationExtractor<Auth, UserOnly>,
+ Json(body): Json<AddChat>,
+) -> Result<Json<AddedChat>, ApiError> {
+ let chat = state
+ .service
+ .add(
+ &auth.authorization.macro_user_id,
+ &body,
+ authority(&auth.authorization),
+ )
+ .await?;
+ Ok(Json(AddedChat { chat_id: chat.id }))
+}
+
+/// An external chat the caller can read.
+#[utoipa::path(get, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}", operation_id = "external_chat", params(("chat_id" = Uuid, Path)), responses((status = 200, body = ExternalChatDetail)))]
+pub async fn chat<P: ChatProvider, Eas: EntityAccessService, Auth: MacroAuthorizationService>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+) -> Result<Json<ExternalChatDetail>, ApiError> {
+ Ok(Json(
+ state.service.detail(&access.entity_access_receipt).await?,
+ ))
+}
+
+/// Stop syncing the chat, locally. Owner only, in their own session.
+#[utoipa::path(delete, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}", operation_id = "external_chat_remove", params(("chat_id" = Uuid, Path)), responses((status = 204)))]
+pub async fn remove<P: ChatProvider, Eas: EntityAccessService, Auth: MacroAuthorizationService>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ auth: MacroAuthorizationExtractor<Auth, UserOnly>,
+ access: ExternalChatAccessLevelExtractor<OwnerAccessLevel, Eas, Auth>,
+) -> Result<StatusCode, ApiError> {
+ state
+ .service
+ .detach(
+ &access.entity_access_receipt,
+ authority(&auth.authorization),
+ )
+ .await?;
+ Ok(StatusCode::NO_CONTENT)
+}
+
+/// Stable timeline pagination.
+#[derive(Deserialize, utoipa::IntoParams)]
+#[serde(rename_all = "camelCase")]
+pub struct TimelineQuery {
+ /// Last message id in the previous page.
+ pub before: Option<Uuid>,
+ /// Page size, up to [`PAGE_LIMIT`]; [`DEFAULT_PAGE`] when absent.
+ pub limit: Option<i64>,
+}
+
+/// Newest-first timeline through ordinary view access.
+#[utoipa::path(get, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/messages", operation_id = "external_chat_messages", params(("chat_id" = Uuid, Path), TimelineQuery), responses((status = 200, body = ExternalChatMessages)))]
+pub async fn messages<
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+ Query(query): Query<TimelineQuery>,
+) -> Result<Json<ExternalChatMessages>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .messages(&access.entity_access_receipt, query.before, query.limit)
+ .await?,
+ ))
+}
+
+/// Path of one timeline message.
+#[derive(Deserialize)]
+pub struct MessagePath {
+ /// Unused here; the access extractor reads it.
+ pub chat_id: Uuid,
+ /// The message.
+ pub message_id: Uuid,
+}
+
+/// One message of the timeline, as the timeline shows it.
+#[utoipa::path(get, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/messages/{message_id}", operation_id = "external_chat_message", params(("chat_id" = Uuid, Path), ("message_id" = Uuid, Path)), responses((status = 200, body = ExternalChatMessage)))]
+pub async fn message<P: ChatProvider, Eas: EntityAccessService, Auth: MacroAuthorizationService>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+ Path(path): Path<MessagePath>,
+) -> Result<Json<ExternalChatMessage>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .message(&access.entity_access_receipt, path.message_id)
+ .await?,
+ ))
+}
+
+/// Attachment states of messages, by message id (at most 100).
+#[utoipa::path(post, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/messages/attachments", operation_id = "external_chat_message_attachments", params(("chat_id" = Uuid, Path)), request_body = Vec<Uuid>, responses((status = 200, body = Vec<MessageAttachments>)))]
+pub async fn message_attachments<
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+ Json(ids): Json<Vec<Uuid>>,
+) -> Result<Json<Vec<MessageAttachments>>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .message_attachments(&access.entity_access_receipt, &ids)
+ .await?,
+ ))
+}
+
+/// Advance this viewer's read position.
+#[utoipa::path(post, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/read", operation_id = "external_chat_read", params(("chat_id" = Uuid, Path)), request_body = MarkRead, responses((status = 204)))]
+pub async fn mark_read<
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+ Json(body): Json<MarkRead>,
+) -> Result<StatusCode, ApiError> {
+ state
+ .service
+ .mark_read(&access.entity_access_receipt, body.message_id)
+ .await?;
+ Ok(StatusCode::NO_CONTENT)
+}
+
+/// Generic Share dialog policy.
+#[utoipa::path(get, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/permissions", operation_id = "external_chat_permissions", params(("chat_id" = Uuid, Path)), responses((status = 200, body = SharePermissionV2)))]
+pub async fn permissions<
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+) -> Result<Json<SharePermissionV2>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .permissions(&access.entity_access_receipt)
+ .await?,
+ ))
+}
+
+/// The owner, in their own session, updates link, team and channel sharing.
+#[utoipa::path(patch, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/permissions", operation_id = "external_chat_update_permissions", params(("chat_id" = Uuid, Path)), request_body = UpdateSharePermissionRequestV2, responses((status = 204)))]
+pub async fn update_permissions<
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ auth: MacroAuthorizationExtractor<Auth, UserOnly>,
+ access: ExternalChatAccessLevelExtractor<OwnerAccessLevel, Eas, Auth>,
+ Json(body): Json<UpdateSharePermissionRequestV2>,
+) -> Result<StatusCode, ApiError> {
+ state
+ .service
+ .update_permissions(
+ &access.entity_access_receipt,
+ &body,
+ authority(&auth.authorization),
+ )
+ .await?;
+ Ok(StatusCode::NO_CONTENT)
+}
+
+/// Caller-owned pending, failed and uncertain sends.
+#[utoipa::path(get, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/sends", operation_id = "external_chat_sends", params(("chat_id" = Uuid, Path)), responses((status = 200, body = Vec<SendProgress>)))]
+pub async fn open_sends<
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+) -> Result<Json<Vec<SendProgress>>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .open_sends(&access.entity_access_receipt)
+ .await?,
+ ))
+}
+
+/// Send to the chat from the caller's own provider account (one request per second per user).
+#[utoipa::path(post, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/sends", operation_id = "external_chat_send", params(("chat_id" = Uuid, Path)), request_body = SendReply, responses((status = 200, body = SendProgress), (status = 429, description = "One send request per second per user; Retry-After: 1")))]
+pub async fn send<P: ChatProvider, Eas: EntityAccessService, Auth: MacroAuthorizationService>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ auth: MacroAuthorizationExtractor<Auth, UserOnly>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+ Json(body): Json<SendReply>,
+) -> Result<Json<SendProgress>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .send_reply(
+ &access.entity_access_receipt,
+ &body,
+ authority(&auth.authorization),
+ )
+ .await?,
+ ))
+}
+
+/// Multipart body of the attachments route.
+#[derive(utoipa::ToSchema)]
+#[schema(as = ExternalChatUploadRequest)]
+pub struct UploadRequest {
+ /// One supported image, audio, video, PDF or text file.
+ #[schema(value_type = String, format = Binary)]
+ pub file: Vec<u8>,
+}
+
+/// Stage one attachment for a later media send in this chat.
+#[utoipa::path(post, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/attachments", operation_id = "external_chat_upload", params(("chat_id" = Uuid, Path)), request_body(content = UploadRequest, content_type = "multipart/form-data"), responses((status = 200, body = UploadedAttachment)))]
+pub async fn upload<P: ChatProvider, Eas: EntityAccessService, Auth: MacroAuthorizationService>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ auth: MacroAuthorizationExtractor<Auth, UserOnly>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+ mut input: Multipart,
+) -> Result<Json<UploadedAttachment>, ApiError> {
+ let invalid = |message| ApiError(ExternalChatError::Invalid(message));
+ let field = input
+ .next_field()
+ .await
+ .map_err(|_| invalid("invalid attachment"))?
+ .ok_or_else(|| invalid("choose a file"))?;
+ if field.name() != Some("file") {
+ return Err(invalid("one file field is required"));
+ }
+ let filename = field
+ .file_name()
+ .ok_or_else(|| invalid("a filename is required"))?
+ .to_owned();
+ let mime = field
+ .content_type()
+ .unwrap_or("application/octet-stream")
+ .to_owned();
+ let bytes = field
+ .bytes()
+ .await
+ .map_err(|_| invalid("attachment exceeds the size limit"))?;
+ Ok(Json(
+ state
+ .service
+ .upload_attachment(
+ &access.entity_access_receipt,
+ &filename,
+ &mime,
+ &bytes,
+ authority(&auth.authorization),
+ )
+ .await?,
+ ))
+}
+
+/// The caller's send progress for intents or messages.
+#[utoipa::path(post, tag = "whatsapp_linked::inbound::router", path = "/external-chats/{chat_id}/delivery", operation_id = "external_chat_delivery", params(("chat_id" = Uuid, Path)), request_body = Vec<Uuid>, responses((status = 200, body = Vec<SendProgress>)))]
+pub async fn delivery<
+ P: ChatProvider,
+ Eas: EntityAccessService,
+ Auth: MacroAuthorizationService,
+>(
+ State(state): State<ExternalChatRouterState<P, Eas, Auth>>,
+ access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
+ Json(ids): Json<Vec<Uuid>>,
+) -> Result<Json<Vec<SendProgress>>, ApiError> {
+ Ok(Json(
+ state
+ .service
+ .delivery(&access.entity_access_receipt, &ids)
+ .await?,
+ ))
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/external_chat/src/inbound/router/test.rs b/crates/external_chat/src/inbound/router/test.rs
new file mode 100644
index 0000000000..6d688e15ea
--- /dev/null
+++ b/crates/external_chat/src/inbound/router/test.rs
@@ -0,0 +1,52 @@
+use super::*;
+
+#[tokio::test]
+async fn a_rate_limited_request_returns_429_with_retry_after() {
+ let response = ApiError(ExternalChatError::RateLimited).into_response();
+ assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
+ assert_eq!(response.headers()[header::RETRY_AFTER], "1");
+ let body = axum::body::to_bytes(response.into_body(), 1024)
+ .await
+ .unwrap();
+ let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
+ assert_eq!(body["code"], "rate_limited");
+}
+
+#[test]
+fn verified_owner_api_credentials_remain_distinct_from_sessions_and_bots() {
+ let agent = || Some(serde_json::from_value(serde_json::json!(Uuid::now_v7())).unwrap());
+ for (origin, agent_id, expected) in [
+ (CredentialOrigin::Session, None, SendAuthority::Human),
+ (
+ CredentialOrigin::Session,
+ agent(),
+ SendAuthority::Automation,
+ ),
+ (
+ CredentialOrigin::MacroApiJwt,
+ None,
+ SendAuthority::UserApiToken,
+ ),
+ (
+ CredentialOrigin::MacroApiJwt,
+ agent(),
+ SendAuthority::UserApiToken,
+ ),
+ (
+ CredentialOrigin::UserApiKey,
+ None,
+ SendAuthority::UserApiToken,
+ ),
+ (CredentialOrigin::Bot, None, SendAuthority::Automation),
+ (CredentialOrigin::Harness, None, SendAuthority::Automation),
+ (CredentialOrigin::Internal, None, SendAuthority::Automation),
+ ] {
+ let user = MacroUserAuthentication {
+ origin,
+ agent_id,
+ macro_user_id: "macro|[email protected]".to_string().try_into().unwrap(),
+ user_context: Default::default(),
+ };
+ assert_eq!(authority(&user), expected, "{origin:?}");
+ }
+}
diff --git a/crates/external_chat/src/lib.rs b/crates/external_chat/src/lib.rs
new file mode 100644
index 0000000000..0c73cd80ea
--- /dev/null
+++ b/crates/external_chat/src/lib.rs
@@ -0,0 +1,17 @@
+#![deny(missing_docs)]
+//! External chats: chats that live at another service (WhatsApp is the one
+//! provider), added to Macro as independently shared entities. This crate is
+//! the provider-neutral part: timeline and read state, sharing, deliberate
+//! sends and uploads, notification effects and the `/external-chats` routes.
+//! A provider implements [`domain::ports::ChatProvider`].
+
+/// Models, ports and use cases.
+pub mod domain;
+/// DSS HTTP routes.
+#[cfg(feature = "inbound")]
+pub mod inbound;
+/// Postgres adapters.
+#[cfg(feature = "outbound")]
+pub mod outbound;
+/// `NOTIFY` channels and payloads DSS listens on.
+pub mod signals;
diff --git a/crates/external_chat/src/outbound.rs b/crates/external_chat/src/outbound.rs
new file mode 100644
index 0000000000..1ed4174965
--- /dev/null
+++ b/crates/external_chat/src/outbound.rs
@@ -0,0 +1,2 @@
+/// Postgres repositories.
+pub mod pg;
diff --git a/crates/external_chat/src/outbound/pg.rs b/crates/external_chat/src/outbound/pg.rs
new file mode 100644
index 0000000000..91e79acf7f
--- /dev/null
+++ b/crates/external_chat/src/outbound/pg.rs
@@ -0,0 +1,145 @@
+//! Postgres adapter for the external chat repository ports, over the
+//! `external_chat*` tables. The helpers taking a transaction are public so a
+//! provider's own adapter writes these tables and signals the same way.
+
+use crate::domain::{models::*, ports::ExternalChatResult};
+use crate::signals;
+use sqlx::{PgPool, Postgres, Transaction};
+use uuid::Uuid;
+
+mod chats;
+mod effects;
+mod media;
+mod timeline;
+
+pub use media::{fail_attachment_in, queue_publication_in};
+
+/// Repository over MacroDB.
+#[derive(Clone)]
+pub struct PgExternalChatRepository {
+ pool: PgPool,
+}
+
+impl PgExternalChatRepository {
+ /// Wrap the shared pool.
+ pub fn new(pool: PgPool) -> Self {
+ Self { pool }
+ }
+}
+
+/// Map an infrastructure error, keeping its cause for logs.
+pub fn failure(
+ error: impl std::error::Error + Send + Sync + 'static,
+) -> rootcause::Report<ExternalChatError> {
+ rootcause::Report::new(error).context(ExternalChatError::Unavailable)
+}
+
+/// Map an `anyhow` error from another domain's transactional helper.
+pub fn helper_failure(error: impl std::fmt::Display) -> rootcause::Report<ExternalChatError> {
+ rootcause::report!("{error:#}").context(ExternalChatError::Unavailable)
+}
+
+/// Stored data that breaks an invariant this crate or a provider maintains.
+pub fn corrupt(what: impl std::fmt::Display) -> rootcause::Report<ExternalChatError> {
+ rootcause::report!("stored WhatsApp data is invalid: {what}")
+ .context(ExternalChatError::Internal)
+}
+
+/// Decode a JSON column written by this crate or a provider.
+pub fn decode<T: serde::de::DeserializeOwned>(value: serde_json::Value) -> ExternalChatResult<T> {
+ serde_json::from_value(value)
+ .map_err(|error| rootcause::Report::new(error).context(ExternalChatError::Internal))
+}
+
+/// Signal `channel` when the transaction commits, carrying the current trace.
+pub async fn notify(
+ tx: &mut Transaction<'_, Postgres>,
+ channel: &str,
+ id: Uuid,
+) -> ExternalChatResult<()> {
+ let payload = serde_json::to_string(&signals::Signal {
+ id,
+ traceparent: signals::trace_context::current(),
+ })
+ .map_err(failure)?;
+ sqlx::query!("SELECT pg_notify($1, $2)", channel, payload)
+ .execute(&mut **tx)
+ .await
+ .map_err(failure)?;
+ Ok(())
+}
+
+/// Tell DSS, when the transaction commits, that pages showing this changed.
+pub async fn notify_change(
+ tx: &mut Transaction<'_, Postgres>,
+ change: signals::Change,
+) -> ExternalChatResult<()> {
+ let payload = serde_json::to_string(&signals::ChangeSignal {
+ change,
+ traceparent: signals::trace_context::current(),
+ })
+ .map_err(failure)?;
+ sqlx::query!("SELECT pg_notify($1, $2)", signals::CHANGED, payload)
+ .execute(&mut **tx)
+ .await
+ .map_err(failure)?;
+ Ok(())
+}
+
+/// Serialize mutations of a chat's messages, membership and sends before
+/// locking any child row. Provider accounts, when needed, must be locked first.
+pub async fn lock_chat_in(
+ tx: &mut Transaction<'_, Postgres>,
+ chat: Uuid,
+) -> ExternalChatResult<()> {
+ sqlx::query!(
+ "SELECT id FROM external_chat WHERE id = $1 FOR UPDATE",
+ chat
+ )
+ .fetch_optional(&mut **tx)
+ .await
+ .map_err(failure)?;
+ Ok(())
+}
+
+/// An `external_chat` row as selected for a [`ChatRef`].
+pub struct ChatRow {
+ /// Entity id.
+ pub id: Uuid,
+ /// Provider spelling.
+ pub provider: String,
+ /// Remote chat id.
+ pub remote_id: String,
+ /// Kind spelling.
+ pub kind: String,
+ /// Owner.
+ pub user_id: String,
+ /// State spelling.
+ pub state: String,
+ /// History boundary.
+ pub adopted_at: chrono::DateTime<chrono::Utc>,
+}
+
+/// Read a stored chat row.
+pub fn chat_ref(row: ChatRow) -> ExternalChatResult<ChatRef> {
+ Ok(ChatRef {
+ id: row.id,
+ provider: row
+ .provider
+ .parse::<ExternalChatProvider>()
+ .map_err(|_| corrupt(format_args!("provider {:?}", row.provider)))?,
+ remote_id: row.remote_id,
+ kind: row
+ .kind
+ .parse::<ExternalChatKind>()
+ .ok()
+ .ok_or_else(|| corrupt(format_args!("kind {:?}", row.kind)))?,
+ user_id: row.user_id,
+ state: match row.state.as_str() {
+ "active" => ChatState::Active,
+ "detached" => ChatState::Detached,
+ other => return Err(corrupt(format_args!("chat state {other:?}"))),
+ },
+ adopted_at: row.adopted_at,
+ })
+}
diff --git a/crates/external_chat/src/outbound/pg/chats.rs b/crates/external_chat/src/outbound/pg/chats.rs
new file mode 100644
index 0000000000..5ed7c9bba0
--- /dev/null
+++ b/crates/external_chat/src/outbound/pg/chats.rs
@@ -0,0 +1,115 @@
+//! Added external chat entities, their attachments and staged uploads.
+use super::*;
+use crate::domain::ports::{ChatRepository, StagedUpload};
+use async_trait::async_trait;
+use macro_user_id::user_id::MacroUserIdStr;
+
+/// A shape value as an integer column.
+fn column(value: Option<u32>) -> Option<i32> {
+ value.map(|value| value.min(i32::MAX as u32) as i32)
+}
+
+#[async_trait]
+impl ChatRepository for PgExternalChatRepository {
+ async fn chat(&self, chat: Uuid) -> ExternalChatResult<Option<ChatRef>> {
+ sqlx::query_as!(ChatRow, "SELECT id, provider, remote_id, kind, user_id, state, adopted_at FROM external_chat WHERE id = $1", chat)
+ .fetch_optional(&self.pool).await.map_err(failure)?.map(chat_ref).transpose()
+ }
+
+ async fn message_attachments(
+ &self,
+ chat: &ChatRef,
+ messages: &[Uuid],
+ ) -> ExternalChatResult<Vec<MessageAttachments>> {
+ let rows = sqlx::query!(
+ r#"SELECT m.message_id AS "message_id!", a.ordinal, a.state, a.filename, a.mime, a.size, a.file_id,
+ a.width, a.height, a.duration_seconds, a.presentation
+ FROM external_chat_attachment a
+ JOIN external_chat_message m ON m.chat_id = a.chat_id AND m.person_id = a.person_id AND m.source_id = a.source_id
+ WHERE a.chat_id = $1 AND m.message_id = ANY($2) AND a.state <> 'deleted'
+ ORDER BY m.message_id, a.ordinal"#,
+ chat.id,
+ messages
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(failure)?;
+ let mut result: Vec<MessageAttachments> = vec![];
+ for row in rows {
+ let mime = row.mime.unwrap_or_default();
+ let state = match row.state.as_str() {
+ "ready" => AttachmentState::Ready,
+ "failed" if MediaKind::of(&mime).is_none() => AttachmentState::Unsupported,
+ "failed" => AttachmentState::Unavailable,
+ _ => AttachmentState::Loading,
+ };
+ let attachment = Attachment {
+ ordinal: row.ordinal,
+ state,
+ file_name: row.filename.unwrap_or_default(),
+ mime,
+ size: row.size.unwrap_or(0).max(0) as u64,
+ file_id: row.file_id,
+ width: row.width.map(|value| value.max(0) as u32),
+ height: row.height.map(|value| value.max(0) as u32),
+ duration_seconds: row.duration_seconds.map(|value| value.max(0) as u32),
+ presentation: row
+ .presentation
+ .as_deref()
+ .and_then(MediaPresentation::parse),
+ };
+ match result.last_mut() {
+ Some(last) if last.message_id == row.message_id => {
+ last.attachments.push(attachment)
+ }
+ _ => result.push(MessageAttachments {
+ message_id: row.message_id,
+ attachments: vec![attachment],
+ }),
+ }
+ }
+ Ok(result)
+ }
+
+ async fn stage_upload(&self, upload: StagedUpload<'_>) -> ExternalChatResult<Uuid> {
+ let id = Uuid::now_v7();
+ sqlx::query!(
+ r#"INSERT INTO external_chat_media_blob (id, chat_id, user_id, mime, filename, sha256, bytes, width, height, duration_seconds)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)"#,
+ id,
+ upload.chat_id,
+ upload.user.as_ref(),
+ upload.mime,
+ upload.filename,
+ &upload.sha256[..],
+ upload.bytes,
+ column(upload.shape.width.filter(|width| *width > 0)),
+ column(upload.shape.height.filter(|height| *height > 0)),
+ column(upload.shape.duration_seconds)
+ )
+ .execute(&self.pool)
+ .await
+ .map_err(failure)?;
+ Ok(id)
+ }
+
+ async fn staged_upload_mime(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ chat_id: Uuid,
+ upload: Uuid,
+ ) -> ExternalChatResult<Option<String>> {
+ let user = user.as_ref();
+ sqlx::query_scalar!(
+ r#"SELECT mime FROM external_chat_media_blob b
+ WHERE id = $1 AND chat_id = $2 AND user_id = $3
+ AND NOT EXISTS (SELECT 1 FROM external_chat_attachment a WHERE a.blob_id = b.id)"#,
+ upload,
+ chat_id,
+ user
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(failure)
+ }
+}
diff --git a/crates/whatsapp_linked/src/outbound/pg/effects.rs b/crates/external_chat/src/outbound/pg/effects.rs
similarity index 76%
rename from crates/whatsapp_linked/src/outbound/pg/effects.rs
rename to crates/external_chat/src/outbound/pg/effects.rs
index 6cf324fa00..deb91c7ab1 100644
--- a/crates/whatsapp_linked/src/outbound/pg/effects.rs
+++ b/crates/external_chat/src/outbound/pg/effects.rs
@@ -2,13 +2,13 @@
use super::*;
use crate::domain::ports::{
- AudienceRepository, EffectsRepository, GroupEffect, MessageNotice, Origin,
+ AudienceRepository, ChatEffect, EffectsRepository, MessageNotice, Origin,
};
use async_trait::async_trait;
#[async_trait]
-impl EffectsRepository for PgLinkedRepository {
- async fn pending_effects(&self, limit: i64) -> LinkedResult<Vec<GroupEffect>> {
+impl EffectsRepository for PgExternalChatRepository {
+ async fn pending_effects(&self, limit: i64) -> ExternalChatResult<Vec<ChatEffect>> {
sqlx::query!(
r#"SELECT b.id AS chat_id, m.message_id AS "message_id!", m.effect_revision, m.published_revision, m.origin, m.content_revision, m.deleted_at, p.user_id, m.kind
FROM external_chat_message m
@@ -23,7 +23,7 @@ impl EffectsRepository for PgLinkedRepository {
.map_err(failure)?
.into_iter()
.map(|row| {
- Ok(GroupEffect {
+ Ok(ChatEffect {
chat_id: row.chat_id,
message_id: row.message_id,
revision: row.effect_revision,
@@ -41,7 +41,7 @@ impl EffectsRepository for PgLinkedRepository {
.collect()
}
- async fn published(&self, message: Uuid, revision: i64) -> LinkedResult<()> {
+ async fn published(&self, message: Uuid, revision: i64) -> ExternalChatResult<()> {
sqlx::query!(
"UPDATE external_chat_message SET published_revision = GREATEST(published_revision, $2) WHERE message_id = $1",
message,
@@ -53,7 +53,7 @@ impl EffectsRepository for PgLinkedRepository {
Ok(())
}
- async fn defer_effect(&self, message: Uuid) -> LinkedResult<()> {
+ async fn defer_effect(&self, message: Uuid) -> ExternalChatResult<()> {
sqlx::query!(
"UPDATE external_chat_message SET notification_retry_at = now() + interval '30 seconds' WHERE message_id = $1",
message
@@ -66,21 +66,11 @@ impl EffectsRepository for PgLinkedRepository {
}
#[async_trait]
-impl AudienceRepository for PgLinkedRepository {
- async fn account_owner(&self, account: Uuid) -> LinkedResult<Option<String>> {
- sqlx::query_scalar!(
- "SELECT user_id FROM whatsapp_linked_account WHERE id = $1",
- account
- )
- .fetch_optional(&self.pool)
- .await
- .map_err(failure)
- }
-
- async fn link_viewers(&self, group: Uuid) -> LinkedResult<Vec<String>> {
+impl AudienceRepository for PgExternalChatRepository {
+ async fn link_viewers(&self, chat: Uuid) -> ExternalChatResult<Vec<String>> {
sqlx::query_scalar!(
"SELECT user_id FROM external_chat_read_state WHERE chat_id = $1 AND external_chat_can_view(chat_id, user_id)",
- group
+ chat
)
.fetch_all(&self.pool)
.await
@@ -89,20 +79,20 @@ impl AudienceRepository for PgLinkedRepository {
async fn message_notice(
&self,
- group: Uuid,
+ chat: Uuid,
message: Uuid,
- ) -> LinkedResult<Option<MessageNotice>> {
+ ) -> ExternalChatResult<Option<MessageNotice>> {
Ok(sqlx::query!(
r#"SELECT g.provider, g.name, m.deleted_at IS NOT NULL OR m.content_revision > 0 AS "superseded!"
FROM external_chat g JOIN external_chat_message m ON m.chat_id = g.id
WHERE g.id = $1 AND m.id = $2"#,
- group,
+ chat,
message
)
.fetch_optional(&self.pool)
.await
.map_err(failure)?
- .map(|row| -> LinkedResult<MessageNotice> {
+ .map(|row| -> ExternalChatResult<MessageNotice> {
Ok(MessageNotice {
provider: row
.provider
diff --git a/crates/external_chat/src/outbound/pg/media.rs b/crates/external_chat/src/outbound/pg/media.rs
new file mode 100644
index 0000000000..5af70a6355
--- /dev/null
+++ b/crates/external_chat/src/outbound/pg/media.rs
@@ -0,0 +1,226 @@
+//! Importing attachment bytes into static files (DSS), and the static files
+//! no attachment references any more. Attachment states: `pending` →
+//! `downloaded` (bytes in `external_chat_media_blob`) → `ready` (`file_id`
+//! set), or `failed`; `deleted` follows the message. A file an attachment
+//! lets go of is queued in `external_chat_file_deletion` by a trigger.
+
+use super::*;
+use crate::domain::ports::{FileImport, MediaRepository};
+use async_trait::async_trait;
+
+/// Mark one attachment failed and tell readers on its message.
+pub async fn fail_attachment_in(
+ tx: &mut Transaction<'_, Postgres>,
+ attachment: &AttachmentRef,
+) -> ExternalChatResult<()> {
+ let row = sqlx::query!(
+ r#"UPDATE external_chat_attachment a SET state = 'failed', claimed_until = NULL, locator = NULL
+ FROM external_chat_message m, external_chat b
+ WHERE a.chat_id = $1 AND a.person_id = $2 AND a.source_id = $3 AND a.ordinal = $4
+ AND a.state IN ('pending', 'downloaded')
+ AND m.chat_id = a.chat_id AND m.person_id = a.person_id AND m.source_id = a.source_id
+ AND b.id = a.chat_id
+ RETURNING a.blob_id, a.filename, m.message_id, b.id AS chat_id"#,
+ attachment.chat_id,
+ attachment.person_id,
+ attachment.source_id,
+ attachment.ordinal
+ )
+ .fetch_optional(&mut **tx)
+ .await
+ .map_err(failure)?;
+ let Some(row) = row else { return Ok(()) };
+ sqlx::query!(
+ "DELETE FROM external_chat_media_blob WHERE id = $1",
+ row.blob_id
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(failure)?;
+ if let Some(message) = row.message_id {
+ queue_publication_in(tx, attachment, message).await?;
+ }
+ Ok(())
+}
+
+/// Queue publication of the attachment's message.
+pub async fn queue_publication_in(
+ tx: &mut Transaction<'_, Postgres>,
+ attachment: &AttachmentRef,
+ message: Uuid,
+) -> ExternalChatResult<()> {
+ sqlx::query!(
+ r#"UPDATE external_chat_message SET effect_revision = effect_revision + 1, notification_retry_at = now()
+ WHERE chat_id = $1 AND person_id = $2 AND source_id = $3"#,
+ attachment.chat_id,
+ attachment.person_id,
+ attachment.source_id
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(failure)?;
+ notify(tx, signals::CHAT_EFFECTS, message).await?;
+ notify_change(
+ tx,
+ signals::Change::Chat {
+ chat_id: attachment.chat_id,
+ },
+ )
+ .await
+}
+
+#[async_trait]
+impl MediaRepository for PgExternalChatRepository {
+ async fn due_imports(&self, limit: i64) -> ExternalChatResult<Vec<FileImport>> {
+ // Claiming pushes retry_at forward, so DSS replicas don't import twice.
+ let rows = sqlx::query!(
+ r#"UPDATE external_chat_attachment a SET retry_at = now() + interval '5 minutes'
+ FROM external_chat_media_blob blob
+ WHERE (a.chat_id, a.person_id, a.source_id, a.ordinal) IN (
+ SELECT chat_id, person_id, source_id, ordinal FROM external_chat_attachment
+ WHERE state = 'downloaded' AND retry_at <= now()
+ ORDER BY retry_at LIMIT $1 FOR UPDATE SKIP LOCKED)
+ AND blob.id = a.blob_id
+ RETURNING a.chat_id, a.person_id, a.source_id, a.ordinal, a.blob_id, a.attempts,
+ blob.user_id, blob.filename, blob.mime"#,
+ limit
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(failure)?;
+ Ok(rows
+ .into_iter()
+ .map(|row| FileImport {
+ attachment: AttachmentRef {
+ chat_id: row.chat_id,
+ person_id: row.person_id,
+ source_id: row.source_id,
+ ordinal: row.ordinal,
+ },
+ blob_id: row.blob_id,
+ chat_id: row.chat_id,
+ owner: row.user_id,
+ filename: row.filename,
+ mime: row.mime,
+ attempts: row.attempts,
+ })
+ .collect())
+ }
+
+ async fn import_bytes(&self, import: &FileImport) -> ExternalChatResult<Option<Vec<u8>>> {
+ sqlx::query_scalar!(
+ "SELECT bytes FROM external_chat_media_blob WHERE id = $1",
+ import.blob_id
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(failure)
+ }
+
+ async fn import_stored(&self, import: &FileImport, file_id: Uuid) -> ExternalChatResult<()> {
+ let attachment = &import.attachment;
+ let mut tx = self.pool.begin().await.map_err(failure)?;
+ lock_chat_in(&mut tx, import.attachment.chat_id).await?;
+ let row = sqlx::query!(
+ r#"UPDATE external_chat_attachment a SET state = 'ready', file_id = $5
+ FROM external_chat_message m
+ WHERE a.chat_id = $1 AND a.person_id = $2 AND a.source_id = $3 AND a.ordinal = $4 AND a.state = 'downloaded'
+ AND m.chat_id = a.chat_id AND m.person_id = a.person_id AND m.source_id = a.source_id
+ RETURNING m.message_id"#,
+ attachment.chat_id,
+ attachment.person_id,
+ attachment.source_id,
+ attachment.ordinal,
+ file_id
+ )
+ .fetch_optional(&mut *tx)
+ .await
+ .map_err(failure)?;
+ match row {
+ Some(row) => {
+ if let Some(message) = row.message_id {
+ queue_publication_in(&mut tx, attachment, message).await?;
+ }
+ }
+ // Deleted or imported meanwhile: nothing will reference the file.
+ None => {
+ sqlx::query!(
+ "INSERT INTO external_chat_file_deletion (file_id) VALUES ($1) ON CONFLICT DO NOTHING",
+ file_id
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(failure)?;
+ }
+ }
+ sqlx::query!(
+ "DELETE FROM external_chat_media_blob WHERE id = $1",
+ import.blob_id
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(failure)?;
+ tx.commit().await.map_err(failure)
+ }
+
+ async fn import_failed(&self, import: &FileImport, permanent: bool) -> ExternalChatResult<()> {
+ let mut tx = self.pool.begin().await.map_err(failure)?;
+ lock_chat_in(&mut tx, import.attachment.chat_id).await?;
+ if permanent {
+ fail_attachment_in(&mut tx, &import.attachment).await?;
+ } else {
+ let attachment = &import.attachment;
+ sqlx::query!(
+ r#"UPDATE external_chat_attachment SET attempts = attempts + 1,
+ retry_at = now() + least(interval '1 minute' * power(2, attempts), interval '1 hour')
+ WHERE chat_id = $1 AND person_id = $2 AND source_id = $3 AND ordinal = $4 AND state = 'downloaded'"#,
+ attachment.chat_id,
+ attachment.person_id,
+ attachment.source_id,
+ attachment.ordinal
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(failure)?;
+ }
+ tx.commit().await.map_err(failure)
+ }
+
+ async fn expire_uploads(&self) -> ExternalChatResult<u64> {
+ Ok(sqlx::query!(
+ r#"DELETE FROM external_chat_media_blob b WHERE b.created_at < now() - interval '1 day'
+ AND NOT EXISTS (SELECT 1 FROM external_chat_attachment a WHERE a.blob_id = b.id)"#
+ )
+ .execute(&self.pool)
+ .await
+ .map_err(failure)?
+ .rows_affected())
+ }
+
+ async fn due_file_deletions(&self, limit: i64) -> ExternalChatResult<Vec<Uuid>> {
+ // Claiming pushes retry_at forward, so DSS replicas don't delete twice
+ // and a failing file waits before it is tried again.
+ sqlx::query_scalar!(
+ r#"UPDATE external_chat_file_deletion SET retry_at = now() + interval '5 minutes'
+ WHERE file_id IN (
+ SELECT file_id FROM external_chat_file_deletion WHERE retry_at <= now()
+ ORDER BY retry_at LIMIT $1 FOR UPDATE SKIP LOCKED)
+ RETURNING file_id"#,
+ limit
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(failure)
+ }
+
+ async fn files_deleted(&self, files: &[Uuid]) -> ExternalChatResult<()> {
+ sqlx::query!(
+ "DELETE FROM external_chat_file_deletion WHERE file_id = ANY($1)",
+ files
+ )
+ .execute(&self.pool)
+ .await
+ .map_err(failure)?;
+ Ok(())
+ }
+}
diff --git a/crates/whatsapp_linked/src/outbound/pg/timeline.rs b/crates/external_chat/src/outbound/pg/timeline.rs
similarity index 55%
rename from crates/whatsapp_linked/src/outbound/pg/timeline.rs
rename to crates/external_chat/src/outbound/pg/timeline.rs
index d54d5d0570..b2aaa682db 100644
--- a/crates/whatsapp_linked/src/outbound/pg/timeline.rs
+++ b/crates/external_chat/src/outbound/pg/timeline.rs
@@ -1,12 +1,9 @@
-//! Queries over standalone WhatsApp entities, messages and read cursors.
+//! Queries over external chat entities, messages and read cursors.
use super::*;
use crate::domain::{
- ports::BindingRepository,
+ ports::ChatRepository,
rich::RichContent,
- timeline::{
- view::{self, People, StoredVote},
- *,
- },
+ timeline::{view::StoredVote, *},
};
use async_trait::async_trait;
use macro_user_id::user_id::MacroUserIdStr;
@@ -21,120 +18,35 @@ fn backfill(state: Option<&str>, imported: i32) -> Option<Backfill> {
})
}
-impl PgLinkedRepository {
- /// Names for WhatsApp ids in a group: people seen in it first (their
- /// WhatsApp name and verified Macro user), then the owner's roster.
- async fn people(&self, binding: &Binding, jids: &[String]) -> LinkedResult<People> {
- let mut people = People::default();
- if jids.is_empty() {
- return Ok(people);
- }
- let seen = sqlx::query!(
- r#"SELECT a.jid, p.user_id, p.display_name FROM whatsapp_identity_alias a
- JOIN external_chat_person p ON p.id = a.person_id
- WHERE a.chat_id = $1 AND a.jid = ANY($2)"#,
- binding.id,
- jids
- )
- .fetch_all(&self.pool)
- .await
- .map_err(failure)?;
- let roster = sqlx::query_scalar!(
- r#"SELECT r.metadata FROM whatsapp_roster r
- JOIN whatsapp_linked_account a ON a.id = r.account_id
- JOIN external_chat g ON g.user_id = a.user_id AND g.remote_id = r.group_jid
- WHERE g.id = $1"#,
- binding.id
- )
- .fetch_optional(&self.pool)
- .await
- .map_err(failure)?
- .map(decode::<GroupInfo>)
- .transpose()?;
- for row in seen {
- let roster_name = roster.as_ref().and_then(|roster| {
- roster
- .participants
- .iter()
- .find(|participant| {
- participant.identity.jid == row.jid
- || participant.identity.alternate_jid.as_deref() == Some(&row.jid)
- })
- .and_then(|participant| participant.identity.label())
- });
- let name = row
- .display_name
- .filter(|name| !name.trim().is_empty())
- .or(roster_name);
- if let Some(name) = name {
- people.learn(
- &row.jid,
- ExternalChatPersonRef {
- name,
- user_id: row.user_id,
- },
- );
- }
- }
- for participant in roster.iter().flat_map(|roster| &roster.participants) {
- let Some(name) = participant.identity.label() else {
- continue;
- };
- for jid in [
- Some(&participant.identity.jid),
- participant.identity.alternate_jid.as_ref(),
- ]
- .into_iter()
- .flatten()
- {
- people.learn(
- jid,
- ExternalChatPersonRef {
- name: name.clone(),
- user_id: None,
- },
- );
- }
- }
- Ok(people)
- }
+impl PgExternalChatRepository {
/// A newest-first page of the timeline, or only the message `only`.
async fn timeline_page(
&self,
- group: Uuid,
+ chat: &ChatRef,
user: &str,
before: Option<Uuid>,
only: Option<Uuid>,
limit: i64,
- ) -> LinkedResult<ExternalChatMessages> {
+ ) -> ExternalChatResult<StoredPage> {
let rows = sqlx::query!(r#"SELECT m.id,m.person_id,p.user_id,COALESCE(p.display_name,'WhatsApp participant') AS "name!",
- m.body,m.reply_to,m.source_at,m.ingested_at,m.revised_at,m.deleted_at,m.kind,m.rich,m.mentions,m.forwarded,m.view_once,
- (SELECT state FROM whatsapp_send s WHERE s.message_id = m.id AND s.user_id = $2 ORDER BY s.created_at DESC LIMIT 1) AS delivery
+ m.body,m.reply_to,m.source_at,m.ingested_at,m.revised_at,m.deleted_at,m.kind,m.rich,m.mentions,m.forwarded,m.view_once
FROM external_chat_message m JOIN external_chat_person p ON p.id = m.person_id
WHERE m.chat_id = $1 AND m.message_id IS NOT NULL
- AND ($3::uuid IS NULL OR (m.source_at,m.id) < (SELECT source_at,id FROM external_chat_message WHERE id = $3 AND chat_id = $1 AND message_id IS NOT NULL))
- AND ($5::uuid IS NULL OR m.id = $5)
- ORDER BY m.source_at DESC,m.id DESC LIMIT $4"#,group,user,before,limit+1,only)
+ AND ($2::uuid IS NULL OR (m.source_at,m.id) < (SELECT source_at,id FROM external_chat_message WHERE id = $2 AND chat_id = $1 AND message_id IS NOT NULL))
+ AND ($4::uuid IS NULL OR m.id = $4)
+ ORDER BY m.source_at DESC,m.id DESC LIMIT $3"#,chat.id,before,limit+1,only)
.fetch_all(&self.pool).await.map_err(failure)?;
let more = rows.len() > limit as usize;
- let mut messages = Vec::with_capacity(rows.len());
- let binding = self.binding(group).await?.ok_or(LinkedError::NotFound)?;
let ids: Vec<_> = rows.iter().take(limit as usize).map(|r| r.id).collect();
- let attachments = self.message_attachments(&binding, &ids).await?;
+ let attachments = self.message_attachments(chat, &ids).await?;
let mut rich = Vec::with_capacity(ids.len());
- let mut wanted = vec![];
for row in rows.iter().take(limit as usize) {
let content: Option<RichContent> = match (&row.deleted_at, &row.rich) {
(None, Some(value)) => Some(decode(value.clone())?),
_ => None,
};
- if let Some(RichContent::System(line)) = &content {
- wanted.extend(line.participants.iter().cloned());
- }
- wanted.extend(row.mentions.iter().cloned());
rich.push(content);
}
- let people = self.people(&binding, &wanted).await?;
let polls: Vec<Uuid> = rows
.iter()
.zip(&rich)
@@ -164,48 +76,36 @@ impl PgLinkedRepository {
let reactions=sqlx::query!(r#"SELECT message_id,emoji,count(*) AS "count!",COALESCE(bool_or(user_id=$2),false) AS "mine!"
FROM external_chat_reaction WHERE message_id=ANY($1) AND emoji<>'' GROUP BY message_id,emoji ORDER BY message_id,emoji"#,&ids,user)
.fetch_all(&self.pool).await.map_err(failure)?;
+ let mut messages = Vec::with_capacity(ids.len());
for (row, content) in rows.into_iter().take(limit as usize).zip(rich) {
- let sender = ExternalChatPersonRef {
- name: row.name.clone(),
- user_id: row.user_id.clone(),
- };
- let message_votes: Vec<&StoredVote> = votes
- .iter()
- .filter(|vote| vote.message_id == row.id)
- .collect();
- let grouped = reactions
- .iter()
- .filter(|r| r.message_id == row.id)
- .map(|r| ExternalChatReaction {
- emoji: r.emoji.clone(),
- count: r.count,
- mine: r.mine,
- })
- .collect();
- messages.push(ExternalChatMessage {
+ let deleted = row.deleted_at.is_some();
+ messages.push(StoredMessage {
id: row.id,
sender: ExternalChatSender {
person_id: row.person_id,
user_id: row.user_id,
name: row.name,
},
- body: if row.deleted_at.is_some() {
- String::new()
- } else {
- row.body
- },
+ body: if deleted { String::new() } else { row.body },
reply_to: row.reply_to,
sent_at: row.source_at,
received_at: row.ingested_at,
edited_at: row.revised_at,
- deleted: row.deleted_at.is_some(),
- reactions: grouped,
+ deleted,
+ reactions: reactions
+ .iter()
+ .filter(|r| r.message_id == row.id)
+ .map(|r| ExternalChatReaction {
+ emoji: r.emoji.clone(),
+ count: r.count,
+ mine: r.mine,
+ })
+ .collect(),
attachments: attachments
.iter()
.find(|a| a.message_id == row.id)
.map(|a| a.attachments.clone())
.unwrap_or_default(),
- delivery_state: row.delivery.as_deref().and_then(DeliveryState::parse),
kind: if row.kind == "system" {
ExternalChatMessageKind::System
} else {
@@ -213,28 +113,30 @@ impl PgLinkedRepository {
},
forwarded: row.forwarded,
view_once: row.view_once,
- mentions: if row.deleted_at.is_some() {
- vec![]
- } else {
- view::mentions_view(&row.mentions, &people)
- },
- rich: content.and_then(|content| {
- view::rich_view(content, &sender, &message_votes, user, &people)
- }),
+ mentions: if deleted { vec![] } else { row.mentions },
+ rich: content,
+ votes: votes
+ .iter()
+ .filter(|vote| vote.message_id == row.id)
+ .cloned()
+ .collect(),
});
}
let next_cursor = more.then(|| messages.last().map(|m| m.id)).flatten();
- Ok(ExternalChatMessages {
+ Ok(StoredPage {
messages,
next_cursor,
})
}
- /// The chats `user` can see (or just `only`, already authorized), with
- /// the caller's own roster. Candidates come from the viewer's grants and
- /// read cursors before the visibility check, so a viewer who sees few
- /// chats never scans every chat.
- async fn group_rows(&self, user: &str, only: Option<Uuid>) -> LinkedResult<Vec<VisibleGroup>> {
+ /// The chats `user` can see (or just `only`, already authorized).
+ /// Candidates come from the viewer's grants and read cursors before the
+ /// visibility check, so a viewer who sees few chats never scans every chat.
+ async fn chat_rows(
+ &self,
+ user: &str,
+ only: Option<Uuid>,
+ ) -> ExternalChatResult<Vec<VisibleChat>> {
let rows = sqlx::query!(r#"WITH sources AS MATERIALIZED (
SELECT array_append(ARRAY(SELECT team_id::text FROM team_user WHERE user_id = $1)
|| ARRAY(SELECT channel_id::text FROM comms_channel_participants WHERE user_id = $1 AND left_at IS NULL), $1) AS ids),
@@ -250,28 +152,16 @@ impl PgLinkedRepository {
AND m.deleted_at IS NULL AND m.origin<>'history' AND m.kind<>'system'
AND NOT EXISTS(SELECT 1 FROM external_chat_person p WHERE p.id=m.person_id AND p.user_id=$1)
AND (r.message_id IS NULL OR (m.source_at,m.id)>(r.read_at,r.message_id)) LIMIT $3) unread) AS "unread_count!",
- (SELECT max(source_at) FROM external_chat_message WHERE chat_id=g.id AND message_id IS NOT NULL) AS last_message_at,
- roster.metadata AS "metadata?",
- seen.community_jid AS "community_jid?",seen.community_announcements AS "community_announcements?",parent.name AS "community_name?"
+ (SELECT max(source_at) FROM external_chat_message WHERE chat_id=g.id AND message_id IS NOT NULL) AS last_message_at
FROM candidates c JOIN external_chat g ON g.id=c.id
LEFT JOIN external_chat_read_state r ON r.chat_id=g.id AND r.user_id=$1
- LEFT JOIN whatsapp_linked_account a ON a.user_id=$1
- LEFT JOIN whatsapp_roster roster ON roster.account_id=a.id AND roster.group_jid=g.remote_id
- LEFT JOIN LATERAL (SELECT r.metadata->>'communityJid' AS community_jid,COALESCE((r.metadata->>'communityAnnouncements')::boolean,false) AS community_announcements
- FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id
- WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=g.remote_id
- ORDER BY r.observed_at DESC LIMIT 1) seen ON true
- LEFT JOIN LATERAL (SELECT r.metadata->>'name' AS name
- FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id
- WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=seen.community_jid
- ORDER BY r.observed_at DESC LIMIT 1) parent ON true
-- On the candidate, not the chat: Postgres would otherwise check every chat.
WHERE ($2::uuid IS NOT NULL OR external_chat_can_view(c.id,$1))
ORDER BY g.adopted_at DESC LIMIT 2000"#,user,only,UNREAD_COUNT_CAP)
.fetch_all(&self.pool).await.map_err(failure)?;
rows.into_iter()
.map(|row| {
- let binding = bindings::binding(bindings::BindingRow {
+ let chat = chat_ref(ChatRow {
id: row.id,
provider: row.provider,
remote_id: row.remote_id,
@@ -280,24 +170,18 @@ impl PgLinkedRepository {
state: row.state,
adopted_at: row.adopted_at,
})?;
- Ok(VisibleGroup {
- facts: GroupFacts {
+ Ok(VisibleChat {
+ facts: ChatFacts {
id: row.id,
- provider: binding.provider,
+ provider: chat.provider,
name: row.name,
- kind: binding.kind,
+ kind: chat.kind,
user_id: row.user_id,
unread_count: row.unread_count,
last_message_at: row.last_message_at,
backfill: backfill(row.backfill_state.as_deref(), row.backfill_imported),
- community: row.community_jid.map(|jid| CommunityFacts {
- jid,
- name: row.community_name,
- announcements: row.community_announcements.unwrap_or(false),
- }),
},
- binding,
- roster: row.metadata.map(decode).transpose()?,
+ chat,
})
})
.collect()
@@ -305,66 +189,69 @@ impl PgLinkedRepository {
}
#[async_trait]
-impl TimelineRepository for PgLinkedRepository {
- async fn visible_groups(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<Vec<VisibleGroup>> {
+impl TimelineRepository for PgExternalChatRepository {
+ async fn visible_chats(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<Vec<VisibleChat>> {
let user = user.as_ref();
- self.group_rows(user, None).await
+ self.chat_rows(user, None).await
}
- async fn group_facts(&self, group: Uuid, user: &str) -> LinkedResult<GroupFacts> {
- self.group_rows(user, Some(group))
+ async fn chat_facts(&self, chat: Uuid, user: &str) -> ExternalChatResult<ChatFacts> {
+ self.chat_rows(user, Some(chat))
.await?
.pop()
.map(|visible| visible.facts)
- .ok_or_else(|| LinkedError::NotFound.into())
+ .ok_or_else(|| ExternalChatError::NotFound.into())
}
async fn messages(
&self,
- group: Uuid,
+ chat: &ChatRef,
user: &str,
before: Option<Uuid>,
limit: i64,
- ) -> LinkedResult<ExternalChatMessages> {
- self.timeline_page(group, user, before, None, limit).await
+ ) -> ExternalChatResult<StoredPage> {
+ self.timeline_page(chat, user, before, None, limit).await
}
async fn message(
&self,
- group: Uuid,
+ chat: &ChatRef,
user: &str,
message: Uuid,
- ) -> LinkedResult<Option<ExternalChatMessage>> {
+ ) -> ExternalChatResult<Option<StoredMessage>> {
Ok(self
- .timeline_page(group, user, None, Some(message), 1)
+ .timeline_page(chat, user, None, Some(message), 1)
.await?
.messages
.pop())
}
async fn mark_read(
&self,
- group: Uuid,
+ chat: Uuid,
user: &MacroUserIdStr<'_>,
message: Uuid,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let user = user.as_ref();
let changed = sqlx::query!(r#"INSERT INTO external_chat_read_state (user_id,chat_id,message_id,read_at)
SELECT $2,$1,id,source_at FROM external_chat_message WHERE id = $3 AND chat_id = $1 AND message_id IS NOT NULL
ON CONFLICT (user_id,chat_id) DO UPDATE SET message_id = excluded.message_id,read_at = excluded.read_at
- WHERE (external_chat_read_state.read_at,external_chat_read_state.message_id) < (excluded.read_at,excluded.message_id)"#,group,user,message)
+ WHERE (external_chat_read_state.read_at,external_chat_read_state.message_id) < (excluded.read_at,excluded.message_id)"#,chat,user,message)
.execute(&self.pool).await.map_err(failure)?;
if changed.rows_affected() == 0 {
- let exists=sqlx::query_scalar!("SELECT id FROM external_chat_message WHERE id=$1 AND chat_id=$2 AND message_id IS NOT NULL",message,group).fetch_optional(&self.pool).await.map_err(failure)?;
+ let exists=sqlx::query_scalar!("SELECT id FROM external_chat_message WHERE id=$1 AND chat_id=$2 AND message_id IS NOT NULL",message,chat).fetch_optional(&self.pool).await.map_err(failure)?;
if exists.is_none() {
- return Err(LinkedError::NotFound.into());
+ return Err(ExternalChatError::NotFound.into());
}
}
Ok(())
}
- async fn permissions(&self, group: Uuid) -> LinkedResult<SharePermissionV2> {
+ async fn permissions(&self, chat: Uuid) -> ExternalChatResult<SharePermissionV2> {
let r=sqlx::query!(r#"SELECT sp.id,sp."linkShare" AS "link_share: LinkShare",sp."linkShareAccessLevel" AS "link_level: AccessLevel",
- sp.team_share_access_level AS "team_level: AccessLevel",g.user_id FROM external_chat g JOIN "SharePermission" sp ON sp.id=g.share_permission_id WHERE g.id=$1"#,group)
- .fetch_optional(&self.pool).await.map_err(failure)?.ok_or(LinkedError::NotFound)?;
+ sp.team_share_access_level AS "team_level: AccessLevel",g.user_id FROM external_chat g JOIN "SharePermission" sp ON sp.id=g.share_permission_id WHERE g.id=$1"#,chat)
+ .fetch_optional(&self.pool).await.map_err(failure)?.ok_or(ExternalChatError::NotFound)?;
let channels = entity_access_db_utils::get_direct_channel_grants(
&self.pool,
- &group,
+ &chat,
entity_access_db_utils::EntityType::ExternalChat,
)
.await
@@ -380,12 +267,12 @@ impl TimelineRepository for PgLinkedRepository {
}
async fn share_facts(
&self,
- group: Uuid,
- ) -> LinkedResult<models_permissions::share_permission::team_share::TeamShareFacts> {
+ chat: Uuid,
+ ) -> ExternalChatResult<models_permissions::share_permission::team_share::TeamShareFacts> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let result = share_permission_db_utils::team_share::load_facts(
&mut tx,
- &model_entity::EntityType::ExternalChat.with_entity_string(group.to_string()),
+ &model_entity::EntityType::ExternalChat.with_entity_string(chat.to_string()),
)
.await
.map_err(helper_failure)?;
@@ -394,13 +281,13 @@ impl TimelineRepository for PgLinkedRepository {
}
async fn update_permissions(
&self,
- group: Uuid,
+ chat: Uuid,
user: &MacroUserIdStr<'_>,
update: &UpdateSharePermissionRequestV2,
command: Option<
&models_permissions::share_permission::team_share::AuthorizedTeamShareCommand,
>,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let user = user.as_ref();
let mut tx = self.pool.begin().await.map_err(failure)?;
share_permission_db_utils::team_share::acquire_guard(&mut tx)
@@ -408,13 +295,13 @@ impl TimelineRepository for PgLinkedRepository {
.map_err(failure)?;
let permission = sqlx::query_scalar!(
"SELECT share_permission_id FROM external_chat WHERE id=$1 AND user_id=$2 FOR UPDATE",
- group,
+ chat,
user
)
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::Forbidden)?;
+ .ok_or(ExternalChatError::Forbidden)?;
if let Some(command) = command {
share_permission_db_utils::team_share::apply(&mut tx, command)
.await
@@ -430,7 +317,7 @@ impl TimelineRepository for PgLinkedRepository {
{
entity_access_db_utils::update_entity_access_channel_share_permissions(
&mut tx,
- &group,
+ &chat,
entity_access_db_utils::EntityType::ExternalChat,
channels,
)
diff --git a/crates/external_chat/src/signals.rs b/crates/external_chat/src/signals.rs
new file mode 100644
index 0000000000..d0b143e079
--- /dev/null
+++ b/crates/external_chat/src/signals.rs
@@ -0,0 +1,74 @@
+//! Signals to DSS over Postgres `NOTIFY`, and the trace context they carry.
+//! Each payload is a JSON [`Signal`] (or [`ChangeSignal`] for [`CHANGED`])
+//! with the sender's `traceparent`, so the receiver joins its trace. Provider
+//! processes send them too; not an adapter.
+
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+/// `<blob_id>`: attachment bytes are ready to import (DSS listens).
+pub const MEDIA_READY: &str = "whatsapp_media_ready";
+/// `<message_id>`: an external chat message notification is ready (DSS listens).
+pub const CHAT_EFFECTS: &str = "external_chat_effects";
+/// A [`ChangeSignal`]: something a page shows changed (DSS listens and
+/// pushes `external_chat_changed` to the people who see it).
+pub const CHANGED: &str = "external_chat_changed";
+
+/// Payload of every signal except [`CHANGED`], including the providers' own.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Signal {
+ /// The id the channel documents.
+ pub id: Uuid,
+ /// The sender's W3C trace context.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub traceparent: Option<String>,
+}
+
+impl Signal {
+ /// Read a payload; `None` when malformed.
+ pub fn parse(payload: &str) -> Option<Self> {
+ serde_json::from_str(payload).ok()
+ }
+}
+
+/// What changed.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[serde(tag = "scope", rename_all = "snake_case")]
+pub enum Change {
+ /// A provider account's own chat list changed: its owner's add list.
+ Chats {
+ /// Account whose chat list changed.
+ account_id: Uuid,
+ },
+ /// An external chat's details, timeline or sends changed: everyone who
+ /// can view it.
+ Chat {
+ /// External chat entity.
+ chat_id: Uuid,
+ },
+}
+
+/// Payload of [`CHANGED`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ChangeSignal {
+ /// What changed.
+ #[serde(flatten)]
+ pub change: Change,
+ /// The sender's W3C trace context.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub traceparent: Option<String>,
+}
+
+impl ChangeSignal {
+ /// Read a payload; `None` when malformed.
+ pub fn parse(payload: &str) -> Option<Self> {
+ serde_json::from_str(payload).ok()
+ }
+}
+
+/// W3C trace context across the Postgres hops.
+#[cfg(feature = "outbound")]
+pub mod trace_context;
+
+#[cfg(test)]
+mod test;
diff --git a/crates/whatsapp_linked/src/signals/test.rs b/crates/external_chat/src/signals/test.rs
similarity index 100%
rename from crates/whatsapp_linked/src/signals/test.rs
rename to crates/external_chat/src/signals/test.rs
diff --git a/crates/whatsapp_linked/src/signals/trace_context.rs b/crates/external_chat/src/signals/trace_context.rs
similarity index 100%
rename from crates/whatsapp_linked/src/signals/trace_context.rs
rename to crates/external_chat/src/signals/trace_context.rs
diff --git a/crates/macro_db_client/AGENTS.md b/crates/macro_db_client/AGENTS.md
index 3c546abc03..d30825b706 100644
--- a/crates/macro_db_client/AGENTS.md
+++ b/crates/macro_db_client/AGENTS.md
@@ -89,7 +89,7 @@ unreferenced replicas. These additive tables do not change agent-session claims.
from resurrecting deleted definitions. Use a fresh disposable database when
checking replacement branch migrations.
-- External chats (`external_chat*` tables: chats, people, messages, attachments, reactions, poll votes, read state) are provider-neutral; WhatsApp keeps its account, pairing, roster, chat list, membership, alias, observation, receipt and send tables (`whatsapp_*`). Domain queries live in `whatsapp_linked`.
+- External chats (`external_chat*` tables: chats, people, messages, attachments, reactions, poll votes, read state) are provider-neutral; WhatsApp keeps its account, pairing, roster, chat list, membership, alias, observation, receipt and send tables (`whatsapp_*`). Domain queries live in `external_chat` (generic tables) and `whatsapp_linked` (`whatsapp_*` tables and ingest).
## Related
diff --git a/crates/whatsapp_e2e/AGENTS.md b/crates/whatsapp_e2e/AGENTS.md
index 78d40d7e0d..930d31ffa3 100644
--- a/crates/whatsapp_e2e/AGENTS.md
+++ b/crates/whatsapp_e2e/AGENTS.md
@@ -5,7 +5,8 @@
End-to-end tests for WhatsApp linked devices. Real session-service replicas
(supervisor, advisory locks, account runners, whatsmeow clients, the encrypted
Postgres device store) run against `whatsapp_fake_server`. The DSS side runs
-through `whatsapp_linked::LinkedService`, and both share one `#[sqlx::test]`
+through `WhatsAppService` and `ExternalChatService<WhatsAppProvider>`, and
+both share one `#[sqlx::test]`
database. Nothing talks to WhatsApp or AWS (the KMS is
`whatsapp_store_pg`'s `FakeWhatsAppKms`).
diff --git a/crates/whatsapp_e2e/Cargo.toml b/crates/whatsapp_e2e/Cargo.toml
index 6e6f8d0e51..fe67c5de53 100644
--- a/crates/whatsapp_e2e/Cargo.toml
+++ b/crates/whatsapp_e2e/Cargo.toml
@@ -15,6 +15,7 @@ tokio-util = { workspace = true, features = ["rt"] }
tracing = { workspace = true }
uuid = { workspace = true }
whatsapp_fake_server = { path = "../whatsapp_fake_server" }
+external_chat = { path = "../external_chat", features = ["outbound"] }
whatsapp_linked = { path = "../whatsapp_linked", features = ["outbound"] }
whatsapp_session_service = { path = "../../services/whatsapp_session_service" }
whatsapp_store_pg = { path = "../whatsapp_store_pg", features = ["fake-kms"] }
diff --git a/crates/whatsapp_e2e/src/test.rs b/crates/whatsapp_e2e/src/test.rs
index 97d33e4573..3162bbf7f3 100644
--- a/crates/whatsapp_e2e/src/test.rs
+++ b/crates/whatsapp_e2e/src/test.rs
@@ -9,17 +9,21 @@ use entity_access::domain::models::{
AccessLevel, Entity, EntityAccessAuth, EntityAccessReceipt, EntityPermission, EntityType,
ViewAccessLevel,
};
+use external_chat::domain::models::{
+ AddChat, ChatRef, ExternalChatKind, ExternalChatProvider, ReplyContent, SendAuthority,
+ SendReply,
+};
+use external_chat::domain::ports::ExternalChatResult;
+use external_chat::domain::service::ExternalChatService;
+use external_chat::outbound::pg::PgExternalChatRepository;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use sqlx::{Connection, PgConnection, PgPool};
use uuid::Uuid;
use whatsapp_fake_server::{FakePhone, FakeWhatsApp};
-use whatsapp_linked::domain::models::{
- AccountState, AddChat, Binding, ExternalChatKind, ExternalChatProvider, ReplyContent,
- SendAuthority, SendReply,
-};
-use whatsapp_linked::domain::ports::LinkedResult;
-use whatsapp_linked::domain::service::LinkedService;
-use whatsapp_linked::outbound::pg::PgLinkedRepository;
+use whatsapp_linked::domain::models::AccountState;
+use whatsapp_linked::domain::provider::WhatsAppProvider;
+use whatsapp_linked::domain::service::WhatsAppService;
+use whatsapp_linked::outbound::pg::PgWhatsAppRepository;
use whatsmeow::types::Jid;
use super::*;
@@ -63,7 +67,8 @@ struct World {
/// Holds the fleet lock until the scenario ends (see [`take_fleet_slot`]).
_fleet_slot: PgConnection,
pool: PgPool,
- linked: LinkedService,
+ linked: WhatsAppService,
+ external: ExternalChatService<WhatsAppProvider>,
whatsapp: FakeWhatsApp,
kms: SharedKms,
alice: FakePhone,
@@ -79,8 +84,11 @@ impl World {
for id in [ALICE, BOB] {
user(&pool, id).await;
}
- let linked = LinkedService::new(
- Arc::new(PgLinkedRepository::new(pool.clone())),
+ let repository = Arc::new(PgWhatsAppRepository::new(pool.clone()));
+ let linked = WhatsAppService::new(repository.clone());
+ let external = ExternalChatService::new(
+ Arc::new(WhatsAppProvider::new(repository)),
+ Arc::new(PgExternalChatRepository::new(pool.clone())),
Arc::new(Allow),
);
let whatsapp = FakeWhatsApp::start().await.unwrap();
@@ -92,6 +100,7 @@ impl World {
_fleet_slot: fleet_slot,
pool,
linked,
+ external,
whatsapp,
kms: SharedKms::new(),
alice,
@@ -152,7 +161,7 @@ impl World {
}
/// Adopts the group for `user` once the replica has listed it.
- async fn adopt(&self, user: &str, users: &[&str]) -> Binding {
+ async fn adopt(&self, user: &str, users: &[&str]) -> ChatRef {
let group = self.group.to_string();
wait_for_rows(&self.pool, TIMEOUT, || async {
let chats = self.linked.chats(&person(user)).await.ok()?.chats;
@@ -164,8 +173,8 @@ impl World {
.await
.expect("the replica lists the group");
let binding = self
- .linked
- .adopt(
+ .external
+ .add(
&person(user),
&AddChat {
provider: ExternalChatProvider::WhatsApp,
@@ -195,7 +204,7 @@ impl World {
/// A viewer opens the WhatsApp page; their account's refreshed roster
/// makes it an observer of a group shared with them.
- async fn observe(&self, user: &str, binding: &Binding) {
+ async fn observe(&self, user: &str, binding: &ChatRef) {
self.linked.chats(&person(user)).await.unwrap();
let observing = wait_for_rows(&self.pool, TIMEOUT, || async {
sqlx::query_scalar!(
@@ -215,7 +224,7 @@ impl World {
}
/// Alice paired and observing the adopted group.
- async fn alice_adopted(&self) -> (Jid, Binding) {
+ async fn alice_adopted(&self) -> (Jid, ChatRef) {
let device = self.pair(ALICE, &self.alice).await;
let binding = self.adopt(ALICE, &[]).await;
(device, binding)
@@ -223,7 +232,7 @@ impl World {
/// Waits until the WhatsApp message `source_id` is a standalone group message and
/// returns every stored copy of it (there must be one).
- async fn timeline(&self, binding: &Binding, source_id: &str) -> Vec<String> {
+ async fn timeline(&self, binding: &ChatRef, source_id: &str) -> Vec<String> {
let copies = wait_for_rows(&self.pool, TIMEOUT, || async {
let copies = group_messages(&self.pool, binding.id, source_id).await;
(!copies.is_empty()).then_some(copies)
@@ -326,8 +335,8 @@ impl World {
dump
}
- async fn send(&self, user: &str, binding: &Binding, text: &str) -> Uuid {
- self.linked
+ async fn send(&self, user: &str, binding: &ChatRef, text: &str) -> Uuid {
+ self.external
.send_reply(
&receipt::<ViewAccessLevel>(user, binding.id),
&SendReply {
@@ -751,7 +760,7 @@ async fn a_macro_attachment_reaches_the_group_intact(pool: PgPool) {
let photo = b"a photo attached in Macro".repeat(40);
let uploaded = world
- .linked
+ .external
.upload_attachment(
&receipt::<ViewAccessLevel>(ALICE, binding.id),
"label.jpg",
@@ -762,7 +771,7 @@ async fn a_macro_attachment_reaches_the_group_intact(pool: PgPool) {
.await
.unwrap();
let send = world
- .linked
+ .external
.send_reply(
&receipt::<ViewAccessLevel>(ALICE, binding.id),
&SendReply {
@@ -851,8 +860,11 @@ async fn a_message_queued_before_a_crash_is_ingested_once(pool: PgPool) {
}
#[async_trait]
-impl whatsapp_linked::domain::ports::SendRateLimiter for Allow {
- async fn check(&self, _: &macro_user_id::user_id::MacroUserIdStr<'_>) -> LinkedResult<()> {
+impl external_chat::domain::ports::SendRateLimiter for Allow {
+ async fn check(
+ &self,
+ _: &macro_user_id::user_id::MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<()> {
Ok(())
}
}
@@ -918,8 +930,8 @@ async fn group_renames_and_removals_in_whatsapp_reach_macro_live(pool: PgPool) {
.expect(left, "removal drops the group from Alice's list")
.await;
let detail = world
- .linked
- .adopted_groups(&person(ALICE))
+ .external
+ .list(&person(ALICE))
.await
.unwrap()
.into_iter()
@@ -1000,7 +1012,7 @@ async fn senders_named_only_by_lid_land_as_the_people_they_are(pool: PgPool) {
}
/// The group's backfill state and count once it is one of `states`.
-async fn backfill_state(world: &World, binding: &Binding, states: &[&str]) -> (String, i32) {
+async fn backfill_state(world: &World, binding: &ChatRef, states: &[&str]) -> (String, i32) {
let state = wait_for_rows(&world.pool, TIMEOUT, || async {
let row = sqlx::query!(
"SELECT backfill_state, backfill_imported FROM external_chat WHERE id = $1",
@@ -1081,8 +1093,8 @@ async fn adding_a_group_imports_its_earlier_messages_once_without_notifying(pool
]
);
let summary = world
- .linked
- .adopted_groups(&person(ALICE))
+ .external
+ .list(&person(ALICE))
.await
.unwrap()
.into_iter()
diff --git a/crates/whatsapp_e2e/src/test/direct_chats.rs b/crates/whatsapp_e2e/src/test/direct_chats.rs
index ab30f2366d..e4d03a2a4e 100644
--- a/crates/whatsapp_e2e/src/test/direct_chats.rs
+++ b/crates/whatsapp_e2e/src/test/direct_chats.rs
@@ -3,7 +3,7 @@
//! phone number or LID landing in the same chat, and shared viewers reading
//! without sending.
-use whatsapp_linked::domain::models::ReplyBlock;
+use external_chat::domain::models::ReplyBlock;
use super::*;
@@ -70,8 +70,8 @@ async fn a_direct_chat_imports_its_history_and_works_both_ways(pool: PgPool) {
(ExternalChatKind::Direct, true, "Customer")
);
let binding = world
- .linked
- .adopt(
+ .external
+ .add(
&person(ALICE),
&AddChat {
provider: ExternalChatProvider::WhatsApp,
@@ -168,8 +168,8 @@ async fn a_direct_chat_imports_its_history_and_works_both_ways(pool: PgPool) {
.unwrap();
tx.commit().await.unwrap();
let bobs = world
- .linked
- .group_detail(&receipt::<ViewAccessLevel>(BOB, binding.id))
+ .external
+ .detail(&receipt::<ViewAccessLevel>(BOB, binding.id))
.await
.unwrap();
assert_eq!(
@@ -178,7 +178,7 @@ async fn a_direct_chat_imports_its_history_and_works_both_ways(pool: PgPool) {
);
assert!(
world
- .linked
+ .external
.send_reply(
&receipt::<ViewAccessLevel>(BOB, binding.id),
&SendReply {
@@ -195,7 +195,7 @@ async fn a_direct_chat_imports_its_history_and_works_both_ways(pool: PgPool) {
.is_err()
);
let read = world
- .linked
+ .external
.messages(
&receipt::<ViewAccessLevel>(BOB, binding.id),
None,
diff --git a/crates/whatsapp_e2e/src/test/group_kinds.rs b/crates/whatsapp_e2e/src/test/group_kinds.rs
index c3527f6ad5..8d2604df53 100644
--- a/crates/whatsapp_e2e/src/test/group_kinds.rs
+++ b/crates/whatsapp_e2e/src/test/group_kinds.rs
@@ -2,8 +2,8 @@
//! end: added, kept and sent to like any other group; the community itself
//! can't be added.
+use external_chat::domain::models::{CannotAdd, CommunityReference, ExternalChatError, ReplyBlock};
use whatsapp_fake_server::GroupChange;
-use whatsapp_linked::domain::models::{CannotAdd, CommunityReference, LinkedError, ReplyBlock};
use whatsmeow::proto::wa_e2e::{ContextInfo, ExtendedTextMessage, FutureProofMessage, Message};
use super::*;
@@ -30,8 +30,8 @@ async fn a_disappearing_group_is_kept_in_macro_and_macro_sends_disappear(pool: P
let replica = world.replica().await;
let (_, binding) = world.alice_adopted().await;
let detail = world
- .linked
- .group_detail(&receipt::<ViewAccessLevel>(ALICE, binding.id))
+ .external
+ .detail(&receipt::<ViewAccessLevel>(ALICE, binding.id))
.await
.unwrap();
assert_eq!(detail.disappearing_seconds, Some(WEEK));
@@ -90,8 +90,8 @@ async fn a_disappearing_group_is_kept_in_macro_and_macro_sends_disappear(pool: P
);
let off = wait_for_rows(&world.pool, TIMEOUT, || async {
let detail = world
- .linked
- .group_detail(&receipt::<ViewAccessLevel>(ALICE, binding.id))
+ .external
+ .detail(&receipt::<ViewAccessLevel>(ALICE, binding.id))
.await
.ok()?;
detail.disappearing_seconds.is_none().then_some(())
@@ -158,23 +158,23 @@ async fn a_communitys_groups_are_added_and_used_but_not_the_community(pool: PgPo
remote_id: jid.to_string(),
kind: ExternalChatKind::Group,
};
- let linked = &world.linked;
+ let external = &world.external;
async move {
- linked
- .adopt(&person(ALICE), &request, SendAuthority::Human)
+ external
+ .add(&person(ALICE), &request, SendAuthority::Human)
.await
}
};
assert!(matches!(
adopt(&community).await.unwrap_err().current_context(),
- LinkedError::CannotAdd(CannotAdd::Community)
+ ExternalChatError::CannotAdd(CannotAdd::Community)
));
// A community's group works like any group, both ways.
let binding = adopt(&garden).await.unwrap();
let detail = world
- .linked
- .group_detail(&receipt::<ViewAccessLevel>(ALICE, binding.id))
+ .external
+ .detail(&receipt::<ViewAccessLevel>(ALICE, binding.id))
.await
.unwrap();
assert_eq!(detail.chat.community, Some(neighbours(false)));
@@ -201,8 +201,8 @@ async fn a_communitys_groups_are_added_and_used_but_not_the_community(pool: PgPo
// Everyone reads the announcement group; only its admins post.
let notices = adopt(&announcements).await.unwrap();
let detail = world
- .linked
- .group_detail(&receipt::<ViewAccessLevel>(ALICE, notices.id))
+ .external
+ .detail(&receipt::<ViewAccessLevel>(ALICE, notices.id))
.await
.unwrap();
assert_eq!(detail.chat.community, Some(neighbours(true)));
diff --git a/crates/whatsapp_e2e/src/test/message_types.rs b/crates/whatsapp_e2e/src/test/message_types.rs
index 1b14d9de72..b998185af9 100644
--- a/crates/whatsapp_e2e/src/test/message_types.rs
+++ b/crates/whatsapp_e2e/src/test/message_types.rs
@@ -5,17 +5,17 @@
use std::sync::Mutex;
-use whatsapp_fake_server::GroupChange;
-use whatsapp_linked::domain::effects::EffectsService;
-use whatsapp_linked::domain::models::MediaPresentation;
-use whatsapp_linked::domain::ports::{GroupEffect, GroupPublisher};
-use whatsapp_linked::domain::rich::{
+use external_chat::domain::effects::EffectsService;
+use external_chat::domain::models::MediaPresentation;
+use external_chat::domain::ports::{ChatEffect, ChatPublisher};
+use external_chat::domain::rich::{
Call, CallOutcome, ContactCard, Contacts, Event, Location, SystemAction,
};
-use whatsapp_linked::domain::timeline::{
+use external_chat::domain::timeline::{
ExternalChatMention, ExternalChatMessage, ExternalChatMessageKind, ExternalChatPersonRef,
ExternalChatPoll, ExternalChatPollOption, ExternalChatRichContent, ExternalChatSystemLine,
};
+use whatsapp_fake_server::GroupChange;
use whatsmeow::download::MediaType;
use whatsmeow::proto::wa_common::MessageKey;
use whatsmeow::proto::wa_e2e::{
@@ -27,7 +27,7 @@ use whatsmeow::proto::wa_e2e::{
use super::*;
/// The timeline entry for WhatsApp message `source`, as Alice reads it.
-async fn shown(world: &World, binding: &Binding, source: &str) -> ExternalChatMessage {
+async fn shown(world: &World, binding: &ChatRef, source: &str) -> ExternalChatMessage {
let id = sqlx::query_scalar!(
"SELECT id FROM external_chat_message WHERE chat_id = $1 AND source_id = $2",
binding.id,
@@ -37,7 +37,7 @@ async fn shown(world: &World, binding: &Binding, source: &str) -> ExternalChatMe
.await
.unwrap();
world
- .linked
+ .external
.messages(
&receipt::<ViewAccessLevel>(ALICE, binding.id),
None,
@@ -781,7 +781,7 @@ async fn polls_places_cards_events_mentions_pins_and_calls_arrive_structured(poo
// Alice mentions the customer from Macro.
let text = format!("@{} on it", world.customer.lid().user);
let send = world
- .linked
+ .external
.send_reply(
&receipt::<ViewAccessLevel>(ALICE, binding.id),
&SendReply {
@@ -821,8 +821,8 @@ async fn polls_places_cards_events_mentions_pins_and_calls_arrive_structured(poo
struct Published(Mutex<Vec<Uuid>>);
#[async_trait]
-impl GroupPublisher for Published {
- async fn publish(&self, effect: &GroupEffect) -> LinkedResult<()> {
+impl ChatPublisher for Published {
+ async fn publish(&self, effect: &ChatEffect) -> ExternalChatResult<()> {
self.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
@@ -907,7 +907,7 @@ async fn group_changes_land_once_as_system_lines_that_never_notify(pool: PgPool)
);
let page = world
- .linked
+ .external
.messages(
&receipt::<ViewAccessLevel>(ALICE, binding.id),
None,
@@ -972,8 +972,8 @@ async fn group_changes_land_once_as_system_lines_that_never_notify(pool: PgPool)
// Only the three texts are unread, and only they notify.
let summary = world
- .linked
- .adopted_groups(&person(ALICE))
+ .external
+ .list(&person(ALICE))
.await
.unwrap()
.into_iter()
@@ -982,7 +982,7 @@ async fn group_changes_land_once_as_system_lines_that_never_notify(pool: PgPool)
assert_eq!(summary.unread_count, 3, "system lines are never unread");
let published = Arc::new(Published::default());
let effects = EffectsService::new(
- Arc::new(PgLinkedRepository::new(world.pool.clone())),
+ Arc::new(PgExternalChatRepository::new(world.pool.clone())),
published.clone(),
);
while effects.publish_pending().await.unwrap() > 0 {}
diff --git a/crates/whatsapp_linked/AGENTS.md b/crates/whatsapp_linked/AGENTS.md
index d3a9b067ed..d6fb6aa6c6 100644
--- a/crates/whatsapp_linked/AGENTS.md
+++ b/crates/whatsapp_linked/AGENTS.md
@@ -2,42 +2,42 @@
## What it is
-External chats and their one provider, personal WhatsApp accounts linked as
-devices: owner-bound pairing, adding an existing group or direct chat as the
-adder's own, independently shared `ExternalChat` entity (one per owner,
-provider and remote chat, named after WhatsApp), ingesting chat traffic, and
-deliberate sends from each person's own account. The crate holds both the
-provider-neutral domain (timeline, read state, sharing, sends, uploads,
-notification effects, `/external-chats` routes) and the WhatsApp provider
-(pairing, roster, chat list, JIDs, ingest, dispatch, `/whatsapp` routes).
-Internal names such as `Binding` (an added chat) and `GroupFacts` predate the
-split. `Binding.kind` / `external_chat.kind` says group or direct (only the
-owner sends in a direct chat). DSS serves the HTTP API (`/external-chats/*`,
-`/whatsapp/*`); the WhatsApp session service calls
-`domain::session::SessionService` in-process. They share no HTTP: only MacroDB
-rows and `NOTIFY` (the `outbound::pg` notify helper).
+The WhatsApp provider of external chats (`crates/external_chat`): personal
+WhatsApp accounts linked as devices, owner-bound pairing, the account's chat
+list and rosters, adding an existing group or direct chat as the adder's
+own `ExternalChat` entity (one per owner, provider and remote chat, named
+after WhatsApp), ingesting chat traffic, and dispatching deliberate sends
+from each person's own account. `WhatsAppProvider` implements
+`external_chat`'s `ChatProvider`; `external_chat` owns the timeline, read
+state, sharing, send validation, uploads, notification effects and the
+`/external-chats` routes. `ChatRef.kind` / `external_chat.kind` says group or
+direct (only the owner sends in a direct chat). DSS serves `/whatsapp/*`; the
+WhatsApp session service calls `domain::session::SessionService` in-process.
+They share no HTTP: only MacroDB rows and `NOTIFY` (`external_chat`'s
+`outbound::pg` notify helpers).
## Start here
-- `src/domain/service.rs`: DSS use cases and send eligibility.
+- `src/domain/service.rs`: DSS use cases for the account, pairing and chat
+ list. `src/domain/provider.rs`: `ChatProvider` for WhatsApp (adoption
+ validation, who can send, members, names, the send outbox).
- `src/domain/session.rs` + `src/domain/events.rs`: the session-service API and
its event types (no whatsmeow types).
-- `src/domain/effects.rs`: notification outbox drained by DSS.
- `src/outbound/pg/ingest.rs`: canonical identity `(binding, person, source id)`,
quarantine, deferral and the standalone message write. Senders are resolved
first (`resolve_sender_in`): WhatsApp's from-me flag means the account's own
identity; a LID-only sender takes its phone number from the account's roster
pair. Only what is still LID-only is quarantined.
-- `src/domain/rich.rs`: stored structured content (`RichContent`: poll, poll
- vote, location, contact cards, event, system line) in
+- Structured content (`external_chat::domain::rich::RichContent`: poll, poll
+ vote, location, contact cards, event, system line) is stored in
`external_chat_message.rich`; `kind = 'system'` rows never notify or count
- as unread. `src/domain/timeline/view.rs` resolves ids in mentions, system
- lines and votes to names and tallies polls. Poll votes are stored per voter
- in `external_chat_poll_vote` through the reaction path of `reserve_in`. A
- re-ingested message fills in `rich`/`mentions`/`forwarded` an earlier copy
- lacked (re-importing history upgrades old placeholders).
+ as unread. `GroupRepository::people` names the WhatsApp ids in mentions,
+ system lines and votes for `external_chat`'s timeline view. Poll votes are
+ stored per voter in `external_chat_poll_vote` through the reaction path of
+ `reserve_in`. A re-ingested message fills in `rich`/`mentions`/`forwarded`
+ an earlier copy lacked (re-importing history upgrades old placeholders).
- `src/outbound/pg/chats.rs`: the account's chat list (`whatsapp_chat`,
- `GET /whatsapp/chats` via `LinkedService::chats`) and importing earlier messages of
+ `GET /whatsapp/chats` via `WhatsAppService::chats`) and importing earlier messages of
added chats (`backfill_*` columns on `external_chat`; limits
`BACKFILL_MESSAGE_LIMIT` / `BACKFILL_MAX_AGE_DAYS` in `models.rs`). The
anchor is the chat's `whatsapp_chat.anchor`, else the newest stored message
@@ -47,12 +47,11 @@ rows and `NOTIFY` (the `outbound::pg` notify helper).
own groups (`observed_bindings_in`).
- `src/outbound/pg/sends.rs`: enqueue, claim, dispatch fence, outcome. A
repeated intent key returns the stored send before any other check.
-- `src/signals.rs`: `NOTIFY` channels and JSON payloads with `traceparent`
- (`src/signals/trace_context.rs`), public so DSS and the session service
- never import `outbound`; `src/outbound/pg.rs` sends them inside the
- writing transaction. Every write a page shows calls
- `notify_change` (`external_chat_changed`); DSS pushes it to viewers. Roster writes
- signal only real changes (`groups.rs` `same_facts`).
+- `src/signals.rs`: the DSS ↔ session-service `NOTIFY` channels; payloads,
+ trace context and the channels DSS listens on are `external_chat::signals`.
+ Writes send them inside the writing transaction. Every write a page shows
+ calls `notify_change` (`external_chat_changed`); DSS pushes it to viewers.
+ Roster writes signal only real changes (`groups.rs` `same_facts`).
- Migrations:
`crates/macro_db_client/migrations/20261027061310_whatsapp_linked.sql` (the
first schema, with its entity type validation), then
@@ -66,47 +65,38 @@ rows and `NOTIFY` (the `outbound::pg` notify helper).
aliases outlive the account that saw them.
## Boundaries
-- Depends on: `entity_access` (group receipts; owner-only routes take an owner receipt), `entity_access_db_utils`
- (ordinary grants), `entity_registry_db_utils`, and `share_permission_db_utils`.
+- Depends on: `external_chat` (models, ports, generic table helpers),
+ `entity_access_db_utils` (the adopter's owner grant),
+ `entity_registry_db_utils`, and `share_permission_db_utils`.
- Used by: DSS (`src/external_chat.rs` composition), the session service.
## Checks
-- `cargo test -p whatsapp_linked --all-features` (`SQLX_OFFLINE` unset).
+- `cargo test -p whatsapp_linked --all-features` (`SQLX_OFFLINE` unset); its
+ Postgres tests also cover `external_chat`'s reads and writes end to end.
- Check `--no-default-features --features ports` and `inbound` independently; domain dependencies must be enabled by `ports`, not only by the Postgres adapter.
## Gotchas
-- Only the connection owner sends or uploads, using their session or own API credential; bot/harness/internal principals never send. Pairing reads and account/access controls require a human session. Preserve verified Macro API JWT origin even without agent attribution.
-- Deliberate sends consume a shared per-user request budget (one per second across all groups and DSS replicas); DSS supplies the Redis-backed `SendRateLimiter`. Exhaustion returns HTTP 429 with `Retry-After: 1`; counter failure fails closed.
+- Pairing reads and account controls require a human session (the
+ authority rules, send budget and `SendRateLimiter` are `external_chat`'s).
- Lock accounts before groups, then child rows; lock every group in a batch in UUID order. Pairing expiry locks affected accounts before pairing rows. Media completion takes the group lock before attachment/message writes.
- Every session-service write takes the account row lock at the caller's
- `lease_epoch`; `LinkedError::Fenced` means stop the account now.
+ `lease_epoch`; `ExternalChatError::Fenced` means stop the account now.
- `dispatch_started_at` is written before submission. A dispatched send without
an outcome becomes `unknown` on takeover and is never resent; a later echo
with its `allocated_source_id` settles it.
-- Bot view receipts can read a known shared group, but never borrow an acting user's phone, send records, or unread cursor.
-- View access uses ordinary `entity_access` grants (people, teams, and channels
- through mentions) and link/team sharing, independent of WhatsApp membership;
- `external_chat_can_view` and entity_access both call `external_chat_access_level`; change the rule only there. Only the adopter receives an owner grant; use the generic
- direct-access endpoints to share with people.
-- `src/domain/timeline.rs` and `src/domain/service/timeline.rs` own list/detail,
- timeline, unread and sharing use cases. Chat HTTP paths are
- `/external-chats/{chat_id}`; queries look chats up by `(provider,
- remote_id)`. Soup/search are deferred.
+- View access is independent of WhatsApp membership (see
+ `crates/external_chat`); only the adopter receives an owner grant. Queries
+ look chats up by `(provider, remote_id)`.
- Store messages/reactions/attachments/poll votes in `external_chat_message`,
`external_chat_reaction`, `external_chat_attachment` and
`external_chat_poll_vote`; never native comms tables.
- Attachment bytes cross Postgres (`external_chat_media_blob`, ≤ 32 MiB) because the
- session service has no file storage; DSS stores them as static files (like
- Chat's media, readable by id) through the `FileStore` port and deletes the
- blob. A file no attachment references is queued in `external_chat_file_deletion`
- (trigger) and deleted by `MediaService::delete_released`.
- `events::Media::locator` is sealed by the session service; never read it.
-- `src/domain/media.rs` + `src/outbound/pg/media.rs`: download claims, import
- queue. `src/domain/shape.rs` reads a staged upload's pixel size and length
- (stored on `external_chat_media_blob`, sent with the message). Not implemented:
- participant-receipt UI.
+ session service has no file storage; `external_chat`'s `MediaService` in DSS
+ turns them into static files. `events::Media::locator` is sealed by the
+ session service; never read it. `src/outbound/pg/media.rs`: download
+ claims. Not implemented: participant-receipt UI.
## Related
diff --git a/crates/whatsapp_linked/Cargo.toml b/crates/whatsapp_linked/Cargo.toml
index 6507c87d70..e06a796a73 100644
--- a/crates/whatsapp_linked/Cargo.toml
+++ b/crates/whatsapp_linked/Cargo.toml
@@ -9,69 +9,60 @@ default = []
# Port traits and the domain services built on them.
ports = [
"dep:async-trait",
- "dep:entity_access",
- "dep:image",
"dep:macro_user_id",
"dep:rootcause",
- "entity_access/ports",
+ "external_chat/ports",
]
# Postgres adapters, shared by DSS and the session service.
outbound = [
- "dep:entity_access",
"dep:entity_access_db_utils",
"dep:entity_registry_db_utils",
"dep:model_owner",
"dep:share_permission_db_utils",
- "dep:opentelemetry",
"dep:sqlx",
- "dep:tracing-opentelemetry",
- "entity_access/ports",
- "entity_access/outbound",
+ "external_chat/outbound",
"ports",
]
# DSS HTTP routes.
inbound = [
"dep:axum",
- "dep:entity_access",
"dep:macro_authorization",
- "entity_access/inbound",
+ "external_chat/inbound",
"ports",
"schema",
]
-schema = ["dep:utoipa"]
+schema = ["dep:utoipa", "external_chat/schema"]
[dependencies]
-models_permissions = { path = "../models_permissions" }
-share_permission_db_utils = { path = "../share_permission_db_utils", optional = true }
-entity_registry_db_utils = { path = "../entity_registry/entity_registry_db_utils", optional = true }
-entity_access_db_utils = { path = "../entity_access_db_utils", optional = true }
-model_owner = { path = "../model_owner", optional = true }
-model-entity = { path = "../model-entity" }
-opentelemetry = { workspace = true, features = ["trace"], optional = true }
async-trait = { workspace = true, optional = true }
-axum = { workspace = true, features = ["multipart"], optional = true }
+axum = { workspace = true, optional = true }
chrono = { workspace = true }
-entity_access = { path = "../entity_access", default-features = false, optional = true }
-image = { workspace = true, optional = true }
+entity_access_db_utils = { path = "../entity_access_db_utils", optional = true }
+entity_registry_db_utils = { path = "../entity_registry/entity_registry_db_utils", optional = true }
+external_chat = { path = "../external_chat" }
macro_authorization = { path = "../macro_authorization", default-features = false, features = ["axum"], optional = true }
macro_user_id = { path = "../macro_user_id", optional = true }
+model-entity = { path = "../model-entity" }
+model_owner = { path = "../model_owner", optional = true }
nom = { workspace = true }
rootcause = { workspace = true, optional = true }
serde = { workspace = true }
serde_json = { workspace = true }
sha2 = { workspace = true }
+share_permission_db_utils = { path = "../share_permission_db_utils", optional = true }
sqlx = { workspace = true, optional = true }
strum = { workspace = true }
-thiserror = { workspace = true }
tracing = { workspace = true }
-tracing-opentelemetry = { workspace = true, optional = true }
utoipa = { workspace = true, optional = true }
uuid = { workspace = true }
workspace-hack = { version = "0.1", path = "../workspace-hack" }
[dev-dependencies]
+entity_access = { path = "../entity_access", default-features = false, features = ["inbound", "outbound", "ports"] }
harness_id = { path = "../harness_id", features = ["test-utils"] }
+image = { workspace = true }
macro_db_migrator = { path = "../macro_db_migrator" }
model_user = { path = "../model_user" }
+models_permissions = { path = "../models_permissions" }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tower = { workspace = true, features = ["util"] }
diff --git a/crates/whatsapp_linked/src/domain.rs b/crates/whatsapp_linked/src/domain.rs
index c084108294..bd0f643d94 100644
--- a/crates/whatsapp_linked/src/domain.rs
+++ b/crates/whatsapp_linked/src/domain.rs
@@ -2,29 +2,18 @@
pub mod events;
/// WhatsApp JIDs.
pub mod jid;
-/// Account, binding and send models shared with the web app.
+/// Account, pairing, roster and chat list models shared with the web app.
pub mod models;
-/// Polls, locations, contact cards, events and system lines.
-pub mod rich;
-/// Native notification outbox.
-#[cfg(feature = "ports")]
-pub mod effects;
-/// Attachment import into static files.
-#[cfg(feature = "ports")]
-pub mod media;
/// Port traits.
#[cfg(feature = "ports")]
pub mod ports;
-/// DSS use cases.
+/// WhatsApp as an external chat provider.
+#[cfg(feature = "ports")]
+pub mod provider;
+/// DSS use cases: the account, pairing and chat list.
#[cfg(feature = "ports")]
pub mod service;
/// In-process API for the session service.
#[cfg(feature = "ports")]
pub mod session;
-/// Pixel size and length of files sent from Macro.
-#[cfg(feature = "ports")]
-pub mod shape;
-
-#[cfg(feature = "ports")]
-pub mod timeline;
diff --git a/crates/whatsapp_linked/src/domain/events.rs b/crates/whatsapp_linked/src/domain/events.rs
index 1428122d7a..7d5f985878 100644
--- a/crates/whatsapp_linked/src/domain/events.rs
+++ b/crates/whatsapp_linked/src/domain/events.rs
@@ -1,9 +1,14 @@
//! Types exchanged in-process with the WhatsApp session service. They carry no
//! whatsmeow types: the session service maps protocol events onto them.
-use super::models::{BackfillState, ExternalChatKind, Identity, MediaPresentation, PairingState};
-pub use super::rich::RichContent;
+use super::models::{Identity, PairingState};
use chrono::{DateTime, Utc};
+use external_chat::domain::{
+ models::{
+ AttachmentRef, BackfillState, ExternalChatKind, MediaKind, MediaPresentation, MediaShape,
+ },
+ rich::RichContent,
+};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@@ -303,7 +308,7 @@ pub enum OutgoingContent {
/// One attachment; fetch its bytes with `outbound_media`.
Media {
/// Category.
- kind: super::models::MediaKind,
+ kind: MediaKind,
/// MIME type.
mime: String,
/// Display filename.
@@ -317,30 +322,6 @@ pub enum OutgoingContent {
},
}
-/// Pixel size and length of an attachment, where known.
-#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
-pub struct MediaShape {
- /// Width in pixels (images and videos).
- pub width: Option<u32>,
- /// Height in pixels.
- pub height: Option<u32>,
- /// Length in whole seconds (audio and video).
- pub duration_seconds: Option<u32>,
-}
-
-/// One attachment of one source message.
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
-pub struct AttachmentRef {
- /// Binding the message belongs to.
- pub chat_id: Uuid,
- /// Verified author.
- pub person_id: Uuid,
- /// WhatsApp message id.
- pub source_id: String,
- /// Position within the message.
- pub ordinal: i16,
-}
-
/// An attachment this replica should download.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MediaImport {
@@ -371,7 +352,7 @@ pub enum MediaFailure {
#[derive(Clone)]
pub struct OutboundMedia {
/// Category.
- pub kind: super::models::MediaKind,
+ pub kind: MediaKind,
/// MIME type.
pub mime: String,
/// Display filename.
diff --git a/crates/whatsapp_linked/src/domain/models.rs b/crates/whatsapp_linked/src/domain/models.rs
index 4dcfd0083f..a9ef701f2d 100644
--- a/crates/whatsapp_linked/src/domain/models.rs
+++ b/crates/whatsapp_linked/src/domain/models.rs
@@ -1,8 +1,9 @@
-//! Personal accounts, added external chats and deliberate sends, as DSS and
-//! the web app see them. Session-service events live in [`super::events`].
+//! Personal accounts, pairing, group rosters and the WhatsApp chat list, as
+//! DSS and the web app see them. Session-service events live in
+//! [`super::events`]; added chats and sends are `external_chat` models.
use chrono::{DateTime, Utc};
-pub use model_entity::{ExternalChatKind, ExternalChatProvider};
+use external_chat::domain::models::{CannotAdd, CommunityReference, ExternalChatKind};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@@ -148,54 +149,6 @@ pub struct PairState {
pub expires_at: DateTime<Utc>,
}
-/// Explicit request to add a chat; the provider account is selected
-/// server-side. The chat's name always follows the provider.
-#[derive(Debug, Clone, Deserialize, Serialize)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatAdd))]
-pub struct AddChat {
- /// The service the chat lives in.
- pub provider: ExternalChatProvider,
- /// The chat's id at the provider, as its chat list names it. WhatsApp: a
- /// group the requester's account has joined, or a direct chat's other
- /// person's JID.
- pub remote_id: String,
- /// Group or direct chat; must match what `remote_id` is.
- pub kind: ExternalChatKind,
-}
-
-/// Binding lifecycle.
-#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "snake_case")]
-pub enum BindingState {
- /// Observed and sendable.
- Active,
- /// Locally detached; history stays readable.
- Detached,
-}
-
-/// One remote chat bound to a person's external chat entity.
-#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
-pub struct Binding {
- /// Stable binding identity, independent of any observing account; the
- /// external chat entity id.
- pub id: Uuid,
- /// The service the chat lives in.
- pub provider: ExternalChatProvider,
- /// Remote chat identity, never its display name. WhatsApp: the group's
- /// JID, or a direct chat's other person.
- pub remote_id: String,
- /// Group or direct chat.
- pub kind: ExternalChatKind,
- /// The person who added the group, its owner.
- pub user_id: String,
- /// Lifecycle.
- pub state: BindingState,
- /// History boundary for the binding.
- pub adopted_at: DateTime<Utc>,
-}
-
/// Verified sender aliases, supplied only by protocol metadata. Stored in
/// roster metadata and retained events as camelCase JSON.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
@@ -239,7 +192,7 @@ pub struct Participant {
/// One full metadata read of a group, never an incomplete membership delta.
/// The session service reports it and it is stored as roster metadata
-/// (camelCase JSON); the API shows it as [`crate::domain::timeline::ExternalChatMembers`].
+/// (camelCase JSON); the API shows it as [`external_chat::domain::timeline::ExternalChatMembers`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct GroupInfo {
@@ -280,39 +233,6 @@ pub struct ChatReference {
pub name: String,
}
-/// The WhatsApp community a group belongs to.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatCommunity))]
-pub struct CommunityReference {
- /// The community's WhatsApp JID.
- pub id: String,
- /// The community's name.
- pub name: String,
- /// This group is the community's announcement group.
- pub announcements: bool,
-}
-
-/// Shown when the community itself is not in the account's group list.
-const UNKNOWN_COMMUNITY_NAME: &str = "WhatsApp community";
-
-impl CommunityReference {
- /// The community `id`, named after its own metadata's `name` when known.
- pub fn named(id: String, name: Option<&str>, announcements: bool) -> Self {
- let name = name
- .map(str::trim)
- .filter(|name| !name.is_empty())
- .unwrap_or(UNKNOWN_COMMUNITY_NAME)
- .to_owned();
- Self {
- id,
- name,
- announcements,
- }
- }
-}
-
/// Another person's external chat entity the caller can view.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
@@ -383,26 +303,6 @@ impl GroupInfo {
}
}
-/// Why a chat can't be added to Macro; the app words it from the code.
-#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq, thiserror::Error)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = WhatsAppCannotAdd))]
-pub enum CannotAdd {
- /// The account's WhatsApp left the group or was removed.
- #[error("your WhatsApp is not in this group any more")]
- NotInGroup,
- /// A community itself has no messages; its groups can be added.
- #[error("a community has no messages of its own; add one of its groups")]
- Community,
- /// The chat with yourself.
- #[error("your chat with yourself can't be added to Macro")]
- OwnChat,
- /// A direct chat that is not in the account's chat list.
- #[error("this chat is not in your WhatsApp chat list")]
- NotInChatList,
-}
-
/// One chat of the caller's WhatsApp chat list, as WhatsApp Web lists it.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
@@ -448,427 +348,17 @@ pub struct ChatList {
pub chats: Vec<Chat>,
}
-/// Where importing a group's earlier messages stands.
-#[derive(
- Debug,
- Clone,
- Copy,
- Serialize,
- Deserialize,
- PartialEq,
- Eq,
- strum::IntoStaticStr,
- strum::EnumString,
-)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatBackfillState))]
-#[strum(serialize_all = "snake_case")]
-pub enum BackfillState {
- /// Nothing to start from: the owner's account knows no message in this
- /// chat yet (paired before WhatsApp sent its chat list, and quiet since).
- /// Becomes `pending` with the chat's first message.
- WaitingForMessage,
- /// About to start: the owner's account has not picked it up yet.
- Pending,
- /// Asking the owner's phone for earlier messages.
- Running,
- /// The owner's phone did not answer; retried when it is back.
- WaitingForPhone,
- /// Everything the phone had (within the limits) was imported.
- Done,
- /// Stopped at the limits ([`BACKFILL_MESSAGE_LIMIT`] messages or
- /// [`BACKFILL_MAX_AGE_DAYS`] days); older messages stay in WhatsApp.
- Partial,
-}
-
-impl BackfillState {
- /// Stable storage spelling.
- pub fn as_str(self) -> &'static str {
- self.into()
- }
-
- /// Parse the storage spelling.
- pub fn parse(value: &str) -> Option<Self> {
- value.parse().ok()
- }
-}
-
-/// Earlier messages imported after adding a chat.
-#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatBackfill))]
-pub struct Backfill {
- /// Progress.
- pub state: BackfillState,
- /// Messages received from the phone so far (including ones already shown).
- pub imported: u32,
-}
-
-/// Most earlier messages imported when a group is added.
-pub const BACKFILL_MESSAGE_LIMIT: u32 = 500;
-/// Earlier messages older than this many days before adding are not imported.
-pub const BACKFILL_MAX_AGE_DAYS: i64 = 90;
-
-/// Deliberate operations on an existing message.
-#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatMutation))]
-pub enum Mutation {
- /// Add, replace or remove the caller's reaction.
- Reaction,
- /// Edit the caller's own recent message.
- Edit,
- /// Delete the caller's own recent message for everyone.
- Delete,
-}
-
-/// Content of a deliberate send.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(tag = "type", rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatReplyContent))]
-pub enum ReplyContent {
- /// Plain text.
- Text {
- /// UTF-8 text, at most [`TEXT_LIMIT_BYTES`] bytes.
- text: String,
- /// Group participants the text @-mentions, by their WhatsApp id
- /// (`jid` of a roster participant); the text must contain
- /// `@<digits before the @ of the id>` for each.
- #[serde(default)]
- mentions: Vec<String>,
- },
- /// One attachment staged through the attachments route.
- Media {
- /// The staged upload, imported as the message's static file.
- #[serde(rename = "uploadId")]
- upload_id: Uuid,
- /// Optional caption; audio has none.
- caption: Option<String>,
- },
- /// An operation on an existing message in this chat.
- Mutation {
- /// Existing message, resolved to its provider source by the server.
- #[serde(rename = "messageId")]
- message_id: Uuid,
- /// Explicit operation.
- operation: Mutation,
- /// Replacement text or emoji; absent for delete, empty to remove a reaction.
- text: Option<String>,
- },
-}
-
-/// Explicit send action from a person. Only deliberate sends reach WhatsApp.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatSendReply))]
-pub struct SendReply {
- /// Stable client intent key. A changed payload with this key is a conflict.
- pub idempotency_key: Uuid,
- /// Content.
- pub message: ReplyContent,
- /// Message being quoted, within this chat.
- #[serde(default)]
- pub reply_to: Option<Uuid>,
-}
-
-/// Observed send state. `unknown` means the request may have reached WhatsApp;
-/// it is never sent again automatically.
-#[derive(
- Debug,
- Clone,
- Copy,
- Serialize,
- Deserialize,
- PartialEq,
- Eq,
- strum::IntoStaticStr,
- strum::EnumString,
-)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatDeliveryState))]
-#[strum(serialize_all = "snake_case")]
-pub enum DeliveryState {
- /// Durable intent, not yet claimed.
- Pending,
- /// Claimed by the account's session; may already be dispatching.
- Sending,
- /// The outcome of a dispatched request is not known.
- Unknown,
- /// WhatsApp accepted the message; this does not establish delivery.
- Accepted,
- /// Rejected, expired or no longer authorized.
- Failed,
-}
-
-impl DeliveryState {
- /// Stable storage spelling.
- pub fn as_str(self) -> &'static str {
- self.into()
- }
-
- /// Parse the storage spelling.
- pub fn parse(value: &str) -> Option<Self> {
- value.parse().ok()
- }
-}
-
-/// Durable result of a deliberate send.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatSendProgress))]
-pub struct SendProgress {
- /// Stable intent id.
- pub id: Uuid,
- /// Person who submitted it.
- pub user_id: String,
- /// External chat.
- pub chat_id: Uuid,
- /// The original request, so a failed draft can be recovered.
- pub payload: SendReply,
- /// Current state.
- pub state: DeliveryState,
- /// The chat's message, once the provider accepted it.
- pub message_id: Option<Uuid>,
- /// Sanitized failure code.
- pub error_code: Option<String>,
- /// Intent creation time.
- pub created_at: DateTime<Utc>,
-}
-
-/// Attachment categories WhatsApp distinguishes.
-#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatMediaKind))]
-pub enum MediaKind {
- /// JPEG, PNG or WebP.
- Image,
- /// MP3, Ogg, AAC or MP4 audio. Ogg/Opus is sent as a voice note.
- Audio,
- /// MP4 video.
- Video,
- /// PDF or plain text.
- Document,
-}
-
-/// Every MIME type Macro sends to WhatsApp, with its category.
-pub const SENDABLE_MEDIA: [(&str, MediaKind); 10] = [
- ("image/jpeg", MediaKind::Image),
- ("image/png", MediaKind::Image),
- ("image/webp", MediaKind::Image),
- ("audio/mpeg", MediaKind::Audio),
- ("audio/ogg", MediaKind::Audio),
- ("audio/aac", MediaKind::Audio),
- ("audio/mp4", MediaKind::Audio),
- ("video/mp4", MediaKind::Video),
- ("application/pdf", MediaKind::Document),
- ("text/plain", MediaKind::Document),
-];
-
-impl MediaKind {
- /// Category of a supported MIME type; anything else is refused.
- pub fn of(mime: &str) -> Option<Self> {
- SENDABLE_MEDIA
- .iter()
- .find(|(sendable, _)| *sendable == mime)
- .map(|(_, kind)| *kind)
- }
-}
-
-/// How WhatsApp shows an attachment, when not as a plain file of its kind.
-#[derive(
- Debug,
- Clone,
- Copy,
- Serialize,
- Deserialize,
- PartialEq,
- Eq,
- strum::IntoStaticStr,
- strum::EnumString,
-)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatMediaPresentation))]
-#[strum(serialize_all = "snake_case")]
-pub enum MediaPresentation {
- /// A sticker: a small image (WebP, possibly animated), no caption.
- Sticker,
- /// A short video WhatsApp plays as a looping, muted GIF.
- Gif,
- /// A voice note recorded in WhatsApp (push to talk).
- VoiceNote,
-}
-
-impl MediaPresentation {
- /// Stable storage spelling.
- pub fn as_str(self) -> &'static str {
- self.into()
- }
-
- /// Parse the storage spelling.
- pub fn parse(value: &str) -> Option<Self> {
- value.parse().ok()
- }
-}
-
-/// Largest attachment imported or sent, in bytes.
-pub const MEDIA_LIMIT_BYTES: usize = 32 * 1024 * 1024;
-
-/// A staged attachment, sent only by a later deliberate send.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatUploadedAttachment))]
-pub struct UploadedAttachment {
- /// Use as `upload_id` in a media send.
- pub upload_id: Uuid,
- /// Display filename.
- pub filename: String,
- /// Category.
- pub kind: MediaKind,
-}
-
-/// Where an attachment of a message stands.
-#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatAttachmentState))]
-pub enum AttachmentState {
- /// Downloading or importing.
- Loading,
- /// Imported; `file_id` is set.
- Ready,
- /// Download or import failed for good.
- Unavailable,
- /// A file type Macro does not import.
- Unsupported,
-}
-
-/// One attachment of a message.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatAttachment))]
-pub struct Attachment {
- /// Position within the message.
- pub ordinal: i16,
- /// Current state.
- pub state: AttachmentState,
- /// Display filename.
- pub file_name: String,
- /// Declared MIME type.
- pub mime: String,
- /// Declared size in bytes.
- pub size: u64,
- /// The static file holding it, once ready.
- pub file_id: Option<Uuid>,
- /// Pixel width of an image, sticker or video, when WhatsApp declared it.
- pub width: Option<u32>,
- /// Pixel height, when declared.
- pub height: Option<u32>,
- /// Length of audio or video in whole seconds, when declared.
- pub duration_seconds: Option<u32>,
- /// Sticker, GIF or voice note; `null` for an ordinary file of its kind.
- pub presentation: Option<MediaPresentation>,
-}
-
-/// The attachments of one message.
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatMessageAttachments))]
-pub struct MessageAttachments {
- /// The message.
- pub message_id: Uuid,
- /// Its attachments in order; deleted ones are omitted.
- pub attachments: Vec<Attachment>,
-}
-
/// Most people one text send may @-mention.
pub const MENTION_LIMIT: usize = 64;
/// Largest WebAuthn challenge or assertion passed through a pairing.
pub const PASSKEY_LIMIT_BYTES: usize = 32 * 1024;
-/// Most messages or send ids one attachments or delivery request names.
-pub const BATCH_LIMIT: usize = 100;
-
-/// Most messages in one timeline page, and the page size when none is asked.
-pub const PAGE_LIMIT: i64 = 100;
-/// Timeline page size when the caller names none.
-pub const DEFAULT_PAGE: i64 = 50;
-
/// Days a participant's receipt is kept.
pub const RECEIPT_RETENTION_DAYS: i32 = 30;
-/// Maximum UTF-8 bytes in one text send.
-pub const TEXT_LIMIT_BYTES: usize = 16 * 1024;
-/// Edits and deletes are offered for this long after the original send.
-pub const MUTATION_WINDOW_SECONDS: i64 = 10 * 60;
/// Unattempted intents expire this long after they were created.
pub const SEND_EXPIRY_SECONDS: i64 = 10 * 60;
-/// Operations implemented for a chat, independent of current permission.
-#[derive(Debug, Clone, Serialize)]
-#[serde(rename_all = "camelCase")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatCapabilities))]
-pub struct Capabilities {
- /// Maximum UTF-8 bytes per text send.
- pub text_limit_bytes: u32,
- /// Supported message operations.
- pub mutations: Vec<Mutation>,
- /// Edits and deletes are offered this long after sending.
- pub mutation_window_seconds: u32,
- /// Attachments per send.
- pub max_attachments: u8,
- /// Largest attachment in bytes.
- pub attachment_limit_bytes: u32,
- /// Supported attachment MIME types.
- pub attachment_types: Vec<String>,
-}
-
-impl Default for Capabilities {
- fn default() -> Self {
- Self {
- text_limit_bytes: TEXT_LIMIT_BYTES as u32,
- mutations: vec![Mutation::Reaction, Mutation::Edit, Mutation::Delete],
- mutation_window_seconds: MUTATION_WINDOW_SECONDS as u32,
- max_attachments: 1,
- attachment_limit_bytes: MEDIA_LIMIT_BYTES as u32,
- attachment_types: SENDABLE_MEDIA
- .iter()
- .map(|(mime, _)| (*mime).to_owned())
- .collect(),
- }
- }
-}
-
-/// Why the viewer cannot send now.
-#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
-#[serde(rename_all = "snake_case")]
-#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
-#[cfg_attr(feature = "schema", schema(as = ExternalChatReplyBlock))]
-pub enum ReplyBlock {
- /// The viewer has not connected their own account at the provider.
- NoAccount,
- /// The viewer's provider account is not connected.
- Disconnected,
- /// The viewer has not added this chat from their own account.
- NotAdded,
- /// The viewer is not in the chat, or may not post in it now.
- NotAllowed,
- /// The chat is detached.
- Unavailable,
-}
-
/// Owner-scoped account state, including not connected.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
@@ -889,82 +379,3 @@ pub struct Disconnect {
/// unlinks asynchronously; WhatsApp's Linked devices list is authoritative.
pub remote_unlink_requested: bool,
}
-
-/// Who is sending, as authenticated by the invoking path.
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum SendAuthority {
- /// A person acting in the app.
- Human,
- /// The person's verified API credential; sends only from their own account.
- UserApiToken,
- /// Acting for the person without being them: a session carrying an agent
- /// attribution. Bots, harnesses and internal callers never reach these use
- /// cases (`UserOnly` rejects them). Never sends to WhatsApp.
- Automation,
-}
-
-/// Sanitized failures safe to show in the native UI.
-#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
-pub enum LinkedError {
- /// Invalid input with actionable detail.
- #[error("{0}")]
- Invalid(&'static str),
- /// The chat can't be added to Macro.
- #[error("{0}")]
- CannotAdd(CannotAdd),
- /// Absent or outside the caller's authorized scope.
- #[error("not found")]
- NotFound,
- /// The caller lacks authority for this operation.
- #[error("you do not have permission for this WhatsApp operation")]
- Forbidden,
- /// Conflicts with an existing identity, transaction or idempotency key.
- #[error("this conflicts with an existing WhatsApp connection or request")]
- Conflict,
- /// The account is not connected.
- #[error("connect your WhatsApp account first")]
- Disconnected,
- /// The intent expired before it was attempted.
- #[error("send_expired")]
- SendExpired,
- /// The session-service lease epoch is stale; the replica must stop the account.
- #[error("lease_fenced")]
- Fenced,
- /// The person's deliberate send request budget is exhausted.
- #[error("wait one second before sending another WhatsApp request")]
- RateLimited,
- /// Retryable infrastructure failure.
- #[error("WhatsApp is temporarily unavailable; retry shortly")]
- Unavailable,
- /// The account has too many events waiting to be stored; the session
- /// service leaves the event unacknowledged and WhatsApp redelivers it.
- #[error("too many WhatsApp events are waiting for this account")]
- Backlogged,
- /// Stored data broke an invariant; retrying does not help. The report
- /// carries the cause.
- #[error("something went wrong with this WhatsApp chat")]
- Internal,
-}
-
-impl LinkedError {
- /// Stable code for logs, spans and clients.
- pub const fn code(&self) -> &'static str {
- match self {
- Self::Invalid(_) => "invalid_input",
- Self::CannotAdd(CannotAdd::NotInGroup) => "cannot_add_not_in_group",
- Self::CannotAdd(CannotAdd::Community) => "cannot_add_community",
- Self::CannotAdd(CannotAdd::OwnChat) => "cannot_add_own_chat",
- Self::CannotAdd(CannotAdd::NotInChatList) => "cannot_add_not_in_chat_list",
- Self::NotFound => "not_found",
- Self::Forbidden => "forbidden",
- Self::Conflict => "conflict",
- Self::Disconnected => "disconnected",
- Self::SendExpired => "send_expired",
- Self::Fenced => "lease_fenced",
- Self::RateLimited => "rate_limited",
- Self::Unavailable => "temporarily_unavailable",
- Self::Backlogged => "backlogged",
- Self::Internal => "internal_error",
- }
- }
-}
diff --git a/crates/whatsapp_linked/src/domain/ports.rs b/crates/whatsapp_linked/src/domain/ports.rs
index d9b9bd742d..cafc5cbc61 100644
--- a/crates/whatsapp_linked/src/domain/ports.rs
+++ b/crates/whatsapp_linked/src/domain/ports.rs
@@ -1,5 +1,6 @@
-//! Capabilities the linked-device use cases need. Postgres adapters implement
-//! the repositories; DSS supplies the group notification publisher.
+//! Capabilities the WhatsApp use cases need; the Postgres adapter implements
+//! them. The provider-neutral ports (timeline, sends' generic side,
+//! notifications) are `external_chat`'s.
//!
//! Repository methods that change state are atomic and re-check, under lock, the
//! facts their contract names. Methods that signal another process do it with
@@ -9,25 +10,20 @@
use super::{events::*, models::*};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
+use external_chat::domain::{
+ models::*,
+ ports::{ExternalChatResult, MutationTarget, SendIntent, StoredSend},
+ timeline::view::People,
+};
use macro_user_id::user_id::MacroUserIdStr;
use uuid::Uuid;
-/// Result of every port and service call.
-pub type LinkedResult<T> = Result<T, rootcause::Report<LinkedError>>;
-
-/// Shared request budget for deliberate sends, keyed by authenticated person.
-#[async_trait]
-pub trait SendRateLimiter: Send + Sync {
- /// Consume one request across all groups and service replicas. Requests
- /// rejected later still count; infrastructure failure must fail closed.
- async fn check(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<()>;
-}
-
/// Owner-scoped account and pairing transitions.
#[async_trait]
pub trait AccountRepository: Send + Sync {
/// The person's account, if they ever started pairing.
- async fn owned_account(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<Option<Account>>;
+ async fn owned_account(&self, user: &MacroUserIdStr<'_>)
+ -> ExternalChatResult<Option<Account>>;
/// Return the live transaction, or start one: create the account if absent,
/// otherwise move it to `pairing` and bump its generation. Fails with
/// `Conflict` when the account is connected and `replace` is false. With
@@ -38,12 +34,15 @@ pub trait AccountRepository: Send + Sync {
&self,
user: &MacroUserIdStr<'_>,
replace: bool,
- ) -> LinkedResult<Pairing>;
+ ) -> ExternalChatResult<Pairing>;
/// The owner's transaction with its current codes; an unconsumed transaction
/// past its deadline is expired first (and its pairing account disconnected).
/// `NotFound` for another user's transaction.
- async fn pair_state(&self, user: &MacroUserIdStr<'_>, pairing: Uuid)
- -> LinkedResult<PairState>;
+ async fn pair_state(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ pairing: Uuid,
+ ) -> ExternalChatResult<PairState>;
/// Store the owner's WebAuthn assertion for a live transaction waiting on a
/// passkey; `NotFound` otherwise. Signals `whatsapp_pairing_requested`.
async fn submit_passkey(
@@ -51,15 +50,28 @@ pub trait AccountRepository: Send + Sync {
user: &MacroUserIdStr<'_>,
pairing: Uuid,
response: &serde_json::Value,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// Cancel the owner's live transaction and end the pairing consent.
/// Signals `whatsapp_pairing_requested`. Idempotent.
- async fn cancel_pairing(&self, user: &MacroUserIdStr<'_>, pairing: Uuid) -> LinkedResult<()>;
+ async fn cancel_pairing(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ pairing: Uuid,
+ ) -> ExternalChatResult<()>;
/// End consent at exactly this generation: state `disconnected`, generation
/// + 1, memberships end, live pairings cancel, undispatched sends fail.
/// Returns false when that generation was already disconnected.
/// Signals `whatsapp_account_changed`.
- async fn disconnect(&self, user: &MacroUserIdStr<'_>, generation: i64) -> LinkedResult<bool>;
+ async fn disconnect(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ generation: i64,
+ ) -> ExternalChatResult<bool>;
+ /// The Macro user who linked this account.
+ async fn account_owner(&self, account: Uuid) -> ExternalChatResult<Option<String>>;
+ /// Delete participant receipts older than [`RECEIPT_RETENTION_DAYS`] and
+ /// pairings consumed over a day ago (they keep the expected identity).
+ async fn expire_retained(&self) -> ExternalChatResult<u64>;
}
/// An external chat bound to a remote chat, as one viewer can see it.
@@ -79,27 +91,47 @@ pub struct VisibleAdoption {
#[async_trait]
pub trait GroupRepository: Send + Sync {
/// Groups the account has joined, as last observed.
- async fn account_groups(&self, account: Uuid) -> LinkedResult<Vec<GroupInfo>>;
+ async fn account_groups(&self, account: Uuid) -> ExternalChatResult<Vec<GroupInfo>>;
/// One joined group, as last observed.
- async fn account_group(&self, account: Uuid, group: &str) -> LinkedResult<Option<GroupInfo>>;
+ async fn account_group(
+ &self,
+ account: Uuid,
+ group: &str,
+ ) -> ExternalChatResult<Option<GroupInfo>>;
/// Ask each account's replica to re-read its groups
/// (`whatsapp_roster_requested`); the replica rate limits refreshes.
- async fn request_groups_refresh(&self, accounts: &[Uuid]) -> LinkedResult<()>;
+ async fn request_groups_refresh(&self, accounts: &[Uuid]) -> ExternalChatResult<()>;
/// Accounts whose rosters describe a group entity: its observers, and the
/// viewer's own connected account (a bot viewer has none).
- async fn group_accounts(&self, chat_id: Uuid, viewer: &str) -> LinkedResult<Vec<Uuid>>;
+ async fn group_accounts(&self, chat_id: Uuid, viewer: &str) -> ExternalChatResult<Vec<Uuid>>;
/// Group entities bound to these remote groups that the person can see,
/// newest first.
async fn visible_adoptions(
&self,
user: &MacroUserIdStr<'_>,
groups: &[String],
- ) -> LinkedResult<Vec<VisibleAdoption>>;
- /// Latest metadata for a binding's group from any account consenting to it.
- async fn binding_roster(&self, binding: &Binding) -> LinkedResult<Option<GroupInfo>>;
+ ) -> ExternalChatResult<Vec<VisibleAdoption>>;
+ /// Latest metadata for a chat's group from any account consenting to it,
+ /// with names of people who wrote in it.
+ async fn chat_roster(&self, chat: &ChatRef) -> ExternalChatResult<Option<GroupInfo>>;
+ /// The account's own rosters of these groups, by group JID.
+ async fn account_rosters(
+ &self,
+ account: Uuid,
+ groups: &[String],
+ ) -> ExternalChatResult<Vec<GroupInfo>>;
+ /// The community each chat belongs to, from the newest roster any
+ /// consenting observer read, in order.
+ async fn communities(
+ &self,
+ chats: &[ChatRef],
+ ) -> ExternalChatResult<Vec<Option<CommunityReference>>>;
+ /// Names for WhatsApp ids in a chat: people seen in it first (their
+ /// WhatsApp name and verified Macro user), then the owner's roster.
+ async fn people(&self, chat: &ChatRef, jids: &[String]) -> ExternalChatResult<People>;
/// The account's stored chat list (without the roster), newest first.
- async fn account_chats(&self, account: Uuid) -> LinkedResult<Vec<StoredChat>>;
+ async fn account_chats(&self, account: Uuid) -> ExternalChatResult<Vec<StoredChat>>;
}
/// One stored row of an account's chat list.
@@ -134,7 +166,7 @@ pub struct Adoption {
pub observed_at: DateTime<Utc>,
}
-/// A new binding, authorized by the domain.
+/// A new external chat, authorized by the domain.
pub struct AdoptionRecord<'a> {
/// Adopting person.
pub user: &'a MacroUserIdStr<'a>,
@@ -146,178 +178,103 @@ pub struct AdoptionRecord<'a> {
/// Result of adoption.
pub struct Adopted {
- /// The binding.
- pub binding: Binding,
+ /// The external chat.
+ pub chat: ChatRef,
/// Whether this call created the standalone entity.
pub created: bool,
}
-/// Standalone group entities and their lifecycle.
+/// Adding WhatsApp chats as external chats, and detaching them.
#[async_trait]
-pub trait BindingRepository: Send + Sync {
- /// The binding of a group entity.
- async fn binding(&self, chat_id: Uuid) -> LinkedResult<Option<Binding>>;
+pub trait AdoptionRepository: Send + Sync {
/// Adopt atomically, serialized per person and remote group: one live group
/// entity per (owner, remote group), owned by the caller. Records the
/// caller-account's membership and signals its session service.
- async fn adopt(&self, record: AdoptionRecord<'_>) -> LinkedResult<Adopted>;
- /// Attachments of these group messages of the binding.
- async fn message_attachments(
- &self,
- binding: &Binding,
- messages: &[Uuid],
- ) -> LinkedResult<Vec<MessageAttachments>>;
+ async fn adopt(&self, record: AdoptionRecord<'_>) -> ExternalChatResult<Adopted>;
/// Detach locally: end observing consents and fail undispatched sends.
/// History stays. Signals `whatsapp_account_changed` for every observer.
- async fn detach(&self, binding: &Binding) -> LinkedResult<()>;
-}
-
-/// The facts that decide whether a person can send in a binding now.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct SendingFacts {
- /// The account's membership has consent at its current generation.
- pub observing: bool,
- /// The account is in the group.
- pub remote_member: bool,
- /// The account may post under current group settings.
- pub can_post: bool,
-}
-
-/// What a mutation request points at.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct MutationTarget {
- /// Source time of the original message.
- pub source_at: DateTime<Utc>,
- /// The original was sent from this verified identity.
- pub sent_by: Vec<String>,
- /// Already deleted.
- pub deleted: bool,
-}
-
-/// An earlier send found by its intent key.
-#[derive(Debug, Clone)]
-pub struct StoredSend {
- /// SHA-256 of its canonical request; a retry must match it.
- pub request_sha256: [u8; 32],
- /// Where it got to.
- pub progress: SendProgress,
-}
-
-/// A validated intent, authorized by the domain.
-pub struct SendIntent<'a> {
- /// Sending person.
- pub user: &'a MacroUserIdStr<'a>,
- /// Their account at its current generation.
- pub account: &'a Account,
- /// Binding.
- pub binding: &'a Binding,
- /// Request.
- pub request: &'a SendReply,
- /// SHA-256 of the canonical request.
- pub digest: [u8; 32],
+ async fn detach(&self, chat: &ChatRef) -> ExternalChatResult<()>;
}
-/// A validated attachment upload.
-pub struct StagedUpload<'a> {
- /// Uploader, who will own the static file.
- pub user: &'a MacroUserIdStr<'a>,
- /// Group it may be sent to.
- pub chat_id: Uuid,
- /// Display filename.
- pub filename: &'a str,
- /// Supported MIME type.
- pub mime: &'a str,
- /// Bytes, at most [`MEDIA_LIMIT_BYTES`].
- pub bytes: &'a [u8],
- /// SHA-256 of `bytes`.
- pub sha256: [u8; 32],
- /// Pixel size and length read from the file.
- pub shape: crate::domain::events::MediaShape,
-}
-
-/// Durable send intents.
+/// Durable send intents in the `whatsapp_send` outbox.
#[async_trait]
pub trait SendRepository: Send + Sync {
- /// Stage an attachment for a later send by the same person in the same group.
- async fn stage_upload(&self, upload: StagedUpload<'_>) -> LinkedResult<Uuid>;
- /// MIME type of the person's unsent staged upload in this group.
- async fn staged_upload_mime(
- &self,
- user: &MacroUserIdStr<'_>,
- chat_id: Uuid,
- upload: Uuid,
- ) -> LinkedResult<Option<String>>;
- /// The person's own account's roster of the binding's group, if any.
- async fn account_roster(
- &self,
- account: &Account,
- binding: &Binding,
- ) -> LinkedResult<Option<GroupInfo>>;
- /// The source of a group message in this binding, for quotes and mutations.
+ /// The source of a message in this chat, for quotes and mutations.
async fn mutation_target(
&self,
- binding: &Binding,
+ chat: &ChatRef,
message: Uuid,
- ) -> LinkedResult<Option<MutationTarget>>;
- /// The person's earlier send with this intent key in the group.
+ ) -> ExternalChatResult<Option<MutationTarget>>;
+ /// The person's earlier send with this intent key in the chat.
async fn stored_send(
&self,
chat_id: Uuid,
user: &MacroUserIdStr<'_>,
idempotency_key: Uuid,
- ) -> LinkedResult<Option<StoredSend>>;
- /// Record the intent and assign the group sequence. Replays with the same key
+ ) -> ExternalChatResult<Option<StoredSend>>;
+ /// Record the intent and assign the chat sequence. Replays with the same key
/// return the stored progress; a different payload is a `Conflict`. Inserts
- /// only while the account is connected at its generation, the binding is
+ /// only while the account is connected at its generation, the chat is
/// active and the membership still observes. Signals `whatsapp_send_requested`.
- async fn enqueue(&self, intent: SendIntent<'_>) -> LinkedResult<SendProgress>;
- /// The person's sends in a group that are not yet accepted, newest first.
+ async fn enqueue(&self, intent: SendIntent<'_>) -> ExternalChatResult<SendProgress>;
+ /// The person's sends in a chat that are not yet accepted, newest first.
async fn open_sends(
&self,
chat_id: Uuid,
user: &MacroUserIdStr<'_>,
- ) -> LinkedResult<Vec<SendProgress>>;
- /// The person's sends for these group messages or intent ids.
+ ) -> ExternalChatResult<Vec<SendProgress>>;
+ /// The person's sends for these chat messages or intent ids.
async fn sends_for(
&self,
chat_id: Uuid,
user: &MacroUserIdStr<'_>,
ids: &[Uuid],
- ) -> LinkedResult<Vec<SendProgress>>;
+ ) -> ExternalChatResult<Vec<SendProgress>>;
+ /// The state of the viewer's newest send of each of these messages.
+ async fn delivery_states(
+ &self,
+ chat_id: Uuid,
+ viewer: &str,
+ messages: &[Uuid],
+ ) -> ExternalChatResult<Vec<(Uuid, DeliveryState)>>;
}
/// Session-service state transitions, all fenced by the lease epoch.
#[async_trait]
pub trait SessionRepository: Send + Sync {
/// The account's lifecycle, unfenced.
- async fn account_state(&self, account: Uuid) -> LinkedResult<Option<AccountState>>;
+ async fn account_state(&self, account: Uuid) -> ExternalChatResult<Option<AccountState>>;
/// Accounts needing a replica whose advisory lock
/// (`hashtextextended('whatsapp-account:' || id, 0)`) nobody holds.
- async fn discoverable_accounts(&self, limit: i64) -> LinkedResult<Vec<Uuid>>;
+ async fn discoverable_accounts(&self, limit: i64) -> ExternalChatResult<Vec<Uuid>>;
/// Fleet counts for health checks.
- async fn fleet_health(&self) -> LinkedResult<FleetHealth>;
+ async fn fleet_health(&self) -> ExternalChatResult<FleetHealth>;
/// Store an inbound event for later ingestion, fenced; idempotent on id.
- async fn enqueue_inbound(&self, scope: SessionScope, event: &InboundEvent) -> LinkedResult<()>;
+ async fn enqueue_inbound(
+ &self,
+ scope: SessionScope,
+ event: &InboundEvent,
+ ) -> ExternalChatResult<()>;
/// Bump the lease epoch and recover the previous replica's sends: claimed but
/// undispatched become `pending`; dispatched without outcome become `unknown`.
- async fn begin_lease(&self, account: Uuid) -> LinkedResult<Lease>;
+ async fn begin_lease(&self, account: Uuid) -> ExternalChatResult<Lease>;
/// Return claimed but undispatched sends to `pending` before handing over.
- async fn release_lease(&self, scope: SessionScope) -> LinkedResult<()>;
+ async fn release_lease(&self, scope: SessionScope) -> ExternalChatResult<()>;
/// Tell other replicas the account is free to take while it still needs
/// a session (`whatsapp_account_released`).
- async fn announce_released(&self, account: Uuid) -> LinkedResult<()>;
+ async fn announce_released(&self, account: Uuid) -> ExternalChatResult<()>;
/// Remove and return the owner's WebAuthn assertion, so it is forwarded once.
async fn take_passkey_response(
&self,
scope: SessionScope,
pairing: Uuid,
- ) -> LinkedResult<Option<serde_json::Value>>;
+ ) -> ExternalChatResult<Option<serde_json::Value>>;
/// Claim attachments this account observed and has not downloaded yet.
async fn claim_media_imports(
&self,
scope: SessionScope,
limit: i64,
- ) -> LinkedResult<Vec<MediaImport>>;
+ ) -> ExternalChatResult<Vec<MediaImport>>;
/// Keep downloaded bytes for import into a static file. The bytes must
/// match the expected hash; a ready attachment is never rewritten.
async fn complete_media_import(
@@ -325,7 +282,7 @@ pub trait SessionRepository: Send + Sync {
scope: SessionScope,
attachment: &AttachmentRef,
bytes: &[u8],
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// Record a failed download; a permanent failure or too many attempts marks
/// the attachment failed and shows a notice on the message.
async fn fail_media_import(
@@ -333,13 +290,17 @@ pub trait SessionRepository: Send + Sync {
scope: SessionScope,
attachment: &AttachmentRef,
failure: MediaFailure,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// Bytes of the attachment of a send this replica claimed.
- async fn outbound_media(&self, scope: SessionScope, send: Uuid) -> LinkedResult<OutboundMedia>;
+ async fn outbound_media(
+ &self,
+ scope: SessionScope,
+ send: Uuid,
+ ) -> ExternalChatResult<OutboundMedia>;
/// A pairing transaction by id; expires it first when past its deadline.
- async fn pairing_work(&self, pairing: Uuid) -> LinkedResult<Option<PairingWork>>;
+ async fn pairing_work(&self, pairing: Uuid) -> ExternalChatResult<Option<PairingWork>>;
/// The account's live pairing transaction, if any.
- async fn live_pairing(&self, scope: SessionScope) -> LinkedResult<Option<PairingWork>>;
+ async fn live_pairing(&self, scope: SessionScope) -> ExternalChatResult<Option<PairingWork>>;
/// Apply progress to a live transaction. `Success` connects the account when
/// the identity matches the expected one and belongs to no other account;
/// otherwise the transaction fails with `Conflict` or `Forbidden`.
@@ -348,10 +309,10 @@ pub trait SessionRepository: Send + Sync {
scope: SessionScope,
pairing: Uuid,
update: &PairingUpdate,
- ) -> LinkedResult<PairingWork>;
+ ) -> ExternalChatResult<PairingWork>;
/// WhatsApp ended the session: `logged_out`, generation + 1, consents end,
/// undispatched sends fail.
- async fn logged_out(&self, scope: SessionScope) -> LinkedResult<()>;
+ async fn logged_out(&self, scope: SessionScope) -> ExternalChatResult<()>;
/// Store group metadata and reconcile remote memberships of adopted
/// bindings. With `complete`, groups not listed are treated as left.
async fn update_groups(
@@ -359,7 +320,7 @@ pub trait SessionRepository: Send + Sync {
scope: SessionScope,
groups: &[GroupInfo],
complete: bool,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// The account is no longer in this group (it left, was removed, or the
/// group is gone): drop it from the account's list and end its membership.
async fn group_left(
@@ -367,30 +328,35 @@ pub trait SessionRepository: Send + Sync {
scope: SessionScope,
group: &str,
observed_at: DateTime<Utc>,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// Groups this account currently observes for at least one adopted binding.
- async fn observed_groups(&self, scope: SessionScope) -> LinkedResult<Vec<String>>;
+ async fn observed_groups(&self, scope: SessionScope) -> ExternalChatResult<Vec<String>>;
/// Upsert chat-list rows. Signals the owner's list when something changed.
/// Returns whether an added chat waiting for its first message got one
/// (it is `pending` now).
- async fn update_chats(&self, scope: SessionScope, chats: &[ChatUpdate]) -> LinkedResult<bool>;
+ async fn update_chats(
+ &self,
+ scope: SessionScope,
+ chats: &[ChatUpdate],
+ ) -> ExternalChatResult<bool>;
/// The account owner's groups still importing earlier messages.
- async fn backfill_work(&self, scope: SessionScope) -> LinkedResult<Vec<BackfillWork>>;
+ async fn backfill_work(&self, scope: SessionScope) -> ExternalChatResult<Vec<BackfillWork>>;
/// No message of the chat is known to start from (`waiting_for_message`).
async fn backfill_waiting_for_message(
&self,
scope: SessionScope,
group: Uuid,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// A page was requested from the phone just now (`running`).
- async fn backfill_requested(&self, scope: SessionScope, group: Uuid) -> LinkedResult<()>;
+ async fn backfill_requested(&self, scope: SessionScope, group: Uuid) -> ExternalChatResult<()>;
/// The phone did not answer the last request (`waiting_for_phone`).
- async fn backfill_unanswered(&self, scope: SessionScope, group: Uuid) -> LinkedResult<()>;
+ async fn backfill_unanswered(&self, scope: SessionScope, group: Uuid)
+ -> ExternalChatResult<()>;
/// Record a page the phone sent for a chat; returns the resulting state of
/// each of the owner's groups for that chat that was importing.
@@ -398,17 +364,25 @@ pub trait SessionRepository: Send + Sync {
&self,
scope: SessionScope,
page: &HistoryPage,
- ) -> LinkedResult<Vec<(Uuid, BackfillState)>>;
+ ) -> ExternalChatResult<Vec<(Uuid, BackfillState)>>;
/// Persist one validated event into every binding it is observed for.
async fn ingest(
&self,
scope: SessionScope,
event: &InboundEvent,
- ) -> LinkedResult<IngestOutcome>;
+ ) -> ExternalChatResult<IngestOutcome>;
/// Deferred events whose retry time has come.
- async fn due_events(&self, scope: SessionScope, limit: i64) -> LinkedResult<Vec<InboundEvent>>;
+ async fn due_events(
+ &self,
+ scope: SessionScope,
+ limit: i64,
+ ) -> ExternalChatResult<Vec<InboundEvent>>;
/// Claim pending sends in group order; expired ones fail instead.
- async fn claim_sends(&self, scope: SessionScope, limit: i64) -> LinkedResult<Vec<ClaimedSend>>;
+ async fn claim_sends(
+ &self,
+ scope: SessionScope,
+ limit: i64,
+ ) -> ExternalChatResult<Vec<ClaimedSend>>;
/// Record that the request is about to be submitted with this message id,
/// after re-checking every sending fact. After this returns, the send is
/// never retried automatically.
@@ -417,147 +391,12 @@ pub trait SessionRepository: Send + Sync {
scope: SessionScope,
send: Uuid,
source_id: &str,
- ) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
/// Record the outcome; an acceptance also writes the group message.
async fn record_send_outcome(
&self,
scope: SessionScope,
send: Uuid,
outcome: &SendOutcome,
- ) -> LinkedResult<()>;
-}
-
-/// Who wrote a message.
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum Origin {
- /// Someone at the provider: a person's phone or app.
- Remote,
- /// A deliberate send from Macro.
- Macro,
- /// Earlier messages imported after adding the chat.
- History,
-}
-
-/// A group message revision that still needs generic notifications.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct GroupEffect {
- /// Group entity.
- pub chat_id: Uuid,
- /// Group message.
- pub message_id: Uuid,
- /// Revision to publish.
- pub revision: i64,
- /// Nothing was published for this message before.
- pub first_publication: bool,
- /// Who wrote it.
- pub origin: Origin,
- /// Verified Macro user behind a phone-origin message, never notified of it.
- pub associated_user: Option<String>,
- /// A system line (added, left, renamed…), never a notification.
- pub system: bool,
-}
-
-impl GroupEffect {
- /// Only a newly received live message creates a notification.
- pub fn should_notify(&self) -> bool {
- self.first_publication && self.origin != Origin::History && !self.system
- }
-}
-
-/// The notification outbox.
-#[async_trait]
-pub trait EffectsRepository: Send + Sync {
- /// Due revisions, oldest first.
- async fn pending_effects(&self, limit: i64) -> LinkedResult<Vec<GroupEffect>>;
- /// Mark exactly this revision published; later revisions stay due.
- async fn published(&self, message: Uuid, revision: i64) -> LinkedResult<()>;
- /// Retry this message later without blocking others.
- async fn defer_effect(&self, message: Uuid) -> LinkedResult<()>;
-}
-
-/// What a new message's notification names.
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct MessageNotice {
- /// The service the chat lives in.
- pub provider: ExternalChatProvider,
- /// The chat's name.
- pub chat_name: String,
- /// The message was deleted, or edited before it was announced.
- pub superseded: bool,
-}
-
-/// Who a change or a notification reaches, beyond ordinary entity grants.
-#[async_trait]
-pub trait AudienceRepository: Send + Sync {
- /// The Macro user who linked this account.
- async fn account_owner(&self, account: Uuid) -> LinkedResult<Option<String>>;
- /// People who can still view the chat through link or team sharing; they
- /// become known once they read it.
- async fn link_viewers(&self, group: Uuid) -> LinkedResult<Vec<String>>;
- /// The facts a message's notification needs; `None` if it is gone.
- async fn message_notice(
- &self,
- group: Uuid,
- message: Uuid,
- ) -> LinkedResult<Option<MessageNotice>>;
-}
-
-/// Generic notifications for current group viewers.
-#[async_trait]
-pub trait GroupPublisher: Send + Sync {
- /// Publish one message revision to its current viewers.
- async fn publish(&self, effect: &GroupEffect) -> LinkedResult<()>;
-}
-
-/// Downloaded or uploaded bytes waiting to become a static file.
-#[derive(Clone)]
-pub struct FileImport {
- /// Which attachment.
- pub attachment: AttachmentRef,
- /// The staged bytes' id (the upload id of a file sent from Macro).
- pub blob_id: Uuid,
- /// The chat the attachment belongs to.
- pub chat_id: Uuid,
- /// Owner of the static file: the uploader, or the chat's owner.
- pub owner: String,
- /// Display filename.
- pub filename: String,
- /// MIME type.
- pub mime: String,
- /// Earlier failed imports.
- pub attempts: i32,
-}
-
-/// The import queue between blobs and static files, and the static files no
-/// attachment references any more.
-#[async_trait]
-pub trait MediaRepository: Send + Sync {
- /// Imports whose retry time has come, claimed, without their bytes.
- async fn due_imports(&self, limit: i64) -> LinkedResult<Vec<FileImport>>;
- /// One claimed import's plaintext bytes; `None` once the blob is gone.
- async fn import_bytes(&self, import: &FileImport) -> LinkedResult<Option<Vec<u8>>>;
- /// `file_id` holds the bytes: attach it to the message, drop the blob and
- /// queue publication. A file the attachment no longer takes (deleted, or
- /// imported meanwhile) is queued for deletion instead.
- async fn import_stored(&self, import: &FileImport, file_id: Uuid) -> LinkedResult<()>;
- /// Retry later, or give up and show a notice on the message.
- async fn import_failed(&self, import: &FileImport, permanent: bool) -> LinkedResult<()>;
- /// Delete staged uploads nobody sent, participant receipts older than
- /// [`RECEIPT_RETENTION_DAYS`](crate::domain::models::RECEIPT_RETENTION_DAYS)
- /// and pairings consumed over a day ago (they keep the expected identity).
- async fn expire_retained(&self) -> LinkedResult<u64>;
- /// Static files no attachment references any more, claimed for deletion.
- async fn due_file_deletions(&self, limit: i64) -> LinkedResult<Vec<Uuid>>;
- /// The static file service no longer holds these files.
- async fn files_deleted(&self, files: &[Uuid]) -> LinkedResult<()>;
-}
-
-/// Static file storage, the same as channel media's.
-#[async_trait]
-pub trait FileStore: Send + Sync {
- /// Store the bytes as a new static file owned by `import.owner`; returns
- /// its id.
- async fn store(&self, import: &FileImport, bytes: Vec<u8>) -> LinkedResult<Uuid>;
- /// Delete one static file; one that is already gone counts as deleted.
- async fn delete(&self, file_id: Uuid) -> LinkedResult<()>;
+ ) -> ExternalChatResult<()>;
}
diff --git a/crates/whatsapp_linked/src/domain/provider.rs b/crates/whatsapp_linked/src/domain/provider.rs
new file mode 100644
index 0000000000..18d66bf6b6
--- /dev/null
+++ b/crates/whatsapp_linked/src/domain/provider.rs
@@ -0,0 +1,318 @@
+//! WhatsApp as an external chat provider: which chats a person can add, who
+//! can send in them, their members and names, and the `whatsapp_send` outbox
+//! the session service dispatches.
+
+use super::{
+ jid::{Jid, is_group_jid},
+ models::*,
+ ports::*,
+ service::{chat_fallback_name, connected},
+};
+use async_trait::async_trait;
+use external_chat::domain::{
+ models::*,
+ ports::{
+ AccountRef, ChatProvider, ExternalChatResult, Members, MutationTarget, SendIntent,
+ SendingFacts, StoredSend,
+ },
+ timeline::{ExternalChatMembers, ExternalChatParticipant, view::People},
+};
+use macro_user_id::user_id::MacroUserIdStr;
+use std::sync::Arc;
+use uuid::Uuid;
+
+/// Linked WhatsApp accounts behind external chats.
+#[derive(Clone)]
+pub struct WhatsAppProvider {
+ accounts: Arc<dyn AccountRepository>,
+ groups: Arc<dyn GroupRepository>,
+ adoptions: Arc<dyn AdoptionRepository>,
+ sends: Arc<dyn SendRepository>,
+}
+
+impl WhatsAppProvider {
+ /// Wire adapters at the composition root.
+ pub fn new<R>(repository: Arc<R>) -> Self
+ where
+ R: AccountRepository + GroupRepository + AdoptionRepository + SendRepository + 'static,
+ {
+ Self {
+ accounts: repository.clone(),
+ groups: repository.clone(),
+ adoptions: repository.clone(),
+ sends: repository,
+ }
+ }
+}
+
+#[async_trait]
+impl ChatProvider for WhatsAppProvider {
+ fn capabilities(&self, _chat: &ChatRef) -> Capabilities {
+ Capabilities::default()
+ }
+
+ /// Each mention is a person's WhatsApp id that the text names as
+ /// `@<user part>`, the way WhatsApp renders it.
+ fn validate_mentions(&self, text: &str, mentions: &[String]) -> ExternalChatResult<()> {
+ if mentions.len() > MENTION_LIMIT {
+ return Err(ExternalChatError::Invalid("too many mentions in one message").into());
+ }
+ for mention in mentions {
+ let user = Jid::parse(mention)
+ .filter(|jid| jid.is_person())
+ .ok_or(ExternalChatError::Invalid(
+ "a mention is not a WhatsApp participant",
+ ))?
+ .user;
+ if !text.contains(&format!("@{user}")) {
+ return Err(ExternalChatError::Invalid(
+ "a mentioned person is not named in the text",
+ )
+ .into());
+ }
+ }
+ Ok(())
+ }
+
+ async fn account(&self, user: &MacroUserIdStr<'_>) -> ExternalChatResult<Option<AccountRef>> {
+ Ok(self
+ .accounts
+ .owned_account(user)
+ .await?
+ .map(|account| AccountRef {
+ id: account.id,
+ user_id: account.user_id,
+ identity: account.identity_jid,
+ connected: account.state == AccountState::Connected,
+ generation: account.authorization_generation,
+ }))
+ }
+
+ async fn sending_facts(
+ &self,
+ account: &AccountRef,
+ chats: &[ChatRef],
+ ) -> ExternalChatResult<Vec<SendingFacts>> {
+ let groups: Vec<String> = chats
+ .iter()
+ .filter(|chat| chat.kind == ExternalChatKind::Group)
+ .map(|chat| chat.remote_id.clone())
+ .collect();
+ let rosters = if groups.is_empty() {
+ vec![]
+ } else {
+ self.groups.account_rosters(account.id, &groups).await?
+ };
+ Ok(chats
+ .iter()
+ .map(|chat| {
+ sending_facts(
+ account,
+ chat,
+ rosters.iter().find(|roster| roster.jid == chat.remote_id),
+ )
+ })
+ .collect())
+ }
+
+ async fn communities(
+ &self,
+ chats: &[ChatRef],
+ ) -> ExternalChatResult<Vec<Option<CommunityReference>>> {
+ self.groups.communities(chats).await
+ }
+
+ async fn members(&self, chat: &ChatRef) -> ExternalChatResult<Option<Members>> {
+ Ok(self.groups.chat_roster(chat).await?.map(|roster| Members {
+ disappearing_seconds: roster.disappearing_seconds,
+ members: members(&roster),
+ }))
+ }
+
+ async fn people(&self, chat: &ChatRef, ids: &[String]) -> ExternalChatResult<People> {
+ self.groups.people(chat, ids).await
+ }
+
+ async fn request_refresh(&self, chat: &ChatRef, viewer: &str) -> ExternalChatResult<()> {
+ let observers = self.groups.group_accounts(chat.id, viewer).await?;
+ self.groups.request_groups_refresh(&observers).await
+ }
+
+ /// Add an existing WhatsApp group the person has joined, or a direct chat
+ /// from their chat list, as their own external chat; never creates or
+ /// joins anything in WhatsApp. Adding the same chat twice returns the
+ /// existing one.
+ #[tracing::instrument(
+ name = "whatsapp.linked.adopt",
+ skip_all,
+ err,
+ fields(chat_id, created, kind)
+ )]
+ async fn adopt(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ request: &AddChat,
+ ) -> ExternalChatResult<ChatRef> {
+ match request.provider {
+ ExternalChatProvider::WhatsApp => {}
+ }
+ let jid = request.remote_id.as_str();
+ let kind = if is_group_jid(jid) {
+ ExternalChatKind::Group
+ } else {
+ ExternalChatKind::Direct
+ };
+ if kind != request.kind {
+ return Err(ExternalChatError::Invalid("the chat kind does not match its id").into());
+ }
+ let (account, identity) = connected(self.accounts.as_ref(), user).await?;
+ let adoption = if kind == ExternalChatKind::Group {
+ let group = self
+ .groups
+ .account_group(account.id, jid)
+ .await?
+ .ok_or(ExternalChatError::CannotAdd(CannotAdd::NotInGroup))?;
+ if group.community_parent {
+ return Err(ExternalChatError::CannotAdd(CannotAdd::Community).into());
+ }
+ if group.participant(&identity).is_none() {
+ return Err(ExternalChatError::CannotAdd(CannotAdd::NotInGroup).into());
+ }
+ Adoption {
+ jid: group.jid.clone(),
+ kind: ExternalChatKind::Group,
+ name: group.display_name(&identity),
+ can_post: group.can_post(&identity),
+ observed_at: group.observed_at,
+ }
+ } else {
+ if jid == identity {
+ return Err(ExternalChatError::CannotAdd(CannotAdd::OwnChat).into());
+ }
+ let chat = self
+ .groups
+ .account_chats(account.id)
+ .await?
+ .into_iter()
+ .find(|chat| chat.kind == ExternalChatKind::Direct && chat.jid == jid)
+ .ok_or(ExternalChatError::CannotAdd(CannotAdd::NotInChatList))?;
+ Adoption {
+ name: chat
+ .name
+ .filter(|name| !name.trim().is_empty())
+ .unwrap_or_else(|| chat_fallback_name(&chat.jid, chat.kind, &identity)),
+ jid: chat.jid,
+ kind: ExternalChatKind::Direct,
+ can_post: true,
+ observed_at: chrono::Utc::now(),
+ }
+ };
+ let adopted = self
+ .adoptions
+ .adopt(AdoptionRecord {
+ user,
+ account: &account,
+ adoption: &adoption,
+ })
+ .await?;
+ let span = tracing::Span::current();
+ span.record("chat_id", tracing::field::display(adopted.chat.id));
+ span.record("created", adopted.created);
+ span.record("kind", adoption.kind.as_ref());
+ Ok(adopted.chat)
+ }
+
+ async fn detach(&self, chat: &ChatRef) -> ExternalChatResult<()> {
+ self.adoptions.detach(chat).await
+ }
+
+ async fn mutation_target(
+ &self,
+ chat: &ChatRef,
+ message: Uuid,
+ ) -> ExternalChatResult<Option<MutationTarget>> {
+ self.sends.mutation_target(chat, message).await
+ }
+
+ async fn stored_send(
+ &self,
+ chat_id: Uuid,
+ user: &MacroUserIdStr<'_>,
+ idempotency_key: Uuid,
+ ) -> ExternalChatResult<Option<StoredSend>> {
+ self.sends.stored_send(chat_id, user, idempotency_key).await
+ }
+
+ async fn enqueue_send(&self, intent: SendIntent<'_>) -> ExternalChatResult<SendProgress> {
+ self.sends.enqueue(intent).await
+ }
+
+ async fn open_sends(
+ &self,
+ chat_id: Uuid,
+ user: &MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<Vec<SendProgress>> {
+ self.sends.open_sends(chat_id, user).await
+ }
+
+ async fn sends_for(
+ &self,
+ chat_id: Uuid,
+ user: &MacroUserIdStr<'_>,
+ ids: &[Uuid],
+ ) -> ExternalChatResult<Vec<SendProgress>> {
+ self.sends.sends_for(chat_id, user, ids).await
+ }
+
+ async fn delivery_states(
+ &self,
+ chat_id: Uuid,
+ viewer: &str,
+ messages: &[Uuid],
+ ) -> ExternalChatResult<Vec<(Uuid, DeliveryState)>> {
+ self.sends.delivery_states(chat_id, viewer, messages).await
+ }
+}
+
+/// A group's roster as the chat's side panel shows it.
+fn members(info: &GroupInfo) -> ExternalChatMembers {
+ ExternalChatMembers {
+ name: info.name.clone(),
+ participants: info
+ .participants
+ .iter()
+ .map(|participant| ExternalChatParticipant {
+ jid: participant.identity.jid.clone(),
+ alternate_jid: participant.identity.alternate_jid.clone(),
+ display_name: participant.identity.display_name.clone(),
+ admin: participant.admin,
+ })
+ .collect(),
+ announce: info.announce,
+ observed_at: info.observed_at,
+ }
+}
+
+/// Whether the person's own account can send in the chat, from its own
+/// roster of the group. A direct chat has two people: only its owner's own
+/// account is in it.
+fn sending_facts(account: &AccountRef, chat: &ChatRef, roster: Option<&GroupInfo>) -> SendingFacts {
+ if chat.kind == ExternalChatKind::Direct {
+ let owner = account.user_id == chat.user_id;
+ return SendingFacts {
+ observing: owner,
+ remote_member: owner,
+ can_post: owner,
+ };
+ }
+ let identity = account.identity.as_deref();
+ SendingFacts {
+ observing: roster.is_some(),
+ remote_member: roster
+ .zip(identity)
+ .is_some_and(|(group, identity)| group.participant(identity).is_some()),
+ can_post: roster
+ .zip(identity)
+ .is_some_and(|(group, identity)| group.can_post(identity)),
+ }
+}
diff --git a/crates/whatsapp_linked/src/domain/service.rs b/crates/whatsapp_linked/src/domain/service.rs
index 64b56a353a..4b5d2e84ca 100644
--- a/crates/whatsapp_linked/src/domain/service.rs
+++ b/crates/whatsapp_linked/src/domain/service.rs
@@ -1,56 +1,36 @@
-//! Owner-bound account lifecycle, adoption and deliberate sends, as served by DSS.
+//! Owner-bound account lifecycle and the WhatsApp chat list, as served by DSS.
+//! Added chats are `external_chat`'s; [`super::provider`] adds and sends.
-use super::{
- jid::{Jid, is_group_jid},
- models::*,
- ports::*,
- timeline::*,
-};
-use chrono::{DateTime, Utc};
-use entity_access::domain::models::{EntityAccessReceipt, EntityType, RequiredPermission};
+use super::{models::*, ports::*};
+use external_chat::domain::{models::*, ports::ExternalChatResult};
use macro_user_id::user_id::MacroUserIdStr;
-use sha2::{Digest, Sha256};
use std::sync::Arc;
use uuid::Uuid;
#[cfg(test)]
mod test;
-mod timeline;
-/// DSS use cases for personal linked devices.
+/// DSS use cases for a person's linked WhatsApp account.
#[derive(Clone)]
-pub struct LinkedService {
+pub struct WhatsAppService {
accounts: Arc<dyn AccountRepository>,
groups: Arc<dyn GroupRepository>,
- bindings: Arc<dyn BindingRepository>,
- sends: Arc<dyn SendRepository>,
- timeline: Arc<dyn TimelineRepository>,
- send_rate_limiter: Arc<dyn SendRateLimiter>,
}
-impl LinkedService {
+impl WhatsAppService {
/// Wire adapters at the composition root.
- pub fn new<R>(repository: Arc<R>, send_rate_limiter: Arc<dyn SendRateLimiter>) -> Self
+ pub fn new<R>(repository: Arc<R>) -> Self
where
- R: AccountRepository
- + GroupRepository
- + BindingRepository
- + SendRepository
- + TimelineRepository
- + 'static,
+ R: AccountRepository + GroupRepository + 'static,
{
Self {
accounts: repository.clone(),
- groups: repository.clone(),
- bindings: repository.clone(),
- sends: repository.clone(),
- timeline: repository,
- send_rate_limiter,
+ groups: repository,
}
}
/// Personal settings never depend on workspace administration.
- pub async fn account(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<Option<Account>> {
+ pub async fn account(&self, user: &MacroUserIdStr<'_>) -> ExternalChatResult<Option<Account>> {
self.accounts.owned_account(user).await
}
@@ -61,7 +41,7 @@ impl LinkedService {
user: &MacroUserIdStr<'_>,
replace: bool,
authority: SendAuthority,
- ) -> LinkedResult<Pairing> {
+ ) -> ExternalChatResult<Pairing> {
human(authority)?;
let pairing = self.accounts.begin_pairing(user, replace).await?;
let span = tracing::Span::current();
@@ -76,7 +56,7 @@ impl LinkedService {
user: &MacroUserIdStr<'_>,
pairing: Uuid,
authority: SendAuthority,
- ) -> LinkedResult<PairState> {
+ ) -> ExternalChatResult<PairState> {
human(authority)?;
self.accounts.pair_state(user, pairing).await
}
@@ -90,10 +70,10 @@ impl LinkedService {
pairing: Uuid,
response: &serde_json::Value,
authority: SendAuthority,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
human(authority)?;
if !response.is_object() || response.to_string().len() > PASSKEY_LIMIT_BYTES {
- return Err(LinkedError::Invalid("invalid passkey response").into());
+ return Err(ExternalChatError::Invalid("invalid passkey response").into());
}
self.accounts.submit_passkey(user, pairing, response).await
}
@@ -105,7 +85,7 @@ impl LinkedService {
user: &MacroUserIdStr<'_>,
pairing: Uuid,
authority: SendAuthority,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
human(authority)?;
self.accounts.cancel_pairing(user, pairing).await
}
@@ -117,7 +97,7 @@ impl LinkedService {
user: &MacroUserIdStr<'_>,
generation: i64,
authority: SendAuthority,
- ) -> LinkedResult<Disconnect> {
+ ) -> ExternalChatResult<Disconnect> {
human(authority)?;
let changed = self.accounts.disconnect(user, generation).await?;
Ok(Disconnect {
@@ -125,20 +105,6 @@ impl LinkedService {
})
}
- async fn connected(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<(Account, String)> {
- let account = self
- .accounts
- .owned_account(user)
- .await?
- .filter(|a| a.state == AccountState::Connected)
- .ok_or(LinkedError::Disconnected)?;
- let identity = account
- .identity_jid
- .clone()
- .ok_or(LinkedError::Disconnected)?;
- Ok((account, identity))
- }
-
/// The person's WhatsApp chat list, as WhatsApp Web shows it: groups and
/// direct chats, newest activity first. Each chat says whether it is
/// added, shared with the caller or addable; direct chats are addable (not
@@ -150,8 +116,8 @@ impl LinkedService {
err,
fields(count, account_id)
)]
- pub async fn chats(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<ChatList> {
- let (account, identity) = self.connected(user).await?;
+ pub async fn chats(&self, user: &MacroUserIdStr<'_>) -> ExternalChatResult<ChatList> {
+ let (account, identity) = connected(self.accounts.as_ref(), user).await?;
tracing::Span::current().record("account_id", tracing::field::display(account.id));
self.groups.request_groups_refresh(&[account.id]).await?;
let roster = self.groups.account_groups(account.id).await?;
@@ -248,371 +214,41 @@ impl LinkedService {
Ok(ChatList { chats })
}
- /// Add an existing WhatsApp group the person has joined, or a direct chat
- /// from their chat list, as their own external chat; never creates or
- /// joins anything in WhatsApp. Adding the same chat twice returns the
- /// existing one.
- #[tracing::instrument(
- name = "whatsapp.linked.adopt",
- skip_all,
- err,
- fields(chat_id, created, kind)
- )]
- pub async fn adopt(
- &self,
- user: &MacroUserIdStr<'_>,
- request: &AddChat,
- authority: SendAuthority,
- ) -> LinkedResult<Binding> {
- human(authority)?;
- match request.provider {
- ExternalChatProvider::WhatsApp => {}
- }
- let jid = request.remote_id.as_str();
- let kind = if is_group_jid(jid) {
- ExternalChatKind::Group
- } else {
- ExternalChatKind::Direct
- };
- if kind != request.kind {
- return Err(LinkedError::Invalid("the chat kind does not match its id").into());
- }
- let (account, identity) = self.connected(user).await?;
- let adoption = if kind == ExternalChatKind::Group {
- let group = self
- .groups
- .account_group(account.id, jid)
- .await?
- .ok_or(LinkedError::CannotAdd(CannotAdd::NotInGroup))?;
- if group.community_parent {
- return Err(LinkedError::CannotAdd(CannotAdd::Community).into());
- }
- if group.participant(&identity).is_none() {
- return Err(LinkedError::CannotAdd(CannotAdd::NotInGroup).into());
- }
- Adoption {
- jid: group.jid.clone(),
- kind: ExternalChatKind::Group,
- name: group.display_name(&identity),
- can_post: group.can_post(&identity),
- observed_at: group.observed_at,
- }
- } else {
- if jid == identity {
- return Err(LinkedError::CannotAdd(CannotAdd::OwnChat).into());
- }
- let chat = self
- .groups
- .account_chats(account.id)
- .await?
- .into_iter()
- .find(|chat| chat.kind == ExternalChatKind::Direct && chat.jid == jid)
- .ok_or(LinkedError::CannotAdd(CannotAdd::NotInChatList))?;
- Adoption {
- name: chat
- .name
- .filter(|name| !name.trim().is_empty())
- .unwrap_or_else(|| chat_fallback_name(&chat.jid, chat.kind, &identity)),
- jid: chat.jid,
- kind: ExternalChatKind::Direct,
- can_post: true,
- observed_at: chrono::Utc::now(),
- }
- };
- let adopted = self
- .bindings
- .adopt(AdoptionRecord {
- user,
- account: &account,
- adoption: &adoption,
- })
- .await?;
- let span = tracing::Span::current();
- span.record("chat_id", tracing::field::display(adopted.binding.id));
- span.record("created", adopted.created);
- span.record("kind", adoption.kind.as_ref());
- Ok(adopted.binding)
- }
-
- /// Detach locally; nothing changes in WhatsApp. Only the person who
- /// added the chat, in their own session, stops syncing it.
- #[tracing::instrument(name = "whatsapp.linked.detach", skip_all, err, fields(chat_id))]
- pub async fn detach<T: RequiredPermission>(
- &self,
- access: &EntityAccessReceipt<T>,
- authority: SendAuthority,
- ) -> LinkedResult<()> {
- human(authority)?;
- let (user, chat_id) = receipt_scope(access)?;
- tracing::Span::current().record("chat_id", tracing::field::display(chat_id));
- let binding = self
- .bindings
- .binding(chat_id)
- .await?
- .ok_or(LinkedError::NotFound)?;
- if binding.user_id != user.as_ref() {
- return Err(LinkedError::Forbidden.into());
- }
- self.bindings.detach(&binding).await
- }
-
- async fn reply_block(
- &self,
- account: Option<&Account>,
- binding: &Binding,
- ) -> LinkedResult<Option<ReplyBlock>> {
- let facts = match account {
- Some(account) if account.state == AccountState::Connected => {
- let roster = match binding.kind {
- ExternalChatKind::Group => self.sends.account_roster(account, binding).await?,
- ExternalChatKind::Direct => None,
- };
- Some(sending_facts(account, binding, roster))
- }
- _ => None,
- };
- Ok(reply_block(account, binding, facts.as_ref()))
- }
-
- /// Record a deliberate send for the person's own account. The account's
- /// replica dispatches it; uncertain outcomes are never resent.
- #[tracing::instrument(
- name = "whatsapp.linked.send",
- skip_all,
- err,
- fields(chat_id, send_id, kind)
- )]
- pub async fn send_reply<T: RequiredPermission>(
- &self,
- access: &EntityAccessReceipt<T>,
- request: &SendReply,
- authority: SendAuthority,
- ) -> LinkedResult<SendProgress> {
- sender(authority)?;
- let (user, chat_id) = receipt_scope(access)?;
- tracing::Span::current().record("chat_id", tracing::field::display(chat_id));
- validate_request(request)?;
- let digest: [u8; 32] = Sha256::digest(
- serde_json::to_vec(request).map_err(|_| LinkedError::Invalid("invalid request"))?,
- )
- .into();
- // A retry with the same intent key returns the original send, whatever
- // happened since; it never sends twice.
- if let Some(StoredSend {
- request_sha256,
- progress,
- }) = self
- .sends
- .stored_send(chat_id, user, request.idempotency_key)
- .await?
- {
- if request_sha256 != digest {
- return Err(LinkedError::Conflict.into());
- }
- tracing::Span::current().record("send_id", tracing::field::display(progress.id));
- return Ok(progress);
- }
- self.send_rate_limiter.check(user).await?;
- let binding = self
- .bindings
- .binding(chat_id)
- .await?
- .ok_or(LinkedError::NotFound)?;
- let account = self.accounts.owned_account(user).await?;
- if let Some(block) = self.reply_block(account.as_ref(), &binding).await? {
- return Err(refusal(block).into());
- }
- let account = account.ok_or(LinkedError::Disconnected)?;
- let identity = account
- .identity_jid
- .as_deref()
- .ok_or(LinkedError::Disconnected)?;
- if let Some(quoted) = request.reply_to {
- self.sends
- .mutation_target(&binding, quoted)
- .await?
- .filter(|target| !target.deleted)
- .ok_or(LinkedError::NotFound)?;
- }
- if let ReplyContent::Media { upload_id, caption } = &request.message {
- let mime = self
- .sends
- .staged_upload_mime(user, chat_id, *upload_id)
- .await?
- .ok_or(LinkedError::NotFound)?;
- if MediaKind::of(&mime) == Some(MediaKind::Audio) && caption.is_some() {
- return Err(
- LinkedError::Invalid("send an audio caption as a separate message").into(),
- );
- }
- }
- if let ReplyContent::Mutation {
- message_id,
- operation,
- ..
- } = &request.message
- {
- tracing::Span::current().record("kind", tracing::field::debug(operation));
- let target = self
- .sends
- .mutation_target(&binding, *message_id)
- .await?
- .filter(|target| !target.deleted)
- .ok_or(LinkedError::NotFound)?;
- authorize_mutation(*operation, &target, identity, Utc::now())?;
- }
- let progress = self
- .sends
- .enqueue(SendIntent {
- user,
- account: &account,
- binding: &binding,
- request,
- digest,
- })
- .await?;
- tracing::Span::current().record("send_id", tracing::field::display(progress.id));
- Ok(progress)
- }
-
- /// Stage one attachment for a later deliberate send in this group. Nothing
- /// is sent until a media reply names it.
- #[tracing::instrument(name = "whatsapp.linked.upload", skip_all, err, fields(chat_id, size = bytes.len()))]
- pub async fn upload_attachment<T: RequiredPermission>(
- &self,
- access: &EntityAccessReceipt<T>,
- filename: &str,
- mime: &str,
- bytes: &[u8],
- authority: SendAuthority,
- ) -> LinkedResult<UploadedAttachment> {
- sender(authority)?;
- let (user, chat_id) = receipt_scope(access)?;
- tracing::Span::current().record("chat_id", tracing::field::display(chat_id));
- let kind = MediaKind::of(mime).ok_or(LinkedError::Invalid(
- "this file type cannot be sent to WhatsApp",
- ))?;
- let filename = filename.trim();
- if bytes.is_empty()
- || bytes.len() > MEDIA_LIMIT_BYTES
- || filename.is_empty()
- || filename.len() > 255
- || filename
- .chars()
- .any(|c| c.is_control() || c == '/' || c == '\\')
- {
- return Err(LinkedError::Invalid(
- "an attachment needs a filename and must be non-empty and within the size limit",
- )
- .into());
- }
- let binding = self
- .bindings
- .binding(chat_id)
- .await?
- .ok_or(LinkedError::NotFound)?;
- let account = self.accounts.owned_account(user).await?;
- if let Some(block) = self.reply_block(account.as_ref(), &binding).await? {
- return Err(refusal(block).into());
- }
- let upload_id = self
- .sends
- .stage_upload(StagedUpload {
- user,
- chat_id,
- filename,
- mime,
- bytes,
- sha256: Sha256::digest(bytes).into(),
- shape: super::shape::measure(kind, mime, bytes),
- })
- .await?;
- Ok(UploadedAttachment {
- upload_id,
- filename: filename.to_owned(),
- kind,
- })
- }
-
- /// Attachment states of linked messages the viewer can read. The message
- /// body keeps a text fallback for readers that don't know about them.
- pub async fn message_attachments<T: RequiredPermission>(
- &self,
- access: &EntityAccessReceipt<T>,
- messages: &[Uuid],
- ) -> LinkedResult<Vec<MessageAttachments>> {
- let (_, chat_id) = view_scope(access)?;
- if messages.len() > BATCH_LIMIT {
- return Err(LinkedError::Invalid("too many messages in one request").into());
- }
- let binding = self
- .bindings
- .binding(chat_id)
- .await?
- .ok_or(LinkedError::NotFound)?;
- self.bindings.message_attachments(&binding, messages).await
+ /// Delete old participant receipts and old consumed pairings.
+ pub async fn expire_retained(&self) -> ExternalChatResult<u64> {
+ self.accounts.expire_retained().await
}
- /// The viewer's sends for these intents or group messages.
- pub async fn delivery<T: RequiredPermission>(
- &self,
- access: &EntityAccessReceipt<T>,
- ids: &[Uuid],
- ) -> LinkedResult<Vec<SendProgress>> {
- let (user, chat_id) = receipt_scope(access)?;
- if ids.len() > BATCH_LIMIT {
- return Err(LinkedError::Invalid("too many messages in one request").into());
- }
- self.sends.sends_for(chat_id, user, ids).await
+ /// The Macro user who linked this account.
+ pub async fn account_owner(&self, account: Uuid) -> ExternalChatResult<Option<String>> {
+ self.accounts.account_owner(account).await
}
}
-fn human(authority: SendAuthority) -> LinkedResult<()> {
+fn human(authority: SendAuthority) -> ExternalChatResult<()> {
match authority {
SendAuthority::Human => Ok(()),
SendAuthority::UserApiToken | SendAuthority::Automation => {
- Err(LinkedError::Forbidden.into())
+ Err(ExternalChatError::Forbidden.into())
}
}
}
-fn sender(authority: SendAuthority) -> LinkedResult<()> {
- match authority {
- SendAuthority::Human | SendAuthority::UserApiToken => Ok(()),
- SendAuthority::Automation => Err(LinkedError::Forbidden.into()),
- }
-}
-
-/// The WhatsApp chat a receipt names.
-fn receipt_group<T: RequiredPermission>(access: &EntityAccessReceipt<T>) -> LinkedResult<Uuid> {
- let entity = access.entity();
- if entity.entity_type != EntityType::ExternalChat {
- return Err(LinkedError::Forbidden.into());
- }
- Ok(Uuid::parse_str(&entity.entity_id).map_err(|_| LinkedError::NotFound)?)
-}
-
-/// The person and chat of a receipt held by a person.
-fn receipt_scope<T: RequiredPermission>(
- access: &EntityAccessReceipt<T>,
-) -> LinkedResult<(&MacroUserIdStr<'static>, Uuid)> {
- let user = access
- .get_authenticated_user()
- .map_err(|_| LinkedError::Forbidden)?;
- Ok((user, receipt_group(access)?))
-}
-
-/// Read receipts may represent a bot, but never borrow its acting user's phone
-/// identity, send records or unread state.
-fn view_scope<T: RequiredPermission>(
- access: &EntityAccessReceipt<T>,
-) -> LinkedResult<(&str, Uuid)> {
- let principal = match access.auth() {
- entity_access::domain::models::EntityAccessAuth::Authenticated(user) => user.as_ref(),
- entity_access::domain::models::EntityAccessAuth::Bot(bot) => bot.bot_id_str().as_ref(),
- _ => return Err(LinkedError::Forbidden.into()),
- };
- Ok((principal, receipt_group(access)?))
+/// The person's connected account and its verified identity.
+pub(crate) async fn connected(
+ accounts: &dyn AccountRepository,
+ user: &MacroUserIdStr<'_>,
+) -> ExternalChatResult<(Account, String)> {
+ let account = accounts
+ .owned_account(user)
+ .await?
+ .filter(|a| a.state == AccountState::Connected)
+ .ok_or(ExternalChatError::Disconnected)?;
+ let identity = account
+ .identity_jid
+ .clone()
+ .ok_or(ExternalChatError::Disconnected)?;
+ Ok((account, identity))
}
/// The caller's own external chat for a WhatsApp chat, else someone else's
@@ -643,7 +279,7 @@ fn adoption_of(
}
/// A chat WhatsApp gave no name: a direct chat by its phone number.
-fn chat_fallback_name(jid: &str, kind: ExternalChatKind, identity: &str) -> String {
+pub(crate) fn chat_fallback_name(jid: &str, kind: ExternalChatKind, identity: &str) -> String {
match kind {
ExternalChatKind::Group => "WhatsApp group".to_owned(),
ExternalChatKind::Direct if jid == identity => "You".to_owned(),
@@ -652,150 +288,3 @@ fn chat_fallback_name(jid: &str, kind: ExternalChatKind, identity: &str) -> Stri
}
}
}
-
-/// Whether the person's own account can send in the chat, from its own
-/// roster of the group. A direct chat has two people: only its owner's own
-/// account is in it.
-pub(crate) fn sending_facts(
- account: &Account,
- binding: &Binding,
- roster: Option<GroupInfo>,
-) -> SendingFacts {
- if binding.kind == ExternalChatKind::Direct {
- let owner = account.user_id == binding.user_id;
- return SendingFacts {
- observing: owner,
- remote_member: owner,
- can_post: owner,
- };
- }
- let identity = account.identity_jid.as_deref();
- SendingFacts {
- observing: roster.is_some(),
- remote_member: roster
- .as_ref()
- .zip(identity)
- .is_some_and(|(group, identity)| group.participant(identity).is_some()),
- can_post: roster
- .as_ref()
- .zip(identity)
- .is_some_and(|(group, identity)| group.can_post(identity)),
- }
-}
-
-/// The error a send or upload refused for `block` returns.
-fn refusal(block: ReplyBlock) -> LinkedError {
- match block {
- ReplyBlock::NoAccount | ReplyBlock::Disconnected => LinkedError::Disconnected,
- ReplyBlock::NotAdded | ReplyBlock::NotAllowed | ReplyBlock::Unavailable => {
- LinkedError::Forbidden
- }
- }
-}
-
-/// Why the person cannot send now, from the current facts.
-pub(crate) fn reply_block(
- account: Option<&Account>,
- binding: &Binding,
- facts: Option<&SendingFacts>,
-) -> Option<ReplyBlock> {
- let Some(account) = account else {
- return Some(ReplyBlock::NoAccount);
- };
- if account.state != AccountState::Connected {
- return Some(ReplyBlock::Disconnected);
- }
- if binding.state != BindingState::Active {
- return Some(ReplyBlock::Unavailable);
- }
- let Some(facts) = facts.filter(|facts| facts.observing) else {
- return Some(ReplyBlock::NotAdded);
- };
- if !facts.remote_member || !facts.can_post {
- return Some(ReplyBlock::NotAllowed);
- }
- None
-}
-
-fn validate_text(text: &str) -> LinkedResult<()> {
- if text.trim().is_empty() || text.len() > TEXT_LIMIT_BYTES {
- return Err(
- LinkedError::Invalid("the message is empty or longer than the text limit").into(),
- );
- }
- Ok(())
-}
-
-/// Each mention is a person's WhatsApp id that the text names as
-/// `@<user part>`, the way WhatsApp renders it.
-fn validate_mentions(text: &str, mentions: &[String]) -> LinkedResult<()> {
- if mentions.len() > MENTION_LIMIT {
- return Err(LinkedError::Invalid("too many mentions in one message").into());
- }
- for mention in mentions {
- let user = Jid::parse(mention)
- .filter(|jid| jid.is_person())
- .ok_or(LinkedError::Invalid(
- "a mention is not a WhatsApp participant",
- ))?
- .user;
- if !text.contains(&format!("@{user}")) {
- return Err(LinkedError::Invalid("a mentioned person is not named in the text").into());
- }
- }
- Ok(())
-}
-
-pub(crate) fn validate_request(request: &SendReply) -> LinkedResult<()> {
- if request.idempotency_key.is_nil() {
- return Err(LinkedError::Invalid("a stable send intent key is required").into());
- }
- match &request.message {
- ReplyContent::Text { text, mentions } => {
- validate_text(text)?;
- validate_mentions(text, mentions)
- }
- ReplyContent::Media { caption: None, .. } => Ok(()),
- ReplyContent::Media {
- caption: Some(caption),
- ..
- } => validate_text(caption),
- ReplyContent::Mutation {
- operation: Mutation::Edit,
- text: Some(text),
- ..
- } => validate_text(text),
- ReplyContent::Mutation {
- operation: Mutation::Reaction,
- text: Some(emoji),
- ..
- } if emoji.chars().count() <= 32 && !emoji.chars().any(char::is_control) => Ok(()),
- ReplyContent::Mutation {
- operation: Mutation::Delete,
- text: None,
- ..
- } => Ok(()),
- ReplyContent::Mutation { .. } => {
- Err(LinkedError::Invalid("invalid message operation").into())
- }
- }
-}
-
-/// Edits and deletes are limited to the person's own recent messages.
-pub(crate) fn authorize_mutation(
- operation: Mutation,
- target: &MutationTarget,
- identity: &str,
- now: DateTime<Utc>,
-) -> LinkedResult<()> {
- if operation == Mutation::Reaction {
- return Ok(());
- }
- if !target.sent_by.iter().any(|jid| jid == identity) {
- return Err(LinkedError::Forbidden.into());
- }
- if now - target.source_at > chrono::Duration::seconds(MUTATION_WINDOW_SECONDS) {
- return Err(LinkedError::Invalid("the time to change this message has passed").into());
- }
- Ok(())
-}
diff --git a/crates/whatsapp_linked/src/domain/service/test.rs b/crates/whatsapp_linked/src/domain/service/test.rs
index bffac19585..1867093d4d 100644
--- a/crates/whatsapp_linked/src/domain/service/test.rs
+++ b/crates/whatsapp_linked/src/domain/service/test.rs
@@ -1,68 +1,6 @@
use super::*;
-use chrono::Duration;
-
-fn account(state: AccountState) -> Account {
- Account {
- id: Uuid::from_u128(1),
- user_id: "macro|[email protected]".into(),
- identity_jid: Some("[email protected]".into()),
- state,
- authorization_generation: 3,
- }
-}
-
-fn binding(state: BindingState) -> Binding {
- Binding {
- kind: ExternalChatKind::Group,
- id: Uuid::from_u128(2),
- provider: ExternalChatProvider::WhatsApp,
- remote_id: "[email protected]".into(),
- user_id: "macro|[email protected]".into(),
- state,
- adopted_at: Utc::now(),
- }
-}
-
-fn facts() -> SendingFacts {
- SendingFacts {
- observing: true,
- remote_member: true,
- can_post: true,
- }
-}
-
-#[test]
-fn reply_block_names_the_first_missing_fact() {
- let active = binding(BindingState::Active);
- let connected = account(AccountState::Connected);
- assert_eq!(reply_block(Some(&connected), &active, Some(&facts())), None);
- assert_eq!(
- reply_block(None, &active, None),
- Some(ReplyBlock::NoAccount)
- );
- assert_eq!(
- reply_block(Some(&account(AccountState::LoggedOut)), &active, None),
- Some(ReplyBlock::Disconnected)
- );
- assert_eq!(
- reply_block(
- Some(&connected),
- &binding(BindingState::Detached),
- Some(&facts())
- ),
- Some(ReplyBlock::Unavailable)
- );
- assert_eq!(
- reply_block(Some(&connected), &active, None),
- Some(ReplyBlock::NotAdded)
- );
- let mut announce = facts();
- announce.can_post = false;
- assert_eq!(
- reply_block(Some(&connected), &active, Some(&announce)),
- Some(ReplyBlock::NotAllowed)
- );
-}
+use crate::domain::jid::is_group_jid;
+use chrono::Utc;
#[test]
fn unnamed_groups_are_called_after_their_other_members() {
@@ -101,107 +39,6 @@ fn unnamed_groups_are_called_after_their_other_members() {
assert_eq!(group.display_name(me), "Family");
}
-#[test]
-fn requests_are_bounded_and_operations_carry_the_right_text() {
- let key = Uuid::from_u128(9);
- let message = Uuid::from_u128(10);
- let valid = [
- ReplyContent::Text {
- text: "hi".into(),
- mentions: vec![],
- },
- ReplyContent::Mutation {
- message_id: message,
- operation: Mutation::Reaction,
- text: Some(String::new()),
- },
- ReplyContent::Mutation {
- message_id: message,
- operation: Mutation::Edit,
- text: Some("fixed".into()),
- },
- ReplyContent::Mutation {
- message_id: message,
- operation: Mutation::Delete,
- text: None,
- },
- ];
- for content in valid {
- validate_request(&SendReply {
- idempotency_key: key,
- message: content,
- reply_to: None,
- })
- .unwrap();
- }
- let invalid = [
- ReplyContent::Text {
- text: " ".into(),
- mentions: vec![],
- },
- ReplyContent::Text {
- text: "x".repeat(TEXT_LIMIT_BYTES + 1),
- mentions: vec![],
- },
- ReplyContent::Mutation {
- message_id: message,
- operation: Mutation::Delete,
- text: Some("not allowed".into()),
- },
- ReplyContent::Mutation {
- message_id: message,
- operation: Mutation::Edit,
- text: None,
- },
- ];
- for content in invalid {
- assert!(
- validate_request(&SendReply {
- idempotency_key: key,
- message: content,
- reply_to: None,
- })
- .is_err()
- );
- }
- assert!(
- validate_request(&SendReply {
- idempotency_key: Uuid::nil(),
- message: ReplyContent::Text {
- text: "hi".into(),
- mentions: vec![]
- },
- reply_to: None,
- })
- .is_err()
- );
-}
-
-#[test]
-fn only_the_author_changes_a_recent_message() {
- let now = Utc::now();
- let mine = MutationTarget {
- source_at: now - Duration::minutes(1),
- sent_by: vec!["[email protected]".into(), "42@lid".into()],
- deleted: false,
- };
- let identity = "[email protected]";
- authorize_mutation(Mutation::Edit, &mine, identity, now).unwrap();
- authorize_mutation(Mutation::Delete, &mine, identity, now).unwrap();
- assert!(matches!(
- authorize_mutation(Mutation::Edit, &mine, "[email protected]", now)
- .unwrap_err()
- .current_context(),
- LinkedError::Forbidden
- ));
- authorize_mutation(Mutation::Reaction, &mine, "[email protected]", now).unwrap();
- let old = MutationTarget {
- source_at: now - Duration::minutes(11),
- ..mine
- };
- assert!(authorize_mutation(Mutation::Edit, &old, identity, now).is_err());
-}
-
#[test]
fn group_jids_are_validated_by_shape() {
assert!(is_group_jid("[email protected]"));
diff --git a/crates/whatsapp_linked/src/domain/session.rs b/crates/whatsapp_linked/src/domain/session.rs
index 4241a7ec44..028ec86c35 100644
--- a/crates/whatsapp_linked/src/domain/session.rs
+++ b/crates/whatsapp_linked/src/domain/session.rs
@@ -9,6 +9,7 @@ use super::{
models::*,
ports::*,
};
+use external_chat::domain::{models::*, ports::ExternalChatResult, rich::RichContent};
use std::sync::Arc;
use uuid::Uuid;
@@ -34,7 +35,7 @@ impl SessionService {
/// epoch, which fences every older replica, and records the previous
/// replica's in-flight sends as `unknown`.
#[tracing::instrument(name = "whatsapp.session.begin_lease", skip_all, err, fields(account_id = %account, lease_epoch))]
- pub async fn begin_lease(&self, account: Uuid) -> LinkedResult<Lease> {
+ pub async fn begin_lease(&self, account: Uuid) -> ExternalChatResult<Lease> {
let lease = self.repository.begin_lease(account).await?;
tracing::Span::current().record("lease_epoch", lease.scope.lease_epoch);
if lease.sends_marked_unknown > 0 {
@@ -49,42 +50,45 @@ impl SessionService {
/// Hand the account over: undispatched claims return to `pending`. Call after
/// in-flight sends have recorded their outcome and before unlocking.
#[tracing::instrument(name = "whatsapp.session.release_lease", skip_all, err, fields(account_id = %scope.account_id, lease_epoch = scope.lease_epoch))]
- pub async fn release_lease(&self, scope: SessionScope) -> LinkedResult<()> {
+ pub async fn release_lease(&self, scope: SessionScope) -> ExternalChatResult<()> {
self.repository.release_lease(scope).await
}
/// Tell other replicas an unlocked account still needs a session. Call
/// after unlocking, or the next replica's lock attempt loses the race.
- pub async fn announce_released(&self, account: Uuid) -> LinkedResult<()> {
+ pub async fn announce_released(&self, account: Uuid) -> ExternalChatResult<()> {
self.repository.announce_released(account).await
}
/// Lifecycle of an account, for discovery and stop decisions.
- pub async fn account_state(&self, account: Uuid) -> LinkedResult<Option<AccountState>> {
+ pub async fn account_state(&self, account: Uuid) -> ExternalChatResult<Option<AccountState>> {
self.repository.account_state(account).await
}
/// Accounts that need a replica (connected, or pairing with a live
/// transaction) and whose advisory lock nobody holds, least recently updated first.
- pub async fn discoverable_accounts(&self, limit: i64) -> LinkedResult<Vec<Uuid>> {
+ pub async fn discoverable_accounts(&self, limit: i64) -> ExternalChatResult<Vec<Uuid>> {
self.repository
.discoverable_accounts(limit.clamp(1, 1000))
.await
}
/// Fleet-wide counts for health checks and monitors.
- pub async fn fleet_health(&self) -> LinkedResult<FleetHealth> {
+ pub async fn fleet_health(&self) -> ExternalChatResult<FleetHealth> {
self.repository.fleet_health().await
}
/// Resolve a `whatsapp_pairing_requested` payload. Terminal states mean the
/// replica should stop pairing (and log out a cancelled device).
- pub async fn pairing_work(&self, pairing: Uuid) -> LinkedResult<Option<PairingWork>> {
+ pub async fn pairing_work(&self, pairing: Uuid) -> ExternalChatResult<Option<PairingWork>> {
self.repository.pairing_work(pairing).await
}
/// The account's live pairing transaction, checked after taking a lease.
- pub async fn live_pairing(&self, scope: SessionScope) -> LinkedResult<Option<PairingWork>> {
+ pub async fn live_pairing(
+ &self,
+ scope: SessionScope,
+ ) -> ExternalChatResult<Option<PairingWork>> {
self.repository.live_pairing(scope).await
}
@@ -98,7 +102,7 @@ impl SessionService {
scope: SessionScope,
pairing: Uuid,
update: PairingUpdate,
- ) -> LinkedResult<PairingWork> {
+ ) -> ExternalChatResult<PairingWork> {
let (name, valid) = match &update {
PairingUpdate::Qr(code) => ("qr", !code.is_empty() && code.len() <= 4096),
PairingUpdate::PairCode(code) => ("pair_code", !code.is_empty() && code.len() <= 64),
@@ -115,7 +119,7 @@ impl SessionService {
};
tracing::Span::current().record("update", name);
if !valid {
- return Err(LinkedError::Invalid("invalid pairing update").into());
+ return Err(ExternalChatError::Invalid("invalid pairing update").into());
}
self.repository
.update_pairing(scope, pairing, &update)
@@ -127,7 +131,7 @@ impl SessionService {
&self,
scope: SessionScope,
pairing: Uuid,
- ) -> LinkedResult<Option<serde_json::Value>> {
+ ) -> ExternalChatResult<Option<serde_json::Value>> {
self.repository.take_passkey_response(scope, pairing).await
}
@@ -138,7 +142,7 @@ impl SessionService {
&self,
scope: SessionScope,
limit: i64,
- ) -> LinkedResult<Vec<MediaImport>> {
+ ) -> ExternalChatResult<Vec<MediaImport>> {
let claimed = self
.repository
.claim_media_imports(scope, limit.clamp(1, 16))
@@ -155,9 +159,9 @@ impl SessionService {
scope: SessionScope,
attachment: &AttachmentRef,
bytes: &[u8],
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
if bytes.is_empty() || bytes.len() > MEDIA_LIMIT_BYTES {
- return Err(LinkedError::Invalid("attachment is empty or too large").into());
+ return Err(ExternalChatError::Invalid("attachment is empty or too large").into());
}
self.repository
.complete_media_import(scope, attachment, bytes)
@@ -171,7 +175,7 @@ impl SessionService {
scope: SessionScope,
attachment: &AttachmentRef,
failure: MediaFailure,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
self.repository
.fail_media_import(scope, attachment, failure)
.await
@@ -183,14 +187,14 @@ impl SessionService {
&self,
scope: SessionScope,
send: Uuid,
- ) -> LinkedResult<OutboundMedia> {
+ ) -> ExternalChatResult<OutboundMedia> {
self.repository.outbound_media(scope, send).await
}
/// WhatsApp logged the device out or rejected its credentials. Consent ends;
/// the owner must reconnect explicitly.
#[tracing::instrument(name = "whatsapp.session.logged_out", skip_all, err, fields(account_id = %scope.account_id))]
- pub async fn logged_out(&self, scope: SessionScope) -> LinkedResult<()> {
+ pub async fn logged_out(&self, scope: SessionScope) -> ExternalChatResult<()> {
self.repository.logged_out(scope).await
}
@@ -202,13 +206,13 @@ impl SessionService {
scope: SessionScope,
groups: &[GroupInfo],
complete: bool,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
if groups.len() > 10_000
|| groups.iter().any(|g| {
!is_group_jid(&g.jid) || g.name.len() > 1024 || g.participants.len() > 5_000
})
{
- return Err(LinkedError::Invalid("invalid group metadata").into());
+ return Err(ExternalChatError::Invalid("invalid group metadata").into());
}
self.repository.update_groups(scope, groups, complete).await
}
@@ -216,7 +220,11 @@ impl SessionService {
/// Report the current metadata of one group after a live group event (joined,
/// renamed, participants changed). Signals pages only when something changed.
#[tracing::instrument(name = "whatsapp.session.update_group", skip_all, err, fields(account_id = %scope.account_id))]
- pub async fn update_group(&self, scope: SessionScope, group: &GroupInfo) -> LinkedResult<()> {
+ pub async fn update_group(
+ &self,
+ scope: SessionScope,
+ group: &GroupInfo,
+ ) -> ExternalChatResult<()> {
self.update_groups(scope, std::slice::from_ref(group), false)
.await
}
@@ -229,15 +237,15 @@ impl SessionService {
scope: SessionScope,
group: &str,
observed_at: chrono::DateTime<chrono::Utc>,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
if !is_group_jid(group) {
- return Err(LinkedError::Invalid("invalid group").into());
+ return Err(ExternalChatError::Invalid("invalid group").into());
}
self.repository.group_left(scope, group, observed_at).await
}
/// Groups to keep: events from any other group are dropped before storage.
- pub async fn observed_groups(&self, scope: SessionScope) -> LinkedResult<Vec<String>> {
+ pub async fn observed_groups(&self, scope: SessionScope) -> ExternalChatResult<Vec<String>> {
self.repository.observed_groups(scope).await
}
@@ -250,7 +258,7 @@ impl SessionService {
&self,
scope: SessionScope,
chats: &[ChatUpdate],
- ) -> LinkedResult<bool> {
+ ) -> ExternalChatResult<bool> {
let valid: Vec<ChatUpdate> = chats
.iter()
.filter(|chat| match chat.kind {
@@ -277,7 +285,10 @@ impl SessionService {
/// The owner's groups still importing earlier messages from the phone.
#[tracing::instrument(name = "whatsapp.session.backfill_work", skip_all, err, fields(account_id = %scope.account_id, count))]
- pub async fn backfill_work(&self, scope: SessionScope) -> LinkedResult<Vec<BackfillWork>> {
+ pub async fn backfill_work(
+ &self,
+ scope: SessionScope,
+ ) -> ExternalChatResult<Vec<BackfillWork>> {
let work = self.repository.backfill_work(scope).await?;
tracing::Span::current().record("count", work.len());
Ok(work)
@@ -289,7 +300,7 @@ impl SessionService {
&self,
scope: SessionScope,
group: Uuid,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
self.repository
.backfill_waiting_for_message(scope, group)
.await
@@ -297,13 +308,21 @@ impl SessionService {
/// A page of earlier messages was requested from the phone just now.
#[tracing::instrument(name = "whatsapp.session.backfill_requested", skip_all, err, fields(account_id = %scope.account_id, chat_id = %group))]
- pub async fn backfill_requested(&self, scope: SessionScope, group: Uuid) -> LinkedResult<()> {
+ pub async fn backfill_requested(
+ &self,
+ scope: SessionScope,
+ group: Uuid,
+ ) -> ExternalChatResult<()> {
self.repository.backfill_requested(scope, group).await
}
/// The phone did not answer; the import waits for it.
#[tracing::instrument(name = "whatsapp.session.backfill_unanswered", skip_all, err, fields(account_id = %scope.account_id, chat_id = %group))]
- pub async fn backfill_unanswered(&self, scope: SessionScope, group: Uuid) -> LinkedResult<()> {
+ pub async fn backfill_unanswered(
+ &self,
+ scope: SessionScope,
+ group: Uuid,
+ ) -> ExternalChatResult<()> {
self.repository.backfill_unanswered(scope, group).await
}
@@ -314,9 +333,9 @@ impl SessionService {
&self,
scope: SessionScope,
page: &HistoryPage,
- ) -> LinkedResult<Vec<(Uuid, BackfillState)>> {
+ ) -> ExternalChatResult<Vec<(Uuid, BackfillState)>> {
if !is_group_jid(&page.chat_jid) && !is_person_jid(&page.chat_jid) {
- return Err(LinkedError::Invalid("a history page names no WhatsApp chat").into());
+ return Err(ExternalChatError::Invalid("a history page names no WhatsApp chat").into());
}
self.repository.backfill_page(scope, page).await
}
@@ -332,7 +351,7 @@ impl SessionService {
&self,
scope: SessionScope,
event: &InboundEvent,
- ) -> LinkedResult<IngestOutcome> {
+ ) -> ExternalChatResult<IngestOutcome> {
let result = match validate_event(event) {
Ok(()) => self.repository.ingest(scope, event).await,
Err(error) => Err(error),
@@ -353,7 +372,7 @@ impl SessionService {
&self,
scope: SessionScope,
event: &InboundEvent,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
validate_event(event)?;
self.repository.enqueue_inbound(scope, event).await
}
@@ -361,7 +380,7 @@ impl SessionService {
/// Ingest enqueued events and retry deferred ones whose time has come.
/// Returns how many applied. Call after `enqueue_inbound` and periodically.
#[tracing::instrument(name = "whatsapp.session.replay_deferred", skip_all, err, fields(account_id = %scope.account_id, due, applied))]
- pub async fn replay_deferred(&self, scope: SessionScope) -> LinkedResult<usize> {
+ pub async fn replay_deferred(&self, scope: SessionScope) -> ExternalChatResult<usize> {
let due = self.repository.due_events(scope, 64).await?;
tracing::Span::current().record("due", due.len());
let mut applied = 0;
@@ -369,7 +388,7 @@ impl SessionService {
match self.repository.ingest(scope, event).await {
Ok(IngestOutcome::Applied) => applied += 1,
Ok(_) => {}
- Err(error) if matches!(error.current_context(), LinkedError::Fenced) => {
+ Err(error) if matches!(error.current_context(), ExternalChatError::Fenced) => {
return Err(error);
}
// One bad event never blocks the others; it stays deferred.
@@ -391,7 +410,7 @@ impl SessionService {
&self,
scope: SessionScope,
limit: i64,
- ) -> LinkedResult<Vec<ClaimedSend>> {
+ ) -> ExternalChatResult<Vec<ClaimedSend>> {
let claimed = self
.repository
.claim_sends(scope, limit.clamp(1, 64))
@@ -408,9 +427,9 @@ impl SessionService {
scope: SessionScope,
send: Uuid,
source_id: &str,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
if source_id.is_empty() || source_id.len() > 256 {
- return Err(LinkedError::Invalid("invalid message id").into());
+ return Err(ExternalChatError::Invalid("invalid message id").into());
}
self.repository.begin_dispatch(scope, send, source_id).await
}
@@ -422,7 +441,7 @@ impl SessionService {
scope: SessionScope,
send: Uuid,
outcome: SendOutcome,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let name = match &outcome {
SendOutcome::Accepted { .. } => "accepted",
SendOutcome::Rejected { .. } => "rejected",
@@ -435,17 +454,17 @@ impl SessionService {
}
}
-pub(crate) fn validate_event(event: &InboundEvent) -> LinkedResult<()> {
+pub(crate) fn validate_event(event: &InboundEvent) -> ExternalChatResult<()> {
let direct = is_person_jid(&event.group_jid);
if !is_group_jid(&event.group_jid) && !direct {
- return Err(LinkedError::Invalid("invalid chat").into());
+ return Err(ExternalChatError::Invalid("invalid chat").into());
}
if event
.chat_alternate_jid
.as_deref()
.is_some_and(|alternate| !direct || !is_person_jid(alternate))
{
- return Err(LinkedError::Invalid("invalid chat").into());
+ return Err(ExternalChatError::Invalid("invalid chat").into());
}
match &event.kind {
InboundKind::Left => Ok(()),
@@ -458,7 +477,7 @@ pub(crate) fn validate_event(event: &InboundEvent) -> LinkedResult<()> {
.any(|id| id.is_empty() || id.len() > 256)
|| !is_person_jid(&receipt.participant_jid)
{
- return Err(LinkedError::Invalid("invalid receipt").into());
+ return Err(ExternalChatError::Invalid("invalid receipt").into());
}
Ok(())
}
@@ -484,7 +503,7 @@ pub(crate) fn validate_event(event: &InboundEvent) -> LinkedResult<()> {
|| message.revision < 0
|| message.media.len() > 10
{
- return Err(LinkedError::Invalid("invalid message").into());
+ return Err(ExternalChatError::Invalid("invalid message").into());
}
let needs_target = matches!(
message.kind,
@@ -494,7 +513,9 @@ pub(crate) fn validate_event(event: &InboundEvent) -> LinkedResult<()> {
| MessageKind::PollVote
);
if needs_target && message.target.is_none() {
- return Err(LinkedError::Invalid("message operation without a target").into());
+ return Err(
+ ExternalChatError::Invalid("message operation without a target").into(),
+ );
}
let rich_fits = match (message.kind, &message.rich) {
(MessageKind::System, Some(RichContent::System(_)))
@@ -512,7 +533,7 @@ pub(crate) fn validate_event(event: &InboundEvent) -> LinkedResult<()> {
(_, rich) => rich.is_none(),
};
if !rich_fits || message.mentions.len() > 256 {
- return Err(LinkedError::Invalid("invalid message content").into());
+ return Err(ExternalChatError::Invalid("invalid message content").into());
}
// Only the verified author edits or deletes. Admin deletions need
// separate role evidence and are not imported.
@@ -522,7 +543,7 @@ pub(crate) fn validate_event(event: &InboundEvent) -> LinkedResult<()> {
&& sender.alternate_jid.as_deref() != Some(target.sender_jid.as_str())
})
{
- return Err(LinkedError::Forbidden.into());
+ return Err(ExternalChatError::Forbidden.into());
}
Ok(())
}
diff --git a/crates/whatsapp_linked/src/domain/session/test.rs b/crates/whatsapp_linked/src/domain/session/test.rs
index 022a399ef2..a74f9d7b12 100644
--- a/crates/whatsapp_linked/src/domain/session/test.rs
+++ b/crates/whatsapp_linked/src/domain/session/test.rs
@@ -47,7 +47,7 @@ fn only_the_verified_author_edits_or_deletes() {
))
.unwrap_err()
.current_context(),
- LinkedError::Forbidden
+ ExternalChatError::Forbidden
));
// Anyone may react to anyone's message.
validate_event(&message(
@@ -87,13 +87,15 @@ fn system_line(sender: &str) -> InboundEvent {
let mut event = message(MessageKind::System, sender, None);
if let InboundKind::Message(m) = &mut event.kind {
m.sender.alternate_jid = None;
- m.rich = Some(RichContent::System(crate::domain::rich::SystemLine {
- action: crate::domain::rich::SystemAction::SubjectChanged,
- participants: vec![],
- value: Some("Launch".into()),
- call: None,
- by_whatsapp: true,
- }));
+ m.rich = Some(RichContent::System(
+ external_chat::domain::rich::SystemLine {
+ action: external_chat::domain::rich::SystemAction::SubjectChanged,
+ participants: vec![],
+ value: Some("Launch".into()),
+ call: None,
+ by_whatsapp: true,
+ },
+ ));
}
event
}
diff --git a/crates/whatsapp_linked/src/inbound/router.rs b/crates/whatsapp_linked/src/inbound/router.rs
index 04e2700e7f..b99fa7d641 100644
--- a/crates/whatsapp_linked/src/inbound/router.rs
+++ b/crates/whatsapp_linked/src/inbound/router.rs
@@ -1,104 +1,46 @@
-//! Thin HTTP adapter: authenticated owners and external chat receipts flow to
-//! [`LinkedService`]. DSS mounts [`external_chats_router`] at `/external-chats`
-//! (any provider's added chats) and [`whatsapp_router`] at `/whatsapp` (the
-//! WhatsApp account, pairing and chat list).
+//! Thin HTTP adapter for the caller's WhatsApp account, pairing and chat
+//! list: authenticated owners flow to [`WhatsAppService`]. DSS mounts
+//! [`whatsapp_router`] at `/whatsapp`; added chats are served by
+//! `external_chat`'s `/external-chats` routes.
-use crate::domain::{models::*, service::LinkedService, timeline::*};
-use axum::extract::Query;
+use crate::domain::{models::*, service::WhatsAppService};
use axum::{
Json, Router,
- extract::{DefaultBodyLimit, FromRef, Multipart, Path, State},
- http::{HeaderValue, StatusCode, header},
- response::{IntoResponse, Response},
+ extract::{FromRef, Path, State},
+ http::StatusCode,
routing::{get, post},
};
-use entity_access::domain::models::{OwnerAccessLevel, ViewAccessLevel};
-use entity_access::{
- domain::ports::EntityAccessService, inbound::axum_extractors::ExternalChatAccessLevelExtractor,
-};
+use external_chat::inbound::router::{ApiError, authority, no_store};
use macro_authorization::{
- CredentialOrigin, MacroAuthorizationExtractor, MacroAuthorizationService,
- MacroAuthorizationState, MacroUserAuthentication, UserOnly,
+ MacroAuthorizationExtractor, MacroAuthorizationService, MacroAuthorizationState, UserOnly,
};
-use models_permissions::share_permission::{SharePermissionV2, UpdateSharePermissionRequestV2};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use uuid::Uuid;
/// Capabilities supplied by DSS.
-pub struct LinkedRouterState<Eas, Auth> {
+pub struct WhatsAppRouterState<Auth> {
/// Use cases.
- pub service: Arc<LinkedService>,
- /// Standard group receipt provider.
- pub entity_access_service: Arc<Eas>,
+ pub service: Arc<WhatsAppService>,
/// Standard authentication provider.
pub authorization_state: MacroAuthorizationState<Auth>,
}
-impl<Eas, Auth> Clone for LinkedRouterState<Eas, Auth> {
+impl<Auth> Clone for WhatsAppRouterState<Auth> {
fn clone(&self) -> Self {
Self {
service: self.service.clone(),
- entity_access_service: self.entity_access_service.clone(),
authorization_state: self.authorization_state.clone(),
}
}
}
-impl<Eas, Auth> FromRef<LinkedRouterState<Eas, Auth>> for Arc<Eas> {
- fn from_ref(state: &LinkedRouterState<Eas, Auth>) -> Self {
- state.entity_access_service.clone()
- }
-}
-
-impl<Eas, Auth> FromRef<LinkedRouterState<Eas, Auth>> for MacroAuthorizationState<Auth> {
- fn from_ref(state: &LinkedRouterState<Eas, Auth>) -> Self {
+impl<Auth> FromRef<WhatsAppRouterState<Auth>> for MacroAuthorizationState<Auth> {
+ fn from_ref(state: &WhatsAppRouterState<Auth>) -> Self {
state.authorization_state.clone()
}
}
-/// Domain errors as `{code, message}` JSON.
-pub struct ApiError(LinkedError);
-
-impl From<rootcause::Report<LinkedError>> for ApiError {
- fn from(error: rootcause::Report<LinkedError>) -> Self {
- if matches!(
- error.current_context(),
- LinkedError::Unavailable | LinkedError::Fenced | LinkedError::Internal
- ) {
- tracing::error!(error = ?error, "WhatsApp linked request failed");
- }
- Self(error.current_context().clone())
- }
-}
-
-impl IntoResponse for ApiError {
- fn into_response(self) -> Response {
- let status = match self.0 {
- LinkedError::Invalid(_) | LinkedError::CannotAdd(_) => StatusCode::BAD_REQUEST,
- LinkedError::Forbidden => StatusCode::FORBIDDEN,
- LinkedError::RateLimited => StatusCode::TOO_MANY_REQUESTS,
- LinkedError::NotFound => StatusCode::NOT_FOUND,
- LinkedError::Conflict | LinkedError::Disconnected | LinkedError::SendExpired => {
- StatusCode::CONFLICT
- }
- LinkedError::Unavailable | LinkedError::Backlogged => StatusCode::SERVICE_UNAVAILABLE,
- LinkedError::Fenced | LinkedError::Internal => StatusCode::INTERNAL_SERVER_ERROR,
- };
- let mut response = (
- status,
- Json(serde_json::json!({ "code": self.0.code(), "message": self.0.to_string() })),
- )
- .into_response();
- if matches!(self.0, LinkedError::RateLimited) {
- response
- .headers_mut()
- .insert(header::RETRY_AFTER, HeaderValue::from_static("1"));
- }
- response
- }
-}
-
/// Explicit pairing intent; replacement is a separate deliberate action.
#[derive(Deserialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
@@ -118,100 +60,28 @@ pub struct DisconnectRequest {
pub authorization_generation: i64,
}
-fn authority(user: &MacroUserAuthentication) -> SendAuthority {
- match user.origin {
- CredentialOrigin::MacroApiJwt | CredentialOrigin::UserApiKey => SendAuthority::UserApiToken,
- CredentialOrigin::Session if user.agent_id.is_none() => SendAuthority::Human,
- CredentialOrigin::Session
- | CredentialOrigin::Bot
- | CredentialOrigin::Harness
- | CredentialOrigin::Internal => SendAuthority::Automation,
- }
-}
-
-/// Pairing codes and send drafts are never cached.
-fn no_store<S: Clone + Send + Sync + 'static>(router: Router<S>) -> Router<S> {
- router.layer(axum::middleware::map_response(
- |mut response: Response| async move {
- let headers = response.headers_mut();
- headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
- headers.insert(
- header::REFERRER_POLICY,
- HeaderValue::from_static("no-referrer"),
- );
- response
- },
- ))
-}
-
-/// Added chats of any provider, under `/external-chats`.
-pub fn external_chats_router<Eas, Auth, S>(state: LinkedRouterState<Eas, Auth>) -> Router<S>
-where
- Eas: EntityAccessService,
- Auth: MacroAuthorizationService,
- S: Send + Sync + 'static,
-{
- no_store(
- Router::new()
- .route("/", get(list::<Eas, Auth>).post(add::<Eas, Auth>))
- .route(
- "/{chat_id}",
- get(chat::<Eas, Auth>).delete(remove::<Eas, Auth>),
- )
- .route("/{chat_id}/messages", get(messages::<Eas, Auth>))
- .route(
- "/{chat_id}/messages/{message_id}",
- get(message::<Eas, Auth>),
- )
- .route(
- "/{chat_id}/messages/attachments",
- post(message_attachments::<Eas, Auth>),
- )
- .route("/{chat_id}/read", post(mark_read::<Eas, Auth>))
- .route(
- "/{chat_id}/permissions",
- get(permissions::<Eas, Auth>).patch(update_permissions::<Eas, Auth>),
- )
- .route(
- "/{chat_id}/sends",
- get(open_sends::<Eas, Auth>).post(send::<Eas, Auth>),
- )
- .route(
- "/{chat_id}/attachments",
- post(upload::<Eas, Auth>)
- .layer(DefaultBodyLimit::max(MEDIA_LIMIT_BYTES + 64 * 1024)),
- )
- .route("/{chat_id}/delivery", post(delivery::<Eas, Auth>)),
- )
- .with_state(state)
-}
-
/// The caller's WhatsApp account, pairing and chat list, under `/whatsapp`.
-pub fn whatsapp_router<Eas, Auth, S>(state: LinkedRouterState<Eas, Auth>) -> Router<S>
+pub fn whatsapp_router<Auth, S>(state: WhatsAppRouterState<Auth>) -> Router<S>
where
- Eas: EntityAccessService,
Auth: MacroAuthorizationService,
S: Send + Sync + 'static,
{
no_store(
Router::new()
- .route("/account", get(account::<Eas, Auth>))
- .route("/connect", post(connect::<Eas, Auth>))
- .route("/disconnect", post(disconnect::<Eas, Auth>))
- .route(
- "/pairing/{id}",
- get(pairing::<Eas, Auth>).delete(cancel::<Eas, Auth>),
- )
- .route("/pairing/{id}/passkey", post(passkey::<Eas, Auth>))
- .route("/chats", get(chats::<Eas, Auth>)),
+ .route("/account", get(account::<Auth>))
+ .route("/connect", post(connect::<Auth>))
+ .route("/disconnect", post(disconnect::<Auth>))
+ .route("/pairing/{id}", get(pairing::<Auth>).delete(cancel::<Auth>))
+ .route("/pairing/{id}/passkey", post(passkey::<Auth>))
+ .route("/chats", get(chats::<Auth>)),
)
.with_state(state)
}
/// The caller's own account.
#[utoipa::path(get, path = "/whatsapp/account", operation_id = "whatsapp_account", responses((status = 200, body = AccountView)))]
-pub async fn account<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
+pub async fn account<Auth: MacroAuthorizationService>(
+ State(state): State<WhatsAppRouterState<Auth>>,
auth: MacroAuthorizationExtractor<Auth, UserOnly>,
) -> Result<Json<AccountView>, ApiError> {
Ok(Json(AccountView {
@@ -224,8 +94,8 @@ pub async fn account<Eas, Auth: MacroAuthorizationService>(
/// Start or resume the caller's pairing transaction.
#[utoipa::path(post, path = "/whatsapp/connect", operation_id = "whatsapp_connect", request_body = Connect, responses((status = 200, body = Pairing)))]
-pub async fn connect<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
+pub async fn connect<Auth: MacroAuthorizationService>(
+ State(state): State<WhatsAppRouterState<Auth>>,
auth: MacroAuthorizationExtractor<Auth, UserOnly>,
Json(body): Json<Connect>,
) -> Result<Json<Pairing>, ApiError> {
@@ -243,8 +113,8 @@ pub async fn connect<Eas, Auth: MacroAuthorizationService>(
/// Poll the caller's pairing transaction, including its current QR code.
#[utoipa::path(get, path = "/whatsapp/pairing/{id}", operation_id = "whatsapp_pairing", params(("id" = Uuid, Path)), responses((status = 200, body = PairState)))]
-pub async fn pairing<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
+pub async fn pairing<Auth: MacroAuthorizationService>(
+ State(state): State<WhatsAppRouterState<Auth>>,
auth: MacroAuthorizationExtractor<Auth, UserOnly>,
Path(id): Path<Uuid>,
) -> Result<Json<PairState>, ApiError> {
@@ -262,8 +132,8 @@ pub async fn pairing<Eas, Auth: MacroAuthorizationService>(
/// Cancel the caller's pairing transaction.
#[utoipa::path(delete, path = "/whatsapp/pairing/{id}", operation_id = "whatsapp_cancel_pairing", params(("id" = Uuid, Path)), responses((status = 204)))]
-pub async fn cancel<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
+pub async fn cancel<Auth: MacroAuthorizationService>(
+ State(state): State<WhatsAppRouterState<Auth>>,
auth: MacroAuthorizationExtractor<Auth, UserOnly>,
Path(id): Path<Uuid>,
) -> Result<StatusCode, ApiError> {
@@ -280,8 +150,8 @@ pub async fn cancel<Eas, Auth: MacroAuthorizationService>(
/// Forward the owner's WebAuthn assertion to the pairing replica.
#[utoipa::path(post, path = "/whatsapp/pairing/{id}/passkey", operation_id = "whatsapp_passkey", params(("id" = Uuid, Path)), request_body = Object, responses((status = 204)))]
-pub async fn passkey<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
+pub async fn passkey<Auth: MacroAuthorizationService>(
+ State(state): State<WhatsAppRouterState<Auth>>,
auth: MacroAuthorizationExtractor<Auth, UserOnly>,
Path(id): Path<Uuid>,
Json(body): Json<serde_json::Value>,
@@ -298,62 +168,10 @@ pub async fn passkey<Eas, Auth: MacroAuthorizationService>(
Ok(StatusCode::NO_CONTENT)
}
-/// Multipart body of the attachments route.
-#[derive(utoipa::ToSchema)]
-#[schema(as = ExternalChatUploadRequest)]
-pub struct UploadRequest {
- /// One supported image, audio, video, PDF or text file.
- #[schema(value_type = String, format = Binary)]
- pub file: Vec<u8>,
-}
-
-/// Stage one attachment for a later media send in this chat.
-#[utoipa::path(post, path = "/external-chats/{chat_id}/attachments", operation_id = "external_chat_upload", params(("chat_id" = Uuid, Path)), request_body(content = UploadRequest, content_type = "multipart/form-data"), responses((status = 200, body = UploadedAttachment)))]
-pub async fn upload<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- auth: MacroAuthorizationExtractor<Auth, UserOnly>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
- mut input: Multipart,
-) -> Result<Json<UploadedAttachment>, ApiError> {
- let invalid = |message| ApiError(LinkedError::Invalid(message));
- let field = input
- .next_field()
- .await
- .map_err(|_| invalid("invalid attachment"))?
- .ok_or_else(|| invalid("choose a file"))?;
- if field.name() != Some("file") {
- return Err(invalid("one file field is required"));
- }
- let filename = field
- .file_name()
- .ok_or_else(|| invalid("a filename is required"))?
- .to_owned();
- let mime = field
- .content_type()
- .unwrap_or("application/octet-stream")
- .to_owned();
- let bytes = field
- .bytes()
- .await
- .map_err(|_| invalid("attachment exceeds the size limit"))?;
- Ok(Json(
- state
- .service
- .upload_attachment(
- &access.entity_access_receipt,
- &filename,
- &mime,
- &bytes,
- authority(&auth.authorization),
- )
- .await?,
- ))
-}
-
/// End the caller's observation consent.
#[utoipa::path(post, path = "/whatsapp/disconnect", operation_id = "whatsapp_disconnect", request_body = DisconnectRequest, responses((status = 200, body = Disconnect)))]
-pub async fn disconnect<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
+pub async fn disconnect<Auth: MacroAuthorizationService>(
+ State(state): State<WhatsAppRouterState<Auth>>,
auth: MacroAuthorizationExtractor<Auth, UserOnly>,
Json(body): Json<DisconnectRequest>,
) -> Result<Json<Disconnect>, ApiError> {
@@ -372,8 +190,8 @@ pub async fn disconnect<Eas, Auth: MacroAuthorizationService>(
/// The caller's WhatsApp chat list (groups and direct chats), newest
/// activity first, with what can be added to Macro.
#[utoipa::path(get, path = "/whatsapp/chats", operation_id = "whatsapp_chats", responses((status = 200, body = ChatList)))]
-pub async fn chats<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
+pub async fn chats<Auth: MacroAuthorizationService>(
+ State(state): State<WhatsAppRouterState<Auth>>,
auth: MacroAuthorizationExtractor<Auth, UserOnly>,
) -> Result<Json<ChatList>, ApiError> {
Ok(Json(
@@ -384,230 +202,5 @@ pub async fn chats<Eas, Auth: MacroAuthorizationService>(
))
}
-/// Add one of the caller's provider chats as their own external chat.
-#[utoipa::path(post, path = "/external-chats", operation_id = "external_chat_add", request_body = AddChat, responses((status = 200, body = AddedChat)))]
-pub async fn add<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- auth: MacroAuthorizationExtractor<Auth, UserOnly>,
- Json(body): Json<AddChat>,
-) -> Result<Json<AddedChat>, ApiError> {
- let binding = state
- .service
- .adopt(
- &auth.authorization.macro_user_id,
- &body,
- authority(&auth.authorization),
- )
- .await?;
- Ok(Json(AddedChat {
- chat_id: binding.id,
- }))
-}
-
-/// An external chat the caller can read.
-#[utoipa::path(get, path = "/external-chats/{chat_id}", operation_id = "external_chat", params(("chat_id" = Uuid, Path)), responses((status = 200, body = ExternalChatDetail)))]
-pub async fn chat<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
-) -> Result<Json<ExternalChatDetail>, ApiError> {
- Ok(Json(
- state
- .service
- .group_detail(&access.entity_access_receipt)
- .await?,
- ))
-}
-
-/// Send to the chat from the caller's own provider account (one request per second per user).
-#[utoipa::path(post, path = "/external-chats/{chat_id}/sends", operation_id = "external_chat_send", params(("chat_id" = Uuid, Path)), request_body = SendReply, responses((status = 200, body = SendProgress), (status = 429, description = "One send request per second per user; Retry-After: 1")))]
-pub async fn send<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- auth: MacroAuthorizationExtractor<Auth, UserOnly>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
- Json(body): Json<SendReply>,
-) -> Result<Json<SendProgress>, ApiError> {
- Ok(Json(
- state
- .service
- .send_reply(
- &access.entity_access_receipt,
- &body,
- authority(&auth.authorization),
- )
- .await?,
- ))
-}
-
-/// The caller's send progress for intents or messages.
-#[utoipa::path(post, path = "/external-chats/{chat_id}/delivery", operation_id = "external_chat_delivery", params(("chat_id" = Uuid, Path)), request_body = Vec<Uuid>, responses((status = 200, body = Vec<SendProgress>)))]
-pub async fn delivery<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
- Json(ids): Json<Vec<Uuid>>,
-) -> Result<Json<Vec<SendProgress>>, ApiError> {
- Ok(Json(
- state
- .service
- .delivery(&access.entity_access_receipt, &ids)
- .await?,
- ))
-}
-
-/// Attachment states of messages, by message id (at most 100).
-#[utoipa::path(post, path = "/external-chats/{chat_id}/messages/attachments", operation_id = "external_chat_message_attachments", params(("chat_id" = Uuid, Path)), request_body = Vec<Uuid>, responses((status = 200, body = Vec<MessageAttachments>)))]
-pub async fn message_attachments<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
- Json(ids): Json<Vec<Uuid>>,
-) -> Result<Json<Vec<MessageAttachments>>, ApiError> {
- Ok(Json(
- state
- .service
- .message_attachments(&access.entity_access_receipt, &ids)
- .await?,
- ))
-}
-
-/// Stop syncing the chat, locally. Owner only, in their own session.
-#[utoipa::path(delete, path = "/external-chats/{chat_id}", operation_id = "external_chat_remove", params(("chat_id" = Uuid, Path)), responses((status = 204)))]
-pub async fn remove<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- auth: MacroAuthorizationExtractor<Auth, UserOnly>,
- access: ExternalChatAccessLevelExtractor<OwnerAccessLevel, Eas, Auth>,
-) -> Result<StatusCode, ApiError> {
- state
- .service
- .detach(
- &access.entity_access_receipt,
- authority(&auth.authorization),
- )
- .await?;
- Ok(StatusCode::NO_CONTENT)
-}
-
-/// List external chats the caller can view, including without a provider account.
-#[utoipa::path(get, path = "/external-chats", operation_id = "external_chats", responses((status = 200, body = Vec<ExternalChatSummary>)))]
-pub async fn list<Eas, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- auth: MacroAuthorizationExtractor<Auth, UserOnly>,
-) -> Result<Json<Vec<ExternalChatSummary>>, ApiError> {
- Ok(Json(
- state
- .service
- .adopted_groups(&auth.authorization.macro_user_id)
- .await?,
- ))
-}
-
-/// Stable timeline pagination.
-#[derive(Deserialize, utoipa::IntoParams)]
-#[serde(rename_all = "camelCase")]
-pub struct TimelineQuery {
- /// Last message id in the previous page.
- pub before: Option<Uuid>,
- /// Page size, up to [`PAGE_LIMIT`]; [`DEFAULT_PAGE`] when absent.
- pub limit: Option<i64>,
-}
-
-/// Newest-first timeline through ordinary view access.
-#[utoipa::path(get, path = "/external-chats/{chat_id}/messages", operation_id = "external_chat_messages", params(("chat_id" = Uuid, Path), TimelineQuery), responses((status = 200, body = ExternalChatMessages)))]
-pub async fn messages<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
- Query(query): Query<TimelineQuery>,
-) -> Result<Json<ExternalChatMessages>, ApiError> {
- Ok(Json(
- state
- .service
- .messages(&access.entity_access_receipt, query.before, query.limit)
- .await?,
- ))
-}
-
-/// Path of one timeline message.
-#[derive(Deserialize)]
-pub struct MessagePath {
- /// Unused here; the access extractor reads it.
- pub chat_id: Uuid,
- /// The message.
- pub message_id: Uuid,
-}
-
-/// One message of the timeline, as the timeline shows it.
-#[utoipa::path(get, path = "/external-chats/{chat_id}/messages/{message_id}", operation_id = "external_chat_message", params(("chat_id" = Uuid, Path), ("message_id" = Uuid, Path)), responses((status = 200, body = ExternalChatMessage)))]
-pub async fn message<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
- Path(path): Path<MessagePath>,
-) -> Result<Json<ExternalChatMessage>, ApiError> {
- Ok(Json(
- state
- .service
- .message(&access.entity_access_receipt, path.message_id)
- .await?,
- ))
-}
-
-/// Advance this viewer's read position.
-#[utoipa::path(post, path = "/external-chats/{chat_id}/read", operation_id = "external_chat_read", params(("chat_id" = Uuid, Path)), request_body = MarkRead, responses((status = 204)))]
-pub async fn mark_read<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
- Json(body): Json<MarkRead>,
-) -> Result<StatusCode, ApiError> {
- state
- .service
- .mark_read(&access.entity_access_receipt, body.message_id)
- .await?;
- Ok(StatusCode::NO_CONTENT)
-}
-
-/// Caller-owned pending, failed and uncertain sends.
-#[utoipa::path(get, path = "/external-chats/{chat_id}/sends", operation_id = "external_chat_sends", params(("chat_id" = Uuid, Path)), responses((status = 200, body = Vec<SendProgress>)))]
-pub async fn open_sends<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
-) -> Result<Json<Vec<SendProgress>>, ApiError> {
- Ok(Json(
- state
- .service
- .open_sends(&access.entity_access_receipt)
- .await?,
- ))
-}
-
-/// Generic Share dialog policy.
-#[utoipa::path(get, path = "/external-chats/{chat_id}/permissions", operation_id = "external_chat_permissions", params(("chat_id" = Uuid, Path)), responses((status = 200, body = SharePermissionV2)))]
-pub async fn permissions<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- access: ExternalChatAccessLevelExtractor<ViewAccessLevel, Eas, Auth>,
-) -> Result<Json<SharePermissionV2>, ApiError> {
- Ok(Json(
- state
- .service
- .permissions(&access.entity_access_receipt)
- .await?,
- ))
-}
-
-/// The owner, in their own session, updates link, team and channel sharing.
-#[utoipa::path(patch, path = "/external-chats/{chat_id}/permissions", operation_id = "external_chat_update_permissions", params(("chat_id" = Uuid, Path)), request_body = UpdateSharePermissionRequestV2, responses((status = 204)))]
-pub async fn update_permissions<Eas: EntityAccessService, Auth: MacroAuthorizationService>(
- State(state): State<LinkedRouterState<Eas, Auth>>,
- auth: MacroAuthorizationExtractor<Auth, UserOnly>,
- access: ExternalChatAccessLevelExtractor<OwnerAccessLevel, Eas, Auth>,
- Json(body): Json<UpdateSharePermissionRequestV2>,
-) -> Result<StatusCode, ApiError> {
- state
- .service
- .update_permissions(
- &access.entity_access_receipt,
- &body,
- authority(&auth.authorization),
- )
- .await?;
- Ok(StatusCode::NO_CONTENT)
-}
-
#[cfg(test)]
mod test;
diff --git a/crates/whatsapp_linked/src/inbound/router/test.rs b/crates/whatsapp_linked/src/inbound/router/test.rs
index a28b866062..73459d702f 100644
--- a/crates/whatsapp_linked/src/inbound/router/test.rs
+++ b/crates/whatsapp_linked/src/inbound/router/test.rs
@@ -1,54 +1,3 @@
use super::*;
mod http;
-
-#[tokio::test]
-async fn a_rate_limited_request_returns_429_with_retry_after() {
- let response = ApiError(LinkedError::RateLimited).into_response();
- assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
- assert_eq!(response.headers()[header::RETRY_AFTER], "1");
- let body = axum::body::to_bytes(response.into_body(), 1024)
- .await
- .unwrap();
- let body: serde_json::Value = serde_json::from_slice(&body).unwrap();
- assert_eq!(body["code"], "rate_limited");
-}
-
-#[test]
-fn verified_owner_api_credentials_remain_distinct_from_sessions_and_bots() {
- let agent = || Some(serde_json::from_value(serde_json::json!(Uuid::now_v7())).unwrap());
- for (origin, agent_id, expected) in [
- (CredentialOrigin::Session, None, SendAuthority::Human),
- (
- CredentialOrigin::Session,
- agent(),
- SendAuthority::Automation,
- ),
- (
- CredentialOrigin::MacroApiJwt,
- None,
- SendAuthority::UserApiToken,
- ),
- (
- CredentialOrigin::MacroApiJwt,
- agent(),
- SendAuthority::UserApiToken,
- ),
- (
- CredentialOrigin::UserApiKey,
- None,
- SendAuthority::UserApiToken,
- ),
- (CredentialOrigin::Bot, None, SendAuthority::Automation),
- (CredentialOrigin::Harness, None, SendAuthority::Automation),
- (CredentialOrigin::Internal, None, SendAuthority::Automation),
- ] {
- let user = MacroUserAuthentication {
- origin,
- agent_id,
- macro_user_id: "macro|[email protected]".to_string().try_into().unwrap(),
- user_context: Default::default(),
- };
- assert_eq!(authority(&user), expected, "{origin:?}");
- }
-}
diff --git a/crates/whatsapp_linked/src/inbound/router/test/http.rs b/crates/whatsapp_linked/src/inbound/router/test/http.rs
index 229ac44346..62d4dc7d53 100644
--- a/crates/whatsapp_linked/src/inbound/router/test/http.rs
+++ b/crates/whatsapp_linked/src/inbound/router/test/http.rs
@@ -4,21 +4,30 @@
use super::*;
use crate::domain::{
events::{InboundEvent, InboundKind, Message, MessageKind, PairingUpdate},
- ports::{LinkedResult, SendRateLimiter},
+ provider::WhatsAppProvider,
session::SessionService,
};
-use crate::outbound::pg::PgLinkedRepository;
+use crate::outbound::pg::PgWhatsAppRepository;
use async_trait::async_trait;
use axum::body::Body;
-use axum::http::{Method, Request};
+use axum::http::{Method, Request, header};
use entity_access::{
domain::{models::EntityType, service::EntityAccessServiceImpl},
outbound::PgAccessRepository,
};
+use external_chat::{
+ domain::{
+ models::*,
+ ports::{ExternalChatResult, SendRateLimiter},
+ service::ExternalChatService,
+ },
+ inbound::router::{ExternalChatRouterState, external_chats_router},
+ outbound::pg::PgExternalChatRepository,
+};
use macro_authorization::{
- BotActingUserClaims, BotAuthentication, BotScope, HarnessAuthentication,
+ BotActingUserClaims, BotAuthentication, BotScope, CredentialOrigin, HarnessAuthentication,
HarnessAuthorizationOwner, InternalIdentityClaims, MacroAuthorizationError,
- ValidatedCredential, ValidatedIdentity,
+ MacroUserAuthentication, ValidatedCredential, ValidatedIdentity,
};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use model_user::UserContext;
@@ -160,7 +169,10 @@ struct Unlimited;
#[async_trait]
impl SendRateLimiter for Unlimited {
- async fn check(&self, _: &macro_user_id::user_id::MacroUserIdStr<'_>) -> LinkedResult<()> {
+ async fn check(
+ &self,
+ _: &macro_user_id::user_id::MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<()> {
Ok(())
}
}
@@ -238,8 +250,13 @@ async fn every_route_enforces_its_credential_rules(pool: PgPool) {
for id in [ALICE, BOB] {
user(&pool, id).await;
}
- let repository = Arc::new(PgLinkedRepository::new(pool.clone()));
- let service = Arc::new(LinkedService::new(repository.clone(), Arc::new(Unlimited)));
+ let repository = Arc::new(PgWhatsAppRepository::new(pool.clone()));
+ let service = Arc::new(WhatsAppService::new(repository.clone()));
+ let chats = Arc::new(ExternalChatService::new(
+ Arc::new(WhatsAppProvider::new(repository.clone())),
+ Arc::new(PgExternalChatRepository::new(pool.clone())),
+ Arc::new(Unlimited),
+ ));
let session = SessionService::new(repository);
let pairing = service
.connect(&person(ALICE), false, SendAuthority::Human)
@@ -282,8 +299,8 @@ async fn every_route_enforces_its_credential_rules(pool: PgPool) {
)
.await
.unwrap();
- let binding = service
- .adopt(
+ let binding = chats
+ .add(
&person(ALICE),
&AddChat {
provider: ExternalChatProvider::WhatsApp,
@@ -339,16 +356,25 @@ async fn every_route_enforces_its_credential_rules(pool: PgPool) {
.await
.unwrap();
- let state = LinkedRouterState {
- service: service.clone(),
- entity_access_service: Arc::new(EntityAccessServiceImpl::new(PgAccessRepository::new(
- pool.clone(),
- ))),
- authorization_state: MacroAuthorizationState::new(Arc::new(Credentials)),
- };
+ let authorization_state = MacroAuthorizationState::new(Arc::new(Credentials));
let app: Router = Router::new()
- .nest("/external-chats", external_chats_router(state.clone()))
- .nest("/whatsapp", whatsapp_router(state));
+ .nest(
+ "/external-chats",
+ external_chats_router(ExternalChatRouterState {
+ service: chats,
+ entity_access_service: Arc::new(EntityAccessServiceImpl::new(
+ PgAccessRepository::new(pool.clone()),
+ )),
+ authorization_state: authorization_state.clone(),
+ }),
+ )
+ .nest(
+ "/whatsapp",
+ whatsapp_router(WhatsAppRouterState {
+ service: service.clone(),
+ authorization_state,
+ }),
+ );
let group = binding.id;
let generation = service
.account(&person(ALICE))
diff --git a/crates/whatsapp_linked/src/lib.rs b/crates/whatsapp_linked/src/lib.rs
index 6234f17529..6fcb4de01d 100644
--- a/crates/whatsapp_linked/src/lib.rs
+++ b/crates/whatsapp_linked/src/lib.rs
@@ -1,8 +1,11 @@
#![deny(missing_docs)]
-//! Personal WhatsApp accounts linked as devices: pairing, adding chats as
-//! independently shared entities, inbound ingestion and deliberate sends. DSS serves the HTTP
-//! API; the WhatsApp session service calls [`domain::session::SessionService`]
-//! in-process. The two coordinate only through Postgres rows and `NOTIFY`.
+//! Personal WhatsApp accounts linked as devices, the one `external_chat`
+//! provider: pairing, the account's chat list, adding chats, inbound
+//! ingestion and dispatching deliberate sends. DSS serves the `/whatsapp` API
+//! and composes [`domain::provider::WhatsAppProvider`] into the external chat
+//! service; the WhatsApp session service calls
+//! [`domain::session::SessionService`] in-process. The two coordinate only
+//! through Postgres rows and `NOTIFY`.
/// Models, ports and use cases.
pub mod domain;
@@ -12,5 +15,5 @@ pub mod inbound;
/// Postgres adapters.
#[cfg(feature = "outbound")]
pub mod outbound;
-/// `NOTIFY` channels and payloads shared by DSS and the session service.
+/// `NOTIFY` channels between DSS and the session service.
pub mod signals;
diff --git a/crates/whatsapp_linked/src/outbound/pg.rs b/crates/whatsapp_linked/src/outbound/pg.rs
index 4618f36712..abb9ed0e90 100644
--- a/crates/whatsapp_linked/src/outbound/pg.rs
+++ b/crates/whatsapp_linked/src/outbound/pg.rs
@@ -1,14 +1,22 @@
-//! Postgres adapter for every linked-device repository port. DSS and the
-//! session service each build one over the shared MacroDB pool.
-
-use crate::domain::{events::SessionScope, models::*, ports::LinkedResult};
+//! Postgres adapter for every WhatsApp repository port. DSS and the session
+//! service each build one over the shared MacroDB pool. It writes the
+//! generic `external_chat*` tables with `external_chat`'s helpers.
+
+use crate::domain::{events::SessionScope, models::*};
+pub(crate) use external_chat::{
+ domain::{models::*, ports::ExternalChatResult},
+ outbound::pg::{
+ ChatRow, chat_ref, corrupt, decode, failure, helper_failure, lock_chat_in, notify,
+ notify_change,
+ },
+ signals::Change,
+};
use sqlx::{PgPool, Postgres, Transaction};
use uuid::Uuid;
mod accounts;
-mod bindings;
+mod adoptions;
mod chats;
-mod effects;
mod groups;
mod ingest;
mod media;
@@ -16,98 +24,26 @@ mod sends;
mod session;
#[cfg(test)]
mod test;
-mod timeline;
use crate::signals;
/// Repository over MacroDB.
#[derive(Clone)]
-pub struct PgLinkedRepository {
+pub struct PgWhatsAppRepository {
pool: PgPool,
}
-impl PgLinkedRepository {
+impl PgWhatsAppRepository {
/// Wrap the shared pool.
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
}
-/// Map an infrastructure error, keeping its cause for logs.
-pub(crate) fn failure(
- error: impl std::error::Error + Send + Sync + 'static,
-) -> rootcause::Report<LinkedError> {
- rootcause::Report::new(error).context(LinkedError::Unavailable)
-}
-
-/// Map an `anyhow` error from another domain's transactional helper.
-pub(crate) fn helper_failure(error: impl std::fmt::Display) -> rootcause::Report<LinkedError> {
- rootcause::report!("{error:#}").context(LinkedError::Unavailable)
-}
-
-/// Stored data that breaks an invariant this crate maintains.
-pub(crate) fn corrupt(what: impl std::fmt::Display) -> rootcause::Report<LinkedError> {
- rootcause::report!("stored WhatsApp data is invalid: {what}").context(LinkedError::Internal)
-}
-
-/// Decode a JSON column written by this crate.
-pub(crate) fn decode<T: serde::de::DeserializeOwned>(value: serde_json::Value) -> LinkedResult<T> {
- serde_json::from_value(value)
- .map_err(|error| rootcause::Report::new(error).context(LinkedError::Internal))
-}
-
-pub(crate) fn account_state(value: &str) -> LinkedResult<AccountState> {
+pub(crate) fn account_state(value: &str) -> ExternalChatResult<AccountState> {
AccountState::parse(value).ok_or_else(|| corrupt(format_args!("account state {value:?}")))
}
-/// Signal `channel` when the transaction commits, carrying the current trace.
-pub(crate) async fn notify(
- tx: &mut Transaction<'_, Postgres>,
- channel: &str,
- id: Uuid,
-) -> LinkedResult<()> {
- let payload = serde_json::to_string(&signals::Signal {
- id,
- traceparent: signals::trace_context::current(),
- })
- .map_err(failure)?;
- sqlx::query!("SELECT pg_notify($1, $2)", channel, payload)
- .execute(&mut **tx)
- .await
- .map_err(failure)?;
- Ok(())
-}
-
-/// Tell DSS, when the transaction commits, that pages showing this changed.
-pub(crate) async fn notify_change(
- tx: &mut Transaction<'_, Postgres>,
- change: signals::Change,
-) -> LinkedResult<()> {
- let payload = serde_json::to_string(&signals::ChangeSignal {
- change,
- traceparent: signals::trace_context::current(),
- })
- .map_err(failure)?;
- sqlx::query!("SELECT pg_notify($1, $2)", signals::CHANGED, payload)
- .execute(&mut **tx)
- .await
- .map_err(failure)?;
- Ok(())
-}
-
-/// Serialize mutations of a group's messages, membership and sends before
-/// locking any child row. Accounts, when needed, must be locked first.
-async fn lock_group_in(tx: &mut Transaction<'_, Postgres>, group: Uuid) -> LinkedResult<()> {
- sqlx::query!(
- "SELECT id FROM external_chat WHERE id = $1 FOR UPDATE",
- group
- )
- .fetch_optional(&mut **tx)
- .await
- .map_err(failure)?;
- Ok(())
-}
-
/// The account a lease holder acts for, locked for the rest of the transaction.
pub(crate) struct Fenced {
pub id: Uuid,
@@ -121,7 +57,7 @@ pub(crate) struct Fenced {
pub(crate) async fn fence_in(
tx: &mut Transaction<'_, Postgres>,
scope: SessionScope,
-) -> LinkedResult<Fenced> {
+) -> ExternalChatResult<Fenced> {
let row = sqlx::query!(
"SELECT id, user_id, identity_jid, state, generation FROM whatsapp_linked_account WHERE id = $1 AND lease_epoch = $2 FOR SHARE",
scope.account_id,
@@ -130,7 +66,7 @@ pub(crate) async fn fence_in(
.fetch_optional(&mut **tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::Fenced)?;
+ .ok_or(ExternalChatError::Fenced)?;
Ok(Fenced {
id: row.id,
user_id: row.user_id,
@@ -145,7 +81,7 @@ pub(crate) async fn fence_in(
pub(crate) async fn fence_exclusive_in(
tx: &mut Transaction<'_, Postgres>,
scope: SessionScope,
-) -> LinkedResult<Fenced> {
+) -> ExternalChatResult<Fenced> {
let row = sqlx::query!(
"SELECT id, user_id, identity_jid, state, generation FROM whatsapp_linked_account WHERE id = $1 AND lease_epoch = $2 FOR NO KEY UPDATE",
scope.account_id,
@@ -154,7 +90,7 @@ pub(crate) async fn fence_exclusive_in(
.fetch_optional(&mut **tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::Fenced)?;
+ .ok_or(ExternalChatError::Fenced)?;
Ok(Fenced {
id: row.id,
user_id: row.user_id,
@@ -171,7 +107,7 @@ pub(crate) async fn end_consent_in(
account: Uuid,
state: AccountState,
error_code: &str,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
sqlx::query!(
"UPDATE whatsapp_linked_account SET state = $2, generation = generation + 1, updated_at = now() WHERE id = $1",
account,
diff --git a/crates/whatsapp_linked/src/outbound/pg/accounts.rs b/crates/whatsapp_linked/src/outbound/pg/accounts.rs
index 7f773f6c51..91af3a239f 100644
--- a/crates/whatsapp_linked/src/outbound/pg/accounts.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/accounts.rs
@@ -12,7 +12,7 @@ const PAIRING_SECONDS: f64 = 5.0 * 60.0;
pub(super) async fn account_by_user_in(
executor: impl sqlx::PgExecutor<'_>,
user: &str,
-) -> LinkedResult<Option<Account>> {
+) -> ExternalChatResult<Option<Account>> {
sqlx::query!(
"SELECT id, user_id, identity_jid, state, generation FROM whatsapp_linked_account WHERE user_id = $1",
user
@@ -39,7 +39,7 @@ pub(super) async fn expire_pairings_in(
user: Option<&str>,
pairing: Option<Uuid>,
account: Option<Uuid>,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
// Pairing transitions always acquire accounts before pairing rows. Global
// expiry locks all affected accounts in order; scoped callers never sweep
// other accounts while holding their own fence.
@@ -89,7 +89,7 @@ struct PairingRow {
expires_at: DateTime<Utc>,
}
-fn pairing(row: PairingRow) -> LinkedResult<Pairing> {
+fn pairing(row: PairingRow) -> ExternalChatResult<Pairing> {
Ok(Pairing {
id: row.id,
account_id: row.account_id,
@@ -100,8 +100,11 @@ fn pairing(row: PairingRow) -> LinkedResult<Pairing> {
}
#[async_trait]
-impl AccountRepository for PgLinkedRepository {
- async fn owned_account(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<Option<Account>> {
+impl AccountRepository for PgWhatsAppRepository {
+ async fn owned_account(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<Option<Account>> {
let user = user.as_ref();
account_by_user_in(&self.pool, user).await
}
@@ -110,7 +113,7 @@ impl AccountRepository for PgLinkedRepository {
&self,
user: &MacroUserIdStr<'_>,
replace: bool,
- ) -> LinkedResult<Pairing> {
+ ) -> ExternalChatResult<Pairing> {
let user = user.as_ref();
let mut tx = self.pool.begin().await.map_err(failure)?;
// Serialize per person, including before their account row exists.
@@ -158,7 +161,7 @@ impl AccountRepository for PgLinkedRepository {
(id, None)
}
Some(existing) if existing.state == "connected" && !replace => {
- return Err(LinkedError::Conflict.into());
+ return Err(ExternalChatError::Conflict.into());
}
Some(existing) => {
if replace {
@@ -202,7 +205,11 @@ impl AccountRepository for PgLinkedRepository {
pairing(row)
}
- async fn pair_state(&self, user: &MacroUserIdStr<'_>, id: Uuid) -> LinkedResult<PairState> {
+ async fn pair_state(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ id: Uuid,
+ ) -> ExternalChatResult<PairState> {
let user = user.as_ref();
let mut tx = self.pool.begin().await.map_err(failure)?;
expire_pairings_in(&mut tx, Some(user), Some(id), None).await?;
@@ -216,7 +223,7 @@ impl AccountRepository for PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
tx.commit().await.map_err(failure)?;
let state = PairingState::parse(&row.state)
.ok_or_else(|| corrupt(format_args!("state {:?}", row.state)))?;
@@ -237,7 +244,7 @@ impl AccountRepository for PgLinkedRepository {
user: &MacroUserIdStr<'_>,
id: Uuid,
response: &serde_json::Value,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let user = user.as_ref();
let mut tx = self.pool.begin().await.map_err(failure)?;
expire_pairings_in(&mut tx, Some(user), Some(id), None).await?;
@@ -254,12 +261,12 @@ impl AccountRepository for PgLinkedRepository {
.rows_affected()
.eq(&1)
.then_some(())
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
notify(&mut tx, signals::PAIRING_REQUESTED, id).await?;
tx.commit().await.map_err(failure)
}
- async fn cancel_pairing(&self, user: &MacroUserIdStr<'_>, id: Uuid) -> LinkedResult<()> {
+ async fn cancel_pairing(&self, user: &MacroUserIdStr<'_>, id: Uuid) -> ExternalChatResult<()> {
let user = user.as_ref();
let mut tx = self.pool.begin().await.map_err(failure)?;
sqlx::query!(
@@ -300,13 +307,17 @@ impl AccountRepository for PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
}
}
tx.commit().await.map_err(failure)
}
- async fn disconnect(&self, user: &MacroUserIdStr<'_>, generation: i64) -> LinkedResult<bool> {
+ async fn disconnect(
+ &self,
+ user: &MacroUserIdStr<'_>,
+ generation: i64,
+ ) -> ExternalChatResult<bool> {
let user = user.as_ref();
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = sqlx::query!(
@@ -316,7 +327,7 @@ impl AccountRepository for PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
if account.generation != generation {
// A retried disconnect of the same generation is a no-op.
if account.state == "disconnected"
@@ -324,7 +335,7 @@ impl AccountRepository for PgLinkedRepository {
{
return Ok(false);
}
- return Err(LinkedError::Conflict.into());
+ return Err(ExternalChatError::Conflict.into());
}
end_consent_in(
&mut tx,
@@ -337,4 +348,33 @@ impl AccountRepository for PgLinkedRepository {
tx.commit().await.map_err(failure)?;
Ok(true)
}
+
+ async fn account_owner(&self, account: Uuid) -> ExternalChatResult<Option<String>> {
+ sqlx::query_scalar!(
+ "SELECT user_id FROM whatsapp_linked_account WHERE id = $1",
+ account
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(failure)
+ }
+
+ async fn expire_retained(&self) -> ExternalChatResult<u64> {
+ let receipts = sqlx::query!(
+ "DELETE FROM whatsapp_participant_receipt WHERE source_at < now() - make_interval(days => $1)",
+ crate::domain::models::RECEIPT_RETENTION_DAYS
+ )
+ .execute(&self.pool)
+ .await
+ .map_err(failure)?
+ .rows_affected();
+ let pairings = sqlx::query!(
+ "DELETE FROM whatsapp_pairing WHERE consumed_at < now() - interval '1 day'"
+ )
+ .execute(&self.pool)
+ .await
+ .map_err(failure)?
+ .rows_affected();
+ Ok(receipts + pairings)
+ }
}
diff --git a/crates/whatsapp_linked/src/outbound/pg/bindings.rs b/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
similarity index 50%
rename from crates/whatsapp_linked/src/outbound/pg/bindings.rs
rename to crates/whatsapp_linked/src/outbound/pg/adoptions.rs
index fd5c6a5693..a16de5b110 100644
--- a/crates/whatsapp_linked/src/outbound/pg/bindings.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/adoptions.rs
@@ -1,50 +1,12 @@
-//! Added external chat entities and their lifecycle.
+//! Adding WhatsApp chats as external chat entities, and detaching them.
use super::*;
-use crate::domain::ports::{Adopted, AdoptionRecord, BindingRepository};
+use crate::domain::ports::{Adopted, AdoptionRecord, AdoptionRepository};
use async_trait::async_trait;
-use chrono::Utc;
use model_entity::EntityType;
use model_owner::Owner;
-pub(super) struct BindingRow {
- pub id: Uuid,
- pub provider: String,
- pub remote_id: String,
- pub kind: String,
- pub user_id: String,
- pub state: String,
- pub adopted_at: chrono::DateTime<Utc>,
-}
-
-pub(super) fn binding(row: BindingRow) -> LinkedResult<Binding> {
- Ok(Binding {
- id: row.id,
- provider: row
- .provider
- .parse::<ExternalChatProvider>()
- .map_err(|_| corrupt(format_args!("provider {:?}", row.provider)))?,
- remote_id: row.remote_id,
- kind: row
- .kind
- .parse::<ExternalChatKind>()
- .ok()
- .ok_or_else(|| corrupt(format_args!("kind {:?}", row.kind)))?,
- user_id: row.user_id,
- state: match row.state.as_str() {
- "active" => BindingState::Active,
- "detached" => BindingState::Detached,
- other => return Err(corrupt(format_args!("binding state {other:?}"))),
- },
- adopted_at: row.adopted_at,
- })
-}
-
#[async_trait]
-impl BindingRepository for PgLinkedRepository {
- async fn binding(&self, group: Uuid) -> LinkedResult<Option<Binding>> {
- sqlx::query_as!(BindingRow, "SELECT id, provider, remote_id, kind, user_id, state, adopted_at FROM external_chat WHERE id = $1", group)
- .fetch_optional(&self.pool).await.map_err(failure)?.map(binding).transpose()
- }
- async fn adopt(&self, record: AdoptionRecord<'_>) -> LinkedResult<Adopted> {
+impl AdoptionRepository for PgWhatsAppRepository {
+ async fn adopt(&self, record: AdoptionRecord<'_>) -> ExternalChatResult<Adopted> {
let AdoptionRecord {
user,
account,
@@ -54,7 +16,7 @@ impl BindingRepository for PgLinkedRepository {
account
.identity_jid
.as_deref()
- .ok_or(LinkedError::Forbidden)?;
+ .ok_or(ExternalChatError::Forbidden)?;
let mut tx = self.pool.begin().await.map_err(failure)?;
sqlx::query!(
"SELECT pg_advisory_xact_lock(hashtextextended('external-chat:whatsapp:' || $1 || ':' || $2, 0))",
@@ -65,12 +27,12 @@ impl BindingRepository for PgLinkedRepository {
.await
.map_err(failure)?;
sqlx::query_scalar!("SELECT id FROM whatsapp_linked_account WHERE id = $1 AND user_id = $2 AND generation = $3 AND state = 'connected' FOR SHARE", account.id,user,account.authorization_generation)
- .fetch_optional(&mut *tx).await.map_err(failure)?.ok_or(LinkedError::Disconnected)?;
- let existing = sqlx::query_as!(BindingRow,"SELECT id, provider, remote_id, kind, user_id, state, adopted_at FROM external_chat WHERE user_id = $1 AND provider = 'whatsapp' AND remote_id = $2 AND state <> 'detached' FOR UPDATE",user,adoption.jid)
+ .fetch_optional(&mut *tx).await.map_err(failure)?.ok_or(ExternalChatError::Disconnected)?;
+ let existing = sqlx::query_as!(ChatRow,"SELECT id, provider, remote_id, kind, user_id, state, adopted_at FROM external_chat WHERE user_id = $1 AND provider = 'whatsapp' AND remote_id = $2 AND state <> 'detached' FOR UPDATE",user,adoption.jid)
.fetch_optional(&mut *tx).await.map_err(failure)?;
let adopted = match existing {
Some(row) => Adopted {
- binding: binding(row)?,
+ chat: chat_ref(row)?,
created: false,
},
None => {
@@ -81,7 +43,7 @@ impl BindingRepository for PgLinkedRepository {
.map_err(failure)?;
// Importing starts from a message of the chat; with none known yet
// it waits for the first one.
- let row = sqlx::query_as!(BindingRow,r#"INSERT INTO external_chat (id, provider, remote_id, kind, name, user_id, state, share_permission_id, backfill_state)
+ let row = sqlx::query_as!(ChatRow,r#"INSERT INTO external_chat (id, provider, remote_id, kind, name, user_id, state, share_permission_id, backfill_state)
VALUES ($1,'whatsapp',$2,$3,$4,$5,'active',$6,
CASE WHEN EXISTS (SELECT 1 FROM whatsapp_chat WHERE account_id = $7 AND chat_jid = $2 AND anchor IS NOT NULL)
OR EXISTS (SELECT 1 FROM external_chat_message m JOIN external_chat g ON g.id = m.chat_id
@@ -112,7 +74,7 @@ impl BindingRepository for PgLinkedRepository {
.await
.map_err(failure)?;
Adopted {
- binding: binding(row)?,
+ chat: chat_ref(row)?,
created: true,
}
}
@@ -121,86 +83,31 @@ impl BindingRepository for PgLinkedRepository {
VALUES ($1,$2,$3,clock_timestamp(),true,$4,$5,true)
ON CONFLICT (chat_id,account_id) DO UPDATE SET generation = excluded.generation,consent_ended_at = NULL,
remote_member = true,can_post = excluded.can_post,observed_at = excluded.observed_at,reconciled = true"#,
- adopted.binding.id,account.id,account.authorization_generation,adoption.can_post,adoption.observed_at)
+ adopted.chat.id,account.id,account.authorization_generation,adoption.can_post,adoption.observed_at)
.execute(&mut *tx).await.map_err(failure)?;
notify(&mut tx, signals::ACCOUNT_CHANGED, account.id).await?;
tx.commit().await.map_err(failure)?;
Ok(adopted)
}
- async fn message_attachments(
- &self,
- binding: &Binding,
- messages: &[Uuid],
- ) -> LinkedResult<Vec<MessageAttachments>> {
- let rows = sqlx::query!(
- r#"SELECT m.message_id AS "message_id!", a.ordinal, a.state, a.filename, a.mime, a.size, a.file_id,
- a.width, a.height, a.duration_seconds, a.presentation
- FROM external_chat_attachment a
- JOIN external_chat_message m ON m.chat_id = a.chat_id AND m.person_id = a.person_id AND m.source_id = a.source_id
- WHERE a.chat_id = $1 AND m.message_id = ANY($2) AND a.state <> 'deleted'
- ORDER BY m.message_id, a.ordinal"#,
- binding.id,
- messages
- )
- .fetch_all(&self.pool)
- .await
- .map_err(failure)?;
- let mut result: Vec<MessageAttachments> = vec![];
- for row in rows {
- let mime = row.mime.unwrap_or_default();
- let state = match row.state.as_str() {
- "ready" => AttachmentState::Ready,
- "failed" if MediaKind::of(&mime).is_none() => AttachmentState::Unsupported,
- "failed" => AttachmentState::Unavailable,
- _ => AttachmentState::Loading,
- };
- let attachment = Attachment {
- ordinal: row.ordinal,
- state,
- file_name: row.filename.unwrap_or_default(),
- mime,
- size: row.size.unwrap_or(0).max(0) as u64,
- file_id: row.file_id,
- width: row.width.map(|value| value.max(0) as u32),
- height: row.height.map(|value| value.max(0) as u32),
- duration_seconds: row.duration_seconds.map(|value| value.max(0) as u32),
- presentation: row
- .presentation
- .as_deref()
- .and_then(MediaPresentation::parse),
- };
- match result.last_mut() {
- Some(last) if last.message_id == row.message_id => {
- last.attachments.push(attachment)
- }
- _ => result.push(MessageAttachments {
- message_id: row.message_id,
- attachments: vec![attachment],
- }),
- }
- }
- Ok(result)
- }
-
- async fn detach(&self, binding: &Binding) -> LinkedResult<()> {
+ async fn detach(&self, chat: &ChatRef) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
sqlx::query!(
"UPDATE external_chat SET state = 'detached' WHERE id = $1",
- binding.id
+ chat.id
)
.execute(&mut *tx)
.await
.map_err(failure)?;
let observers = sqlx::query_scalar!(
"UPDATE whatsapp_chat_membership SET consent_ended_at = COALESCE(consent_ended_at, now()) WHERE chat_id = $1 RETURNING account_id",
- binding.id
+ chat.id
)
.fetch_all(&mut *tx)
.await
.map_err(failure)?;
sqlx::query!(
"UPDATE whatsapp_send SET state = 'failed', error_code = 'chat_detached', claimed_epoch = NULL, updated_at = now() WHERE chat_id = $1 AND dispatch_started_at IS NULL AND state IN ('pending', 'sending')",
- binding.id
+ chat.id
)
.execute(&mut *tx)
.await
@@ -208,13 +115,7 @@ impl BindingRepository for PgLinkedRepository {
for account in observers {
notify(&mut tx, signals::ACCOUNT_CHANGED, account).await?;
}
- notify_change(
- &mut tx,
- signals::Change::Chat {
- chat_id: binding.id,
- },
- )
- .await?;
+ notify_change(&mut tx, Change::Chat { chat_id: chat.id }).await?;
tx.commit().await.map_err(failure)
}
}
diff --git a/crates/whatsapp_linked/src/outbound/pg/chats.rs b/crates/whatsapp_linked/src/outbound/pg/chats.rs
index 17717ab106..59766e1a42 100644
--- a/crates/whatsapp_linked/src/outbound/pg/chats.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/chats.rs
@@ -32,7 +32,7 @@ pub(super) async fn update_chats_in(
tx: &mut Transaction<'_, Postgres>,
account: &Fenced,
chats: &[ChatUpdate],
-) -> LinkedResult<bool> {
+) -> ExternalChatResult<bool> {
let mut merged: std::collections::BTreeMap<String, ChatUpdate> = Default::default();
for chat in chats {
let mut chat = chat.clone();
@@ -163,12 +163,12 @@ pub(super) async fn update_chats_in(
.await
.map_err(failure)?;
for chat_id in ready.into_iter().chain(renamed) {
- notify_change(tx, signals::Change::Chat { chat_id }).await?;
+ notify_change(tx, Change::Chat { chat_id }).await?;
}
if changed {
notify_change(
tx,
- signals::Change::Chats {
+ Change::Chats {
account_id: account.id,
},
)
@@ -177,7 +177,10 @@ pub(super) async fn update_chats_in(
Ok(anchored)
}
-pub(super) async fn account_chats(pool: &PgPool, account: Uuid) -> LinkedResult<Vec<StoredChat>> {
+pub(super) async fn account_chats(
+ pool: &PgPool,
+ account: Uuid,
+) -> ExternalChatResult<Vec<StoredChat>> {
sqlx::query!(
r#"SELECT chat_jid, kind, name, last_activity_at, archived, pinned FROM whatsapp_chat
WHERE account_id = $1 ORDER BY last_activity_at DESC NULLS LAST, chat_jid LIMIT 5000"#,
@@ -204,14 +207,14 @@ pub(super) async fn account_chats(pool: &PgPool, account: Uuid) -> LinkedResult<
.collect()
}
-fn backfill_state(value: &str) -> LinkedResult<BackfillState> {
+fn backfill_state(value: &str) -> ExternalChatResult<BackfillState> {
BackfillState::parse(value).ok_or_else(|| corrupt(format_args!("backfill state {value:?}")))
}
pub(super) async fn backfill_work_in(
tx: &mut Transaction<'_, Postgres>,
account: &Fenced,
-) -> LinkedResult<Vec<BackfillWork>> {
+) -> ExternalChatResult<Vec<BackfillWork>> {
// The anchor is the chat list's newest message, else the newest message
// of the chat stored in Macro that this account saw (in this group, or in
// an earlier or someone else's group for the same chat).
@@ -271,7 +274,7 @@ pub(super) async fn set_backfill_in(
group: Uuid,
state: BackfillState,
stamp_request: bool,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
let previous = sqlx::query_scalar!(
r#"UPDATE external_chat g SET backfill_state = $3,
backfill_requested_at = CASE WHEN $4 THEN now() ELSE g.backfill_requested_at END
@@ -288,7 +291,7 @@ pub(super) async fn set_backfill_in(
.await
.map_err(failure)?;
if previous.is_some_and(|previous| previous != state.as_str()) {
- notify_change(tx, signals::Change::Chat { chat_id: group }).await?;
+ notify_change(tx, Change::Chat { chat_id: group }).await?;
}
Ok(())
}
@@ -325,7 +328,7 @@ pub(super) async fn backfill_page_in(
tx: &mut Transaction<'_, Postgres>,
account: &Fenced,
page: &HistoryPage,
-) -> LinkedResult<Vec<(Uuid, BackfillState)>> {
+) -> ExternalChatResult<Vec<(Uuid, BackfillState)>> {
let rows = sqlx::query!(
r#"SELECT id, backfill_imported, backfill_cursor, adopted_at FROM external_chat
WHERE user_id = $1 AND provider = 'whatsapp' AND remote_id = $2 AND state = 'active'
@@ -364,7 +367,7 @@ pub(super) async fn backfill_page_in(
.execute(&mut **tx)
.await
.map_err(failure)?;
- notify_change(tx, signals::Change::Chat { chat_id: row.id }).await?;
+ notify_change(tx, Change::Chat { chat_id: row.id }).await?;
states.push((row.id, state));
}
Ok(states)
diff --git a/crates/whatsapp_linked/src/outbound/pg/groups.rs b/crates/whatsapp_linked/src/outbound/pg/groups.rs
index f637e3f04a..9a1dbf70b6 100644
--- a/crates/whatsapp_linked/src/outbound/pg/groups.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/groups.rs
@@ -4,6 +4,7 @@ use super::*;
use crate::domain::ports::{GroupRepository, StoredChat, VisibleAdoption};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
+use external_chat::domain::timeline::{ExternalChatPersonRef, view::People};
use macro_user_id::user_id::MacroUserIdStr;
/// The same facts, ignoring when they were read and listing-only fields.
@@ -23,7 +24,7 @@ pub(super) async fn store_groups_in(
account: &Fenced,
groups: &[GroupInfo],
complete: bool,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
let listed: Vec<String> = groups.iter().map(|g| g.jid.clone()).collect();
// Lock the entire batch, including removals, in one stable order before
// touching roster or membership rows. Observers may list groups differently.
@@ -48,7 +49,7 @@ pub(super) async fn store_groups_in(
.map_err(failure)?
.into_iter()
.map(|row| Ok((row.group_jid, decode::<GroupInfo>(row.metadata)?)))
- .collect::<LinkedResult<_>>()?;
+ .collect::<ExternalChatResult<_>>()?;
// The newest read of each group, skipping reads older than what is stored.
let mut fresh: std::collections::BTreeMap<&str, &GroupInfo> = Default::default();
for group in groups {
@@ -69,7 +70,7 @@ pub(super) async fn store_groups_in(
let metadata = fresh
.iter()
.map(|group| serde_json::to_value(group).map_err(failure))
- .collect::<LinkedResult<Vec<_>>>()?;
+ .collect::<ExternalChatResult<Vec<_>>>()?;
sqlx::query!(
r#"INSERT INTO whatsapp_roster (account_id, group_jid, observed_at, metadata)
SELECT $1, read.group_jid, read.observed_at, read.metadata
@@ -164,11 +165,11 @@ async fn signal_changes_in(
account: &Fenced,
list_changed: bool,
mut groups: Vec<Uuid>,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
if list_changed {
notify_change(
tx,
- signals::Change::Chats {
+ Change::Chats {
account_id: account.id,
},
)
@@ -177,7 +178,7 @@ async fn signal_changes_in(
groups.sort();
groups.dedup();
for chat_id in groups {
- notify_change(tx, signals::Change::Chat { chat_id }).await?;
+ notify_change(tx, Change::Chat { chat_id }).await?;
}
Ok(())
}
@@ -188,7 +189,7 @@ pub(super) async fn group_left_in(
account: &Fenced,
group: &str,
observed_at: DateTime<Utc>,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
let listed = sqlx::query_scalar!(
"SELECT 1 AS listed FROM whatsapp_roster WHERE account_id = $1 AND group_jid = $2",
account.id,
@@ -211,7 +212,7 @@ async fn reconcile_in(
account: &Fenced,
groups: &[&GroupInfo],
locked: &[Uuid],
-) -> LinkedResult<Vec<(Uuid, String)>> {
+) -> ExternalChatResult<Vec<(Uuid, String)>> {
let jids: Vec<String> = groups.iter().map(|group| group.jid.clone()).collect();
let permitted: Vec<(Uuid, String)> = sqlx::query!(
r#"SELECT b.id, b.remote_id FROM external_chat b
@@ -260,7 +261,7 @@ pub(super) async fn left_in(
account: &Fenced,
group: &str,
observed_at: DateTime<Utc>,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
group_left_in(tx, account, group, observed_at).await
}
@@ -271,7 +272,7 @@ async fn left_selected_in(
group: &str,
observed_at: DateTime<Utc>,
locked: Option<&[Uuid]>,
-) -> LinkedResult<Vec<Uuid>> {
+) -> ExternalChatResult<Vec<Uuid>> {
sqlx::query!("SELECT id FROM external_chat WHERE provider = 'whatsapp' AND remote_id = $1 AND ($2::uuid[] IS NULL OR id = ANY($2)) ORDER BY id FOR UPDATE", group, locked)
.fetch_all(&mut **tx).await.map_err(failure)?;
sqlx::query!(
@@ -300,8 +301,8 @@ async fn left_selected_in(
}
#[async_trait]
-impl GroupRepository for PgLinkedRepository {
- async fn account_groups(&self, account: Uuid) -> LinkedResult<Vec<GroupInfo>> {
+impl GroupRepository for PgWhatsAppRepository {
+ async fn account_groups(&self, account: Uuid) -> ExternalChatResult<Vec<GroupInfo>> {
sqlx::query_scalar!(
"SELECT metadata FROM whatsapp_roster WHERE account_id = $1 ORDER BY group_jid LIMIT 2000",
account
@@ -314,7 +315,11 @@ impl GroupRepository for PgLinkedRepository {
.collect()
}
- async fn account_group(&self, account: Uuid, group: &str) -> LinkedResult<Option<GroupInfo>> {
+ async fn account_group(
+ &self,
+ account: Uuid,
+ group: &str,
+ ) -> ExternalChatResult<Option<GroupInfo>> {
sqlx::query_scalar!(
"SELECT metadata FROM whatsapp_roster WHERE account_id = $1 AND group_jid = $2",
account,
@@ -327,7 +332,7 @@ impl GroupRepository for PgLinkedRepository {
.transpose()
}
- async fn request_groups_refresh(&self, accounts: &[Uuid]) -> LinkedResult<()> {
+ async fn request_groups_refresh(&self, accounts: &[Uuid]) -> ExternalChatResult<()> {
if accounts.is_empty() {
return Ok(());
}
@@ -338,7 +343,7 @@ impl GroupRepository for PgLinkedRepository {
tx.commit().await.map_err(failure)
}
- async fn group_accounts(&self, chat_id: Uuid, user: &str) -> LinkedResult<Vec<Uuid>> {
+ async fn group_accounts(&self, chat_id: Uuid, user: &str) -> ExternalChatResult<Vec<Uuid>> {
sqlx::query_scalar!(
r#"SELECT m.account_id AS "account_id!" FROM whatsapp_chat_membership m
JOIN whatsapp_linked_account a ON a.id = m.account_id
@@ -357,7 +362,7 @@ impl GroupRepository for PgLinkedRepository {
&self,
user: &MacroUserIdStr<'_>,
groups: &[String],
- ) -> LinkedResult<Vec<VisibleAdoption>> {
+ ) -> ExternalChatResult<Vec<VisibleAdoption>> {
let user = user.as_ref();
sqlx::query_as!(
VisibleAdoption,
@@ -373,14 +378,14 @@ impl GroupRepository for PgLinkedRepository {
.map_err(failure)
}
- async fn binding_roster(&self, binding: &Binding) -> LinkedResult<Option<GroupInfo>> {
+ async fn chat_roster(&self, chat: &ChatRef) -> ExternalChatResult<Option<GroupInfo>> {
let roster = sqlx::query_scalar!(
r#"SELECT r.metadata FROM whatsapp_roster r
JOIN whatsapp_chat_membership m ON m.account_id = r.account_id
WHERE m.chat_id = $1 AND m.consent_ended_at IS NULL AND r.group_jid = $2
ORDER BY r.observed_at DESC LIMIT 1"#,
- binding.id,
- binding.remote_id
+ chat.id,
+ chat.remote_id
)
.fetch_optional(&self.pool)
.await
@@ -411,7 +416,7 @@ impl GroupRepository for PgLinkedRepository {
r#"SELECT a.jid, p.display_name AS "display_name!" FROM whatsapp_identity_alias a
JOIN external_chat_person p ON p.id = a.person_id
WHERE a.chat_id = $1 AND a.jid = ANY($2) AND p.display_name IS NOT NULL"#,
- binding.id,
+ chat.id,
&jids
)
.fetch_all(&self.pool)
@@ -432,7 +437,135 @@ impl GroupRepository for PgLinkedRepository {
Ok(Some(roster))
}
- async fn account_chats(&self, account: Uuid) -> LinkedResult<Vec<StoredChat>> {
+ async fn account_rosters(
+ &self,
+ account: Uuid,
+ groups: &[String],
+ ) -> ExternalChatResult<Vec<GroupInfo>> {
+ sqlx::query_scalar!(
+ "SELECT metadata FROM whatsapp_roster WHERE account_id = $1 AND group_jid = ANY($2)",
+ account,
+ groups
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(failure)?
+ .into_iter()
+ .map(decode)
+ .collect()
+ }
+
+ async fn communities(
+ &self,
+ chats: &[ChatRef],
+ ) -> ExternalChatResult<Vec<Option<CommunityReference>>> {
+ let ids: Vec<Uuid> = chats.iter().map(|chat| chat.id).collect();
+ let rows = sqlx::query!(
+ r#"SELECT g.id AS "id!", seen.community_jid AS "community_jid?",
+ seen.community_announcements AS "community_announcements?", parent.name AS "community_name?"
+ FROM external_chat g
+ LEFT JOIN LATERAL (SELECT r.metadata->>'communityJid' AS community_jid,COALESCE((r.metadata->>'communityAnnouncements')::boolean,false) AS community_announcements
+ FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id
+ WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=g.remote_id
+ ORDER BY r.observed_at DESC LIMIT 1) seen ON true
+ LEFT JOIN LATERAL (SELECT r.metadata->>'name' AS name
+ FROM whatsapp_roster r JOIN whatsapp_chat_membership m ON m.account_id=r.account_id
+ WHERE m.chat_id=g.id AND m.consent_ended_at IS NULL AND r.group_jid=seen.community_jid
+ ORDER BY r.observed_at DESC LIMIT 1) parent ON true
+ WHERE g.id = ANY($1)"#,
+ &ids
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(failure)?;
+ Ok(chats
+ .iter()
+ .map(|chat| {
+ let row = rows.iter().find(|row| row.id == chat.id)?;
+ Some(CommunityReference::named(
+ row.community_jid.clone()?,
+ row.community_name.as_deref(),
+ row.community_announcements.unwrap_or(false),
+ ))
+ })
+ .collect())
+ }
+
+ async fn people(&self, chat: &ChatRef, jids: &[String]) -> ExternalChatResult<People> {
+ let mut people = People::new(crate::domain::jid::phone_number);
+ if jids.is_empty() {
+ return Ok(people);
+ }
+ let seen = sqlx::query!(
+ r#"SELECT a.jid, p.user_id, p.display_name FROM whatsapp_identity_alias a
+ JOIN external_chat_person p ON p.id = a.person_id
+ WHERE a.chat_id = $1 AND a.jid = ANY($2)"#,
+ chat.id,
+ jids
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(failure)?;
+ let roster = sqlx::query_scalar!(
+ r#"SELECT r.metadata FROM whatsapp_roster r
+ JOIN whatsapp_linked_account a ON a.id = r.account_id
+ JOIN external_chat g ON g.user_id = a.user_id AND g.remote_id = r.group_jid
+ WHERE g.id = $1"#,
+ chat.id
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(failure)?
+ .map(decode::<GroupInfo>)
+ .transpose()?;
+ for row in seen {
+ let roster_name = roster.as_ref().and_then(|roster| {
+ roster
+ .participants
+ .iter()
+ .find(|participant| {
+ participant.identity.jid == row.jid
+ || participant.identity.alternate_jid.as_deref() == Some(&row.jid)
+ })
+ .and_then(|participant| participant.identity.label())
+ });
+ let name = row
+ .display_name
+ .filter(|name| !name.trim().is_empty())
+ .or(roster_name);
+ if let Some(name) = name {
+ people.learn(
+ &row.jid,
+ ExternalChatPersonRef {
+ name,
+ user_id: row.user_id,
+ },
+ );
+ }
+ }
+ for participant in roster.iter().flat_map(|roster| &roster.participants) {
+ let Some(name) = participant.identity.label() else {
+ continue;
+ };
+ for jid in [
+ Some(&participant.identity.jid),
+ participant.identity.alternate_jid.as_ref(),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ people.learn(
+ jid,
+ ExternalChatPersonRef {
+ name: name.clone(),
+ user_id: None,
+ },
+ );
+ }
+ }
+ Ok(people)
+ }
+ async fn account_chats(&self, account: Uuid) -> ExternalChatResult<Vec<StoredChat>> {
super::chats::account_chats(&self.pool, account).await
}
}
diff --git a/crates/whatsapp_linked/src/outbound/pg/ingest.rs b/crates/whatsapp_linked/src/outbound/pg/ingest.rs
index 0bcedbd65d..8c51238e35 100644
--- a/crates/whatsapp_linked/src/outbound/pg/ingest.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/ingest.rs
@@ -5,6 +5,7 @@
use super::*;
use crate::domain::events::*;
use chrono::{DateTime, Utc};
+use external_chat::domain::rich::RichContent;
/// Deferred and quarantined events kept per account before ingestion pushes back.
const MAX_RETAINED_EVENTS: i64 = 4096;
@@ -17,7 +18,7 @@ pub(super) struct Reservation {
pub observer: Uuid,
/// External author of a reaction, distinct from the target's author.
pub reaction_actor: Option<Uuid>,
- pub binding: Binding,
+ pub binding: ChatRef,
/// Verified external person who authored the source message.
pub person: Uuid,
/// Group message.
@@ -66,9 +67,9 @@ pub(super) async fn observed_bindings_in(
group: &str,
source_at: DateTime<Utc>,
history: bool,
-) -> LinkedResult<Vec<Binding>> {
+) -> ExternalChatResult<Vec<ChatRef>> {
sqlx::query_as!(
- bindings::BindingRow,
+ ChatRow,
r#"SELECT b.id, b.provider, b.remote_id, b.kind, b.user_id, b.state, b.adopted_at
FROM external_chat b JOIN whatsapp_chat_membership m ON m.chat_id = b.id
WHERE b.provider = 'whatsapp' AND b.remote_id = $1 AND b.state = 'active'
@@ -91,7 +92,7 @@ pub(super) async fn observed_bindings_in(
.await
.map_err(failure)?
.into_iter()
- .map(bindings::binding)
+ .map(chat_ref)
.collect()
}
@@ -101,11 +102,11 @@ pub(super) async fn reserve_in(
tx: &mut Transaction<'_, Postgres>,
observer: Uuid,
generation: i64,
- binding: &Binding,
+ binding: &ChatRef,
event: &InboundEvent,
message: &Message,
accepted_send: Option<Uuid>,
-) -> LinkedResult<Reserve> {
+) -> ExternalChatResult<Reserve> {
let current = sqlx::query_scalar!(
"SELECT state FROM external_chat WHERE id = $1 FOR UPDATE",
binding.id
@@ -183,7 +184,7 @@ pub(super) async fn reserve_in(
let target = message
.target
.as_ref()
- .ok_or(LinkedError::Invalid("reaction without a target"))?;
+ .ok_or(ExternalChatError::Invalid("reaction without a target"))?;
let row = sqlx::query!(
r#"SELECT m.person_id, m.message_id, m.origin, m.deleted_at, m.revision_content, m.content_revision, m.revised_at
FROM external_chat_message m JOIN whatsapp_identity_alias a ON a.chat_id = m.chat_id AND a.person_id = m.person_id
@@ -217,7 +218,7 @@ pub(super) async fn reserve_in(
message
.target
.as_ref()
- .ok_or(LinkedError::Invalid("revision without a target"))?
+ .ok_or(ExternalChatError::Invalid("revision without a target"))?
.source_id
.clone()
} else {
@@ -242,7 +243,7 @@ pub(super) async fn reserve_in(
.map_err(failure)?
};
if accepted_send.is_some() && !revision && send.is_none() {
- return Err(LinkedError::Conflict.into());
+ return Err(ExternalChatError::Conflict.into());
}
let new_origin = if send.is_some() {
"macro"
@@ -361,7 +362,7 @@ async fn reserve_attachments_in(
tx: &mut Transaction<'_, Postgres>,
r: &Reservation,
message: &Message,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
for (ordinal, media) in message.media.iter().enumerate() {
let state = if MediaKind::of(&media.mime).is_some() {
"pending"
@@ -405,11 +406,11 @@ pub(super) async fn persist_in(
event: &InboundEvent,
message: &Message,
r: &Reservation,
-) -> LinkedResult<bool> {
+) -> ExternalChatResult<bool> {
if message.kind == MessageKind::PollVote {
let (Some(actor), Some(RichContent::PollVote(vote))) = (r.reaction_actor, &message.rich)
else {
- return Err(LinkedError::Invalid("poll vote without a choice").into());
+ return Err(ExternalChatError::Invalid("poll vote without a choice").into());
};
return Ok(sqlx::query!(
r#"INSERT INTO external_chat_poll_vote (message_id, person_id, selected, user_id, source_id, revision, updated_at)
@@ -489,7 +490,7 @@ pub(super) async fn finish_in(
tx: &mut Transaction<'_, Postgres>,
r: &Reservation,
changed: bool,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
if r.reaction_actor.is_none() {
sqlx::query!(
r#"UPDATE external_chat_message SET message_id = $4, effect_revision = effect_revision + CASE WHEN $5 THEN 1 ELSE 0 END,
@@ -532,12 +533,12 @@ pub(super) async fn finish_in(
.map_err(failure)?;
}
if changed {
- notify(tx, signals::CHAT_EFFECTS, r.message).await?;
+ notify(tx, external_chat::signals::CHAT_EFFECTS, r.message).await?;
}
if changed || r.actor.is_some() {
notify_change(
tx,
- signals::Change::Chat {
+ Change::Chat {
chat_id: r.binding.id,
},
)
@@ -561,7 +562,7 @@ async fn resolve_sender_in(
account: &Fenced,
group: &str,
message: &Message,
-) -> LinkedResult<Identity> {
+) -> ExternalChatResult<Identity> {
let mut sender = message.sender.clone();
if message.from_me
&& let Some(own) = &account.identity_jid
@@ -600,7 +601,7 @@ async fn retain_in(
account: &Fenced,
event: &InboundEvent,
state: &str,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
let retained = sqlx::query_scalar!(
r#"SELECT count(*) AS "count!" FROM whatsapp_inbound_event WHERE account_id = $1"#,
account.id
@@ -609,7 +610,7 @@ async fn retain_in(
.await
.map_err(failure)?;
if retained >= MAX_RETAINED_EVENTS {
- return Err(LinkedError::Backlogged.into());
+ return Err(ExternalChatError::Backlogged.into());
}
let payload = serde_json::to_value(event).map_err(failure)?;
sqlx::query!(
@@ -633,7 +634,7 @@ async fn apply_in(
tx: &mut Transaction<'_, Postgres>,
account: &Fenced,
event: &InboundEvent,
-) -> LinkedResult<IngestOutcome> {
+) -> ExternalChatResult<IngestOutcome> {
match &event.kind {
InboundKind::Left => {
groups::left_in(tx, account, &event.group_jid, event.observed_at).await?;
@@ -723,12 +724,12 @@ async fn apply_in(
}
}
-impl PgLinkedRepository {
+impl PgWhatsAppRepository {
pub(super) async fn ingest_event(
&self,
scope: SessionScope,
event: &InboundEvent,
- ) -> LinkedResult<IngestOutcome> {
+ ) -> ExternalChatResult<IngestOutcome> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
if account.state != AccountState::Connected {
@@ -750,7 +751,7 @@ impl PgLinkedRepository {
tx.commit().await.map_err(failure)?;
Ok(outcome)
}
- Err(error) if matches!(error.current_context(), LinkedError::Unavailable) => {
+ Err(error) if matches!(error.current_context(), ExternalChatError::Unavailable) => {
// Infrastructure failure: retain the event for retry when possible.
tx.rollback().await.map_err(failure)?;
let mut tx = self.pool.begin().await.map_err(failure)?;
@@ -785,7 +786,7 @@ impl PgLinkedRepository {
&self,
scope: SessionScope,
event: &InboundEvent,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let observed = sqlx::query_scalar!(
@@ -813,7 +814,7 @@ impl PgLinkedRepository {
.await
.map_err(failure)?;
if retained >= MAX_RETAINED_EVENTS {
- return Err(LinkedError::Backlogged.into());
+ return Err(ExternalChatError::Backlogged.into());
}
let payload = serde_json::to_value(event).map_err(failure)?;
sqlx::query!(
@@ -834,7 +835,7 @@ impl PgLinkedRepository {
&self,
scope: SessionScope,
limit: i64,
- ) -> LinkedResult<Vec<InboundEvent>> {
+ ) -> ExternalChatResult<Vec<InboundEvent>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let dropped = sqlx::query_scalar!(
diff --git a/crates/whatsapp_linked/src/outbound/pg/media.rs b/crates/whatsapp_linked/src/outbound/pg/media.rs
index 6fbb7f963e..bb0c32efd8 100644
--- a/crates/whatsapp_linked/src/outbound/pg/media.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/media.rs
@@ -4,83 +4,19 @@
//! an attachment lets go of is queued in `external_chat_file_deletion` by a trigger.
use super::*;
-use crate::domain::{
- events::*,
- ports::{FileImport, MediaRepository},
-};
-use async_trait::async_trait;
+use crate::domain::events::*;
+use external_chat::outbound::pg::fail_attachment_in;
use sha2::{Digest, Sha256};
/// Download attempts before the attachment is shown as unavailable.
const MAX_DOWNLOAD_ATTEMPTS: i32 = 5;
-/// Mark one attachment failed and tell readers on its message.
-async fn fail_in(
- tx: &mut Transaction<'_, Postgres>,
- attachment: &AttachmentRef,
-) -> LinkedResult<()> {
- let row = sqlx::query!(
- r#"UPDATE external_chat_attachment a SET state = 'failed', claimed_until = NULL, locator = NULL
- FROM external_chat_message m, external_chat b
- WHERE a.chat_id = $1 AND a.person_id = $2 AND a.source_id = $3 AND a.ordinal = $4
- AND a.state IN ('pending', 'downloaded')
- AND m.chat_id = a.chat_id AND m.person_id = a.person_id AND m.source_id = a.source_id
- AND b.id = a.chat_id
- RETURNING a.blob_id, a.filename, m.message_id, b.id AS chat_id"#,
- attachment.chat_id,
- attachment.person_id,
- attachment.source_id,
- attachment.ordinal
- )
- .fetch_optional(&mut **tx)
- .await
- .map_err(failure)?;
- let Some(row) = row else { return Ok(()) };
- sqlx::query!(
- "DELETE FROM external_chat_media_blob WHERE id = $1",
- row.blob_id
- )
- .execute(&mut **tx)
- .await
- .map_err(failure)?;
- if let Some(message) = row.message_id {
- bump_in(tx, attachment, message).await?;
- }
- Ok(())
-}
-
-/// Queue publication of the attachment's message.
-async fn bump_in(
- tx: &mut Transaction<'_, Postgres>,
- attachment: &AttachmentRef,
- message: Uuid,
-) -> LinkedResult<()> {
- sqlx::query!(
- r#"UPDATE external_chat_message SET effect_revision = effect_revision + 1, notification_retry_at = now()
- WHERE chat_id = $1 AND person_id = $2 AND source_id = $3"#,
- attachment.chat_id,
- attachment.person_id,
- attachment.source_id
- )
- .execute(&mut **tx)
- .await
- .map_err(failure)?;
- notify(tx, signals::CHAT_EFFECTS, message).await?;
- notify_change(
- tx,
- signals::Change::Chat {
- chat_id: attachment.chat_id,
- },
- )
- .await
-}
-
-impl PgLinkedRepository {
+impl PgWhatsAppRepository {
pub(super) async fn claim_media_imports(
&self,
scope: SessionScope,
limit: i64,
- ) -> LinkedResult<Vec<MediaImport>> {
+ ) -> ExternalChatResult<Vec<MediaImport>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
if account.state != AccountState::Connected {
@@ -129,10 +65,10 @@ impl PgLinkedRepository {
scope: SessionScope,
attachment: &AttachmentRef,
bytes: &[u8],
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
- lock_group_in(&mut tx, attachment.chat_id).await?;
+ lock_chat_in(&mut tx, attachment.chat_id).await?;
let row = sqlx::query!(
r#"SELECT a.blob_id, a.sha256, a.state, a.mime AS "mime!", a.filename AS "filename!", b.id AS chat_id, b.user_id
FROM external_chat_attachment a JOIN external_chat b ON b.id = a.chat_id
@@ -147,16 +83,18 @@ impl PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
// Ready, failed or deleted attachments are never rewritten.
if row.state != "pending" {
return Ok(());
}
let digest: [u8; 32] = Sha256::digest(bytes).into();
if digest[..] != row.sha256[..] {
- fail_in(&mut tx, attachment).await?;
+ fail_attachment_in(&mut tx, attachment).await?;
tx.commit().await.map_err(failure)?;
- return Err(LinkedError::Invalid("attachment bytes do not match their hash").into());
+ return Err(
+ ExternalChatError::Invalid("attachment bytes do not match their hash").into(),
+ );
}
sqlx::query!(
r#"INSERT INTO external_chat_media_blob (id, chat_id, user_id, mime, filename, sha256, bytes)
@@ -183,7 +121,7 @@ impl PgLinkedRepository {
.execute(&mut *tx)
.await
.map_err(failure)?;
- notify(&mut tx, signals::MEDIA_READY, row.blob_id).await?;
+ notify(&mut tx, external_chat::signals::MEDIA_READY, row.blob_id).await?;
tx.commit().await.map_err(failure)
}
@@ -192,10 +130,10 @@ impl PgLinkedRepository {
scope: SessionScope,
attachment: &AttachmentRef,
reason: MediaFailure,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
- lock_group_in(&mut tx, attachment.chat_id).await?;
+ lock_chat_in(&mut tx, attachment.chat_id).await?;
let attempts = sqlx::query_scalar!(
r#"UPDATE external_chat_attachment SET attempts = attempts + 1, claimed_until = NULL,
retry_at = now() + least(interval '1 minute' * power(2, attempts), interval '6 hours')
@@ -213,180 +151,8 @@ impl PgLinkedRepository {
if let Some(attempts) = attempts
&& (reason == MediaFailure::Permanent || attempts >= MAX_DOWNLOAD_ATTEMPTS)
{
- fail_in(&mut tx, attachment).await?;
- }
- tx.commit().await.map_err(failure)
- }
-}
-
-#[async_trait]
-impl MediaRepository for PgLinkedRepository {
- async fn due_imports(&self, limit: i64) -> LinkedResult<Vec<FileImport>> {
- // Claiming pushes retry_at forward, so DSS replicas don't import twice.
- let rows = sqlx::query!(
- r#"UPDATE external_chat_attachment a SET retry_at = now() + interval '5 minutes'
- FROM external_chat_media_blob blob
- WHERE (a.chat_id, a.person_id, a.source_id, a.ordinal) IN (
- SELECT chat_id, person_id, source_id, ordinal FROM external_chat_attachment
- WHERE state = 'downloaded' AND retry_at <= now()
- ORDER BY retry_at LIMIT $1 FOR UPDATE SKIP LOCKED)
- AND blob.id = a.blob_id
- RETURNING a.chat_id, a.person_id, a.source_id, a.ordinal, a.blob_id, a.attempts,
- blob.user_id, blob.filename, blob.mime"#,
- limit
- )
- .fetch_all(&self.pool)
- .await
- .map_err(failure)?;
- Ok(rows
- .into_iter()
- .map(|row| FileImport {
- attachment: AttachmentRef {
- chat_id: row.chat_id,
- person_id: row.person_id,
- source_id: row.source_id,
- ordinal: row.ordinal,
- },
- blob_id: row.blob_id,
- chat_id: row.chat_id,
- owner: row.user_id,
- filename: row.filename,
- mime: row.mime,
- attempts: row.attempts,
- })
- .collect())
- }
-
- async fn import_bytes(&self, import: &FileImport) -> LinkedResult<Option<Vec<u8>>> {
- sqlx::query_scalar!(
- "SELECT bytes FROM external_chat_media_blob WHERE id = $1",
- import.blob_id
- )
- .fetch_optional(&self.pool)
- .await
- .map_err(failure)
- }
-
- async fn import_stored(&self, import: &FileImport, file_id: Uuid) -> LinkedResult<()> {
- let attachment = &import.attachment;
- let mut tx = self.pool.begin().await.map_err(failure)?;
- lock_group_in(&mut tx, import.attachment.chat_id).await?;
- let row = sqlx::query!(
- r#"UPDATE external_chat_attachment a SET state = 'ready', file_id = $5
- FROM external_chat_message m
- WHERE a.chat_id = $1 AND a.person_id = $2 AND a.source_id = $3 AND a.ordinal = $4 AND a.state = 'downloaded'
- AND m.chat_id = a.chat_id AND m.person_id = a.person_id AND m.source_id = a.source_id
- RETURNING m.message_id"#,
- attachment.chat_id,
- attachment.person_id,
- attachment.source_id,
- attachment.ordinal,
- file_id
- )
- .fetch_optional(&mut *tx)
- .await
- .map_err(failure)?;
- match row {
- Some(row) => {
- if let Some(message) = row.message_id {
- bump_in(&mut tx, attachment, message).await?;
- }
- }
- // Deleted or imported meanwhile: nothing will reference the file.
- None => {
- sqlx::query!(
- "INSERT INTO external_chat_file_deletion (file_id) VALUES ($1) ON CONFLICT DO NOTHING",
- file_id
- )
- .execute(&mut *tx)
- .await
- .map_err(failure)?;
- }
+ fail_attachment_in(&mut tx, attachment).await?;
}
- sqlx::query!(
- "DELETE FROM external_chat_media_blob WHERE id = $1",
- import.blob_id
- )
- .execute(&mut *tx)
- .await
- .map_err(failure)?;
tx.commit().await.map_err(failure)
}
-
- async fn import_failed(&self, import: &FileImport, permanent: bool) -> LinkedResult<()> {
- let mut tx = self.pool.begin().await.map_err(failure)?;
- lock_group_in(&mut tx, import.attachment.chat_id).await?;
- if permanent {
- fail_in(&mut tx, &import.attachment).await?;
- } else {
- let attachment = &import.attachment;
- sqlx::query!(
- r#"UPDATE external_chat_attachment SET attempts = attempts + 1,
- retry_at = now() + least(interval '1 minute' * power(2, attempts), interval '1 hour')
- WHERE chat_id = $1 AND person_id = $2 AND source_id = $3 AND ordinal = $4 AND state = 'downloaded'"#,
- attachment.chat_id,
- attachment.person_id,
- attachment.source_id,
- attachment.ordinal
- )
- .execute(&mut *tx)
- .await
- .map_err(failure)?;
- }
- tx.commit().await.map_err(failure)
- }
-
- async fn expire_retained(&self) -> LinkedResult<u64> {
- let uploads = sqlx::query!(
- r#"DELETE FROM external_chat_media_blob b WHERE b.created_at < now() - interval '1 day'
- AND NOT EXISTS (SELECT 1 FROM external_chat_attachment a WHERE a.blob_id = b.id)"#
- )
- .execute(&self.pool)
- .await
- .map_err(failure)?
- .rows_affected();
- let receipts = sqlx::query!(
- "DELETE FROM whatsapp_participant_receipt WHERE source_at < now() - make_interval(days => $1)",
- crate::domain::models::RECEIPT_RETENTION_DAYS
- )
- .execute(&self.pool)
- .await
- .map_err(failure)?
- .rows_affected();
- let pairings = sqlx::query!(
- "DELETE FROM whatsapp_pairing WHERE consumed_at < now() - interval '1 day'"
- )
- .execute(&self.pool)
- .await
- .map_err(failure)?
- .rows_affected();
- Ok(uploads + receipts + pairings)
- }
-
- async fn due_file_deletions(&self, limit: i64) -> LinkedResult<Vec<Uuid>> {
- // Claiming pushes retry_at forward, so DSS replicas don't delete twice
- // and a failing file waits before it is tried again.
- sqlx::query_scalar!(
- r#"UPDATE external_chat_file_deletion SET retry_at = now() + interval '5 minutes'
- WHERE file_id IN (
- SELECT file_id FROM external_chat_file_deletion WHERE retry_at <= now()
- ORDER BY retry_at LIMIT $1 FOR UPDATE SKIP LOCKED)
- RETURNING file_id"#,
- limit
- )
- .fetch_all(&self.pool)
- .await
- .map_err(failure)
- }
-
- async fn files_deleted(&self, files: &[Uuid]) -> LinkedResult<()> {
- sqlx::query!(
- "DELETE FROM external_chat_file_deletion WHERE file_id = ANY($1)",
- files
- )
- .execute(&self.pool)
- .await
- .map_err(failure)?;
- Ok(())
- }
}
diff --git a/crates/whatsapp_linked/src/outbound/pg/sends.rs b/crates/whatsapp_linked/src/outbound/pg/sends.rs
index 0716967189..d1759ae0d6 100644
--- a/crates/whatsapp_linked/src/outbound/pg/sends.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/sends.rs
@@ -3,12 +3,10 @@
//! dispatched send without an outcome is `unknown` and never sent again.
use super::*;
-use crate::domain::{
- events::*,
- ports::{MutationTarget, SendIntent, SendRepository, StagedUpload, StoredSend},
-};
+use crate::domain::{events::*, ports::SendRepository};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
+use external_chat::domain::ports::{MutationTarget, SendIntent, StoredSend};
use macro_user_id::user_id::MacroUserIdStr;
struct SendRow {
@@ -22,7 +20,7 @@ struct SendRow {
created_at: DateTime<Utc>,
}
-fn progress(row: SendRow) -> LinkedResult<SendProgress> {
+fn progress(row: SendRow) -> ExternalChatResult<SendProgress> {
Ok(SendProgress {
id: row.id,
user_id: row.user_id,
@@ -36,89 +34,25 @@ fn progress(row: SendRow) -> LinkedResult<SendProgress> {
})
}
-/// A shape value as an integer column.
-fn column(value: Option<u32>) -> Option<i32> {
- value.map(|value| value.min(i32::MAX as u32) as i32)
-}
-
/// An integer column as a shape value.
fn unsigned(value: Option<i32>) -> Option<u32> {
value.and_then(|value| u32::try_from(value).ok())
}
#[async_trait]
-impl SendRepository for PgLinkedRepository {
- async fn stage_upload(&self, upload: StagedUpload<'_>) -> LinkedResult<Uuid> {
- let id = Uuid::now_v7();
- sqlx::query!(
- r#"INSERT INTO external_chat_media_blob (id, chat_id, user_id, mime, filename, sha256, bytes, width, height, duration_seconds)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)"#,
- id,
- upload.chat_id,
- upload.user.as_ref(),
- upload.mime,
- upload.filename,
- &upload.sha256[..],
- upload.bytes,
- column(upload.shape.width.filter(|width| *width > 0)),
- column(upload.shape.height.filter(|height| *height > 0)),
- column(upload.shape.duration_seconds)
- )
- .execute(&self.pool)
- .await
- .map_err(failure)?;
- Ok(id)
- }
-
- async fn staged_upload_mime(
- &self,
- user: &MacroUserIdStr<'_>,
- chat_id: Uuid,
- upload: Uuid,
- ) -> LinkedResult<Option<String>> {
- let user = user.as_ref();
- sqlx::query_scalar!(
- r#"SELECT mime FROM external_chat_media_blob b
- WHERE id = $1 AND chat_id = $2 AND user_id = $3
- AND NOT EXISTS (SELECT 1 FROM external_chat_attachment a WHERE a.blob_id = b.id)"#,
- upload,
- chat_id,
- user
- )
- .fetch_optional(&self.pool)
- .await
- .map_err(failure)
- }
-
- async fn account_roster(
- &self,
- account: &Account,
- binding: &Binding,
- ) -> LinkedResult<Option<GroupInfo>> {
- sqlx::query_scalar!(
- "SELECT metadata FROM whatsapp_roster WHERE account_id = $1 AND group_jid = $2",
- account.id,
- binding.remote_id
- )
- .fetch_optional(&self.pool)
- .await
- .map_err(failure)?
- .map(decode::<GroupInfo>)
- .transpose()
- }
-
+impl SendRepository for PgWhatsAppRepository {
async fn mutation_target(
&self,
- binding: &Binding,
+ chat: &ChatRef,
message: Uuid,
- ) -> LinkedResult<Option<MutationTarget>> {
+ ) -> ExternalChatResult<Option<MutationTarget>> {
let row = sqlx::query!(
r#"SELECT m.source_at, m.deleted_at, array_agg(a.jid) AS "sent_by!"
FROM external_chat_message m
JOIN whatsapp_identity_alias a ON a.chat_id = m.chat_id AND a.person_id = m.person_id
WHERE m.chat_id = $1 AND m.message_id = $2
GROUP BY m.source_at, m.deleted_at"#,
- binding.id,
+ chat.id,
message
)
.fetch_optional(&self.pool)
@@ -136,7 +70,7 @@ impl SendRepository for PgLinkedRepository {
chat_id: Uuid,
user: &MacroUserIdStr<'_>,
idempotency_key: Uuid,
- ) -> LinkedResult<Option<StoredSend>> {
+ ) -> ExternalChatResult<Option<StoredSend>> {
let user = user.as_ref();
let Some(row) = sqlx::query!(
r#"SELECT id, user_id, chat_id, payload, state, message_id, error_code, created_at, request_sha256
@@ -170,11 +104,11 @@ impl SendRepository for PgLinkedRepository {
}))
}
- async fn enqueue(&self, intent: SendIntent<'_>) -> LinkedResult<SendProgress> {
+ async fn enqueue(&self, intent: SendIntent<'_>) -> ExternalChatResult<SendProgress> {
let SendIntent {
user,
account,
- binding,
+ chat: binding,
request,
digest,
} = intent;
@@ -183,9 +117,9 @@ impl SendRepository for PgLinkedRepository {
let mut tx = self.pool.begin().await.map_err(failure)?;
sqlx::query!(
"SELECT id FROM whatsapp_linked_account WHERE id = $1 AND user_id = $2 AND state = 'connected' AND generation = $3 FOR SHARE",
- account.id, user, account.authorization_generation
- ).fetch_optional(&mut *tx).await.map_err(failure)?.ok_or(LinkedError::Forbidden)?;
- lock_group_in(&mut tx, binding.id).await?;
+ account.id, user, account.generation
+ ).fetch_optional(&mut *tx).await.map_err(failure)?.ok_or(ExternalChatError::Forbidden)?;
+ lock_chat_in(&mut tx, binding.id).await?;
let replayed = sqlx::query_scalar!(
"SELECT id FROM whatsapp_send WHERE chat_id = $1 AND user_id = $2 AND idempotency_key = $3",
binding.id,
@@ -197,9 +131,9 @@ impl SendRepository for PgLinkedRepository {
.map_err(failure)?
.is_some();
let identity = account
- .identity_jid
+ .identity
.as_deref()
- .ok_or(LinkedError::Forbidden)?;
+ .ok_or(ExternalChatError::Forbidden)?;
let observed_at = match binding.kind {
ExternalChatKind::Group => {
let metadata = sqlx::query_scalar!(
@@ -212,21 +146,21 @@ impl SendRepository for PgLinkedRepository {
.map_err(failure)?
.map(decode::<GroupInfo>)
.transpose()?
- .ok_or(LinkedError::Forbidden)?;
+ .ok_or(ExternalChatError::Forbidden)?;
if !metadata.can_post(identity) {
- return Err(LinkedError::Forbidden.into());
+ return Err(ExternalChatError::Forbidden.into());
}
metadata.observed_at
}
// Only the owner's own account is in a direct chat.
ExternalChatKind::Direct if binding.user_id == user => Utc::now(),
- ExternalChatKind::Direct => return Err(LinkedError::Forbidden.into()),
+ ExternalChatKind::Direct => return Err(ExternalChatError::Forbidden.into()),
};
sqlx::query!(r#"INSERT INTO whatsapp_chat_membership (chat_id,account_id,generation,consent_at,remote_member,can_post,reconciled,observed_at)
VALUES ($1,$2,$3,clock_timestamp(),true,true,true,$4)
ON CONFLICT (chat_id,account_id) DO UPDATE SET generation = excluded.generation,consent_ended_at = NULL,
remote_member = true,can_post = true,reconciled = true,observed_at = excluded.observed_at"#,
- binding.id,account.id,account.authorization_generation,observed_at).execute(&mut *tx).await.map_err(failure)?;
+ binding.id,account.id,account.generation,observed_at).execute(&mut *tx).await.map_err(failure)?;
// The group lock also serializes idempotency and sequence allocation.
let sequence = if replayed {
None
@@ -244,12 +178,12 @@ impl SendRepository for PgLinkedRepository {
binding.id,
account.id,
user,
- account.authorization_generation
+ account.generation
)
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::Forbidden)?)
+ .ok_or(ExternalChatError::Forbidden)?)
};
if let Some(sequence) = sequence {
sqlx::query!(
@@ -263,9 +197,9 @@ impl SendRepository for PgLinkedRepository {
request.idempotency_key,
&digest[..],
payload,
- account.authorization_generation,
+ account.generation,
sequence,
- crate::signals::trace_context::current()
+ external_chat::signals::trace_context::current()
)
.execute(&mut *tx)
.await
@@ -283,9 +217,9 @@ impl SendRepository for PgLinkedRepository {
.map_err(failure)?;
if stored.request_sha256 != digest
|| stored.account_id != account.id
- || stored.generation != account.authorization_generation
+ || stored.generation != account.generation
{
- return Err(LinkedError::Conflict.into());
+ return Err(ExternalChatError::Conflict.into());
}
notify(&mut tx, signals::SEND_REQUESTED, account.id).await?;
tx.commit().await.map_err(failure)?;
@@ -305,7 +239,7 @@ impl SendRepository for PgLinkedRepository {
&self,
chat_id: Uuid,
user: &MacroUserIdStr<'_>,
- ) -> LinkedResult<Vec<SendProgress>> {
+ ) -> ExternalChatResult<Vec<SendProgress>> {
let user = user.as_ref();
sqlx::query_as!(
SendRow,
@@ -324,12 +258,40 @@ impl SendRepository for PgLinkedRepository {
.collect()
}
+ async fn delivery_states(
+ &self,
+ chat_id: Uuid,
+ viewer: &str,
+ messages: &[Uuid],
+ ) -> ExternalChatResult<Vec<(Uuid, DeliveryState)>> {
+ sqlx::query!(
+ r#"SELECT DISTINCT ON (message_id) message_id AS "message_id!", state FROM whatsapp_send
+ WHERE chat_id = $1 AND user_id = $2 AND message_id = ANY($3)
+ ORDER BY message_id, created_at DESC"#,
+ chat_id,
+ viewer,
+ messages
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(failure)?
+ .into_iter()
+ .map(|row| {
+ Ok((
+ row.message_id,
+ DeliveryState::parse(&row.state)
+ .ok_or_else(|| corrupt(format_args!("state {:?}", row.state)))?,
+ ))
+ })
+ .collect()
+ }
+
async fn sends_for(
&self,
chat_id: Uuid,
user: &MacroUserIdStr<'_>,
ids: &[Uuid],
- ) -> LinkedResult<Vec<SendProgress>> {
+ ) -> ExternalChatResult<Vec<SendProgress>> {
let user = user.as_ref();
sqlx::query_as!(
SendRow,
@@ -356,7 +318,7 @@ async fn target_in(
binding: Uuid,
message: Uuid,
identity: Option<&str>,
-) -> LinkedResult<Option<Target>> {
+) -> ExternalChatResult<Option<Target>> {
Ok(sqlx::query!(
r#"SELECT m.source_id, a.jid FROM external_chat_message m
JOIN whatsapp_identity_alias a ON a.chat_id = m.chat_id AND a.person_id = m.person_id
@@ -380,7 +342,7 @@ async fn content_in(
binding: Uuid,
request: &SendReply,
identity: Option<&str>,
-) -> LinkedResult<Option<OutgoingContent>> {
+) -> ExternalChatResult<Option<OutgoingContent>> {
Ok(match &request.message {
ReplyContent::Media { upload_id, caption } => {
let Some(blob) = sqlx::query!(
@@ -443,12 +405,12 @@ async fn content_in(
})
}
-impl PgLinkedRepository {
+impl PgWhatsAppRepository {
pub(super) async fn claim_sends(
&self,
scope: SessionScope,
limit: i64,
- ) -> LinkedResult<Vec<ClaimedSend>> {
+ ) -> ExternalChatResult<Vec<ClaimedSend>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let mut failed = sqlx::query_scalar!(
@@ -516,7 +478,7 @@ impl PgLinkedRepository {
failed.sort();
failed.dedup();
for chat_id in failed {
- notify_change(&mut tx, signals::Change::Chat { chat_id }).await?;
+ notify_change(&mut tx, Change::Chat { chat_id }).await?;
}
claimed.sort_by_key(|send| send.chat_sequence);
tx.commit().await.map_err(failure)?;
@@ -528,7 +490,7 @@ impl PgLinkedRepository {
scope: SessionScope,
send: Uuid,
source_id: &str,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let expired = sqlx::query_scalar!(
@@ -544,9 +506,9 @@ impl PgLinkedRepository {
.await
.map_err(failure)?;
if let Some(chat_id) = expired {
- notify_change(&mut tx, signals::Change::Chat { chat_id }).await?;
+ notify_change(&mut tx, Change::Chat { chat_id }).await?;
tx.commit().await.map_err(failure)?;
- return Err(LinkedError::SendExpired.into());
+ return Err(ExternalChatError::SendExpired.into());
}
// Every sending fact is re-checked at the moment of the fence.
let changed = sqlx::query!(
@@ -577,7 +539,7 @@ impl PgLinkedRepository {
.await
.map_err(failure)?;
tx.commit().await.map_err(failure)?;
- return Err(LinkedError::Forbidden.into());
+ return Err(ExternalChatError::Forbidden.into());
}
tx.commit().await.map_err(failure)
}
@@ -587,7 +549,7 @@ impl PgLinkedRepository {
scope: SessionScope,
send: Uuid,
outcome: &SendOutcome,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let binding = sqlx::query_scalar!(
@@ -598,8 +560,8 @@ impl PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
- lock_group_in(&mut tx, binding).await?;
+ .ok_or(ExternalChatError::NotFound)?;
+ lock_chat_in(&mut tx, binding).await?;
let row = sqlx::query!(
r#"SELECT s.chat_id, s.payload, s.allocated_source_id, s.state, s.created_at
FROM whatsapp_send s WHERE s.id = $1 AND s.account_id = $2 AND s.dispatch_started_at IS NOT NULL FOR UPDATE"#,
@@ -609,7 +571,7 @@ impl PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
match outcome {
SendOutcome::Unknown => {
sqlx::query!(
@@ -637,17 +599,17 @@ impl PgLinkedRepository {
let identity = account
.identity_jid
.clone()
- .ok_or(LinkedError::Disconnected)?;
+ .ok_or(ExternalChatError::Disconnected)?;
let request: SendReply = decode(row.payload)?;
let binding = sqlx::query_as!(
- bindings::BindingRow,
+ ChatRow,
"SELECT id, provider, remote_id, kind, user_id, state, adopted_at FROM external_chat WHERE id = $1",
row.chat_id
)
.fetch_one(&mut *tx)
.await
.map_err(failure)
- .and_then(bindings::binding)?;
+ .and_then(chat_ref)?;
let mut media = vec![];
let mut upload = None;
let (kind, text, target) = match &request.message {
@@ -788,12 +750,12 @@ impl PgLinkedRepository {
.execute(&mut *tx)
.await
.map_err(failure)?;
- notify(&mut tx, signals::MEDIA_READY, upload).await?;
+ notify(&mut tx, external_chat::signals::MEDIA_READY, upload).await?;
}
}
}
}
- notify_change(&mut tx, signals::Change::Chat { chat_id: binding }).await?;
+ notify_change(&mut tx, Change::Chat { chat_id: binding }).await?;
tx.commit().await.map_err(failure)
}
@@ -801,7 +763,7 @@ impl PgLinkedRepository {
&self,
scope: SessionScope,
send: Uuid,
- ) -> LinkedResult<OutboundMedia> {
+ ) -> ExternalChatResult<OutboundMedia> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let row = sqlx::query!(
@@ -817,7 +779,7 @@ impl PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
tx.commit().await.map_err(failure)?;
Ok(OutboundMedia {
kind: MediaKind::of(&row.mime)
@@ -832,7 +794,7 @@ impl PgLinkedRepository {
})
}
- pub(super) async fn release_sends(&self, scope: SessionScope) -> LinkedResult<()> {
+ pub(super) async fn release_sends(&self, scope: SessionScope) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
sqlx::query!(
diff --git a/crates/whatsapp_linked/src/outbound/pg/session.rs b/crates/whatsapp_linked/src/outbound/pg/session.rs
index ea7ca1f00b..3e7ff7d3d4 100644
--- a/crates/whatsapp_linked/src/outbound/pg/session.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/session.rs
@@ -14,7 +14,7 @@ struct WorkRow {
passkey_response: Option<serde_json::Value>,
}
-fn work(row: WorkRow) -> LinkedResult<PairingWork> {
+fn work(row: WorkRow) -> ExternalChatResult<PairingWork> {
Ok(PairingWork {
id: row.id,
account_id: row.account_id,
@@ -32,7 +32,7 @@ async fn fail_pairing_in(
pairing: Uuid,
account: Uuid,
generation: i64,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
end_pairing_in(tx, pairing, account, generation, PairingState::Failed).await
}
@@ -43,7 +43,7 @@ async fn end_pairing_in(
account: Uuid,
generation: i64,
state: PairingState,
-) -> LinkedResult<()> {
+) -> ExternalChatResult<()> {
sqlx::query!(
"UPDATE whatsapp_pairing SET state = $2, consumed_at = clock_timestamp(), qr_code = NULL, pair_code = NULL, passkey_challenge = NULL, passkey_response = NULL, updated_at = now() WHERE id = $1",
pairing,
@@ -64,8 +64,8 @@ async fn end_pairing_in(
}
#[async_trait]
-impl SessionRepository for PgLinkedRepository {
- async fn account_state(&self, account: Uuid) -> LinkedResult<Option<AccountState>> {
+impl SessionRepository for PgWhatsAppRepository {
+ async fn account_state(&self, account: Uuid) -> ExternalChatResult<Option<AccountState>> {
sqlx::query_scalar!(
"SELECT state FROM whatsapp_linked_account WHERE id = $1",
account
@@ -77,7 +77,7 @@ impl SessionRepository for PgLinkedRepository {
.transpose()
}
- async fn discoverable_accounts(&self, limit: i64) -> LinkedResult<Vec<Uuid>> {
+ async fn discoverable_accounts(&self, limit: i64) -> ExternalChatResult<Vec<Uuid>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
accounts::expire_pairings_in(&mut tx, None, None, None).await?;
let accounts = sqlx::query_scalar!(
@@ -99,13 +99,13 @@ impl SessionRepository for PgLinkedRepository {
Ok(accounts)
}
- async fn announce_released(&self, account: Uuid) -> LinkedResult<()> {
+ async fn announce_released(&self, account: Uuid) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
notify(&mut tx, signals::ACCOUNT_RELEASED, account).await?;
tx.commit().await.map_err(failure)
}
- async fn fleet_health(&self) -> LinkedResult<FleetHealth> {
+ async fn fleet_health(&self) -> ExternalChatResult<FleetHealth> {
let row = sqlx::query!(
r#"WITH needing AS (
SELECT EXISTS (
@@ -130,11 +130,15 @@ impl SessionRepository for PgLinkedRepository {
})
}
- async fn enqueue_inbound(&self, scope: SessionScope, event: &InboundEvent) -> LinkedResult<()> {
- PgLinkedRepository::enqueue_inbound(self, scope, event).await
+ async fn enqueue_inbound(
+ &self,
+ scope: SessionScope,
+ event: &InboundEvent,
+ ) -> ExternalChatResult<()> {
+ PgWhatsAppRepository::enqueue_inbound(self, scope, event).await
}
- async fn begin_lease(&self, account: Uuid) -> LinkedResult<Lease> {
+ async fn begin_lease(&self, account: Uuid) -> ExternalChatResult<Lease> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let row = sqlx::query!(
r#"UPDATE whatsapp_linked_account SET lease_epoch = lease_epoch + 1 WHERE id = $1
@@ -144,7 +148,7 @@ impl SessionRepository for PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
let unknown = sqlx::query!(
"UPDATE whatsapp_send SET state = 'unknown', claimed_epoch = NULL, updated_at = now() WHERE account_id = $1 AND state = 'sending' AND dispatch_started_at IS NOT NULL",
account
@@ -173,11 +177,11 @@ impl SessionRepository for PgLinkedRepository {
})
}
- async fn release_lease(&self, scope: SessionScope) -> LinkedResult<()> {
+ async fn release_lease(&self, scope: SessionScope) -> ExternalChatResult<()> {
self.release_sends(scope).await
}
- async fn pairing_work(&self, pairing: Uuid) -> LinkedResult<Option<PairingWork>> {
+ async fn pairing_work(&self, pairing: Uuid) -> ExternalChatResult<Option<PairingWork>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
accounts::expire_pairings_in(&mut tx, None, Some(pairing), None).await?;
let row = sqlx::query_as!(
@@ -192,7 +196,7 @@ impl SessionRepository for PgLinkedRepository {
row.map(work).transpose()
}
- async fn live_pairing(&self, scope: SessionScope) -> LinkedResult<Option<PairingWork>> {
+ async fn live_pairing(&self, scope: SessionScope) -> ExternalChatResult<Option<PairingWork>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
accounts::expire_pairings_in(&mut tx, None, None, Some(scope.account_id)).await?;
let account = fence_in(&mut tx, scope).await?;
@@ -214,7 +218,7 @@ impl SessionRepository for PgLinkedRepository {
scope: SessionScope,
pairing: Uuid,
update: &PairingUpdate,
- ) -> LinkedResult<PairingWork> {
+ ) -> ExternalChatResult<PairingWork> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_exclusive_in(&mut tx, scope).await?;
accounts::expire_pairings_in(&mut tx, None, Some(pairing), Some(scope.account_id)).await?;
@@ -227,12 +231,12 @@ impl SessionRepository for PgLinkedRepository {
.fetch_optional(&mut *tx)
.await
.map_err(failure)?
- .ok_or(LinkedError::NotFound)?;
+ .ok_or(ExternalChatError::NotFound)?;
let mut rejection = None;
if current.consumed_at.is_none() {
if current.generation != account.generation {
fail_pairing_in(&mut tx, pairing, account.id, current.generation).await?;
- rejection = Some(LinkedError::Conflict);
+ rejection = Some(ExternalChatError::Conflict);
} else {
match update {
PairingUpdate::Qr(code) => {
@@ -295,12 +299,12 @@ impl SessionRepository for PgLinkedRepository {
// A reconnect must keep its verified identity.
fail_pairing_in(&mut tx, pairing, account.id, account.generation)
.await?;
- rejection = Some(LinkedError::Conflict);
+ rejection = Some(ExternalChatError::Conflict);
} else if taken.is_some() {
// A verified identity belongs to one Macro person.
fail_pairing_in(&mut tx, pairing, account.id, account.generation)
.await?;
- rejection = Some(LinkedError::Forbidden);
+ rejection = Some(ExternalChatError::Forbidden);
} else {
sqlx::query!(
"UPDATE whatsapp_linked_account SET identity_jid = $2, state = 'connected', updated_at = now() WHERE id = $1",
@@ -341,7 +345,7 @@ impl SessionRepository for PgLinkedRepository {
&self,
scope: SessionScope,
pairing: Uuid,
- ) -> LinkedResult<Option<serde_json::Value>> {
+ ) -> ExternalChatResult<Option<serde_json::Value>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let response = sqlx::query_scalar!(
@@ -363,8 +367,8 @@ impl SessionRepository for PgLinkedRepository {
&self,
scope: SessionScope,
limit: i64,
- ) -> LinkedResult<Vec<MediaImport>> {
- PgLinkedRepository::claim_media_imports(self, scope, limit).await
+ ) -> ExternalChatResult<Vec<MediaImport>> {
+ PgWhatsAppRepository::claim_media_imports(self, scope, limit).await
}
async fn complete_media_import(
@@ -372,8 +376,8 @@ impl SessionRepository for PgLinkedRepository {
scope: SessionScope,
attachment: &AttachmentRef,
bytes: &[u8],
- ) -> LinkedResult<()> {
- PgLinkedRepository::complete_media_import(self, scope, attachment, bytes).await
+ ) -> ExternalChatResult<()> {
+ PgWhatsAppRepository::complete_media_import(self, scope, attachment, bytes).await
}
async fn fail_media_import(
@@ -381,15 +385,19 @@ impl SessionRepository for PgLinkedRepository {
scope: SessionScope,
attachment: &AttachmentRef,
failure: MediaFailure,
- ) -> LinkedResult<()> {
- PgLinkedRepository::fail_media_import(self, scope, attachment, failure).await
+ ) -> ExternalChatResult<()> {
+ PgWhatsAppRepository::fail_media_import(self, scope, attachment, failure).await
}
- async fn outbound_media(&self, scope: SessionScope, send: Uuid) -> LinkedResult<OutboundMedia> {
- PgLinkedRepository::outbound_media(self, scope, send).await
+ async fn outbound_media(
+ &self,
+ scope: SessionScope,
+ send: Uuid,
+ ) -> ExternalChatResult<OutboundMedia> {
+ PgWhatsAppRepository::outbound_media(self, scope, send).await
}
- async fn logged_out(&self, scope: SessionScope) -> LinkedResult<()> {
+ async fn logged_out(&self, scope: SessionScope) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_exclusive_in(&mut tx, scope).await?;
if account.state == AccountState::Connected {
@@ -409,7 +417,7 @@ impl SessionRepository for PgLinkedRepository {
scope: SessionScope,
groups: &[GroupInfo],
complete: bool,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_exclusive_in(&mut tx, scope).await?;
if account.state != AccountState::Connected {
@@ -424,7 +432,7 @@ impl SessionRepository for PgLinkedRepository {
scope: SessionScope,
group: &str,
observed_at: chrono::DateTime<chrono::Utc>,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_exclusive_in(&mut tx, scope).await?;
if account.state != AccountState::Connected {
@@ -434,7 +442,7 @@ impl SessionRepository for PgLinkedRepository {
tx.commit().await.map_err(failure)
}
- async fn observed_groups(&self, scope: SessionScope) -> LinkedResult<Vec<String>> {
+ async fn observed_groups(&self, scope: SessionScope) -> ExternalChatResult<Vec<String>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let groups = sqlx::query_scalar!(
@@ -457,7 +465,11 @@ impl SessionRepository for PgLinkedRepository {
})
}
- async fn update_chats(&self, scope: SessionScope, chats: &[ChatUpdate]) -> LinkedResult<bool> {
+ async fn update_chats(
+ &self,
+ scope: SessionScope,
+ chats: &[ChatUpdate],
+ ) -> ExternalChatResult<bool> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
if account.state != AccountState::Connected {
@@ -468,7 +480,7 @@ impl SessionRepository for PgLinkedRepository {
Ok(anchored)
}
- async fn backfill_work(&self, scope: SessionScope) -> LinkedResult<Vec<BackfillWork>> {
+ async fn backfill_work(&self, scope: SessionScope) -> ExternalChatResult<Vec<BackfillWork>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
if account.state != AccountState::Connected {
@@ -483,7 +495,7 @@ impl SessionRepository for PgLinkedRepository {
&self,
scope: SessionScope,
group: Uuid,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
chats::set_backfill_in(
@@ -497,14 +509,18 @@ impl SessionRepository for PgLinkedRepository {
tx.commit().await.map_err(failure)
}
- async fn backfill_requested(&self, scope: SessionScope, group: Uuid) -> LinkedResult<()> {
+ async fn backfill_requested(&self, scope: SessionScope, group: Uuid) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
chats::set_backfill_in(&mut tx, &account, group, BackfillState::Running, true).await?;
tx.commit().await.map_err(failure)
}
- async fn backfill_unanswered(&self, scope: SessionScope, group: Uuid) -> LinkedResult<()> {
+ async fn backfill_unanswered(
+ &self,
+ scope: SessionScope,
+ group: Uuid,
+ ) -> ExternalChatResult<()> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
chats::set_backfill_in(
@@ -522,7 +538,7 @@ impl SessionRepository for PgLinkedRepository {
&self,
scope: SessionScope,
page: &HistoryPage,
- ) -> LinkedResult<Vec<(Uuid, BackfillState)>> {
+ ) -> ExternalChatResult<Vec<(Uuid, BackfillState)>> {
let mut tx = self.pool.begin().await.map_err(failure)?;
let account = fence_in(&mut tx, scope).await?;
let states = chats::backfill_page_in(&mut tx, &account, page).await?;
@@ -534,16 +550,24 @@ impl SessionRepository for PgLinkedRepository {
&self,
scope: SessionScope,
event: &InboundEvent,
- ) -> LinkedResult<IngestOutcome> {
+ ) -> ExternalChatResult<IngestOutcome> {
self.ingest_event(scope, event).await
}
- async fn due_events(&self, scope: SessionScope, limit: i64) -> LinkedResult<Vec<InboundEvent>> {
- PgLinkedRepository::due_events(self, scope, limit).await
+ async fn due_events(
+ &self,
+ scope: SessionScope,
+ limit: i64,
+ ) -> ExternalChatResult<Vec<InboundEvent>> {
+ PgWhatsAppRepository::due_events(self, scope, limit).await
}
- async fn claim_sends(&self, scope: SessionScope, limit: i64) -> LinkedResult<Vec<ClaimedSend>> {
- PgLinkedRepository::claim_sends(self, scope, limit).await
+ async fn claim_sends(
+ &self,
+ scope: SessionScope,
+ limit: i64,
+ ) -> ExternalChatResult<Vec<ClaimedSend>> {
+ PgWhatsAppRepository::claim_sends(self, scope, limit).await
}
async fn begin_dispatch(
@@ -551,8 +575,8 @@ impl SessionRepository for PgLinkedRepository {
scope: SessionScope,
send: Uuid,
source_id: &str,
- ) -> LinkedResult<()> {
- PgLinkedRepository::begin_dispatch(self, scope, send, source_id).await
+ ) -> ExternalChatResult<()> {
+ PgWhatsAppRepository::begin_dispatch(self, scope, send, source_id).await
}
async fn record_send_outcome(
@@ -560,7 +584,7 @@ impl SessionRepository for PgLinkedRepository {
scope: SessionScope,
send: Uuid,
outcome: &SendOutcome,
- ) -> LinkedResult<()> {
- PgLinkedRepository::record_send_outcome(self, scope, send, outcome).await
+ ) -> ExternalChatResult<()> {
+ PgWhatsAppRepository::record_send_outcome(self, scope, send, outcome).await
}
}
diff --git a/crates/whatsapp_linked/src/outbound/pg/test.rs b/crates/whatsapp_linked/src/outbound/pg/test.rs
index 88029b1120..b8655d2f4b 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test.rs
@@ -1,10 +1,6 @@
use super::*;
-use crate::domain::timeline::TimelineRepository;
use crate::domain::{
- events::*,
- ports::{EffectsRepository, GroupEffect, GroupPublisher, Origin, SendRateLimiter},
- service::LinkedService,
- session::SessionService,
+ events::*, provider::WhatsAppProvider, service::WhatsAppService, session::SessionService,
};
use async_trait::async_trait;
use chrono::{Duration, Utc};
@@ -14,6 +10,12 @@ use entity_access::domain::models::{
use entity_access::domain::models::{
Entity, EntityAccessAuth, EntityAccessReceipt, EntityPermission, EntityType,
};
+use external_chat::domain::{
+ ports::{ChatEffect, ChatPublisher, EffectsRepository, Origin, SendRateLimiter},
+ service::ExternalChatService,
+ timeline::TimelineRepository,
+};
+use external_chat::outbound::pg::PgExternalChatRepository;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use std::sync::Arc;
@@ -40,10 +42,28 @@ const CUSTOMER_JID: &str = "[email protected]";
struct Allow;
-fn services(pool: &PgPool) -> (LinkedService, SessionService, Arc<PgLinkedRepository>) {
- let repository = Arc::new(PgLinkedRepository::new(pool.clone()));
+/// DSS's use cases over one pool: the WhatsApp account and chat list, and
+/// external chats with WhatsApp as their provider.
+struct Linked {
+ whatsapp: WhatsAppService,
+ chats: ExternalChatService<WhatsAppProvider>,
+ /// The external chat tables.
+ store: Arc<PgExternalChatRepository>,
+}
+
+fn services(pool: &PgPool) -> (Linked, SessionService, Arc<PgWhatsAppRepository>) {
+ let repository = Arc::new(PgWhatsAppRepository::new(pool.clone()));
+ let store = Arc::new(PgExternalChatRepository::new(pool.clone()));
(
- LinkedService::new(repository.clone(), Arc::new(Allow)),
+ Linked {
+ whatsapp: WhatsAppService::new(repository.clone()),
+ chats: ExternalChatService::new(
+ Arc::new(WhatsAppProvider::new(repository.clone())),
+ store.clone(),
+ Arc::new(Allow),
+ ),
+ store,
+ },
SessionService::new(repository.clone()),
repository,
)
@@ -101,13 +121,9 @@ async fn workspace(pool: &PgPool) -> Uuid {
}
/// Pair `user` as `jid` through the DSS and session-service paths.
-async fn pair(
- linked: &LinkedService,
- session: &SessionService,
- user: &str,
- jid: &str,
-) -> SessionScope {
+async fn pair(linked: &Linked, session: &SessionService, user: &str, jid: &str) -> SessionScope {
let pairing = linked
+ .whatsapp
.connect(&person(user), false, SendAuthority::Human)
.await
.unwrap();
@@ -200,10 +216,10 @@ fn receipt<T: entity_access::domain::models::RequiredPermission>(
async fn adopted(
pool: &PgPool,
) -> (
- LinkedService,
+ Linked,
SessionService,
- Arc<PgLinkedRepository>,
- Binding,
+ Arc<PgWhatsAppRepository>,
+ ChatRef,
SessionScope,
SessionScope,
) {
@@ -221,7 +237,8 @@ async fn adopted(
.await
.unwrap();
let binding = linked
- .adopt(
+ .chats
+ .add(
&person(ALICE),
&AddChat {
provider: ExternalChatProvider::WhatsApp,
@@ -253,7 +270,7 @@ async fn adopted(
async fn adoption_creates_a_shared_entity_and_leaving_whatsapp_keeps_view_access(pool: PgPool) {
let (linked, session, _, binding, _, bob) = adopted(&pool).await;
assert_eq!(binding.user_id, ALICE);
- let mine = &linked.chats(&person(ALICE)).await.unwrap().chats[0];
+ let mine = &linked.whatsapp.chats(&person(ALICE)).await.unwrap().chats[0];
assert_eq!(
(mine.added.clone(), mine.shared.clone()),
(
@@ -264,7 +281,7 @@ async fn adoption_creates_a_shared_entity_and_leaving_whatsapp_keeps_view_access
None
)
);
- let shared = &linked.chats(&person(BOB)).await.unwrap().chats[0];
+ let shared = &linked.whatsapp.chats(&person(BOB)).await.unwrap().chats[0];
assert_eq!(
(shared.added.clone(), shared.shared.clone()),
(
@@ -278,7 +295,8 @@ async fn adoption_creates_a_shared_entity_and_leaving_whatsapp_keeps_view_access
);
assert!(
linked
- .group_detail(&receipt::<MessageView>(BOB, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(BOB, binding.id))
.await
.unwrap()
.chat
@@ -301,10 +319,11 @@ async fn adoption_creates_a_shared_entity_and_leaving_whatsapp_keeps_view_access
let mut left = text(9, BOB_JID, "unused", "");
left.kind = InboundKind::Left;
session.ingest_event(bob, &left).await.unwrap();
- assert_eq!(linked.adopted_groups(&person(BOB)).await.unwrap().len(), 1);
+ assert_eq!(linked.chats.list(&person(BOB)).await.unwrap().len(), 1);
assert!(
!linked
- .group_detail(&receipt::<MessageView>(BOB, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(BOB, binding.id))
.await
.unwrap()
.chat
@@ -321,17 +340,19 @@ async fn each_person_adds_their_own_group_once(pool: PgPool) {
kind: ExternalChatKind::Group,
};
let again = linked
- .adopt(&person(ALICE), &request, SendAuthority::Human)
+ .chats
+ .add(&person(ALICE), &request, SendAuthority::Human)
.await
.unwrap();
assert_eq!(again.id, binding.id, "adding twice returns the same group");
let bobs = linked
- .adopt(&person(BOB), &request, SendAuthority::Human)
+ .chats
+ .add(&person(BOB), &request, SendAuthority::Human)
.await
.unwrap();
assert_ne!(bobs.id, binding.id);
assert_eq!(bobs.user_id, BOB);
- let listed = &linked.chats(&person(BOB)).await.unwrap().chats[0];
+ let listed = &linked.whatsapp.chats(&person(BOB)).await.unwrap().chats[0];
assert_eq!(
(listed.added.as_ref().map(|g| g.id), listed.shared.clone()),
(Some(bobs.id), None),
@@ -339,17 +360,18 @@ async fn each_person_adds_their_own_group_once(pool: PgPool) {
);
assert!(matches!(
linked
- .adopt(&person(CAROL), &request, SendAuthority::Human)
+ .chats
+ .add(&person(CAROL), &request, SendAuthority::Human)
.await
.unwrap_err()
.current_context(),
- LinkedError::Disconnected
+ ExternalChatError::Disconnected
));
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn observers_converge_on_one_group_message_and_unadopted_groups_are_dropped(pool: PgPool) {
- let (_, session, repository, binding, alice, bob) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, bob) = adopted(&pool).await;
assert_eq!(
session.observed_groups(alice).await.unwrap(),
vec![GROUP.to_owned()]
@@ -384,15 +406,16 @@ async fn observers_converge_on_one_group_message_and_unadopted_groups_are_droppe
.await
.unwrap();
assert_eq!(observations, 2);
- let effects = repository.pending_effects(10).await.unwrap();
+ let effects = linked.store.pending_effects(10).await.unwrap();
assert_eq!(effects.len(), 1);
assert!(effects[0].first_publication);
assert_eq!(effects[0].origin, Origin::Remote);
- repository
+ linked
+ .store
.published(effects[0].message_id, effects[0].revision)
.await
.unwrap();
- assert!(repository.pending_effects(10).await.unwrap().is_empty());
+ assert!(linked.store.pending_effects(10).await.unwrap().is_empty());
let mut elsewhere = text(3, CUSTOMER_JID, "3EB0BBBB", "private chat");
elsewhere.group_jid = "[email protected]".into();
@@ -505,7 +528,7 @@ async fn a_quarantined_lid_sender_ingests_once_the_roster_pairs_it(pool: PgPool)
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_reaction_before_its_message_is_deferred_and_replayed(pool: PgPool) {
- let (_, session, repository, binding, alice, _) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, _) = adopted(&pool).await;
let reaction = InboundEvent {
kind: InboundKind::Message(Message {
source_id: "3EB0REACT".into(),
@@ -551,8 +574,9 @@ async fn a_reaction_before_its_message_is_deferred_and_replayed(pool: PgPool) {
.fetch_one(&pool)
.await
.unwrap();
- let page = repository
- .messages(binding.id, BOB, None, 50)
+ let page = linked
+ .store
+ .messages(&binding, BOB, None, 50)
.await
.unwrap();
assert_eq!(page.messages[0].id, message);
@@ -637,6 +661,7 @@ async fn a_dispatched_send_becomes_unknown_after_takeover_and_settles_from_its_e
};
assert!(matches!(
linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&request,
@@ -646,7 +671,7 @@ async fn a_dispatched_send_becomes_unknown_after_takeover_and_settles_from_its_e
.err()
.unwrap()
.current_context(),
- LinkedError::Forbidden
+ ExternalChatError::Forbidden
));
let bot_receipt = EntityAccessReceipt::<MessageWrite>::try_new(
EntityAccessAuth::Bot(entity_access::domain::models::BotReceiptAuth::new(
@@ -670,16 +695,18 @@ async fn a_dispatched_send_becomes_unknown_after_takeover_and_settles_from_its_e
assert!(
matches!(
linked
+ .chats
.send_reply(&bot_receipt, &request, SendAuthority::UserApiToken)
.await
.err()
.unwrap()
.current_context(),
- LinkedError::Forbidden
+ ExternalChatError::Forbidden
),
"a bot acting for Alice still cannot send from her connection"
);
let progress = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&request,
@@ -689,6 +716,7 @@ async fn a_dispatched_send_becomes_unknown_after_takeover_and_settles_from_its_e
.unwrap();
assert_eq!(progress.state, DeliveryState::Pending);
let replay = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&request,
@@ -726,6 +754,7 @@ async fn a_dispatched_send_becomes_unknown_after_takeover_and_settles_from_its_e
.is_empty()
);
let state = linked
+ .chats
.delivery(&receipt::<MessageView>(ALICE, binding.id), &[progress.id])
.await
.unwrap();
@@ -746,6 +775,7 @@ async fn a_dispatched_send_becomes_unknown_after_takeover_and_settles_from_its_e
assert_eq!(rows[0].id, progress.id);
assert_eq!(rows[0].sender_id.as_deref(), Some(ALICE));
let state = linked
+ .chats
.delivery(&receipt::<MessageView>(ALICE, binding.id), &[progress.id])
.await
.unwrap();
@@ -767,6 +797,7 @@ async fn an_accepted_send_writes_the_group_message_and_its_echo_converges(pool:
.await
.unwrap();
let progress = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(BOB, binding.id),
&SendReply {
@@ -833,6 +864,7 @@ async fn an_accepted_send_writes_the_group_message_and_its_echo_converges(pool:
// Bob may edit his own recent message but not the customer's.
let edit_customer = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(BOB, binding.id),
&SendReply {
@@ -850,9 +882,10 @@ async fn an_accepted_send_writes_the_group_message_and_its_echo_converges(pool:
.unwrap_err();
assert!(matches!(
edit_customer.current_context(),
- LinkedError::Forbidden
+ ExternalChatError::Forbidden
));
linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(BOB, binding.id),
&SendReply {
@@ -874,6 +907,7 @@ async fn an_accepted_send_writes_the_group_message_and_its_echo_converges(pool:
async fn disconnecting_fails_undispatched_sends_and_stops_observation(pool: PgPool) {
let (linked, session, _, binding, alice, _) = adopted(&pool).await;
let progress = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&SendReply {
@@ -888,8 +922,14 @@ async fn disconnecting_fails_undispatched_sends_and_stops_observation(pool: PgPo
)
.await
.unwrap();
- let account = linked.account(&person(ALICE)).await.unwrap().unwrap();
+ let account = linked
+ .whatsapp
+ .account(&person(ALICE))
+ .await
+ .unwrap()
+ .unwrap();
let result = linked
+ .whatsapp
.disconnect(
&person(ALICE),
account.authorization_generation,
@@ -900,6 +940,7 @@ async fn disconnecting_fails_undispatched_sends_and_stops_observation(pool: PgPo
assert!(result.remote_unlink_requested);
assert!(
!linked
+ .whatsapp
.disconnect(
&person(ALICE),
account.authorization_generation,
@@ -911,6 +952,7 @@ async fn disconnecting_fails_undispatched_sends_and_stops_observation(pool: PgPo
"a retried disconnect is a no-op"
);
let state = linked
+ .chats
.delivery(&receipt::<MessageView>(ALICE, binding.id), &[progress.id])
.await
.unwrap();
@@ -943,11 +985,13 @@ async fn stored_group_facts_authorize_adoption_and_sending_however_old(pool: PgP
kind: ExternalChatKind::Group,
};
let binding = linked
- .adopt(&person(ALICE), &request, SendAuthority::Human)
+ .chats
+ .add(&person(ALICE), &request, SendAuthority::Human)
.await
.unwrap();
let detail = linked
- .group_detail(&receipt::<MessageView>(ALICE, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(ALICE, binding.id))
.await
.unwrap();
assert!(detail.chat.can_send);
@@ -956,6 +1000,7 @@ async fn stored_group_facts_authorize_adoption_and_sending_however_old(pool: PgP
"unnamed groups show members"
);
let progress = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&SendReply {
@@ -985,7 +1030,8 @@ async fn stored_group_facts_authorize_adoption_and_sending_however_old(pool: PgP
.unwrap();
assert!(matches!(
linked
- .adopt(
+ .chats
+ .add(
&person(ALICE),
&AddChat {
provider: ExternalChatProvider::WhatsApp,
@@ -997,7 +1043,7 @@ async fn stored_group_facts_authorize_adoption_and_sending_however_old(pool: PgP
.await
.unwrap_err()
.current_context(),
- LinkedError::CannotAdd(CannotAdd::Community)
+ ExternalChatError::CannotAdd(CannotAdd::Community)
));
}
@@ -1032,7 +1078,10 @@ async fn messages_stamped_around_adoption_follow_the_second_granular_boundary(po
#[async_trait]
impl SendRateLimiter for Allow {
- async fn check(&self, _: &macro_user_id::user_id::MacroUserIdStr<'_>) -> LinkedResult<()> {
+ async fn check(
+ &self,
+ _: &macro_user_id::user_id::MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<()> {
Ok(())
}
}
@@ -1043,13 +1092,20 @@ async fn rate_limited_send_never_creates_a_dispatchable_intent(pool: PgPool) {
#[async_trait]
impl SendRateLimiter for Denied {
- async fn check(&self, _: &macro_user_id::user_id::MacroUserIdStr<'_>) -> LinkedResult<()> {
- Err(LinkedError::RateLimited.into())
+ async fn check(
+ &self,
+ _: &macro_user_id::user_id::MacroUserIdStr<'_>,
+ ) -> ExternalChatResult<()> {
+ Err(ExternalChatError::RateLimited.into())
}
}
- let (_, session, repository, binding, alice, _) = adopted(&pool).await;
- let linked = LinkedService::new(repository, Arc::new(Denied));
+ let (linked, session, repository, binding, alice, _) = adopted(&pool).await;
+ let chats = ExternalChatService::new(
+ Arc::new(WhatsAppProvider::new(repository)),
+ linked.store,
+ Arc::new(Denied),
+ );
let request = SendReply {
idempotency_key: Uuid::now_v7(),
message: ReplyContent::Text {
@@ -1058,7 +1114,7 @@ async fn rate_limited_send_never_creates_a_dispatchable_intent(pool: PgPool) {
},
reply_to: None,
};
- let error = linked
+ let error = chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&request,
@@ -1066,7 +1122,7 @@ async fn rate_limited_send_never_creates_a_dispatchable_intent(pool: PgPool) {
)
.await
.unwrap_err();
- assert_eq!(error.current_context(), &LinkedError::RateLimited);
+ assert_eq!(error.current_context(), &ExternalChatError::RateLimited);
assert!(session.claim_sends(alice, 8).await.unwrap().is_empty());
}
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/changes.rs b/crates/whatsapp_linked/src/outbound/pg/test/changes.rs
index 1d54416200..b82249c2ae 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/changes.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/changes.rs
@@ -1,6 +1,6 @@
//! `external_chat_changed` signals: only for real changes, and to the right scope.
use super::*;
-use signals::{Change, ChangeSignal};
+use external_chat::signals::{self as signals, Change, ChangeSignal};
use sqlx::postgres::PgListener;
async fn listen(pool: &PgPool) -> PgListener {
@@ -40,7 +40,8 @@ async fn rosters_signal_only_real_changes_and_rename_the_owners_group(pool: PgPo
}
);
let detail = linked
- .group_detail(&receipt::<MessageView>(ALICE, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(ALICE, binding.id))
.await
.unwrap();
assert_eq!(detail.chat.name, "Escalations, renamed");
@@ -61,6 +62,7 @@ async fn rosters_signal_only_real_changes_and_rename_the_owners_group(pool: PgPo
);
assert!(
linked
+ .whatsapp
.chats(&person(ALICE))
.await
.unwrap()
@@ -69,7 +71,8 @@ async fn rosters_signal_only_real_changes_and_rename_the_owners_group(pool: PgPo
.all(|chat| !chat.can_add)
);
let detail = linked
- .group_detail(&receipt::<MessageView>(ALICE, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(ALICE, binding.id))
.await
.unwrap();
assert_eq!(detail.chat.send_reason, Some(ReplyBlock::NotAdded));
@@ -90,6 +93,7 @@ async fn messages_and_send_outcomes_signal_their_group(pool: PgPool) {
}
);
let progress = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&SendReply {
@@ -140,6 +144,7 @@ async fn a_retried_send_returns_the_original_whatever_happened_since(pool: PgPoo
};
let access = receipt::<MessageWrite>(ALICE, binding.id);
let original = linked
+ .chats
.send_reply(&access, &request, SendAuthority::Human)
.await
.unwrap();
@@ -155,6 +160,7 @@ async fn a_retried_send_returns_the_original_whatever_happened_since(pool: PgPoo
// Alice is no longer in the group: a new send would be refused.
session.group_left(alice, GROUP, Utc::now()).await.unwrap();
let retried = linked
+ .chats
.send_reply(&access, &request, SendAuthority::Human)
.await
.unwrap();
@@ -170,20 +176,25 @@ async fn a_retried_send_returns_the_original_whatever_happened_since(pool: PgPoo
};
assert_eq!(
linked
+ .chats
.send_reply(&access, &changed, SendAuthority::Human)
.await
.unwrap_err()
.current_context(),
- &LinkedError::Conflict
+ &ExternalChatError::Conflict
);
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn change_audiences_come_from_the_store(pool: PgPool) {
- use crate::domain::ports::{AudienceRepository, MessageNotice};
- let (linked, session, repository, binding, alice, _) = adopted(&pool).await;
+ use external_chat::domain::ports::{AudienceRepository, MessageNotice};
+ let (linked, session, _, binding, alice, _) = adopted(&pool).await;
assert_eq!(
- repository.account_owner(alice.account_id).await.unwrap(),
+ linked
+ .whatsapp
+ .account_owner(alice.account_id)
+ .await
+ .unwrap(),
Some(ALICE.to_owned())
);
session
@@ -191,6 +202,7 @@ async fn change_audiences_come_from_the_store(pool: PgPool) {
.await
.unwrap();
let message = linked
+ .chats
.messages(&receipt::<MessageView>(ALICE, binding.id), None, Some(1))
.await
.unwrap()
@@ -204,7 +216,11 @@ async fn change_audiences_come_from_the_store(pool: PgPool) {
.await
.unwrap();
assert_eq!(
- repository.message_notice(binding.id, stored).await.unwrap(),
+ linked
+ .store
+ .message_notice(binding.id, stored)
+ .await
+ .unwrap(),
Some(MessageNotice {
provider: ExternalChatProvider::WhatsApp,
chat_name: "Customer escalations".into(),
@@ -212,7 +228,8 @@ async fn change_audiences_come_from_the_store(pool: PgPool) {
})
);
assert!(
- repository
+ linked
+ .store
.link_viewers(binding.id)
.await
.unwrap()
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/chats.rs b/crates/whatsapp_linked/src/outbound/pg/test/chats.rs
index 92b1d566ba..e4733783b4 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/chats.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/chats.rs
@@ -77,7 +77,7 @@ async fn the_chat_list_merges_history_activity_and_the_roster(pool: PgPool) {
.await
.unwrap();
- let chats = linked.chats(&person(ALICE)).await.unwrap().chats;
+ let chats = linked.whatsapp.chats(&person(ALICE)).await.unwrap().chats;
let summary: Vec<_> = chats
.iter()
.map(|chat| {
@@ -283,8 +283,14 @@ async fn ending_consent_forgets_the_old_numbers_chat_list(pool: PgPool) {
.unwrap()
};
assert!(stored(pool.clone()).await > 0);
- let account = linked.account(&person(ALICE)).await.unwrap().unwrap();
+ let account = linked
+ .whatsapp
+ .account(&person(ALICE))
+ .await
+ .unwrap()
+ .unwrap();
linked
+ .whatsapp
.disconnect(
&person(ALICE),
account.authorization_generation,
@@ -316,7 +322,7 @@ async fn one_batch_may_name_a_chat_twice(pool: PgPool) {
)
.await
.unwrap();
- let listed = linked.chats(&person(ALICE)).await.unwrap().chats;
+ let listed = linked.whatsapp.chats(&person(ALICE)).await.unwrap().chats;
let direct = listed.iter().find(|chat| chat.jid == DIRECT).unwrap();
assert_eq!(
(direct.name.as_str(), direct.last_activity_at),
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/communities.rs b/crates/whatsapp_linked/src/outbound/pg/test/communities.rs
index e3935ab9da..f691e4ad8f 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/communities.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/communities.rs
@@ -49,7 +49,7 @@ async fn disappearing_and_community_groups_are_added_like_any_other(pool: PgPool
.await
.unwrap();
- let chats = linked.chats(&person(ALICE)).await.unwrap().chats;
+ let chats = linked.whatsapp.chats(&person(ALICE)).await.unwrap().chats;
let listed = |jid: &str| {
let chat = chats.iter().find(|chat| chat.jid == jid).unwrap();
(chat.can_add, chat.cannot_add_reason, chat.community.clone())
@@ -73,19 +73,21 @@ async fn disappearing_and_community_groups_are_added_like_any_other(pool: PgPool
let linked = &linked;
async move {
linked
- .adopt(&person(ALICE), &request, SendAuthority::Human)
+ .chats
+ .add(&person(ALICE), &request, SendAuthority::Human)
.await
}
};
assert!(matches!(
adopt(COMMUNITY).await.unwrap_err().current_context(),
- LinkedError::CannotAdd(CannotAdd::Community)
+ ExternalChatError::CannotAdd(CannotAdd::Community)
));
- let detail = |binding: Binding| {
+ let detail = |binding: ChatRef| {
let linked = &linked;
async move {
linked
- .group_detail(&receipt::<MessageView>(ALICE, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(ALICE, binding.id))
.await
.unwrap()
}
@@ -108,7 +110,7 @@ async fn disappearing_and_community_groups_are_added_like_any_other(pool: PgPool
assert_eq!(notices.chat.send_reason, Some(ReplyBlock::NotAllowed));
// The list carries the community too, so it can group the chats.
- let listed = linked.adopted_groups(&person(ALICE)).await.unwrap();
+ let listed = linked.chats.list(&person(ALICE)).await.unwrap();
let community_of = |id: Uuid| {
listed
.iter()
@@ -130,6 +132,7 @@ async fn disappearing_and_community_groups_are_added_like_any_other(pool: PgPool
IngestOutcome::Applied
);
let kept = linked
+ .chats
.messages(&receipt::<MessageView>(ALICE, vanishing.id), None, Some(10))
.await
.unwrap()
@@ -143,6 +146,7 @@ async fn disappearing_and_community_groups_are_added_like_any_other(pool: PgPool
// Macro's sends carry the group's timer.
linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, vanishing.id),
&SendReply {
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs b/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
index 6fad8f0796..0b9b87ede2 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/concurrency.rs
@@ -1,12 +1,13 @@
use super::*;
-use crate::domain::ports::{AccountRepository, SendIntent, SendRepository};
+use crate::domain::{ports::SendRepository, provider::WhatsAppProvider};
+use external_chat::domain::ports::{ChatProvider, SendIntent};
use tokio::sync::Barrier;
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn sends_and_roster_refreshes_share_a_consistent_lock_order(pool: PgPool) {
let (_, session, repository, binding, alice, _) = adopted(&pool).await;
- let account = repository
- .owned_account(&person(ALICE))
+ let account = WhatsAppProvider::new(repository.clone())
+ .account(&person(ALICE))
.await
.unwrap()
.unwrap();
@@ -26,7 +27,7 @@ async fn sends_and_roster_refreshes_share_a_consistent_lock_order(pool: PgPool)
.enqueue(SendIntent {
user: &person(ALICE),
account: &account,
- binding: &binding,
+ chat: &binding,
request: &request,
digest: [index; 32],
})
@@ -57,7 +58,8 @@ async fn observers_can_refresh_groups_in_opposite_orders(pool: PgPool) {
.await
.unwrap();
linked
- .adopt(
+ .chats
+ .add(
&person(ALICE),
&AddChat {
provider: ExternalChatProvider::WhatsApp,
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/deletion.rs b/crates/whatsapp_linked/src/outbound/pg/test/deletion.rs
index cf154a6b4e..1af9341fac 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/deletion.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/deletion.rs
@@ -58,6 +58,7 @@ async fn deleting_people_who_sent_removes_their_whatsapp_data(pool: PgPool) {
.unwrap();
for (user, text) in [(ALICE, "from alice"), (BOB, "from bob")] {
linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(user, binding.id),
&text_reply(text),
@@ -118,7 +119,7 @@ async fn an_alias_outlives_the_account_that_saw_it(pool: PgPool) {
async fn receipts_are_stored_once_per_message_and_expire(pool: PgPool) {
use crate::domain::{
events::{Receipt, ReceiptState},
- ports::MediaRepository,
+ ports::AccountRepository,
};
let (_, session, repository, binding, alice, _) = adopted(&pool).await;
let receipt = InboundEvent {
@@ -158,35 +159,37 @@ async fn receipts_are_stored_once_per_message_and_expire(pool: PgPool) {
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn only_the_owners_session_stops_syncing_a_chat(pool: PgPool) {
- let (linked, _, repository, binding, _, _) = adopted(&pool).await;
+ let (linked, _, _, binding, _, _) = adopted(&pool).await;
for (user, authority) in [
(ALICE, SendAuthority::UserApiToken),
(ALICE, SendAuthority::Automation),
(BOB, SendAuthority::Human),
] {
let error = linked
+ .chats
.detach(&receipt::<MessageView>(user, binding.id), authority)
.await
.unwrap_err();
- assert_eq!(error.current_context(), &LinkedError::Forbidden);
+ assert_eq!(error.current_context(), &ExternalChatError::Forbidden);
}
linked
+ .chats
.detach(
&receipt::<MessageView>(ALICE, binding.id),
SendAuthority::Human,
)
.await
.unwrap();
- use crate::domain::ports::BindingRepository;
+ use external_chat::domain::ports::ChatRepository;
assert_eq!(
- repository.binding(binding.id).await.unwrap().unwrap().state,
- BindingState::Detached
+ linked.store.chat(binding.id).await.unwrap().unwrap().state,
+ ChatState::Detached
);
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn consumed_pairings_are_pruned_after_a_day(pool: PgPool) {
- use crate::domain::ports::MediaRepository;
+ use crate::domain::ports::AccountRepository;
let (_, _, repository, _, alice, _) = adopted(&pool).await;
let pairings = |pool: PgPool| async move {
sqlx::query_scalar!(
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/direct.rs b/crates/whatsapp_linked/src/outbound/pg/test/direct.rs
index 2cad17ee28..8fa05d566c 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/direct.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/direct.rs
@@ -41,7 +41,7 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
.await
.unwrap();
- let listed = linked.chats(&person(ALICE)).await.unwrap().chats;
+ let listed = linked.whatsapp.chats(&person(ALICE)).await.unwrap().chats;
let chat = listed.iter().find(|chat| chat.jid == CUSTOMER_JID).unwrap();
assert_eq!(
(chat.kind, chat.can_add, chat.cannot_add_reason),
@@ -54,11 +54,12 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
};
assert!(matches!(
linked
- .adopt(&person(ALICE), &own, SendAuthority::Human)
+ .chats
+ .add(&person(ALICE), &own, SendAuthority::Human)
.await
.unwrap_err()
.current_context(),
- LinkedError::CannotAdd(CannotAdd::OwnChat)
+ ExternalChatError::CannotAdd(CannotAdd::OwnChat)
));
let as_group = AddChat {
provider: ExternalChatProvider::WhatsApp,
@@ -67,11 +68,12 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
};
assert!(matches!(
linked
- .adopt(&person(ALICE), &as_group, SendAuthority::Human)
+ .chats
+ .add(&person(ALICE), &as_group, SendAuthority::Human)
.await
.unwrap_err()
.current_context(),
- LinkedError::Invalid(_)
+ ExternalChatError::Invalid(_)
));
let request = AddChat {
provider: ExternalChatProvider::WhatsApp,
@@ -79,13 +81,15 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
kind: ExternalChatKind::Direct,
};
let binding = linked
- .adopt(&person(ALICE), &request, SendAuthority::Human)
+ .chats
+ .add(&person(ALICE), &request, SendAuthority::Human)
.await
.unwrap();
assert_eq!(binding.kind, ExternalChatKind::Direct);
assert_eq!(
linked
- .adopt(&person(ALICE), &request, SendAuthority::Human)
+ .chats
+ .add(&person(ALICE), &request, SendAuthority::Human)
.await
.unwrap()
.id,
@@ -94,7 +98,8 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
);
let detail = linked
- .group_detail(&receipt::<MessageView>(ALICE, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(ALICE, binding.id))
.await
.unwrap();
assert_eq!(
@@ -106,7 +111,7 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
(ExternalChatKind::Direct, "Customer", true)
);
assert!(detail.members.is_none());
- let bobs_list = linked.chats(&person(BOB)).await.unwrap().chats;
+ let bobs_list = linked.whatsapp.chats(&person(BOB)).await.unwrap().chats;
let bobs = bobs_list
.iter()
.find(|chat| chat.jid == CUSTOMER_JID)
@@ -128,6 +133,7 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
);
}
let messages = linked
+ .chats
.messages(&receipt::<MessageView>(ALICE, binding.id), None, Some(10))
.await
.unwrap()
@@ -170,7 +176,8 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
tx.commit().await.unwrap();
session.update_groups(bob, &[], true).await.unwrap();
let bobs_view = linked
- .group_detail(&receipt::<MessageView>(BOB, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(BOB, binding.id))
.await
.unwrap();
assert_eq!(
@@ -187,6 +194,7 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
};
assert!(
linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(BOB, binding.id),
&send(BOB),
@@ -196,6 +204,7 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
.is_err()
);
linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&send(ALICE),
@@ -221,7 +230,8 @@ async fn a_direct_chat_is_added_kept_and_sent_to_only_by_its_owner(pool: PgPool)
session.update_groups(alice, &[], true).await.unwrap();
assert_eq!(
linked
- .group_detail(&receipt::<MessageView>(ALICE, binding.id))
+ .chats
+ .detail(&receipt::<MessageView>(ALICE, binding.id))
.await
.unwrap()
.chat
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/media.rs b/crates/whatsapp_linked/src/outbound/pg/test/media.rs
index 25d4028594..e398b61386 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/media.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/media.rs
@@ -1,5 +1,5 @@
use super::*;
-use crate::domain::{
+use external_chat::domain::{
media::MediaService,
ports::{FileImport, FileStore, MediaRepository},
};
@@ -25,11 +25,11 @@ struct Files {
#[async_trait]
impl FileStore for Files {
- async fn store(&self, import: &FileImport, bytes: Vec<u8>) -> LinkedResult<Uuid> {
+ async fn store(&self, import: &FileImport, bytes: Vec<u8>) -> ExternalChatResult<Uuid> {
let mut fail = self.fail.lock().unwrap();
if *fail > 0 {
*fail -= 1;
- return Err(LinkedError::Unavailable.into());
+ return Err(ExternalChatError::Unavailable.into());
}
let file_id = Uuid::now_v7();
self.stored.lock().unwrap().push(StoredFile {
@@ -41,7 +41,7 @@ impl FileStore for Files {
Ok(file_id)
}
- async fn delete(&self, file_id: Uuid) -> LinkedResult<()> {
+ async fn delete(&self, file_id: Uuid) -> ExternalChatResult<()> {
self.deleted.lock().unwrap().push(file_id);
Ok(())
}
@@ -84,7 +84,7 @@ async fn content(pool: &PgPool, chat_id: Uuid) -> Vec<String> {
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn an_inbound_photo_is_downloaded_once_and_becomes_a_static_file(pool: PgPool) {
- let (linked, session, repository, binding, alice, bob) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, bob) = adopted(&pool).await;
let bytes = b"jpeg bytes".to_vec();
session
.ingest_event(alice, &photo(1, CUSTOMER_JID, "3EB0PIC", &bytes))
@@ -120,7 +120,7 @@ async fn an_inbound_photo_is_downloaded_once_and_becomes_a_static_file(pool: PgP
.await
.unwrap_err()
.current_context(),
- LinkedError::Invalid(_)
+ ExternalChatError::Invalid(_)
));
assert_eq!(content(&pool, binding.id).await, vec![""]);
let first = sqlx::query_scalar!(
@@ -131,6 +131,7 @@ async fn an_inbound_photo_is_downloaded_once_and_becomes_a_static_file(pool: PgP
.await
.unwrap();
let states = linked
+ .chats
.message_attachments(&receipt::<MessageView>(ALICE, binding.id), &[first])
.await
.unwrap();
@@ -149,7 +150,7 @@ async fn an_inbound_photo_is_downloaded_once_and_becomes_a_static_file(pool: PgP
.unwrap();
let files = Arc::new(Files::default());
*files.fail.lock().unwrap() = 1;
- let media = MediaService::new(repository.clone(), files.clone());
+ let media = MediaService::new(linked.store.clone(), files.clone());
assert_eq!(media.import_pending().await.unwrap(), 0);
sqlx::query!("UPDATE external_chat_attachment SET retry_at = now() WHERE state = 'downloaded'")
.execute(&pool)
@@ -182,6 +183,7 @@ async fn an_inbound_photo_is_downloaded_once_and_becomes_a_static_file(pool: PgP
.await
.unwrap();
let states = linked
+ .chats
.message_attachments(&receipt::<MessageView>(ALICE, binding.id), &[second])
.await
.unwrap();
@@ -225,7 +227,7 @@ async fn an_inbound_photo_is_downloaded_once_and_becomes_a_static_file(pool: PgP
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn deleting_a_chats_owner_deletes_its_files(pool: PgPool) {
- let (_, session, repository, binding, alice, _) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, _) = adopted(&pool).await;
let bytes = b"jpeg bytes".to_vec();
session
.ingest_event(alice, &photo(1, CUSTOMER_JID, "3EB0PIC", &bytes))
@@ -237,7 +239,7 @@ async fn deleting_a_chats_owner_deletes_its_files(pool: PgPool) {
.await
.unwrap();
let files = Arc::new(Files::default());
- let media = MediaService::new(repository, files.clone());
+ let media = MediaService::new(linked.store.clone(), files.clone());
assert_eq!(media.import_pending().await.unwrap(), 1);
let file_id = files.stored.lock().unwrap()[0].file_id;
assert!(released_files(&pool).await.is_empty());
@@ -253,7 +255,7 @@ async fn deleting_a_chats_owner_deletes_its_files(pool: PgPool) {
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_file_stored_after_its_message_was_deleted_is_deleted(pool: PgPool) {
- let (_, session, repository, _, alice, _) = adopted(&pool).await;
+ let (linked, session, _, _, alice, _) = adopted(&pool).await;
let bytes = b"jpeg bytes".to_vec();
session
.ingest_event(alice, &photo(1, CUSTOMER_JID, "3EB0PIC", &bytes))
@@ -264,7 +266,7 @@ async fn a_file_stored_after_its_message_was_deleted_is_deleted(pool: PgPool) {
.complete_media_import(alice, &claimed[0].attachment, &bytes)
.await
.unwrap();
- let imports = repository.due_imports(8).await.unwrap();
+ let imports = linked.store.due_imports(8).await.unwrap();
let mut delete = text(2, CUSTOMER_JID, "3EB0DEL", "");
if let InboundKind::Message(message) = &mut delete.kind {
message.kind = MessageKind::Delete;
@@ -276,7 +278,7 @@ async fn a_file_stored_after_its_message_was_deleted_is_deleted(pool: PgPool) {
session.ingest_event(alice, &delete).await.unwrap();
// The import was already under way: its file has no attachment to go to.
let late = Uuid::now_v7();
- repository.import_stored(&imports[0], late).await.unwrap();
+ linked.store.import_stored(&imports[0], late).await.unwrap();
assert_eq!(released_files(&pool).await, vec![late]);
let ready = sqlx::query_scalar!(
r#"SELECT count(*) AS "count!" FROM external_chat_attachment WHERE file_id IS NOT NULL"#
@@ -289,9 +291,10 @@ async fn a_file_stored_after_its_message_was_deleted_is_deleted(pool: PgPool) {
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn an_uploaded_attachment_is_sent_once_and_becomes_the_messages_file(pool: PgPool) {
- let (linked, session, repository, binding, alice, _) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, _) = adopted(&pool).await;
let bytes = b"%PDF-1.7 quote".to_vec();
let uploaded = linked
+ .chats
.upload_attachment(
&receipt::<MessageWrite>(ALICE, binding.id),
"quote.pdf",
@@ -304,6 +307,7 @@ async fn an_uploaded_attachment_is_sent_once_and_becomes_the_messages_file(pool:
assert_eq!(uploaded.kind, MediaKind::Document);
assert!(
linked
+ .chats
.upload_attachment(
&receipt::<MessageWrite>(ALICE, binding.id),
"tool.exe",
@@ -325,6 +329,7 @@ async fn an_uploaded_attachment_is_sent_once_and_becomes_the_messages_file(pool:
};
assert!(
linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(BOB, binding.id),
&media(None),
@@ -334,6 +339,7 @@ async fn an_uploaded_attachment_is_sent_once_and_becomes_the_messages_file(pool:
.is_err()
);
let progress = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&media(Some("Signed quote")),
@@ -375,7 +381,7 @@ async fn an_uploaded_attachment_is_sent_once_and_becomes_the_messages_file(pool:
"attachment state is separate from the plain caption"
);
let files = Arc::new(Files::default());
- MediaService::new(repository, files.clone())
+ MediaService::new(linked.store.clone(), files.clone())
.import_pending()
.await
.unwrap();
@@ -391,6 +397,7 @@ async fn a_passkey_assertion_is_forwarded_once(pool: PgPool) {
workspace(&pool).await;
let (linked, session, _) = services(&pool);
let pairing = linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap();
@@ -398,6 +405,7 @@ async fn a_passkey_assertion_is_forwarded_once(pool: PgPool) {
let assertion = serde_json::json!({ "id": "credential", "response": {} });
assert!(
linked
+ .whatsapp
.passkey(&person(ALICE), pairing.id, &assertion, SendAuthority::Human)
.await
.is_err(),
@@ -414,6 +422,7 @@ async fn a_passkey_assertion_is_forwarded_once(pool: PgPool) {
.await
.unwrap();
let state = linked
+ .whatsapp
.pair_state(&person(ALICE), pairing.id, SendAuthority::Human)
.await
.unwrap();
@@ -423,11 +432,13 @@ async fn a_passkey_assertion_is_forwarded_once(pool: PgPool) {
);
assert!(
linked
+ .whatsapp
.passkey(&person(BOB), pairing.id, &assertion, SendAuthority::Human)
.await
.is_err()
);
linked
+ .whatsapp
.passkey(&person(ALICE), pairing.id, &assertion, SendAuthority::Human)
.await
.unwrap();
@@ -492,6 +503,7 @@ async fn a_sent_image_keeps_the_size_read_from_its_file(pool: PgPool) {
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
let uploaded = linked
+ .chats
.upload_attachment(
&receipt::<MessageWrite>(ALICE, binding.id),
"plan.png",
@@ -502,6 +514,7 @@ async fn a_sent_image_keeps_the_size_read_from_its_file(pool: PgPool) {
.await
.unwrap();
let progress = linked
+ .chats
.send_reply(
&receipt::<MessageWrite>(ALICE, binding.id),
&SendReply {
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/pairing.rs b/crates/whatsapp_linked/src/outbound/pg/test/pairing.rs
index 12cbc9a176..03614f230a 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/pairing.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/pairing.rs
@@ -6,12 +6,14 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
workspace(&pool).await;
let (linked, session, _) = services(&pool);
let pairing = linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap();
assert_eq!(pairing.state, PairingState::Waiting);
assert_eq!(
linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap()
@@ -21,12 +23,13 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
);
assert!(matches!(
linked
+ .whatsapp
.pair_state(&person(BOB), pairing.id, SendAuthority::Human)
.await
.err()
.unwrap()
.current_context(),
- LinkedError::NotFound
+ ExternalChatError::NotFound
));
let lease = session.begin_lease(pairing.account_id).await.unwrap();
session
@@ -40,16 +43,18 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
assert!(
matches!(
linked
+ .whatsapp
.pair_state(&person(ALICE), pairing.id, SendAuthority::UserApiToken)
.await
.err()
.unwrap()
.current_context(),
- LinkedError::Forbidden
+ ExternalChatError::Forbidden
),
"automation cannot read live enrollment material"
);
let state = linked
+ .whatsapp
.pair_state(&person(ALICE), pairing.id, SendAuthority::Human)
.await
.unwrap();
@@ -66,6 +71,7 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
.await
.unwrap();
let state = linked
+ .whatsapp
.pair_state(&person(ALICE), pairing.id, SendAuthority::Human)
.await
.unwrap();
@@ -77,15 +83,17 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
assert_eq!(state.identity_jid.as_deref(), Some(ALICE_JID));
assert!(matches!(
linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap_err()
.current_context(),
- LinkedError::Conflict
+ ExternalChatError::Conflict
));
// Bob cannot pair Alice's WhatsApp account.
let bob = linked
+ .whatsapp
.connect(&person(BOB), false, SendAuthority::Human)
.await
.unwrap();
@@ -100,9 +108,13 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
)
.await
.unwrap_err();
- assert!(matches!(stolen.current_context(), LinkedError::Forbidden));
+ assert!(matches!(
+ stolen.current_context(),
+ ExternalChatError::Forbidden
+ ));
assert_eq!(
linked
+ .whatsapp
.pair_state(&person(BOB), bob.id, SendAuthority::Human)
.await
.unwrap()
@@ -110,13 +122,25 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
PairingState::Failed
);
assert_eq!(
- linked.account(&person(BOB)).await.unwrap().unwrap().state,
+ linked
+ .whatsapp
+ .account(&person(BOB))
+ .await
+ .unwrap()
+ .unwrap()
+ .state,
AccountState::Disconnected
);
// A reconnect must come back as the same identity.
- let account = linked.account(&person(ALICE)).await.unwrap().unwrap();
+ let account = linked
+ .whatsapp
+ .account(&person(ALICE))
+ .await
+ .unwrap()
+ .unwrap();
linked
+ .whatsapp
.disconnect(
&person(ALICE),
account.authorization_generation,
@@ -125,6 +149,7 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
.await
.unwrap();
let reconnect = linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap();
@@ -139,7 +164,10 @@ async fn pairing_is_owner_bound_and_a_verified_identity_cannot_change_hands(pool
)
.await
.unwrap_err();
- assert!(matches!(swapped.current_context(), LinkedError::Conflict));
+ assert!(matches!(
+ swapped.current_context(),
+ ExternalChatError::Conflict
+ ));
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -147,29 +175,39 @@ async fn cancelling_pairing_ends_consent_and_signals_the_replica(pool: PgPool) {
workspace(&pool).await;
let (linked, session, _) = services(&pool);
let pairing = linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap();
assert_eq!(
linked
+ .whatsapp
.cancel(&person(ALICE), pairing.id, SendAuthority::Automation)
.await
.unwrap_err()
.current_context(),
- &LinkedError::Forbidden
+ &ExternalChatError::Forbidden
);
linked
+ .whatsapp
.cancel(&person(ALICE), pairing.id, SendAuthority::Human)
.await
.unwrap();
linked
+ .whatsapp
.cancel(&person(ALICE), pairing.id, SendAuthority::Human)
.await
.unwrap();
let work = session.pairing_work(pairing.id).await.unwrap().unwrap();
assert_eq!(work.state, PairingState::Cancelled);
assert_eq!(
- linked.account(&person(ALICE)).await.unwrap().unwrap().state,
+ linked
+ .whatsapp
+ .account(&person(ALICE))
+ .await
+ .unwrap()
+ .unwrap()
+ .state,
AccountState::Disconnected
);
}
@@ -179,6 +217,7 @@ async fn an_expired_qr_session_reads_as_expired_and_a_new_connect_starts_fresh(p
workspace(&pool).await;
let (linked, session, _) = services(&pool);
let pairing = linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap();
@@ -197,16 +236,24 @@ async fn an_expired_qr_session_reads_as_expired_and_a_new_connect_starts_fresh(p
.unwrap();
assert_eq!(work.state, PairingState::Expired);
let state = linked
+ .whatsapp
.pair_state(&person(ALICE), pairing.id, SendAuthority::Human)
.await
.unwrap();
assert_eq!(state.state, PairingState::Expired);
assert!(state.qr.is_none(), "an expired code is not scannable");
assert_eq!(
- linked.account(&person(ALICE)).await.unwrap().unwrap().state,
+ linked
+ .whatsapp
+ .account(&person(ALICE))
+ .await
+ .unwrap()
+ .unwrap()
+ .state,
AccountState::Disconnected
);
let next = linked
+ .whatsapp
.connect(&person(ALICE), false, SendAuthority::Human)
.await
.unwrap();
@@ -236,7 +283,7 @@ async fn a_newer_lease_fences_the_previous_replica(pool: PgPool) {
] {
assert!(matches!(
result.unwrap_err().current_context(),
- LinkedError::Fenced
+ ExternalChatError::Fenced
));
}
}
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/rich.rs b/crates/whatsapp_linked/src/outbound/pg/test/rich.rs
index f4db5534f5..dc6fe2eccc 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/rich.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/rich.rs
@@ -1,8 +1,8 @@
use super::*;
-use crate::domain::rich::{
+use external_chat::domain::rich::{
Location, Poll, PollOption, PollVote, RichContent, SystemAction, SystemLine,
};
-use crate::domain::timeline::{
+use external_chat::domain::timeline::{
ExternalChatMessageKind, ExternalChatPersonRef, ExternalChatPollOption, ExternalChatRichContent,
};
@@ -29,7 +29,7 @@ fn person(name: &str, user: Option<&str>) -> ExternalChatPersonRef {
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_system_line_names_people_and_never_notifies_or_counts_as_unread(pool: PgPool) {
- let (linked, session, repository, binding, alice, _) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, _) = adopted(&pool).await;
let added = with(
text(30, CUSTOMER_JID, "SYSTEM-ADD", ""),
MessageKind::System,
@@ -46,6 +46,7 @@ async fn a_system_line_names_people_and_never_notifies_or_counts_as_unread(pool:
session.ingest_event(alice, &added).await.unwrap();
let messages = linked
+ .chats
.messages(&receipt::<MessageView>(BOB, binding.id), None, Some(50))
.await
.unwrap()
@@ -59,14 +60,15 @@ async fn a_system_line_names_people_and_never_notifies_or_counts_as_unread(pool:
assert_eq!(line.actor, Some(person("Customer", None)));
assert_eq!(line.participants, vec![person("+15550000002", None)]);
assert_eq!(
- repository
- .group_facts(binding.id, BOB)
+ linked
+ .store
+ .chat_facts(binding.id, BOB)
.await
.unwrap()
.unread_count,
0
);
- let effects = repository.pending_effects(10).await.unwrap();
+ let effects = linked.store.pending_effects(10).await.unwrap();
assert_eq!(effects.len(), 1);
assert!(effects[0].system);
assert!(!effects[0].should_notify());
@@ -108,8 +110,9 @@ fn vote(event: u128, voter: &str, source: &str, revision: i64, selected: &[&str]
event
}
-async fn tallies(linked: &LinkedService, group: Uuid) -> Vec<ExternalChatPollOption> {
+async fn tallies(linked: &Linked, group: Uuid) -> Vec<ExternalChatPollOption> {
let messages = linked
+ .chats
.messages(&receipt::<MessageView>(ALICE, group), None, Some(50))
.await
.unwrap()
@@ -213,6 +216,7 @@ async fn a_re_imported_placeholder_gains_its_content_and_mentions_read_as_names(
session.ingest_event(alice, &mention).await.unwrap();
let messages = linked
+ .chats
.messages(&receipt::<MessageView>(ALICE, binding.id), None, Some(50))
.await
.unwrap()
@@ -258,6 +262,7 @@ async fn a_change_whatsapp_made_without_naming_anyone_is_the_groups_own_line(poo
session.ingest_event(alice, &renamed).await.unwrap();
let messages = linked
+ .chats
.messages(&receipt::<MessageView>(ALICE, binding.id), None, Some(50))
.await
.unwrap()
diff --git a/crates/whatsapp_linked/src/outbound/pg/test/timeline.rs b/crates/whatsapp_linked/src/outbound/pg/test/timeline.rs
index 41dd31198b..2a271cfc2a 100644
--- a/crates/whatsapp_linked/src/outbound/pg/test/timeline.rs
+++ b/crates/whatsapp_linked/src/outbound/pg/test/timeline.rs
@@ -1,7 +1,7 @@
use super::*;
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn shared_viewer_without_whatsapp_reads_history_and_unread_but_cannot_send(pool: PgPool) {
- let (linked, session, repository, binding, alice, _) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, _) = adopted(&pool).await;
session
.ingest_event(alice, &text(20, CUSTOMER_JID, "VIEWER", "Shared history"))
.await
@@ -33,12 +33,16 @@ async fn shared_viewer_without_whatsapp_reads_history_and_unread_but_cannot_send
)
.await
.unwrap();
- let detail = linked.group_detail(&receipt).await.unwrap();
+ let detail = linked.chats.detail(&receipt).await.unwrap();
assert_eq!(detail.chat.name, "Customer escalations");
assert_eq!(detail.chat.unread_count, 1);
assert_eq!(detail.chat.send_reason, Some(ReplyBlock::NoAccount));
assert!(detail.sending_identity.is_none());
- let messages = linked.messages(&receipt, None, Some(50)).await.unwrap();
+ let messages = linked
+ .chats
+ .messages(&receipt, None, Some(50))
+ .await
+ .unwrap();
assert_eq!(messages.messages[0].body, "Shared history");
let bot_access = EntityAccessReceipt::<MessageView>::try_new_bot(
"bot|00000000-0000-0000-0000-000000000123"
@@ -59,6 +63,7 @@ async fn shared_viewer_without_whatsapp_reads_history_and_unread_but_cannot_send
.unwrap();
assert_eq!(
linked
+ .chats
.messages(&bot_access, None, Some(50))
.await
.unwrap()
@@ -66,22 +71,25 @@ async fn shared_viewer_without_whatsapp_reads_history_and_unread_but_cannot_send
.body,
"Shared history"
);
- let bot_detail = linked.group_detail(&bot_access).await.unwrap();
+ let bot_detail = linked.chats.detail(&bot_access).await.unwrap();
assert!(!bot_detail.chat.can_send);
assert!(bot_detail.sending_identity.is_none());
assert!(
linked
+ .chats
.mark_read(&bot_access, messages.messages[0].id)
.await
.is_err()
);
linked
+ .chats
.mark_read(&receipt, messages.messages[0].id)
.await
.unwrap();
assert_eq!(
- repository
- .group_facts(binding.id, CAROL)
+ linked
+ .store
+ .chat_facts(binding.id, CAROL)
.await
.unwrap()
.unread_count,
@@ -97,14 +105,16 @@ async fn shared_viewer_without_whatsapp_reads_history_and_unread_but_cannot_send
};
assert_eq!(
*linked
+ .chats
.send_reply(&receipt, &request, SendAuthority::UserApiToken)
.await
.unwrap_err()
.current_context(),
- LinkedError::Disconnected
+ ExternalChatError::Disconnected
);
assert!(
linked
+ .chats
.upload_attachment(
&receipt,
"test.txt",
@@ -144,7 +154,7 @@ async fn shared_viewer_without_whatsapp_reads_history_and_unread_but_cannot_send
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn timeline_cursor_and_read_cursor_are_group_scoped_and_monotone(pool: PgPool) {
- let (linked, session, repository, binding, alice, _) = adopted(&pool).await;
+ let (linked, session, _, binding, alice, _) = adopted(&pool).await;
let mut early = text(30, CUSTOMER_JID, "EARLY", "older");
early.source_at = Utc::now();
let mut late = text(31, CUSTOMER_JID, "LATE", "newer");
@@ -152,32 +162,41 @@ async fn timeline_cursor_and_read_cursor_are_group_scoped_and_monotone(pool: PgP
session.ingest_event(alice, &early).await.unwrap();
session.ingest_event(alice, &late).await.unwrap();
let receipt = receipt::<MessageView>(BOB, binding.id);
- let page = linked.messages(&receipt, None, Some(1)).await.unwrap();
+ let page = linked
+ .chats
+ .messages(&receipt, None, Some(1))
+ .await
+ .unwrap();
assert_eq!(page.messages[0].body, "newer");
let next = linked
+ .chats
.messages(&receipt, page.next_cursor, Some(1))
.await
.unwrap();
assert_eq!(next.messages[0].body, "older");
assert!(next.next_cursor.is_none());
linked
+ .chats
.mark_read(&receipt, page.messages[0].id)
.await
.unwrap();
linked
+ .chats
.mark_read(&receipt, next.messages[0].id)
.await
.unwrap();
assert_eq!(
- repository
- .group_facts(binding.id, BOB)
+ linked
+ .store
+ .chat_facts(binding.id, BOB)
.await
.unwrap()
.unread_count,
0
);
assert!(
- repository
+ linked
+ .store
.mark_read(Uuid::now_v7(), &person(BOB), page.messages[0].id)
.await
.is_err()
@@ -201,8 +220,8 @@ async fn timeline_cursor_and_read_cursor_are_group_scoped_and_monotone(pool: PgP
#[derive(Default)]
struct Notifications(std::sync::Mutex<Vec<Uuid>>);
#[async_trait]
-impl GroupPublisher for Notifications {
- async fn publish(&self, effect: &GroupEffect) -> LinkedResult<()> {
+impl ChatPublisher for Notifications {
+ async fn publish(&self, effect: &ChatEffect) -> ExternalChatResult<()> {
self.0.lock().unwrap().push(effect.message_id);
Ok(())
}
@@ -210,10 +229,10 @@ impl GroupPublisher for Notifications {
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn notifications_are_once_for_live_messages_and_never_history_or_edits(pool: PgPool) {
- let (_, session, repository, _, alice, _) = adopted(&pool).await;
+ let (linked, session, _, _, alice, _) = adopted(&pool).await;
let notifications = Arc::new(Notifications::default());
let effects =
- crate::domain::effects::EffectsService::new(repository.clone(), notifications.clone());
+ external_chat::domain::effects::EffectsService::new(linked.store, notifications.clone());
let mut history = text(40, CUSTOMER_JID, "HISTORY", "old message");
if let InboundKind::Message(message) = &mut history.kind {
message.history = true;
@@ -254,26 +273,23 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
};
assert!(
linked
+ .chats
.update_permissions(&viewer, &update, SendAuthority::Human)
.await
.is_err()
);
- assert!(
- linked
- .adopted_groups(&person(CAROL))
- .await
- .unwrap()
- .is_empty()
- );
+ assert!(linked.chats.list(&person(CAROL)).await.unwrap().is_empty());
linked
+ .chats
.update_permissions(&owner, &update, SendAuthority::Human)
.await
.unwrap();
- let shared = linked.adopted_groups(&person(CAROL)).await.unwrap();
+ let shared = linked.chats.list(&person(CAROL)).await.unwrap();
assert_eq!(shared[0].id, binding.id);
assert_eq!(shared[0].send_reason, Some(ReplyBlock::NoAccount));
assert_eq!(
linked
+ .chats
.permissions(&owner)
.await
.unwrap()
@@ -281,6 +297,7 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
Some(AccessLevel::View)
);
linked
+ .chats
.update_permissions(
&owner,
&models_permissions::share_permission::UpdateSharePermissionRequestV2 {
@@ -291,13 +308,7 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
)
.await
.unwrap();
- assert!(
- linked
- .adopted_groups(&person(CAROL))
- .await
- .unwrap()
- .is_empty()
- );
+ assert!(linked.chats.list(&person(CAROL)).await.unwrap().is_empty());
session
.ingest_event(alice, &text(50, CUSTOMER_JID, "PUBLIC", "Link sharing"))
.await
@@ -311,6 +322,7 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
};
for refused in [AccessLevel::Comment, AccessLevel::Edit] {
let error = linked
+ .chats
.update_permissions(
&owner,
&UpdateSharePermissionRequestV2 {
@@ -321,20 +333,18 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
)
.await
.unwrap_err();
- assert!(matches!(error.current_context(), LinkedError::Invalid(_)));
+ assert!(matches!(
+ error.current_context(),
+ ExternalChatError::Invalid(_)
+ ));
}
linked
+ .chats
.update_permissions(&owner, &public, SendAuthority::Human)
.await
.unwrap();
// Public links grant known-ID access, never global group discovery.
- assert!(
- linked
- .adopted_groups(&person(CAROL))
- .await
- .unwrap()
- .is_empty()
- );
+ assert!(linked.chats.list(&person(CAROL)).await.unwrap().is_empty());
use entity_access::domain::ports::EntityAccessService;
let access = entity_access::domain::service::EntityAccessServiceImpl::new(
entity_access::outbound::PgAccessRepository::new(pool.clone()),
@@ -360,16 +370,19 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
)
.await
.unwrap();
- let page = linked.messages(&shared, None, Some(50)).await.unwrap();
+ let page = linked
+ .chats
+ .messages(&shared, None, Some(50))
+ .await
+ .unwrap();
linked
+ .chats
.mark_read(&shared, page.messages[0].id)
.await
.unwrap();
- assert_eq!(
- linked.adopted_groups(&person(CAROL)).await.unwrap().len(),
- 1
- );
+ assert_eq!(linked.chats.list(&person(CAROL)).await.unwrap().len(), 1);
linked
+ .chats
.update_permissions(
&owner,
&UpdateSharePermissionRequestV2 {
@@ -381,7 +394,7 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
)
.await
.unwrap();
- let permission = linked.permissions(&owner).await.unwrap();
+ let permission = linked.chats.permissions(&owner).await.unwrap();
assert_eq!(permission.link_share, Some(LinkShare::Public));
assert_eq!(permission.link_share_access_level, Some(AccessLevel::View));
assert_eq!(
@@ -396,6 +409,7 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
Some(AccessLevel::View)
);
linked
+ .chats
.update_permissions(
&owner,
&UpdateSharePermissionRequestV2 {
@@ -418,13 +432,7 @@ async fn canonical_team_sharing_is_owner_only_and_reversible(pool: PgPool) {
.unwrap(),
None
);
- assert!(
- linked
- .adopted_groups(&person(CAROL))
- .await
- .unwrap()
- .is_empty()
- );
+ assert!(linked.chats.list(&person(CAROL)).await.unwrap().is_empty());
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -439,10 +447,11 @@ async fn shared_sender_uses_own_roster_without_adopting_again(pool: PgPool) {
.await
.unwrap();
let access = receipt::<MessageView>(BOB, binding.id);
- let detail = linked.group_detail(&access).await.unwrap();
+ let detail = linked.chats.detail(&access).await.unwrap();
assert!(detail.chat.can_send);
assert_eq!(detail.sending_identity.as_deref(), Some(BOB_JID));
let progress = linked
+ .chats
.send_reply(
&access,
&SendReply {
@@ -483,13 +492,7 @@ async fn a_group_mentioned_in_a_channel_is_readable_by_its_current_members(pool:
.execute(&pool)
.await
.unwrap();
- assert!(
- linked
- .adopted_groups(&person(CAROL))
- .await
- .unwrap()
- .is_empty()
- );
+ assert!(linked.chats.list(&person(CAROL)).await.unwrap().is_empty());
// What mentioning the group in the channel writes.
let mut tx = pool.begin().await.unwrap();
entity_access_db_utils::channel_share::insert_if_absent(
@@ -502,7 +505,7 @@ async fn a_group_mentioned_in_a_channel_is_readable_by_its_current_members(pool:
.await
.unwrap();
tx.commit().await.unwrap();
- let listed = linked.adopted_groups(&person(CAROL)).await.unwrap();
+ let listed = linked.chats.list(&person(CAROL)).await.unwrap();
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].id, binding.id);
assert_eq!(listed[0].send_reason, Some(ReplyBlock::NoAccount));
@@ -515,11 +518,7 @@ async fn a_group_mentioned_in_a_channel_is_readable_by_its_current_members(pool:
.await
.unwrap();
assert!(
- linked
- .adopted_groups(&person(CAROL))
- .await
- .unwrap()
- .is_empty(),
+ linked.chats.list(&person(CAROL)).await.unwrap().is_empty(),
"leaving the channel ends access"
);
}
@@ -538,12 +537,16 @@ async fn only_the_owners_own_session_changes_sharing(pool: PgPool) {
};
for authority in [SendAuthority::UserApiToken, SendAuthority::Automation] {
let error = linked
+ .chats
.update_permissions(&owner, &public, authority)
.await
.unwrap_err();
- assert_eq!(error.current_context(), &LinkedError::Forbidden);
+ assert_eq!(error.current_context(), &ExternalChatError::Forbidden);
}
- assert_eq!(linked.permissions(&owner).await.unwrap().link_share, None);
+ assert_eq!(
+ linked.chats.permissions(&owner).await.unwrap().link_share,
+ None
+ );
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
@@ -581,6 +584,7 @@ async fn the_owner_sees_and_revokes_channel_grants(pool: PgPool) {
transaction.commit().await.unwrap();
assert_eq!(
linked
+ .chats
.permissions(&owner)
.await
.unwrap()
@@ -591,6 +595,7 @@ async fn the_owner_sees_and_revokes_channel_grants(pool: PgPool) {
}])
);
let error = linked
+ .chats
.update_permissions(
&owner,
&grants(UpdateOperation::Replace, Some(AccessLevel::Edit)),
@@ -598,8 +603,12 @@ async fn the_owner_sees_and_revokes_channel_grants(pool: PgPool) {
)
.await
.unwrap_err();
- assert!(matches!(error.current_context(), LinkedError::Invalid(_)));
+ assert!(matches!(
+ error.current_context(),
+ ExternalChatError::Invalid(_)
+ ));
linked
+ .chats
.update_permissions(
&owner,
&grants(UpdateOperation::Remove, None),
@@ -609,6 +618,7 @@ async fn the_owner_sees_and_revokes_channel_grants(pool: PgPool) {
.unwrap();
assert_eq!(
linked
+ .chats
.permissions(&owner)
.await
.unwrap()
@@ -627,10 +637,18 @@ async fn one_message_reads_like_its_timeline_entry(pool: PgPool) {
.unwrap();
}
let viewer = receipt::<MessageView>(BOB, binding.id);
- let page = linked.messages(&viewer, None, Some(50)).await.unwrap();
+ let page = linked
+ .chats
+ .messages(&viewer, None, Some(50))
+ .await
+ .unwrap();
let older = page.messages.last().unwrap();
- let one = linked.message(&viewer, older.id).await.unwrap();
+ let one = linked.chats.message(&viewer, older.id).await.unwrap();
assert_eq!((one.id, one.body.as_str()), (older.id, older.body.as_str()));
- let missing = linked.message(&viewer, Uuid::now_v7()).await.unwrap_err();
- assert_eq!(missing.current_context(), &LinkedError::NotFound);
+ let missing = linked
+ .chats
+ .message(&viewer, Uuid::now_v7())
+ .await
+ .unwrap_err();
+ assert_eq!(missing.current_context(), &ExternalChatError::NotFound);
}
diff --git a/crates/whatsapp_linked/src/signals.rs b/crates/whatsapp_linked/src/signals.rs
index 49095bc978..98eabe79d2 100644
--- a/crates/whatsapp_linked/src/signals.rs
+++ b/crates/whatsapp_linked/src/signals.rs
@@ -1,10 +1,6 @@
-//! Signals between DSS and the session service over Postgres `NOTIFY`, and
-//! the trace context they carry. Each payload is a JSON [`Signal`] (or
-//! [`ChangeSignal`] for [`CHANGED`]) with the sender's `traceparent`, so the
-//! receiver joins its trace. Shared by both sides; not an adapter.
-
-use serde::{Deserialize, Serialize};
-use uuid::Uuid;
+//! Signals between DSS and the session service over Postgres `NOTIFY`. Each
+//! payload is an [`external_chat::signals::Signal`] with the sender's
+//! `traceparent`; the channels DSS listens on are in `external_chat::signals`.
/// `<pairing_id>`: start, update or stop pairing.
pub const PAIRING_REQUESTED: &str = "whatsapp_pairing_requested";
@@ -16,69 +12,3 @@ pub const GROUPS_REQUESTED: &str = "whatsapp_roster_requested";
pub const ACCOUNT_CHANGED: &str = "whatsapp_account_changed";
/// `<account_id>`: a replica released the account (session service only).
pub const ACCOUNT_RELEASED: &str = "whatsapp_account_released";
-/// `<blob_id>`: attachment bytes are ready to import (DSS listens).
-pub const MEDIA_READY: &str = "whatsapp_media_ready";
-/// `<message_id>`: an external chat message notification is ready (DSS listens).
-pub const CHAT_EFFECTS: &str = "external_chat_effects";
-/// A [`ChangeSignal`]: something a page shows changed (DSS listens and
-/// pushes `external_chat_changed` to the people who see it).
-pub const CHANGED: &str = "external_chat_changed";
-
-/// Payload of every signal except [`CHANGED`].
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
-pub struct Signal {
- /// The id the channel documents.
- pub id: Uuid,
- /// The sender's W3C trace context.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub traceparent: Option<String>,
-}
-
-impl Signal {
- /// Read a payload; `None` when malformed.
- pub fn parse(payload: &str) -> Option<Self> {
- serde_json::from_str(payload).ok()
- }
-}
-
-/// What changed.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
-#[serde(tag = "scope", rename_all = "snake_case")]
-pub enum Change {
- /// The account's own chat list changed: its owner's add list.
- Chats {
- /// Account whose chat list changed.
- account_id: Uuid,
- },
- /// An external chat's details, timeline or sends changed: everyone who
- /// can view it.
- Chat {
- /// External chat entity.
- chat_id: Uuid,
- },
-}
-
-/// Payload of [`CHANGED`].
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
-pub struct ChangeSignal {
- /// What changed.
- #[serde(flatten)]
- pub change: Change,
- /// The sender's W3C trace context.
- #[serde(default, skip_serializing_if = "Option::is_none")]
- pub traceparent: Option<String>,
-}
-
-impl ChangeSignal {
- /// Read a payload; `None` when malformed.
- pub fn parse(payload: &str) -> Option<Self> {
- serde_json::from_str(payload).ok()
- }
-}
-
-/// W3C trace context across the Postgres hops.
-#[cfg(feature = "outbound")]
-pub mod trace_context;
-
-#[cfg(test)]
-mod test;
diff --git a/docs/architecture/external-chats.md b/docs/architecture/external-chats.md
index 7db891ce61..f3b0ea31f4 100644
--- a/docs/architecture/external-chats.md
+++ b/docs/architecture/external-chats.md
@@ -100,11 +100,22 @@ and deleted from the static file service by DSS's drain.
## Code
-`crates/whatsapp_linked` holds both the generic domain (timeline, read
-state, sharing, sends, uploads, notification effects, router) and the
-WhatsApp provider. DSS composes it in `src/external_chat.rs`. The web app's
-conversation is `features/external-chat`; the External tab lists every
-provider's chats (`features/external-view`).
+`crates/external_chat` is the provider-neutral domain: timeline and read
+state, sharing, sends, uploads, attachment import, notification effects, the
+`external_chat_changed`/`external_chat_effects` signals, the
+`/external-chats` router and the Postgres adapters for the `external_chat*`
+tables. It reaches a provider only through its `ChatProvider` port
+(accounts, who can send, members and names, adding and detaching, the send
+outbox); `ExternalChatService<P: ChatProvider>` is generic over it, with no
+trait objects or registry. `crates/whatsapp_linked` implements the port
+(`WhatsAppProvider`) and keeps everything WhatsApp: pairing, the account's
+chat list, rosters, ingest, dispatch and the `/whatsapp` router; its ingest
+writes the generic tables with `external_chat::outbound::pg` helpers.
+Dependencies point from `whatsapp_linked` to `external_chat`, never back.
+DSS composes `ExternalChatService<WhatsAppProvider>` in
+`src/external_chat.rs`. The web app's conversation is
+`features/external-chat`; the External tab lists every provider's chats
+(`features/external-view`).
## Follow-ups
diff --git a/docs/architecture/whatsapp-linked-devices.md b/docs/architecture/whatsapp-linked-devices.md
index bce3293d59..ea962c5a06 100644
--- a/docs/architecture/whatsapp-linked-devices.md
+++ b/docs/architecture/whatsapp-linked-devices.md
@@ -16,9 +16,12 @@ Web app ──REST──▶ DSS ──── MacroDB ◀──── whatsapp_se
rows + NOTIFY one task + one client per leased account
```
-- **DSS** serves `/whatsapp/*` (account, pairing, chat list) and
- `/external-chats/*` (`crates/whatsapp_linked/src/inbound`), authorizes
- people, writes chats and send intents, and publishes notifications.
+- **DSS** serves `/whatsapp/*` (account, pairing, chat list;
+ `crates/whatsapp_linked/src/inbound`) and `/external-chats/*`
+ (`crates/external_chat/src/inbound`, with `WhatsAppProvider` from
+ `crates/whatsapp_linked/src/domain/provider.rs` as its `ChatProvider`),
+ authorizes people, writes chats and send intents, and publishes
+ notifications.
- **Session service** holds each account's advisory lock, runs the WhatsApp
socket, and calls `whatsapp_linked::domain::session::SessionService`
in-process. There is no service-to-service HTTP.
@@ -149,7 +152,7 @@ are deferred. Edits follow source revision order; deletes are permanent tombston
Reactions and media live in `external_chat_reaction` and `external_chat_attachment`.
Besides text and media, a message may carry structured content in
-`external_chat_message.rich` (`whatsapp_linked::domain::rich::RichContent`):
+`external_chat_message.rich` (`external_chat::domain::rich::RichContent`):
polls, locations, contact cards and WhatsApp events, plus `mentions` (WhatsApp
ids the text @-mentions) and `forwarded`. Poll votes are decrypted by the
replica with the poll's message secret and kept per voter in
diff --git a/services/document_storage_service/AGENTS.md b/services/document_storage_service/AGENTS.md
index 7caa316774..b6146dafa6 100644
--- a/services/document_storage_service/AGENTS.md
+++ b/services/document_storage_service/AGENTS.md
@@ -23,8 +23,9 @@ threads, pins, recents, notifications, folders, activity.
- `src/server.rs`: composition root, including access-request routes and the
tracked delivery dispatcher; `src/outbound/access_requests.rs` composes the
notification/withdrawal/realtime ports. Policy lives in `crates/access_requests`.
-- `src/external_chat.rs`: mounts `/external-chats` and `/whatsapp` from
- `crates/whatsapp_linked` and runs the notification/import worker (LISTEN
+- `src/external_chat.rs`: composes `ExternalChatService<WhatsAppProvider>`,
+ mounts `/external-chats` (`crates/external_chat`) and `/whatsapp`
+ (`crates/whatsapp_linked`) and runs the notification/import worker (LISTEN
`external_chat_effects`, `whatsapp_media_ready`; attachments become static
files through the static file service, which also deletes released ones) and
the `external_chat_changed` pusher (bare connection-gateway events to the
diff --git a/services/document_storage_service/Cargo.toml b/services/document_storage_service/Cargo.toml
index 90d2f8188b..2577994875 100644
--- a/services/document_storage_service/Cargo.toml
+++ b/services/document_storage_service/Cargo.toml
@@ -245,6 +245,7 @@ secretsmanager_client = { path = "../../crates/secretsmanager_client" }
serde = { workspace = true }
serde_json = { workspace = true }
whatsapp_linked = { path = "../../crates/whatsapp_linked", features = ["inbound", "outbound"] }
+external_chat = { path = "../../crates/external_chat", features = ["inbound", "outbound"] }
slack_integration = { path = "../../crates/slack_integration", features = [
"gateway",
"inbound",
diff --git a/services/document_storage_service/src/api.rs b/services/document_storage_service/src/api.rs
index 4990828cfc..40bee25def 100644
--- a/services/document_storage_service/src/api.rs
+++ b/services/document_storage_service/src/api.rs
@@ -290,13 +290,15 @@ fn api_router(state: ApiContext) -> Router {
)
.nest(
"/external-chats",
- whatsapp_linked::inbound::router::external_chats_router(
- state.external_chat_state.clone(),
+ external_chat::inbound::router::external_chats_router(
+ state.external_chat_state.chats.clone(),
),
)
.nest(
"/whatsapp",
- whatsapp_linked::inbound::router::whatsapp_router(state.external_chat_state.clone()),
+ whatsapp_linked::inbound::router::whatsapp_router(
+ state.external_chat_state.whatsapp.clone(),
+ ),
)
.nest(
"/reminders",
diff --git a/services/document_storage_service/src/api/swagger.rs b/services/document_storage_service/src/api/swagger.rs
index 5d26acab3b..eb70ce398a 100644
--- a/services/document_storage_service/src/api/swagger.rs
+++ b/services/document_storage_service/src/api/swagger.rs
@@ -450,20 +450,20 @@ use utoipa::OpenApi;
slack_integration::inbound::axum_router::jobs::cancel,
// External chats (any provider)
- whatsapp_linked::inbound::router::list,
- whatsapp_linked::inbound::router::add,
- whatsapp_linked::inbound::router::chat,
- whatsapp_linked::inbound::router::remove,
- whatsapp_linked::inbound::router::messages,
- whatsapp_linked::inbound::router::message,
- whatsapp_linked::inbound::router::message_attachments,
- whatsapp_linked::inbound::router::mark_read,
- whatsapp_linked::inbound::router::permissions,
- whatsapp_linked::inbound::router::update_permissions,
- whatsapp_linked::inbound::router::open_sends,
- whatsapp_linked::inbound::router::send,
- whatsapp_linked::inbound::router::upload,
- whatsapp_linked::inbound::router::delivery,
+ external_chat::inbound::router::list,
+ external_chat::inbound::router::add,
+ external_chat::inbound::router::chat,
+ external_chat::inbound::router::remove,
+ external_chat::inbound::router::messages,
+ external_chat::inbound::router::message,
+ external_chat::inbound::router::message_attachments,
+ external_chat::inbound::router::mark_read,
+ external_chat::inbound::router::permissions,
+ external_chat::inbound::router::update_permissions,
+ external_chat::inbound::router::open_sends,
+ external_chat::inbound::router::send,
+ external_chat::inbound::router::upload,
+ external_chat::inbound::router::delivery,
// Personal WhatsApp linked as a device
whatsapp_linked::inbound::router::account,
diff --git a/services/document_storage_service/src/external_chat.rs b/services/document_storage_service/src/external_chat.rs
index d59d21785c..688d87d1dd 100644
--- a/services/document_storage_service/src/external_chat.rs
+++ b/services/document_storage_service/src/external_chat.rs
@@ -1,13 +1,29 @@
//! External chats (WhatsApp is the one provider): composition of the
-//! `whatsapp_linked` crate with DSS's entity access, notification and static file
-//! clients, the notification outbox worker, and the `external_chat_changed`
-//! pushes to open pages. The session service runs the WhatsApp sockets; it
-//! reaches DSS only through MacroDB rows and `NOTIFY`.
+//! `external_chat` crate, with `whatsapp_linked` as its provider, and DSS's
+//! entity access, notification and static file clients; the notification
+//! outbox worker, and the `external_chat_changed` pushes to open pages. The
+//! session service runs the WhatsApp sockets; it reaches DSS only through
+//! MacroDB rows and `NOTIFY`.
use crate::api::context::{AuthorizationService, EntityAccessService, NotificationIngressType};
use async_trait::async_trait;
use connection_gateway_client::client::ConnectionGatewayClient;
use entity_access::domain::ports::EntityAccessService as _;
+use external_chat::{
+ domain::{
+ effects::EffectsService,
+ media::MediaService,
+ models::ExternalChatError,
+ ports::{
+ AudienceRepository, ChatEffect, ChatPublisher, ExternalChatResult, FileImport,
+ FileStore, Origin, SendRateLimiter,
+ },
+ service::ExternalChatService,
+ },
+ inbound::router::ExternalChatRouterState,
+ outbound::pg::PgExternalChatRepository,
+ signals::{self, Change, ChangeSignal, trace_context},
+};
use macro_user_id::user_id::MacroUserIdStr;
use model_entity::EntityType;
use notification::domain::{models::SendNotificationRequestBuilder, service::NotificationIngress};
@@ -18,23 +34,19 @@ use tokio_util::{sync::CancellationToken, task::TaskTracker};
use tracing::Instrument as _;
use uuid::Uuid;
use whatsapp_linked::{
- domain::{
- effects::EffectsService,
- media::MediaService,
- models::LinkedError,
- ports::{
- AudienceRepository, FileImport, FileStore, GroupEffect, GroupPublisher, LinkedResult,
- Origin, SendRateLimiter,
- },
- service::LinkedService,
- },
- inbound::router::LinkedRouterState,
- outbound::pg::PgLinkedRepository,
- signals::{self, Change, ChangeSignal, trace_context},
+ domain::{provider::WhatsAppProvider, service::WhatsAppService},
+ inbound::router::WhatsAppRouterState,
+ outbound::pg::PgWhatsAppRepository,
};
-/// Router state mounted at `/external-chats` and `/whatsapp`.
-pub(crate) type State = LinkedRouterState<EntityAccessService, AuthorizationService>;
+/// Router states mounted at `/external-chats` and `/whatsapp`.
+#[derive(Clone)]
+pub(crate) struct State {
+ /// Added chats of every provider.
+ pub chats: ExternalChatRouterState<WhatsAppProvider, EntityAccessService, AuthorizationService>,
+ /// The caller's WhatsApp account, pairing and chat list.
+ pub whatsapp: WhatsAppRouterState<AuthorizationService>,
+}
/// Clients the linked composition needs from DSS.
pub(crate) struct Dependencies {
@@ -62,14 +74,17 @@ pub(crate) fn build(dependencies: Dependencies) -> State {
tracker,
cancellation,
} = dependencies;
- let repository = Arc::new(PgLinkedRepository::new(pool.clone()));
- let publisher = Arc::new(DssGroupPublisher {
+ let whatsapp_repository = Arc::new(PgWhatsAppRepository::new(pool.clone()));
+ let repository = Arc::new(PgExternalChatRepository::new(pool.clone()));
+ let publisher = Arc::new(DssChatPublisher {
audience: repository.clone(),
access: access.clone(),
notifications,
});
let media = MediaService::new(repository.clone(), Arc::new(StaticFiles(static_files)));
- let service = LinkedService::new(
+ let whatsapp = Arc::new(WhatsAppService::new(whatsapp_repository.clone()));
+ let service = ExternalChatService::new(
+ Arc::new(WhatsAppProvider::new(whatsapp_repository)),
repository.clone(),
Arc::new(SendRequestLimiter(rate_limit::RateLimitServiceImpl {
repo: rate_limit::RedisRateLimitAdapter { redis },
@@ -79,6 +94,7 @@ pub(crate) fn build(dependencies: Dependencies) -> State {
ChangePusher {
pool: pool.clone(),
audience: repository.clone(),
+ whatsapp: whatsapp.clone(),
access: access.clone(),
gateway: connection_gateway,
},
@@ -88,12 +104,19 @@ pub(crate) fn build(dependencies: Dependencies) -> State {
pool,
EffectsService::new(repository, publisher),
media,
+ whatsapp.clone(),
cancellation,
));
- LinkedRouterState {
- service: Arc::new(service),
- entity_access_service: access,
- authorization_state: authorization,
+ State {
+ chats: ExternalChatRouterState {
+ service: Arc::new(service),
+ entity_access_service: access,
+ authorization_state: authorization.clone(),
+ },
+ whatsapp: WhatsAppRouterState {
+ service: whatsapp,
+ authorization_state: authorization,
+ },
}
}
@@ -103,6 +126,7 @@ async fn publish(
pool: PgPool,
effects: EffectsService,
media: MediaService,
+ whatsapp: Arc<WhatsAppService>,
cancellation: CancellationToken,
) {
let Some(mut listener) = listen(
@@ -133,7 +157,7 @@ async fn publish(
};
let span = tracing::info_span!("external_chat.drain");
trace_context::adopt(&span, traceparent.as_deref());
- drain(&effects, &media).instrument(span).await;
+ drain(&effects, &media, &whatsapp).instrument(span).await;
}
}
@@ -164,9 +188,9 @@ async fn listen(
}
}
-/// Import ready attachments, delete released files, expire unsent uploads and
-/// publish notifications.
-async fn drain(effects: &EffectsService, media: &MediaService) {
+/// Import ready attachments, delete released files, expire unsent uploads,
+/// old receipts and pairings, and publish notifications.
+async fn drain(effects: &EffectsService, media: &MediaService, whatsapp: &WhatsAppService) {
loop {
match media.import_pending().await {
Ok(0) => break,
@@ -193,10 +217,16 @@ async fn drain(effects: &EffectsService, media: &MediaService) {
}
}
}
- if let Err(error) = media.expire_retained().await {
+ if let Err(error) = media.expire_uploads().await {
+ tracing::warn!(
+ code = error.current_context().code(),
+ "Expiring unsent uploads will retry"
+ );
+ }
+ if let Err(error) = whatsapp.expire_retained().await {
tracing::warn!(
code = error.current_context().code(),
- "Expiring WhatsApp uploads and receipts will retry"
+ "Expiring WhatsApp receipts and pairings will retry"
);
}
loop {
@@ -222,17 +252,18 @@ const CHANGED_MESSAGE: &str = "external_chat_changed";
/// view a chat. No data travels; the page re-fetches.
struct ChangePusher {
pool: PgPool,
- audience: Arc<PgLinkedRepository>,
+ audience: Arc<PgExternalChatRepository>,
+ whatsapp: Arc<WhatsAppService>,
access: Arc<EntityAccessService>,
gateway: Arc<ConnectionGatewayClient>,
}
impl ChangePusher {
/// Users who see this change.
- async fn recipients(&self, change: Change) -> LinkedResult<Vec<String>> {
+ async fn recipients(&self, change: Change) -> ExternalChatResult<Vec<String>> {
match change {
Change::Chats { account_id } => Ok(self
- .audience
+ .whatsapp
.account_owner(account_id)
.await?
.into_iter()
@@ -261,7 +292,7 @@ impl ChangePusher {
err,
fields(scope, chat_id, account_id, recipients)
)]
- async fn push(&self, change: Change) -> LinkedResult<()> {
+ async fn push(&self, change: Change) -> ExternalChatResult<()> {
let span = tracing::Span::current();
let message = match change {
Change::Chats { account_id } => {
@@ -332,26 +363,26 @@ async fn push_changes(pusher: ChangePusher, cancellation: CancellationToken) {
}
}
-fn unavailable(error: impl std::fmt::Display) -> rootcause::Report<LinkedError> {
- rootcause::report!("{error}").context(LinkedError::Unavailable)
+fn unavailable(error: impl std::fmt::Display) -> rootcause::Report<ExternalChatError> {
+ rootcause::report!("{error}").context(ExternalChatError::Unavailable)
}
/// Generic notifications for shared external chat messages. Revisions,
/// reactions, history and replays never create a second notification.
-struct DssGroupPublisher {
- audience: Arc<PgLinkedRepository>,
+struct DssChatPublisher {
+ audience: Arc<PgExternalChatRepository>,
access: Arc<EntityAccessService>,
notifications: Arc<NotificationIngressType>,
}
#[async_trait]
-impl GroupPublisher for DssGroupPublisher {
+impl ChatPublisher for DssChatPublisher {
#[tracing::instrument(
name = "external_chat.notify",
skip_all,
err,
fields(chat_id = %effect.chat_id, message_id = %effect.message_id)
)]
- async fn publish(&self, effect: &GroupEffect) -> LinkedResult<()> {
+ async fn publish(&self, effect: &ChatEffect) -> ExternalChatResult<()> {
if !effect.first_publication || effect.origin == Origin::History {
return Ok(());
}
@@ -379,7 +410,7 @@ impl GroupPublisher for DssGroupPublisher {
.into_iter()
.filter(|u| effect.associated_user.as_deref() != Some(u.as_ref()))
.map(|u| MacroUserIdStr::try_from(u.to_string()).map_err(unavailable))
- .collect::<LinkedResult<HashSet<_>>>()?;
+ .collect::<ExternalChatResult<HashSet<_>>>()?;
if recipient_ids.is_empty() {
return Ok(());
}
@@ -418,7 +449,7 @@ impl FileStore for StaticFiles {
err,
fields(blob_id = %import.blob_id, chat_id = %import.chat_id, size = bytes.len(), file_id)
)]
- async fn store(&self, import: &FileImport, bytes: Vec<u8>) -> LinkedResult<Uuid> {
+ async fn store(&self, import: &FileImport, bytes: Vec<u8>) -> ExternalChatResult<Uuid> {
let stored = self
.0
.put_named_bytes_for_user(&import.owner, &import.filename, bytes.into(), &import.mime)
@@ -429,7 +460,7 @@ impl FileStore for StaticFiles {
}
#[tracing::instrument(name = "external_chat.delete_file", skip(self), err)]
- async fn delete(&self, file_id: Uuid) -> LinkedResult<()> {
+ async fn delete(&self, file_id: Uuid) -> ExternalChatResult<()> {
match self.0.delete_file(&file_id.to_string()).await {
Ok(_) => Ok(()),
Err(error)
@@ -452,7 +483,7 @@ struct SendRequestLimiter<R>(R);
#[async_trait]
impl<R: rate_limit::RateLimitService> SendRateLimiter for SendRequestLimiter<R> {
#[tracing::instrument(name = "external_chat.send_rate_limit", skip_all, err)]
- async fn check(&self, user: &MacroUserIdStr<'_>) -> LinkedResult<()> {
+ async fn check(&self, user: &MacroUserIdStr<'_>) -> ExternalChatResult<()> {
let key = rate_limit::RateLimitKey::builder(&"whatsapp-linked-send")
.append(&user.as_ref())
.finish();
@@ -463,10 +494,10 @@ impl<R: rate_limit::RateLimitService> SendRateLimiter for SendRequestLimiter<R>
rate_limit::RateLimitConfig::new(1, Duration::from_secs(1)),
)
.await
- .map_err(|error| error.context(LinkedError::Unavailable))?;
+ .map_err(|error| error.context(ExternalChatError::Unavailable))?;
result
.map(|_| ())
- .map_err(|_| LinkedError::RateLimited.into())
+ .map_err(|_| ExternalChatError::RateLimited.into())
}
}
diff --git a/services/document_storage_service/src/external_chat/test.rs b/services/document_storage_service/src/external_chat/test.rs
index 42c36cf609..c23108948a 100644
--- a/services/document_storage_service/src/external_chat/test.rs
+++ b/services/document_storage_service/src/external_chat/test.rs
@@ -58,7 +58,7 @@ async fn a_users_budget_is_shared_across_replicas_and_recovers_after_one_second(
let (first, second) = tokio::join!(first_replica.check(&alice), second_replica.check(&alice));
assert_eq!(usize::from(first.is_ok()) + usize::from(second.is_ok()), 1);
let denied = first.err().or_else(|| second.err()).unwrap();
- assert_eq!(denied.current_context(), &LinkedError::RateLimited);
+ assert_eq!(denied.current_context(), &ExternalChatError::RateLimited);
second_replica.check(&person("bob")).await.unwrap();
assert_eq!(counter.windows.lock().unwrap().len(), 2);
counter.now.store(1, Ordering::Relaxed);
@@ -93,7 +93,7 @@ async fn unavailable_shared_counter_fails_closed() {
.await
.unwrap_err()
.current_context(),
- &LinkedError::Unavailable
+ &ExternalChatError::Unavailable
);
}
@@ -120,7 +120,7 @@ async fn redis_enforces_the_shared_budget_without_affecting_another_user() {
assert_eq!(usize::from(one.is_ok()) + usize::from(two.is_ok()), 1);
assert_eq!(
one.err().or_else(|| two.err()).unwrap().current_context(),
- &LinkedError::RateLimited
+ &ExternalChatError::RateLimited
);
other.unwrap();
}
diff --git a/services/whatsapp_session_service/AGENTS.md b/services/whatsapp_session_service/AGENTS.md
index f13bf14ad1..0535b9ab37 100644
--- a/services/whatsapp_session_service/AGENTS.md
+++ b/services/whatsapp_session_service/AGENTS.md
@@ -44,7 +44,7 @@ current, ingests the traffic of added chats (`external_chat`) through `whatsapp_
`SessionService` (`discoverable_accounts`, `fleet_health`).
- `outbound/signals.rs`: `LISTEN` for `whatsapp_{account_released,
pairing_requested, send_requested, groups_requested, account_changed}`.
- Payloads are `whatsapp_linked::signals::Signal` JSON; each
+ Payloads are `external_chat::signals::Signal` JSON; each
one's `traceparent` parents a `whatsapp.signal.receive` span that travels
with the signal (`ports::Traced`): the supervisor's work and the runner's
`whatsapp.account.signal` span run inside it, and a runner started by it
diff --git a/services/whatsapp_session_service/Cargo.toml b/services/whatsapp_session_service/Cargo.toml
index 381caf960f..a4f6173314 100644
--- a/services/whatsapp_session_service/Cargo.toml
+++ b/services/whatsapp_session_service/Cargo.toml
@@ -41,6 +41,7 @@ whatsapp_store_pg = { path = "../../crates/whatsapp_store_pg" }
whatsmeow = { path = "../../crates/whatsmeow" }
whatsmeow_signal = { path = "../../crates/whatsmeow_signal" }
whatsapp_linked = { path = "../../crates/whatsapp_linked", features = ["outbound"] }
+external_chat = { path = "../../crates/external_chat", features = ["outbound"] }
workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }
[dev-dependencies]
diff --git a/services/whatsapp_session_service/src/domain/account.rs b/services/whatsapp_session_service/src/domain/account.rs
index 8edc104db0..eef18ab5b3 100644
--- a/services/whatsapp_session_service/src/domain/account.rs
+++ b/services/whatsapp_session_service/src/domain/account.rs
@@ -5,6 +5,8 @@
use std::sync::Arc;
use std::time::Duration;
+use external_chat::domain::models::{BackfillState, ExternalChatError};
+use external_chat::domain::ports::ExternalChatResult;
use tokio::sync::{Notify, Semaphore, mpsc, watch};
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
@@ -13,10 +15,7 @@ use whatsapp_linked::domain::events::{
BackfillWork, ClaimedSend, HistoryPage, Lease, OutgoingContent, PairingUpdate, PairingWork,
SendOutcome, SessionScope,
};
-use whatsapp_linked::domain::models::{
- AccountState, BackfillState, GroupInfo, LinkedError, PairingState,
-};
-use whatsapp_linked::domain::ports::LinkedResult;
+use whatsapp_linked::domain::models::{AccountState, GroupInfo, PairingState};
use whatsapp_linked::domain::session::SessionService;
use super::inbound::InboundGate;
@@ -93,13 +92,13 @@ enum Next {
Exit(AccountExit),
}
-fn is_fenced(report: &rootcause::Report<LinkedError>) -> bool {
- matches!(report.current_context(), LinkedError::Fenced)
+fn is_fenced(report: &rootcause::Report<ExternalChatError>) -> bool {
+ matches!(report.current_context(), ExternalChatError::Fenced)
}
/// Fenced ends the account; any other domain error is logged and survived.
fn survive<Value>(
- result: LinkedResult<Value>,
+ result: ExternalChatResult<Value>,
step: &'static str,
) -> Result<Option<Value>, AccountExit> {
match result {
@@ -121,7 +120,7 @@ impl<Connector: SessionConnector> AccountRunner for AccountSessions<Connector> {
async fn run(&self, account: Uuid, control: AccountControl) -> AccountExit {
let lease = match self.sessions.begin_lease(account).await {
Ok(lease) => lease,
- Err(report) if matches!(report.current_context(), LinkedError::NotFound) => {
+ Err(report) if matches!(report.current_context(), ExternalChatError::NotFound) => {
return AccountExit::Idle;
}
Err(report) => {
@@ -964,7 +963,7 @@ impl<Session: WhatsAppSession> Running<'_, Session> {
outcome = tracing::field::Empty,
);
// The dispatch joins the trace of the request that recorded the send.
- whatsapp_linked::signals::trace_context::adopt(&span, send.traceparent.as_deref());
+ external_chat::signals::trace_context::adopt(&span, send.traceparent.as_deref());
async {
let uploaded = self.upload(send).await?;
let message_id = self.session.new_message_id().await;
diff --git a/services/whatsapp_session_service/src/domain/account/test.rs b/services/whatsapp_session_service/src/domain/account/test.rs
index 8214a96077..67245f9f9b 100644
--- a/services/whatsapp_session_service/src/domain/account/test.rs
+++ b/services/whatsapp_session_service/src/domain/account/test.rs
@@ -16,7 +16,7 @@ use whatsapp_linked::domain::events::{
};
use whatsapp_linked::domain::models::{GroupInfo, Identity, Participant};
use whatsapp_linked::domain::session::SessionService;
-use whatsapp_linked::outbound::pg::PgLinkedRepository;
+use whatsapp_linked::outbound::pg::PgWhatsAppRepository;
use super::{AccountConfig, AccountSessions};
use crate::domain::inbound::InboundGate;
@@ -305,7 +305,7 @@ fn runner_with(
open_failures: Arc::default(),
};
let runner = Arc::new(AccountSessions::new(
- SessionService::new(Arc::new(PgLinkedRepository::new(pool.clone()))),
+ SessionService::new(Arc::new(PgWhatsAppRepository::new(pool.clone()))),
whatsapp.clone(),
AccountConfig {
send_timeout: Duration::from_secs(5),
@@ -446,7 +446,7 @@ async fn a_replica_whose_epoch_was_taken_over_cannot_write_and_stops(pool: PgPoo
assert_eq!(calls.recv().await, Some(Call::StartPairing));
assert_eq!(calls.recv().await, Some(Call::Connect));
- let takeover = SessionService::new(Arc::new(PgLinkedRepository::new(pool.clone())))
+ let takeover = SessionService::new(Arc::new(PgWhatsAppRepository::new(pool.clone())))
.begin_lease(account)
.await
.unwrap();
@@ -614,7 +614,7 @@ fn group_text(event: u128) -> InboundEvent {
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn the_gate_acks_unadopted_traffic_unstored_and_refuses_once_fenced(pool: PgPool) {
let (account, _pairing) = insert_pairing_account(&pool).await;
- let sessions = SessionService::new(Arc::new(PgLinkedRepository::new(pool.clone())));
+ let sessions = SessionService::new(Arc::new(PgWhatsAppRepository::new(pool.clone())));
let stale = sessions.begin_lease(account).await.unwrap().scope;
let current = sessions.begin_lease(account).await.unwrap().scope;
@@ -759,7 +759,7 @@ async fn live_group_events_update_the_one_group(pool: PgPool) {
.await
.unwrap();
listener
- .listen(whatsapp_linked::signals::CHANGED)
+ .listen(external_chat::signals::CHANGED)
.await
.unwrap();
let (account, whatsapp, mut calls, started) = connected(&pool, Duration::from_secs(3600)).await;
diff --git a/services/whatsapp_session_service/src/domain/inbound.rs b/services/whatsapp_session_service/src/domain/inbound.rs
index 3515dc2777..aed272f598 100644
--- a/services/whatsapp_session_service/src/domain/inbound.rs
+++ b/services/whatsapp_session_service/src/domain/inbound.rs
@@ -6,12 +6,12 @@ use std::collections::HashSet;
use std::future::Future;
use std::sync::atomic::{AtomicU64, Ordering};
+use external_chat::domain::models::ExternalChatError;
+use external_chat::domain::ports::ExternalChatResult;
use tokio::sync::{Mutex, Notify};
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use whatsapp_linked::domain::events::{InboundEvent, InboundKind, SessionScope};
-use whatsapp_linked::domain::models::LinkedError;
-use whatsapp_linked::domain::ports::LinkedResult;
use whatsapp_linked::domain::session::SessionService;
use super::ports::InboundSink;
@@ -25,22 +25,26 @@ pub trait InboundStore: Send + Sync + 'static {
fn observed_groups(
&self,
scope: SessionScope,
- ) -> impl Future<Output = LinkedResult<Vec<String>>> + Send;
+ ) -> impl Future<Output = ExternalChatResult<Vec<String>>> + Send;
/// Stores an event durably for later ingestion.
fn enqueue_inbound(
&self,
scope: SessionScope,
event: &InboundEvent,
- ) -> impl Future<Output = LinkedResult<()>> + Send;
+ ) -> impl Future<Output = ExternalChatResult<()>> + Send;
}
impl InboundStore for SessionService {
- async fn observed_groups(&self, scope: SessionScope) -> LinkedResult<Vec<String>> {
+ async fn observed_groups(&self, scope: SessionScope) -> ExternalChatResult<Vec<String>> {
SessionService::observed_groups(self, scope).await
}
- async fn enqueue_inbound(&self, scope: SessionScope, event: &InboundEvent) -> LinkedResult<()> {
+ async fn enqueue_inbound(
+ &self,
+ scope: SessionScope,
+ event: &InboundEvent,
+ ) -> ExternalChatResult<()> {
SessionService::enqueue_inbound(self, scope, event).await
}
}
@@ -107,7 +111,7 @@ impl<Store: InboundStore> InboundGate<Store> {
true
}
Err(report) => {
- if matches!(report.current_context(), LinkedError::Fenced) {
+ if matches!(report.current_context(), ExternalChatError::Fenced) {
self.fenced.cancel();
} else {
tracing::warn!(
@@ -204,10 +208,10 @@ impl<Store: InboundStore> InboundSink for InboundGate<Store> {
let error = report.current_context();
span.record("outcome", error.code());
match error {
- LinkedError::Fenced => self.fenced.cancel(),
+ ExternalChatError::Fenced => self.fenced.cancel(),
// Invalid events are never storable; ack so they are not
// redelivered forever.
- LinkedError::Invalid(_) => {
+ ExternalChatError::Invalid(_) => {
tracing::debug!(
name: "whatsapp.inbound.skipped",
parent: &span,
diff --git a/services/whatsapp_session_service/src/domain/inbound/test.rs b/services/whatsapp_session_service/src/domain/inbound/test.rs
index 0a32b1ad1f..ed99d00db5 100644
--- a/services/whatsapp_session_service/src/domain/inbound/test.rs
+++ b/services/whatsapp_session_service/src/domain/inbound/test.rs
@@ -1,12 +1,12 @@
use std::sync::Mutex;
use chrono::Utc;
+use external_chat::domain::ports::ExternalChatResult;
use uuid::Uuid;
use whatsapp_linked::domain::events::{
InboundEvent, InboundKind, Message, MessageKind, SessionScope,
};
use whatsapp_linked::domain::models::Identity;
-use whatsapp_linked::domain::ports::LinkedResult;
use super::{InboundGate, InboundStore};
use crate::domain::ports::InboundSink;
@@ -27,7 +27,7 @@ struct FakeStore {
}
impl InboundStore for FakeStore {
- async fn observed_groups(&self, _scope: SessionScope) -> LinkedResult<Vec<String>> {
+ async fn observed_groups(&self, _scope: SessionScope) -> ExternalChatResult<Vec<String>> {
*self.loads.lock().unwrap() += 1;
Ok(self.adopted.lock().unwrap().clone())
}
@@ -36,7 +36,7 @@ impl InboundStore for FakeStore {
&self,
_scope: SessionScope,
event: &InboundEvent,
- ) -> LinkedResult<()> {
+ ) -> ExternalChatResult<()> {
self.stored.lock().unwrap().push(event.event_id);
self.stored_chats
.lock()
diff --git a/services/whatsapp_session_service/src/domain/media.rs b/services/whatsapp_session_service/src/domain/media.rs
index bed2b97d7d..1c366856ed 100644
--- a/services/whatsapp_session_service/src/domain/media.rs
+++ b/services/whatsapp_session_service/src/domain/media.rs
@@ -5,12 +5,12 @@
use std::sync::Arc;
use std::time::Duration;
+use external_chat::domain::models::ExternalChatError;
use futures::future::join_all;
use tokio::sync::{Notify, Semaphore, watch};
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use whatsapp_linked::domain::events::{MediaFailure, MediaImport, SessionScope};
-use whatsapp_linked::domain::models::LinkedError;
use whatsapp_linked::domain::session::SessionService;
use super::ports::{DownloadFailure, WhatsAppSession};
@@ -69,7 +69,7 @@ impl<Session: WhatsAppSession> MediaImports<Session> {
.await
{
Ok(claimed) => claimed,
- Err(report) if matches!(report.current_context(), LinkedError::Fenced) => {
+ Err(report) if matches!(report.current_context(), ExternalChatError::Fenced) => {
self.fenced.cancel();
return false;
}
@@ -120,7 +120,7 @@ impl<Session: WhatsAppSession> MediaImports<Session> {
Err(DownloadFailure::Permanent) => self.fail(import, MediaFailure::Permanent).await,
};
if let Err(report) = result {
- if matches!(report.current_context(), LinkedError::Fenced) {
+ if matches!(report.current_context(), ExternalChatError::Fenced) {
self.fenced.cancel();
} else {
tracing::warn!(error = %report, "Recording a WhatsApp attachment failed");
@@ -135,7 +135,7 @@ impl<Session: WhatsAppSession> MediaImports<Session> {
&self,
import: &MediaImport,
failure: MediaFailure,
- ) -> whatsapp_linked::domain::ports::LinkedResult<()> {
+ ) -> external_chat::domain::ports::ExternalChatResult<()> {
tracing::Span::current().record(
"outcome",
match failure {
diff --git a/services/whatsapp_session_service/src/outbound/lease.rs b/services/whatsapp_session_service/src/outbound/lease.rs
index 6eac93091c..5d3bb4fbae 100644
--- a/services/whatsapp_session_service/src/outbound/lease.rs
+++ b/services/whatsapp_session_service/src/outbound/lease.rs
@@ -17,7 +17,7 @@ use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;
use uuid::Uuid;
-use whatsapp_linked::domain::models::LinkedError;
+use external_chat::domain::models::ExternalChatError;
use whatsapp_linked::domain::session::SessionService;
use crate::domain::ports::{AccountDirectory, AccountLocks, FleetHealth, LeaseError};
@@ -217,7 +217,7 @@ impl PgAccountDirectory {
}
}
-fn domain_error(report: rootcause::Report<LinkedError>) -> LeaseError {
+fn domain_error(report: rootcause::Report<ExternalChatError>) -> LeaseError {
LeaseError::Internal(report.into_dynamic())
}
diff --git a/services/whatsapp_session_service/src/outbound/lease/test.rs b/services/whatsapp_session_service/src/outbound/lease/test.rs
index 53383fd787..a71a160509 100644
--- a/services/whatsapp_session_service/src/outbound/lease/test.rs
+++ b/services/whatsapp_session_service/src/outbound/lease/test.rs
@@ -12,7 +12,7 @@ use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;
use uuid::Uuid;
use whatsapp_linked::domain::session::SessionService;
-use whatsapp_linked::outbound::pg::PgLinkedRepository;
+use whatsapp_linked::outbound::pg::PgWhatsAppRepository;
use super::{PgAccountDirectory, PgAccountLocks};
use crate::domain::ports::{
@@ -71,7 +71,7 @@ async fn only_one_replica_locks_an_account_and_discovery_skips_it(
.await
.unwrap();
let directory = PgAccountDirectory::new(SessionService::new(Arc::new(
- PgLinkedRepository::new(pool.clone()),
+ PgWhatsAppRepository::new(pool.clone()),
)));
sqlx::query!(
@@ -174,7 +174,7 @@ async fn start_replica(
let (started_sender, started) = mpsc::unbounded_channel();
let supervisor = Supervisor::new(
locks,
- PgAccountDirectory::new(SessionService::new(Arc::new(PgLinkedRepository::new(
+ PgAccountDirectory::new(SessionService::new(Arc::new(PgWhatsAppRepository::new(
pool.clone(),
)))),
Arc::new(ReportingRunner {
diff --git a/services/whatsapp_session_service/src/outbound/signals.rs b/services/whatsapp_session_service/src/outbound/signals.rs
index 57566c7589..48c034b1a4 100644
--- a/services/whatsapp_session_service/src/outbound/signals.rs
+++ b/services/whatsapp_session_service/src/outbound/signals.rs
@@ -14,9 +14,9 @@ use sqlx::postgres::{PgConnectOptions, PgListener, PgPoolOptions};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
+use external_chat::signals::trace_context;
use whatsapp_linked::signals::{
ACCOUNT_CHANGED, ACCOUNT_RELEASED, GROUPS_REQUESTED, PAIRING_REQUESTED, SEND_REQUESTED,
- trace_context,
};
use crate::domain::ports::Traced;
@@ -105,7 +105,7 @@ impl SignalListener {
/// Maps one notification to a signal and the sender's trace context; `None`
/// for unknown channels or payloads.
pub fn parse(channel: &str, payload: &str) -> Option<(Signal, Option<String>)> {
- let payload = whatsapp_linked::signals::Signal::parse(payload)?;
+ let payload = external_chat::signals::Signal::parse(payload)?;
let id = payload.id;
let signal = match channel {
ACCOUNT_RELEASED => Some(Signal::AccountReleased(id)),
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp.rs b/services/whatsapp_session_service/src/outbound/whatsapp.rs
index 46c85b69d4..a72a91b6c3 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp.rs
@@ -6,16 +6,17 @@ use std::time::Duration;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
+use external_chat::domain::models::{MediaKind, MediaShape};
use sqlx::PgPool;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use uuid::Uuid;
use whatsapp_linked::domain::events::{
- ChatUpdate, ClaimedSend, InboundEvent, MediaImport, MediaShape, MessageAnchor, OutboundMedia,
+ ChatUpdate, ClaimedSend, InboundEvent, MediaImport, MessageAnchor, OutboundMedia,
OutgoingContent, SessionScope, Target,
};
-use whatsapp_linked::domain::models::{GroupInfo, Identity, MediaKind, Participant};
+use whatsapp_linked::domain::models::{GroupInfo, Identity, Participant};
use whatsapp_store_pg::kms::WhatsAppKms;
use whatsapp_store_pg::{PgWhatsAppStore, WhatsAppStoreError};
use whatsmeow::download::{DownloadError, MediaType};
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp/events.rs b/services/whatsapp_session_service/src/outbound/whatsapp/events.rs
index 028f334e03..69f21a34b7 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp/events.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp/events.rs
@@ -3,14 +3,18 @@
//! everything outside adopted groups.
use chrono::{DateTime, Utc};
+use external_chat::domain::rich::SystemLine;
+use external_chat::domain::{
+ models::{ExternalChatKind, MediaPresentation},
+ rich::RichContent,
+};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use whatsapp_linked::domain::events::{
ChatUpdate, HistoryPage, InboundEvent, InboundKind, Media, Message, MessageAnchor, MessageKind,
- Receipt, ReceiptState, RichContent, Target,
+ Receipt, ReceiptState, Target,
};
-use whatsapp_linked::domain::models::{ExternalChatKind, Identity, MediaPresentation};
-use whatsapp_linked::domain::rich::SystemLine;
+use whatsapp_linked::domain::models::Identity;
use whatsmeow::Event;
use whatsmeow::download::{DownloadableMessage, MediaType};
use whatsmeow::proto::wa_common::MessageKey;
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp/events/rich.rs b/services/whatsapp_session_service/src/outbound/whatsapp/events/rich.rs
index 9af3379bd3..f998b3ff2e 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp/events/rich.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp/events/rich.rs
@@ -4,15 +4,15 @@
//! messages of history pages.
use chrono::{DateTime, Utc};
+use external_chat::domain::rich::{
+ Call, CallOutcome, ContactCard, Contacts, Event, Location, Poll, PollOption, PollVote,
+ RichContent, SystemAction, SystemLine, web_link,
+};
use nom::Parser;
use nom::bytes::complete::{take_till, take_while1};
use nom::character::complete::char;
use nom::combinator::opt;
use nom::sequence::terminated;
-use whatsapp_linked::domain::rich::{
- Call, CallOutcome, ContactCard, Contacts, Event, Location, Poll, PollOption, PollVote,
- RichContent, SystemAction, SystemLine, web_link,
-};
use whatsmeow::msgsecret::hash_poll_options;
use whatsmeow::proto::wa_e2e;
use whatsmeow::proto::wa_e2e::call_log_message;
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp/events/test.rs b/services/whatsapp_session_service/src/outbound/whatsapp/events/test.rs
index b8c625ded4..0df708c36d 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp/events/test.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp/events/test.rs
@@ -1,10 +1,12 @@
use chrono::{DateTime, TimeZone, Utc};
+use external_chat::domain::models::MediaPresentation;
+use external_chat::domain::rich::{
+ Call, CallOutcome, PollVote, RichContent, SystemAction, SystemLine,
+};
use uuid::Uuid;
use whatsapp_linked::domain::events::{
- InboundKind, Message as InboundMessage, MessageKind, ReceiptState, RichContent, Target,
+ InboundKind, Message as InboundMessage, MessageKind, ReceiptState, Target,
};
-use whatsapp_linked::domain::models::MediaPresentation;
-use whatsapp_linked::domain::rich::{Call, CallOutcome, PollVote, SystemAction, SystemLine};
use whatsmeow::Event;
use whatsmeow::proto::wa_common::MessageKey;
use whatsmeow::proto::wa_e2e;
@@ -639,7 +641,7 @@ fn a_live_message_moves_its_chat_up_with_the_message_as_anchor() {
assert_eq!(chat.jid, "[email protected]");
assert_eq!(
chat.kind,
- whatsapp_linked::domain::models::ExternalChatKind::Group
+ external_chat::domain::models::ExternalChatKind::Group
);
assert_eq!(chat.name, None, "groups are named by their roster");
assert!(chat.create);
diff --git a/services/whatsapp_session_service/src/outbound/whatsapp/test.rs b/services/whatsapp_session_service/src/outbound/whatsapp/test.rs
index d806be69ba..31cc3b50a0 100644
--- a/services/whatsapp_session_service/src/outbound/whatsapp/test.rs
+++ b/services/whatsapp_session_service/src/outbound/whatsapp/test.rs
@@ -4,6 +4,7 @@
use std::sync::Arc;
use std::time::Duration;
+use external_chat::domain::models::{AttachmentRef, MediaKind, MediaShape};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use sha2::{Digest, Sha256};
use sqlx::PgPool;
@@ -12,12 +13,11 @@ use tokio_util::sync::CancellationToken;
use uuid::Uuid;
use whatsapp_fake_server::{FakeWhatsApp, test_signal};
use whatsapp_linked::domain::events::{
- AttachmentRef, ClaimedSend, InboundEvent, InboundKind, MediaImport, MediaShape, OutboundMedia,
- OutgoingContent, SessionScope,
+ ClaimedSend, InboundEvent, InboundKind, MediaImport, OutboundMedia, OutgoingContent,
+ SessionScope,
};
-use whatsapp_linked::domain::models::MediaKind;
use whatsapp_linked::domain::session::SessionService;
-use whatsapp_linked::outbound::pg::PgLinkedRepository;
+use whatsapp_linked::outbound::pg::PgWhatsAppRepository;
use whatsapp_store_pg::kms::FakeWhatsAppKms;
use super::WhatsmeowConnector;
@@ -121,7 +121,7 @@ async fn a_phone_scanning_the_qr_links_the_account_through_the_encrypted_store(p
let account = insert_pairing_account(&pool).await;
let (copies, mut copied) = mpsc::unbounded_channel();
let runner = Arc::new(AccountSessions::new(
- SessionService::new(Arc::new(PgLinkedRepository::new(pool.clone()))),
+ SessionService::new(Arc::new(PgWhatsAppRepository::new(pool.clone()))),
Tee {
inner: WhatsmeowConnector::new(
pool.clone(),
@@ -253,7 +253,7 @@ async fn attachments_travel_both_ways_with_a_sealed_locator(pool: PgPool) {
let owner = whatsapp.add_user("15550001111", "Ada").await;
let peer = whatsapp.add_user("15550002222", "Bob").await;
let account = insert_pairing_account(&pool).await;
- let scope = SessionService::new(Arc::new(PgLinkedRepository::new(pool.clone())))
+ let scope = SessionService::new(Arc::new(PgWhatsAppRepository::new(pool.clone())))
.begin_lease(account)
.await
.unwrap()
@@ -473,7 +473,7 @@ async fn a_fresh_device_replaces_one_whose_data_key_kms_lost(pool: PgPool) {
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
let whatsapp = FakeWhatsApp::start().await.unwrap();
let account = insert_pairing_account(&pool).await;
- let scope = SessionService::new(Arc::new(PgLinkedRepository::new(pool.clone())))
+ let scope = SessionService::new(Arc::new(PgWhatsAppRepository::new(pool.clone())))
.begin_lease(account)
.await
.unwrap()
diff --git a/services/whatsapp_session_service/src/replica.rs b/services/whatsapp_session_service/src/replica.rs
index 306cc96429..fb40e0ba8d 100644
--- a/services/whatsapp_session_service/src/replica.rs
+++ b/services/whatsapp_session_service/src/replica.rs
@@ -12,7 +12,7 @@ use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;
use whatsapp_linked::domain::session::SessionService;
-use whatsapp_linked::outbound::pg::PgLinkedRepository;
+use whatsapp_linked::outbound::pg::PgWhatsAppRepository;
use whatsapp_store_pg::kms::WhatsAppKms;
use whatsmeow::{Dialer, Signal};
@@ -152,7 +152,7 @@ pub async fn start<Kms: WhatsAppKms + 'static>(
PgAccountLocks::connect(&deps.lock_options, timings.lock_keepalive, &tracker).await?;
let lock_backend_pid = locks.backend_pid();
- let sessions = SessionService::new(Arc::new(PgLinkedRepository::new(deps.pool.clone())));
+ let sessions = SessionService::new(Arc::new(PgWhatsAppRepository::new(deps.pool.clone())));
let runner = AccountSessions::new(
sessions.clone(),
WhatsmeowConnector::new(