jmap-chat-server 0.2.0

JMAP Chat extension method handlers — backend-agnostic, plugs into jmap-server Dispatcher
Documentation
//! `/set update` with a `null` PatchObject value resets a property to
//! its spec default (RFC 8620 §5.3, bd:JMAP-sofk).
//!
//! RFC 8620 §5.3: "null: The value to set for this property is the
//! default value if specified for this property". The in-memory backend
//! removes the member on `null`, so each property below must deserialize
//! to its draft-atwood-jmap-chat-00 default when absent; otherwise the
//! following `/get` fails as a whole.

mod common;

use common::{make_chat_group, MemoryBackend, ACCOUNT_ID};
use jmap_chat_server::{
    handle_chat_get, handle_chat_set, handle_contact_get, handle_contact_set, handle_message_get,
    handle_message_set, handle_presence_get, handle_presence_set,
};
use serde_json::{json, Value};

/// Assert a `/set` response updated `id` without error.
fn assert_updated(resp: &Value, id: &str) {
    assert_eq!(resp["notUpdated"], Value::Null, "{resp:?}");
    assert!(
        resp["updated"]
            .as_object()
            .is_some_and(|m| m.contains_key(id)),
        "{resp:?}"
    );
}

async fn chat_update(backend: &MemoryBackend, id: &str, patch: Value) {
    let (resp, _) = handle_chat_set(
        backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "update": { id: patch } }),
    )
    .await
    .expect("Chat/set");
    assert_updated(&resp, id);
}

async fn chat_get(backend: &MemoryBackend, id: &str) -> Value {
    let (resp, _) = handle_chat_get(
        backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "ids": [id] }),
    )
    .await
    .expect("Chat/get");
    resp["list"][0].clone()
}

/// Oracle: draft-atwood-jmap-chat-00 Chat `muted`: "When `true`, push
/// notifications for this chat are suppressed. [...] Default: `false`."
#[tokio::test]
async fn chat_muted_null_resets_to_false() {
    let backend = MemoryBackend::new();
    let id = make_chat_group(&backend, "g").await;
    chat_update(&backend, &id, json!({ "muted": true })).await;

    chat_update(&backend, &id, json!({ "muted": null })).await;

    assert_eq!(chat_get(&backend, &id).await["muted"], json!(false));
}

/// Oracle: draft-atwood-jmap-chat-00 Chat `pinnedMessageIds`: "Ordered
/// list of pinned Message ids, most-recently-pinned first. [...] Empty by
/// default."
#[tokio::test]
async fn chat_pinned_message_ids_null_resets_to_empty() {
    let backend = MemoryBackend::new();
    let id = make_chat_group(&backend, "g").await;

    chat_update(&backend, &id, json!({ "pinnedMessageIds": null })).await;

    assert_eq!(chat_get(&backend, &id).await["pinnedMessageIds"], json!([]));
}

/// Oracle: draft-atwood-jmap-chat-00 Chat `receiveTypingIndicators`:
/// "Default: `true`."
#[tokio::test]
async fn chat_receive_typing_indicators_null_resets_to_true() {
    let backend = MemoryBackend::new();
    let id = make_chat_group(&backend, "g").await;
    chat_update(&backend, &id, json!({ "receiveTypingIndicators": false })).await;

    chat_update(&backend, &id, json!({ "receiveTypingIndicators": null })).await;

    assert_eq!(
        chat_get(&backend, &id).await["receiveTypingIndicators"],
        json!(true)
    );
}

/// Oracle: draft-atwood-jmap-chat-00 ChatContact `blocked`: "When
/// `true`, messages from this ChatContact are silently dropped by this
/// mailbox [...] Default: `false`."
#[tokio::test]
async fn contact_blocked_null_resets_to_false() {
    let backend = MemoryBackend::new();
    // ChatContact objects are server-managed (create is forbidden).
    backend.insert_object_for_test(
        "ChatContact",
        ACCOUNT_ID,
        "ct1",
        json!({
            "id": "ct1",
            "login": "alice@example.com",
            "firstSeenAt": "2024-01-01T00:00:00Z",
            "lastSeenAt": "2024-01-01T00:00:00Z",
            "blocked": true,
        }),
    );

    let (resp, _) = handle_contact_set(
        &backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "update": { "ct1": { "blocked": null } } }),
    )
    .await
    .expect("ChatContact/set");
    assert_updated(&resp, "ct1");

    let (resp, _) = handle_contact_get(
        &backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "ids": ["ct1"] }),
    )
    .await
    .expect("ChatContact/get");
    assert_eq!(resp["list"][0]["blocked"], json!(false), "{resp:?}");
}

