Files
rustification-server/src/schema.rs
MDietermann 099315a538 feat(realtime): add monotonic cursor, change log and WebSocket fan-out
Messages were only reachable by polling. This adds the server half of the
realtime path: a cursor that cannot skip rows, a change log, and a socket that
pushes "conversation X is at seq N".

The cursor is a counter on conversations, drawn with UPDATE ... RETURNING
inside the mutating transaction. That takes the conversation row lock, so a
second writer blocks until the first commits and commit order equals seq order.
A BIGSERIAL on messages does not have this property: it hands out numbers at
INSERT time while rows become visible at COMMIT time, and those two orders
diverge under concurrent senders — a reader following seq > n then loses a
message permanently. Two tests cover this; the second one asserts that the
naive design *does* lose a row, so the first is proven to measure something.

created_at loses its ordering role. It is NOW(), i.e. transaction start, so it
is neither unique nor commit-ordered — the existing ORDER BY created_at was
already wrong, independently of realtime.

Every mutation appends to conversation_events, and that row is the event. A
three-line trigger on that table fires pg_notify, so no future write path can
forget to signal, and NOTIFY being transactional means it fires exactly when
the row becomes visible.

Fan-out always goes through NOTIFY, even with a single instance. One path
instead of two means the tested path is the production path, and running more
than one instance needs no second implementation. Per connection the pending
work is a map rather than a queue: only the newest seq per conversation
matters, so a newer value overwrites an older one, there is no overflow and no
overflow error path. Entries are merged with max(), because NOTIFY only orders
per listener. When the listener reconnects, every client is told to resync —
notifications sent during the gap are gone, as NOTIFY keeps no history.

Sockets are closed by hand on shutdown: axum's graceful shutdown neither waits
for nor closes upgraded connections (axum#3003, fix PR #3831 still open).

Also adds membership checks to the message history endpoint, which had none,
and paginates it — it previously returned entire conversations unbounded.

Not yet done, marked as TODO in the code: reactions and attachments do not emit
events, and create/update/delete still trust user_id from the request body.
2026-08-14 18:54:09 +02:00

110 lines
2.7 KiB
Rust

use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::model::{AuthTokenKind, ConversationKind, WorkspaceRole};
#[derive(Deserialize)]
pub struct RegisterSchema {
pub email: String,
pub username: String,
pub display_name: String,
pub password: String,
}
#[derive(Deserialize)]
pub struct LoginSchema {
pub email: String,
pub password: String,
}
#[derive(Deserialize)]
pub struct RefreshSchema {
pub refresh_token: String,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct UserSchema {
pub email: String,
pub password_hash: String,
pub username: String,
pub display_name: String,
pub avatar_url: Option<String>,
pub bio: Option<String>,
pub email_verified: bool,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct AuthTokenSchema {
pub user_id: Uuid,
pub kind: AuthTokenKind,
pub expires_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct WorkspaceSchema {
pub name: String,
pub slug: String,
pub owner_id: Uuid,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct WorkspaceMemberSchema {
pub workspace_id: Uuid,
pub user_id: Uuid,
pub role: WorkspaceRole,
pub display_name: Option<String>,
pub avatar_url: Option<String>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct WorkspaceInvitationSchema {
pub workspace_id: Uuid,
pub created_by: Uuid,
pub max_uses: Option<i32>,
pub expires_at: Option<chrono::DateTime<chrono::Utc>>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct ConversationSchema {
pub workspace_id: Option<Uuid>,
pub kind: ConversationKind,
pub name: Option<String>,
pub is_public: bool,
pub created_by: Uuid,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct ConversationMemberSchema {
pub conversation_id: Uuid,
pub user_id: Uuid,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct MessageSchema {
/// Supplied by the client, not the server. Without it the client cannot
/// reconcile its optimistically rendered message with the one that comes
/// back over the realtime channel — it would show up twice. Bonus: sending
/// becomes idempotent, so a retry after an unclear network error is a no-op
/// instead of a duplicate.
pub id: Uuid,
pub conversation_id: Uuid,
pub user_id: Uuid,
pub content: String,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct AttachmentSchema {
pub message_id: Uuid,
pub url: String,
pub filename: String,
pub mime_type: String,
pub size_bytes: i64,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct ReactionSchema {
pub message_id: Uuid,
pub user_id: Uuid,
pub emoji: String,
}