feat(communication): federate tenant collaboration with signed delivery

This commit is contained in:
2026-09-30 14:48:05 +02:00
parent ea641bb4d0
commit 7b5696eda4
16 changed files with 914 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
CREATE TABLE collaboration_federation_identity (
singleton boolean PRIMARY KEY DEFAULT true CHECK(singleton),
instance_id uuid NOT NULL UNIQUE,
key_id uuid NOT NULL,
public_key text NOT NULL,
private_key_cipher jsonb NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE collaboration_partners (
tenant_id bigint NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
origin text NOT NULL,
instance_id uuid NOT NULL,
key_id uuid NOT NULL,
public_key text NOT NULL,
active boolean NOT NULL DEFAULT true,
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY(tenant_id,origin)
);
CREATE TABLE collaboration_federation_nonces (
origin text NOT NULL,
nonce uuid NOT NULL,
expires_at timestamptz NOT NULL,
PRIMARY KEY(origin,nonce)
);
CREATE TABLE collaboration_remote_bindings (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id bigint NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
principal_id uuid NOT NULL REFERENCES collaboration_principals(id) ON DELETE CASCADE,
origin text NOT NULL,
room_id uuid NOT NULL,
invitation_id uuid NOT NULL,
status text NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','active','revoked','declined')),
invitation jsonb NOT NULL,
snapshot jsonb,
revision bigint NOT NULL DEFAULT 0,
last_read_seq bigint NOT NULL DEFAULT 0,
sync_seq bigint NOT NULL DEFAULT 0,
sync_at timestamptz,
last_error text,
UNIQUE(principal_id,origin,room_id)
);
CREATE TABLE collaboration_remote_messages (
binding_id uuid NOT NULL REFERENCES collaboration_remote_bindings(id) ON DELETE CASCADE,
id uuid NOT NULL,
seq bigint NOT NULL,
payload jsonb NOT NULL,
PRIMARY KEY(binding_id,id),
UNIQUE(binding_id,seq)
);
CREATE TABLE collaboration_outbox (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
dedup_key text NOT NULL UNIQUE,
tenant_id bigint NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
principal_id uuid NOT NULL REFERENCES collaboration_principals(id),
origin text NOT NULL,
binding_id uuid REFERENCES collaboration_remote_bindings(id) ON DELETE CASCADE,
room_id uuid REFERENCES collaboration_rooms(id) ON DELETE CASCADE,
action text NOT NULL,
payload jsonb NOT NULL,
status text NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','sent','failed')),
attempts integer NOT NULL DEFAULT 0,
next_attempt_at timestamptz NOT NULL DEFAULT now(),
lease_until timestamptz,
last_error text,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX collaboration_outbox_pending ON collaboration_outbox(next_attempt_at) WHERE status='pending';

View File

@@ -512,6 +512,13 @@
"when": 1790726400000,
"tag": "0075_project_collaboration",
"breakpoints": true
},
{
"idx": 73,
"version": "7",
"when": 1790726400001,
"tag": "0076_collaboration_federation",
"breakpoints": true
}
]
}

View File