/// Seed a PresenceStatus with non-default `presence` and `receiptSharing`.
fn seed_presence(backend: &MemoryBackend) {
    // PresenceStatus objects are server-managed (only update is permitted).
    backend.insert_object_for_test(
        "PresenceStatus",
        ACCOUNT_ID,
        "ps1",
        json!({
            "id": "ps1",
            "presence": "busy",
            "receiptSharing": false,
            "updatedAt": "2024-01-01T00:00:00Z",
        }),
    );
}

async fn presence_null_then_get(backend: &MemoryBackend, prop: &str) -> Value {
    let (resp, _) = handle_presence_set(
        backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "update": { "ps1": { prop: null } } }),
    )
    .await
    .expect("PresenceStatus/set");
    assert_updated(&resp, "ps1");

    let (resp, _) = handle_presence_get(
        backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "ids": ["ps1"] }),
    )
    .await
    .expect("PresenceStatus/get");
    resp["list"][0].clone()
}

/// Oracle: draft-atwood-jmap-chat-00 PresenceStatus `presence`: "One of
/// `"online"`, `"away"`, `"busy"`, `"invisible"`, or `"offline"`.
/// Default is `"online"`."
#[tokio::test]
async fn presence_null_resets_to_online() {
    let backend = MemoryBackend::new();
    seed_presence(&backend);

    let ps = presence_null_then_get(&backend, "presence").await;

    assert_eq!(ps["presence"], json!("online"), "{ps:?}");
}

/// Oracle: draft-atwood-jmap-chat-00 PresenceStatus `receiptSharing`:
/// "When `false`, this account does not send read receipts to peers.
/// [...] Default: `true`."
#[tokio::test]
async fn presence_receipt_sharing_null_resets_to_true() {
    let backend = MemoryBackend::new();
    seed_presence(&backend);

    let ps = presence_null_then_get(&backend, "receiptSharing").await;

    assert_eq!(ps["receiptSharing"], json!(true), "{ps:?}");
}

/// Create a Message, patch `prop` to null, and return the fetched Message.
async fn message_null_then_get(prop: &str) -> Value {
    let backend = MemoryBackend::new();
    let (resp, _) = handle_message_set(
        &backend,
        &(),
        json!({
            "accountId": ACCOUNT_ID,
            "create": { "m0": { "chatId": "c1", "body": "hi", "sentAt": "2024-01-01T00:00:00Z" } }
        }),
    )
    .await
    .expect("Message/set create");
    let id = resp["created"]["m0"]["id"]
        .as_str()
        .expect("created id")
        .to_owned();

    let (resp, _) = handle_message_set(
        &backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "update": { &id: { prop: null } } }),
    )
    .await
    .expect("Message/set update");
    assert_updated(&resp, &id);

    let (resp, _) = handle_message_get(
        &backend,
        &(),
        json!({ "accountId": ACCOUNT_ID, "ids": [&id] }),
    )
    .await
    .expect("Message/get");
    resp["list"][0].clone()
}

/// Oracle: draft-atwood-jmap-chat-00 Message `mentions`: "Structured
/// per-user @mention annotations. Empty by default."
#[tokio::test]
async fn message_mentions_null_resets_to_empty() {
    let msg = message_null_then_get("mentions").await;
    assert_eq!(msg["mentions"], json!([]), "{msg:?}");
}

/// Oracle: draft-atwood-jmap-chat-00 Message `actions`: "Out-of-band
/// action invitations associated with this message. Empty by default."
#[tokio::test]
async fn message_actions_null_resets_to_empty() {
    let msg = message_null_then_get("actions").await;
    assert_eq!(msg["actions"], json!([]), "{msg:?}");
}

/// Oracle: draft-atwood-jmap-chat-00 Message `reactions`: "Emoji
/// reactions, keyed by `senderReactionId`. [...] Empty object by
/// default."
#[tokio::test]
async fn message_reactions_null_resets_to_empty() {
    let msg = message_null_then_get("reactions").await;
    assert_eq!(msg["reactions"], json!({}), "{msg:?}");
}

/// Oracle: draft-atwood-jmap-chat-00 Message `broadcastMentions`:
/// "Structured broadcast-scope mention annotations [...]. Empty by
/// default."
#[tokio::test]
async fn message_broadcast_mentions_null_resets_to_empty() {
    let msg = message_null_then_get("broadcastMentions").await;
    assert_eq!(msg["broadcastMentions"], json!([]), "{msg:?}");
}