@@ -0,0 +1,44 @@
import { sql } from "drizzle-orm"
import { pgTable, uuid, bigint, text, timestamp, boolean, integer, jsonb, index, uniqueIndex, primaryKey, check } from "drizzle-orm/pg-core"
import { tenants } from "./tenants"
import { collaborationPrincipals, collaborationRooms } from "./collaboration"
const time = (name: string) => timestamp(name, { withTimezone: true })
export const collaborationFederationIdentity = pgTable("collaboration_federation_identity", {
singleton: boolean("singleton").primaryKey().default(true), instanceId: uuid("instance_id").notNull().unique(),
keyId: uuid("key_id").notNull(), publicKey: text("public_key").notNull(), privateKeyCipher: jsonb("private_key_cipher").notNull(),
createdAt: time("created_at").notNull().defaultNow(),
}, t => ({ singleton: check("collaboration_federation_identity_singleton_check", sql`${t.singleton}`) }))
export const collaborationPartners = pgTable("collaboration_partners", {
tenantId: bigint("tenant_id", { mode: "number" }).notNull().references(() => tenants.id, { onDelete: "cascade" }),
origin: text("origin").notNull(), instanceId: uuid("instance_id").notNull(), keyId: uuid("key_id").notNull(), publicKey: text("public_key").notNull(),
active: boolean("active").notNull().default(true), updatedAt: time("updated_at").notNull().defaultNow(),
}, t => ({ pk: primaryKey({ columns: [t.tenantId, t.origin] }) }))
export const collaborationFederationNonces = pgTable("collaboration_federation_nonces", {
origin: text("origin").notNull(), nonce: uuid("nonce").notNull(), expiresAt: time("expires_at").notNull(),
}, t => ({ pk: primaryKey({ columns: [t.origin, t.nonce] }) }))
export const collaborationRemoteBindings = pgTable("collaboration_remote_bindings", {
id: uuid("id").primaryKey().defaultRandom(),
tenantId: bigint("tenant_id", { mode: "number" }).notNull().references(() => tenants.id, { onDelete: "cascade" }),
principalId: uuid("principal_id").notNull().references(() => collaborationPrincipals.id, { onDelete: "cascade" }),
origin: text("origin").notNull(), roomId: uuid("room_id").notNull(), invitationId: uuid("invitation_id").notNull(),
status: text("status").notNull().default("pending"), invitation: jsonb("invitation").notNull(), snapshot: jsonb("snapshot"),
revision: bigint("revision", { mode: "number" }).notNull().default(0), lastReadSeq: bigint("last_read_seq", { mode: "number" }).notNull().default(0),
syncSeq: bigint("sync_seq", { mode: "number" }).notNull().default(0),
syncAt: time("sync_at"), lastError: text("last_error"),
}, t => ({ identity: uniqueIndex("collaboration_remote_bindings_principal_id_origin_room_id_key").on(t.principalId, t.origin, t.roomId),
status: check("collaboration_remote_bindings_status_check", sql`${t.status} IN ('pending','active','revoked','declined')`) }))
export const collaborationRemoteMessages = pgTable("collaboration_remote_messages", {
bindingId: uuid("binding_id").notNull().references(() => collaborationRemoteBindings.id, { onDelete: "cascade" }),
id: uuid("id").notNull(), seq: bigint("seq", { mode: "number" }).notNull(), payload: jsonb("payload").notNull(),
}, t => ({ pk: primaryKey({ columns: [t.bindingId, t.id] }), seq: uniqueIndex("collaboration_remote_messages_binding_id_seq_key").on(t.bindingId, t.seq) }))
export const collaborationOutbox = pgTable("collaboration_outbox", {
id: uuid("id").primaryKey().defaultRandom(), dedupKey: text("dedup_key").notNull().unique(),
tenantId: bigint("tenant_id", { mode: "number" }).notNull().references(() => tenants.id, { onDelete: "cascade" }),
principalId: uuid("principal_id").notNull().references(() => collaborationPrincipals.id), origin: text("origin").notNull(),
bindingId: uuid("binding_id").references(() => collaborationRemoteBindings.id, { onDelete: "cascade" }),
roomId: uuid("room_id").references(() => collaborationRooms.id, { onDelete: "cascade" }), action: text("action").notNull(), payload: jsonb("payload").notNull(),
status: text("status").notNull().default("pending"), attempts: integer("attempts").notNull().default(0),
nextAttemptAt: time("next_attempt_at").notNull().defaultNow(), leaseUntil: time("lease_until"), lastError: text("last_error"), createdAt: time("created_at").notNull().defaultNow(),
}, t => ({ pending: index("collaboration_outbox_pending").on(t.nextAttemptAt).where(sql`${t.status}='pending'`),
status: check("collaboration_outbox_status_check", sql`${t.status} IN ('pending','sent','failed')`) }))

View File

@@ -102,3 +102,5 @@ export * from "./public_links"
export * from "./wikipages"
export * from "./scanner_inbox"
export * from "./collaboration";
export * from "./collaboration_federation"