jmap-chat-server 0.4.0

JMAP Chat extension method handlers — backend-agnostic, plugs into jmap-server Dispatcher
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! `chatPush` support for RFC 8620 push subscriptions
//! (draft-atwood-jmap-chat-push-00).
//!
//! The `chatPush` property lives in [`PushSubscription::extra`] as a map of
//! accountId to `ChatPushConfig`. A backend calls [`validate_chat_push`] on
//! every create and update, [`strip_unsupported_chat_push`] on every
//! subscription it returns from `list_push_subscriptions`, builds
//! notifications with [`chat_state_change_for`], and builds the
//! `ChatMessagePush` for each new inbound message with
//! [`build_chat_message_push`] after [`should_push`] allows it, with the
//! badge count from [`owner_unread_count`], fitted to the platform size
//! limit with [`fit_chat_message_push`].

use std::collections::{BTreeMap, HashMap, HashSet};
use std::time::{SystemTime, UNIX_EPOCH};

use jmap_chat_types::{
    BodyType, BroadcastScope, Chat, ChatContact, ChatKind, ChatMessageEntry, ChatMessagePush,
    ChatPushCapability, ChatPushConfig, Message, ReadPosition, RichBody, Space, SpaceMember,
    UrgencyLevel,
};
use jmap_server::state_change_for;
use jmap_types::{Id, PushSubscription, State, StateChange, UTCDate, Urgency};
use serde_json::{Map, Value};

use crate::ChatBackend;
use crate::chat::unread_count;
use jmap_server::{SetError, SetErrorType};

/// The `PushSubscription` property this extension adds.
pub(crate) const CHAT_PUSH: &str = "chatPush";

/// The `ChatMessageEntry` fields a server MUST support in `properties`
/// (draft-atwood-jmap-chat-push-00 §ChatPushConfig). `unreadCount` is a
/// `ChatMessagePush` property, not an entry field, so it is rejected like
/// any unknown name.
const ENTRY_PROPERTIES: [&str; 13] = [
    "messageId",
    "chatId",
    "chatKind",
    "chatName",
    "spaceId",
    "spaceName",
    "senderId",
    "senderDisplayName",
    "sentAt",
    "hasMention",
    "mentionScopes",
    "encrypted",
    "bodySnippet",
];

/// The `ChatMessageEntry` fields sent when `properties` is absent
/// (draft-atwood-jmap-chat-push-00 §ChatPushConfig).
const DEFAULT_ENTRY_PROPERTIES: [&str; 10] = [
    "messageId",
    "chatId",
    "chatKind",
    "senderId",
    "senderDisplayName",
    "sentAt",
    "hasMention",
    "mentionScopes",
    "encrypted",
    "bodySnippet",
];

/// Check the `chatPush` property in a `PushSubscription`'s `extra` map
/// before it is stored.
///
/// `caps` maps each account that advertises
/// `urn:ietf:params:jmap:chat:push` to its account-level capability object.
/// An absent or `null` `chatPush`, and a `null` entry for an account, are
/// accepted. Otherwise the first offending value gets an
/// `invalidProperties` SetError naming it:
///
/// - `chatPush`: the value is not an object.
/// - `chatPush/<accountId>`: the account is not in `caps`, or the entry is
///   not a `ChatPushConfig`. draft-atwood-jmap-chat-push-00
///   §PushSubscription Extension: "If a `PushSubscription/set` create or
///   update includes a `chatPush` entry for an account whose
///   `accountCapabilities` does not include
///   `urn:ietf:params:jmap:chat:push`, the server MUST return an
///   `invalidProperties` SetError whose `properties` names the offending
///   entry (e.g., `chatPush/<id>`) and MUST NOT store the subscription
///   change."
/// - `chatPush/<accountId>/kinds`: a value other than `"direct"`,
///   `"group"` or `"channel"`.
/// - `chatPush/<accountId>/properties`: a name that is not a
///   `ChatMessageEntry` field the server supports.
/// - `chatPush/<accountId>/urgency` and `.../mentionUrgency`: a value not
///   in the account's `supportedUrgencyValues`, including any value outside
///   RFC 8030 §5.3, which no capability can list. draft-atwood-jmap-chat-push-00
///   §ChatPushConfig: "the server MUST return an `invalidProperties`
///   SetError on `PushSubscription/set` whose `properties` names the
///   offending path (e.g., `chatPush/<id>/urgency`)".
pub fn validate_chat_push(
    extra: &Map<String, Value>,
    caps: &HashMap<Id, ChatPushCapability>,
) -> Result<(), Box<SetError>> {
    let entries = match extra.get(CHAT_PUSH) {
        None | Some(Value::Null) => return Ok(()),
        Some(Value::Object(entries)) => entries,
        Some(_) => return Err(Box::new(invalid(CHAT_PUSH.to_owned()))),
    };
    for (account, entry) in entries.iter().filter(|(_, e)| !e.is_null()) {
        let path = format!("{CHAT_PUSH}/{account}");
        let Some(cap) = caps.get(account.as_str()) else {
            return Err(Box::new(invalid(path)));
        };
        let cfg: ChatPushConfig =
            serde_json::from_value(entry.clone()).map_err(|_| invalid(path.clone()))?;
        if cfg
            .kinds
            .iter()
            .flatten()
            .any(|k| matches!(k, ChatKind::Other(_)))
        {
            return Err(Box::new(invalid(format!("{path}/kinds"))));
        }
        if cfg
            .properties
            .iter()
            .flatten()
            .any(|p| !ENTRY_PROPERTIES.contains(&p.as_str()))
        {
            return Err(Box::new(invalid(format!("{path}/properties"))));
        }
        let unsupported = |u: &Option<UrgencyLevel>| {
            u.as_ref().is_some_and(|u| {
                u.to_urgency()
                    .is_none_or(|u| !cap.supported_urgency_values.contains(&u))
            })
        };
        if unsupported(&cfg.urgency) {
            return Err(Box::new(invalid(format!("{path}/urgency"))));
        }
        if unsupported(&cfg.mention_urgency) {
            return Err(Box::new(invalid(format!("{path}/mentionUrgency"))));
        }
    }
    Ok(())
}

/// Remove `chatPush` entries for accounts not in `caps` from a stored
/// subscription's `extra` map before it is returned.
///
/// An entry stored while the account advertised
/// `urn:ietf:params:jmap:chat:push` stops being returned once it no longer
/// does. draft-atwood-jmap-chat-push-00 rejects such an entry only on
/// `/set` (§PushSubscription Extension) and states no rule for one stored
/// before the capability was withdrawn. This server strips it so that
/// `/get` matches delivery: [`should_push`] treats the entry as absent, and
/// the draft says "When `chatPush` is absent or null for an account, the
/// server delivers standard `StateChange` notifications for that account
/// without inline message content."
pub fn strip_unsupported_chat_push(
    extra: &mut Map<String, Value>,
    caps: &HashMap<Id, ChatPushCapability>,
) {
    if let Some(Value::Object(entries)) = extra.get_mut(CHAT_PUSH) {
        entries.retain(|account, _| caps.contains_key(account.as_str()));
    }
}

/// The `StateChange` to push to `sub` for the types in `changed`, or `None`
/// when nothing is left to send.
///
/// Wraps [`state_change_for`] (which filters by `types`) and drops the
/// `Message` type for each account in `carried`. The caller puts an
/// account in `carried` only when every Message change for it in `changed`
/// arrived in a `ChatMessagePush` delivered to `sub`; an edit, a destroy,
/// the owner's own message or a message [`should_push`] filtered out keeps
/// the account out, and `Message` stays in its `StateChange`.
/// draft-atwood-jmap-chat-push-00 §PushSubscription Extension: "A
/// `ChatMessagePush` stands in for the `StateChange` that the arrival of
/// the messages it carries would otherwise cause. Every other change in
/// the account is still announced by a standard `StateChange`, as
/// {{RFC8620}} Section 7 describes: this includes Message changes that the
/// payload does not carry (edits, destroys, the owner's own messages, and
/// messages excluded by `ChatPushConfig`, mute, or block) and changes to
/// every other data type."
///
/// A consumer falling back to a plain `StateChange` under rate limiting
/// (§Rate Limiting and Fallback) calls [`state_change_for`] directly.
pub fn chat_state_change_for(
    sub: &PushSubscription,
    changed: &BTreeMap<Id, BTreeMap<String, State>>,
    carried: &HashSet<Id>,
) -> Option<StateChange> {
    let filtered: BTreeMap<Id, BTreeMap<String, State>> = changed
        .iter()
        .map(|(account, types)| {
            let mut types = types.clone();
            if carried.contains(account) {
                types.remove("Message");
            }
            (account.clone(), types)
        })
        .collect();
    state_change_for(sub, &filtered)
}

/// The account owner's standing in the Chat at delivery time, which
/// decides the broadcast scopes that target them.
///
/// draft-atwood-jmap-chat-push-00 §ChatMessageEntry `mentionScopes`:
/// "`"here"` is included only when the owner satisfies the
/// deployment-defined "active" predicate at delivery time; `"admins"` is
/// included only when the owner satisfies the deployment-defined
/// "administrative authority" predicate at delivery time." Both predicates
/// are deployment-defined, so the caller evaluates them.
///
/// Build it with [`OwnerFlags::new`]; more flags may be added.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct OwnerFlags {
    /// The owner satisfies the deployment's "active" predicate.
    pub active: bool,
    /// The owner has administrative authority in the Chat.
    pub admin: bool,
}

impl OwnerFlags {
    /// The owner's flags from the two deployment-defined predicates.
    pub const fn new(active: bool, admin: bool) -> Self {
        Self { active, admin }
    }
}

/// The stored records one `ChatMessageEntry` is built from.
///
/// Build it with [`PushItem::new`] and set the optional fields after; more
/// fields may be added.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct PushItem<'a> {
    /// The new inbound message.
    pub message: &'a Message,
    /// The Chat containing the message.
    pub chat: &'a Chat,
    /// The sender's `ChatContact` on the account owner's contact list.
    pub sender: &'a ChatContact,
    /// The sender's `SpaceMember` record in the Space containing the
    /// Chat, when the Chat is a channel and the record exists. Ignored for
    /// other kinds.
    pub sender_member: Option<&'a SpaceMember>,
    /// The Space containing the Chat, when the Chat is a channel. Its
    /// `name` becomes `spaceName`. Ignored for other kinds.
    pub space: Option<&'a Space>,
}

impl<'a> PushItem<'a> {
    /// The required records, with `sender_member` and `space` unset.
    pub const fn new(message: &'a Message, chat: &'a Chat, sender: &'a ChatContact) -> Self {
        Self {
            message,
            chat,
            sender,
            sender_member: None,
            space: None,
        }
    }
}

/// A built `ChatMessagePush` and the Web Push urgency to deliver it with.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct BuiltPush {
    /// The payload to deliver to the push endpoint.
    pub payload: ChatMessagePush,
    /// The Web Push urgency for this delivery.
    pub urgency: Urgency,
}

/// Build the `ChatMessagePush` for one new inbound message under one
/// subscription's `ChatPushConfig` for `account_id`.
///
/// The caller decides whether to deliver at all (draft-atwood-jmap-chat-push-00
/// §Delivery) and passes `state`, the account's `Message` state after this
/// message was stored. §ChatMessagePush: "The server MUST set this field to
/// the post-delivery state".
///
/// `unread_count` becomes the payload's `unreadCount`, normally the
/// value of [`owner_unread_count`] read after this message was stored.
/// §ChatMessagePush: "The account owner's total unread message count
/// across all non-muted Chats at the time this push was generated.
/// Servers SHOULD include this for platforms that support atomic badge
/// updates". `None` leaves it out.
///
/// The entry holds exactly the fields in `cfg.properties`, or the default
/// set when it is absent. §ChatPushConfig: "The `properties` list is
/// authoritative: the server returns exactly the requested fields." Each
/// field is derived as §ChatMessageEntry says, with these exceptions:
///
/// - `encrypted` comes from `bodyType`: `false` for `text/plain`,
///   `text/markdown` and `application/jmap-chat-rich`, otherwise `true`
///   (§End-to-End Encrypted Deployments). An encrypted entry never has
///   `hasMention`, `mentionScopes` or `bodySnippet`.
/// - `bodySnippet` is the body with control characters removed, cut to at
///   most `cap.maxSnippetBytes` bytes on a UTF-8 character boundary.
///   Markdown markup is left in, which misses the §ChatMessageEntry SHOULD
///   for a plaintext approximation. For `application/jmap-chat-rich` the
///   snippet is rendered from the span `text` values joined in order
///   (draft-atwood-jmap-chat-00 §Span Object: "The plaintext content of
///   this span"), so no markup reaches it; a body that does not parse as a
///   rich body gets no snippet.
/// - `senderDisplayName` treats an empty `login` as absent.
///
/// The urgency is `cfg.mentionUrgency` when it is set and the owner is
/// mentioned directly or by a targeting broadcast scope, otherwise
/// `cfg.urgency`, otherwise `"normal"` (§Urgency). The mention test uses
/// the computed mention, whether or not `properties` asks for
/// `hasMention` or `mentionScopes`. A chosen value outside RFC 8030
/// (`UrgencyLevel::Other`, which [`validate_chat_push`] rejects before a
/// config is stored) is also sent as `"normal"`, the urgency of a push
/// without the header (RFC 8030 §5.3).
pub fn build_chat_message_push(
    account_id: &Id,
    state: State,
    cfg: &ChatPushConfig,
    cap: &ChatPushCapability,
    owner: OwnerFlags,
    item: PushItem<'_>,
    unread_count: Option<u64>,
) -> BuiltPush {
    let msg = item.message;
    let kind = item.chat.kind.clone();
    let body_type = msg.body_type.as_str();
    let plaintext = is_plaintext(body_type);

    let mut mentioned = false;
    let mut entry = if plaintext {
        let has_mention = msg.mentions.iter().any(|m| m.id == *account_id);
        let scopes = mention_scopes(msg, owner);
        mentioned = has_mention || !scopes.is_empty();
        let snippet = if body_type == BodyType::Rich.as_str() {
            rich_text(&msg.body).map(|text| snippet(&text, cap.max_snippet_bytes))
        } else {
            Some(snippet(&msg.body, cap.max_snippet_bytes))
        };
        ChatMessageEntry::plaintext(
            msg.id.clone(),
            msg.chat_id.clone(),
            kind.clone(),
            msg.sender_id.clone(),
            msg.sent_at.clone(),
            has_mention,
            scopes,
            snippet,
        )
    } else {
        ChatMessageEntry::encrypted(
            msg.id.clone(),
            msg.chat_id.clone(),
            kind.clone(),
            msg.sender_id.clone(),
            msg.sent_at.clone(),
        )
    };
    if kind != ChatKind::Direct {
        entry.chat_name = item.chat.name.clone();
    }
    if kind == ChatKind::Channel {
        entry.space_id = item.chat.space_id.clone();
        entry.space_name = item.space.map(|s| s.name.clone());
    }
    entry.sender_display_name = Some(sender_display_name(&kind, &item));

    let wanted = |name: &str| match &cfg.properties {
        Some(properties) => properties.iter().any(|p| p == name),
        None => DEFAULT_ENTRY_PROPERTIES.contains(&name),
    };
    entry.message_id = entry.message_id.take().filter(|_| wanted("messageId"));
    entry.chat_id = entry.chat_id.take().filter(|_| wanted("chatId"));
    entry.chat_kind = entry.chat_kind.take().filter(|_| wanted("chatKind"));
    entry.chat_name = entry.chat_name.take().filter(|_| wanted("chatName"));
    entry.space_id = entry.space_id.take().filter(|_| wanted("spaceId"));
    entry.space_name = entry.space_name.take().filter(|_| wanted("spaceName"));
    entry.sender_id = entry.sender_id.take().filter(|_| wanted("senderId"));
    entry.sender_display_name = entry
        .sender_display_name
        .take()
        .filter(|_| wanted("senderDisplayName"));
    entry.sent_at = entry.sent_at.take().filter(|_| wanted("sentAt"));
    entry.has_mention = entry.has_mention.take().filter(|_| wanted("hasMention"));
    entry.mention_scopes = entry
        .mention_scopes
        .take()
        .filter(|_| wanted("mentionScopes"));
    entry.encrypted = entry.encrypted.take().filter(|_| wanted("encrypted"));
    entry.body_snippet = entry.body_snippet.take().filter(|_| wanted("bodySnippet"));

    let urgency = cfg
        .mention_urgency
        .as_ref()
        .filter(|_| mentioned)
        .or(cfg.urgency.as_ref())
        .and_then(UrgencyLevel::to_urgency)
        .unwrap_or(Urgency::Normal);
    // ponytail: one message per payload, which §Delivery allows ("Contains
    // exactly one entry for a single new message") and §Batching and Retry
    // prefers ("at most one `ChatMessagePush` per message"). The upgrade
    // path is batching, chunked by `maxMessagesPerPush`.
    let mut payload = ChatMessagePush::new(account_id.clone(), state, vec![entry]);
    payload.unread_count = unread_count;
    BuiltPush { payload, urgency }
}

/// The owner of `account_id`'s total unread message count across all
/// non-muted Chats, for [`build_chat_message_push`].
///
/// draft-atwood-jmap-chat-push-00 §ChatMessagePush `unreadCount`: "The
/// account owner's total unread message count across all non-muted Chats
/// at the time this push was generated." Each Chat contributes its
/// `unreadCount` as `Chat/get` reports it: the stored value when
/// [`ChatBackend::maintains_unread_count`] is `true`, otherwise the count
/// derived from the owner's ReadPosition (draft-atwood-jmap-chat-00
/// §Chat `unreadCount`). A Chat is muted as in [`should_push`]: until
/// `muteUntil` when it is set, otherwise when `muted` is `true`.
///
/// # Errors
///
/// Returns the backend's error if reading the Chats, Messages or
/// ReadPositions fails.
// ponytail: like Chat/get, a backend that does not maintain the count is
// scanned in full (O(messages)) per call.
pub async fn owner_unread_count<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    account_id: &Id,
) -> Result<u64, B::Error> {
    let (chats, _) = backend
        .get_objects::<Chat>(caller, account_id, None, None)
        .await?;
    let derived = if backend.maintains_unread_count() {
        None
    } else {
        let (messages, _) = backend
            .get_objects::<Message>(caller, account_id, None, None)
            .await?;
        let (positions, _) = backend
            .get_objects::<ReadPosition>(caller, account_id, None, None)
            .await?;
        Some((messages, positions))
    };
    Ok(chats
        .iter()
        .filter(|chat| !chat_muted(chat))
        .map(|chat| match &derived {
            Some((messages, positions)) => unread_count(messages, positions, chat.id.as_ref()),
            None => chat.unread_count,
        })
        .fold(0, u64::saturating_add))
}

/// Whether the owner has muted `chat` right now: until `muteUntil` when
/// it is set, otherwise when `muted` is `true`.
fn chat_muted(chat: &Chat) -> bool {
    match &chat.mute_until {
        Some(until) => mute_active(until),
        None => chat.muted,
    }
}

/// Fit `push` to a platform payload limit of `max_bytes` serialized JSON
/// bytes, returning the payloads to deliver in order. A `None` element
/// stands for one message whose entry cannot fit; the caller delivers a
/// `StateChange` for the `Message` data type in its place.
///
/// The limit is the delivery platform's; for Web Push,
/// jmap-push-platform-guide §Payload size: "Servers MUST keep plaintext
/// under **3993 bytes**", so pass 3992 there.
///
/// draft-atwood-jmap-chat-push-00 §Delivery: "If a single
/// `ChatMessageEntry` does not fit, the server SHOULD shorten or omit that
/// entry's `bodySnippet`; if the entry still does not fit, the server
/// delivers a `StateChange` for the `Message` data type in place of a
/// `ChatMessagePush` for that message." A `push` that fits is returned
/// unchanged. Otherwise each entry goes in a payload of its own, with the
/// same `accountId`, `state`, `unreadCount` and extension fields, so no message is lost
/// (§Delivery: "the server MUST deliver additional `ChatMessagePush`
/// payloads to cover the remainder"). An entry that does not fit alone
/// loses `bodySnippet`; no other field is ever removed.
// ponytail: the snippet is omitted, not shortened, since JSON escaping
// makes the fitting length a search. Upgrade path: trim it on a UTF-8
// boundary until the payload fits.
pub fn fit_chat_message_push(
    mut push: ChatMessagePush,
    max_bytes: usize,
) -> Vec<Option<ChatMessagePush>> {
    let fits = |p: &ChatMessagePush| serde_json::to_vec(p).is_ok_and(|v| v.len() <= max_bytes);
    if fits(&push) {
        return vec![Some(push)];
    }
    std::mem::take(&mut push.messages)
        .into_iter()
        .map(|entry| {
            let mut single = push.clone();
            single.messages = vec![entry];
            if !fits(&single) {
                single.messages[0].body_snippet = None;
            }
            fits(&single).then_some(single)
        })
        .collect()
}

/// Whether a `ChatMessagePush` for the new inbound message `msg` goes to
/// a subscription whose `chatPush` entry for `account_id` is `cfg`.
///
/// `caps` maps each account that advertises
/// `urn:ietf:params:jmap:chat:push` to its capability object; a `cfg` for
/// an account not in `caps` counts as absent and gets `false`
/// (draft-atwood-jmap-chat-push-00 §PushSubscription Extension). `owner`
/// is the owner's standing at delivery time, as for
/// [`build_chat_message_push`].
///
/// Returns `true` only when the Chat matches `cfg` and none of the
/// §Delivery exclusions applies:
///
/// - `cfg.kinds` and `cfg.chatIds` both match when present. §ChatPushConfig:
///   "If both `kinds` and `chatIds` are present, a Chat matches only if its
///   id appears in `chatIds` and its kind appears in `kinds`."
/// - The sender is not the owner: `senderId` is not `account_id`.
/// - The owner is still a member of the Chat. The account must hold the
///   Chat; for a group Chat its `members` must have an entry whose id is
///   `account_id`, and for a channel Chat its Space's `members` must.
/// - The Chat is not muted, unless the owner is targeted by one of the message's broadcast
///   mentions. Direct mentions never bypass a mute, and neither does an
///   encrypted message (§End-to-End Encrypted Deployments: "never bypasses
///   mute suppression"). When `muteUntil` is set, the Chat is muted until
///   that time whatever `muted` says, because draft-atwood-jmap-chat-00
///   §Chat `muteUntil` says "Muting expires at this time" and clearing
///   `muted` afterwards is only a SHOULD. Otherwise the Chat is muted when
///   `muted` is `true`. A `muteUntil` that is not a seconds-precision
///   UTCDate, or a system clock before 1970, counts as an active mute.
/// - The sender's `ChatContact` is not blocked, per
///   [`ChatBackend::is_contact_blocked`].
///
/// # Messages stored before the subscription
///
/// §Delivery: "The server MUST NOT deliver a `ChatMessagePush` in any of
/// the following cases: The message was stored before the
/// `PushSubscription` was created." This function cannot see creation
/// order. The caller MUST read the set of subscriptions under the same lock
/// or transaction as the message write, and call this function only for
/// subscriptions in that set, including when delivery fans out
/// asynchronously afterwards.
///
/// # Errors
///
/// Returns the backend's error if reading the Chat, its Space or the
/// sender's blocked status fails.
pub async fn should_push<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    account_id: &Id,
    cfg: &ChatPushConfig,
    caps: &HashMap<Id, ChatPushCapability>,
    msg: &Message,
    owner: OwnerFlags,
) -> Result<bool, B::Error> {
    if !caps.contains_key(account_id) || msg.sender_id == *account_id {
        return Ok(false);
    }
    if cfg
        .chat_ids
        .as_ref()
        .is_some_and(|ids| !ids.contains(&msg.chat_id))
    {
        return Ok(false);
    }
    let (chats, _) = backend
        .get_objects::<Chat>(
            caller,
            account_id,
            Some(std::slice::from_ref(&msg.chat_id)),
            None,
        )
        .await?;
    let Some(chat) = chats.into_iter().next() else {
        return Ok(false);
    };
    if cfg
        .kinds
        .as_ref()
        .is_some_and(|kinds| !kinds.contains(&chat.kind))
    {
        return Ok(false);
    }
    if !owner_is_member(backend, caller, account_id, &chat).await? {
        return Ok(false);
    }
    let bypass = is_plaintext(msg.body_type.as_str()) && !mention_scopes(msg, owner).is_empty();
    if chat_muted(&chat) && !bypass {
        return Ok(false);
    }
    Ok(!backend
        .is_contact_blocked(caller, account_id, &msg.sender_id)
        .await?)
}

/// Whether the owner of `account_id` is a current member of `chat`, its
/// own Chat record: a group lists the owner in `members`, a channel's
/// Space lists the owner, and a direct Chat always has its owner
/// (draft-atwood-jmap-chat-00 §Terminology: the owner's member entry is
/// the accountId).
pub(crate) async fn owner_is_member<B: ChatBackend>(
    backend: &B,
    caller: &B::CallerCtx,
    account_id: &Id,
    chat: &Chat,
) -> Result<bool, B::Error> {
    let is_owner = |id: &Id| id == account_id;
    Ok(match chat.kind {
        ChatKind::Group => chat.members.iter().flatten().any(|m| is_owner(&m.id)),
        ChatKind::Channel => match &chat.space_id {
            Some(space_id) => backend
                .get_objects::<Space>(
                    caller,
                    account_id,
                    Some(std::slice::from_ref(space_id)),
                    None,
                )
                .await?
                .0
                .iter()
                .any(|space| space.members.iter().any(|m| is_owner(&m.id))),
            None => false,
        },
        _ => true,
    })
}

/// Whether a mute ending at `until` is still in effect: `until` is later
/// than now. draft-atwood-jmap-chat-00 §Chat `muteUntil`: "Muting expires
/// at this time." An unparseable `until` or clock counts as in effect.
fn mute_active(until: &UTCDate) -> bool {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .ok()
        .and_then(|d| i64::try_from(d.as_secs()).ok());
    match (until.to_epoch_seconds(), now) {
        (Ok(until), Some(now)) => now < until,
        _ => true,
    }
}

/// Whether `body_type` is one of the plaintext body types that
/// draft-atwood-jmap-chat-push-00 §End-to-End Encrypted Deployments says
/// "MUST set `encrypted` to `false`"; every other type is encrypted.
fn is_plaintext(body_type: &str) -> bool {
    [BodyType::Plain, BodyType::Markdown, BodyType::Rich]
        .iter()
        .any(|t| t.as_str() == body_type)
}

/// The deduplicated `Message.broadcastMentions[]` scopes that target the
/// owner, in first-seen order. `"everyone"` always targets a Chat member;
/// unknown scopes never do.
fn mention_scopes(msg: &Message, owner: OwnerFlags) -> Vec<BroadcastScope> {
    let mut scopes: Vec<BroadcastScope> = Vec::new();
    for scope in msg.broadcast_mentions.iter().map(|b| &b.scope) {
        let targets_owner = match scope {
            BroadcastScope::Everyone => true,
            BroadcastScope::Here => owner.active,
            BroadcastScope::Admins => owner.admin,
            _ => false,
        };
        if targets_owner && !scopes.contains(scope) {
            scopes.push(scope.clone());
        }
    }
    scopes
}

/// The plaintext of an `application/jmap-chat-rich` body: its span `text`
/// values joined in order, or `None` when `body` is not a rich body. The
/// spans only render the preview; servers must not validate the body or
/// decide anything from span content (draft-atwood-jmap-chat-00 §Rich Body
/// Format).
fn rich_text(body: &str) -> Option<String> {
    let rich: RichBody = serde_json::from_str(body).ok()?;
    Some(rich.spans.into_iter().map(|s| s.text).collect())
}

/// `body` without control characters, cut to at most `max_bytes` bytes on
/// a UTF-8 character boundary.
fn snippet(body: &str, max_bytes: u64) -> String {
    let mut text: String = body.chars().filter(|c| !c.is_control()).collect();
    let mut end = usize::try_from(max_bytes).map_or(text.len(), |max| max.min(text.len()));
    while !text.is_char_boundary(end) {
        end -= 1;
    }
    text.truncate(end);
    text
}

/// The `senderDisplayName` chain from draft-atwood-jmap-chat-push-00
/// §ChatMessageEntry: the `SpaceMember.nick` (channels only), then
/// `ChatContact.displayName`, then a non-empty `ChatContact.login`, then
/// `ChatContact.identity`.
fn sender_display_name(kind: &ChatKind, item: &PushItem<'_>) -> String {
    let nick = item
        .sender_member
        .filter(|_| *kind == ChatKind::Channel)
        .and_then(|m| m.nick.clone());
    let login = Some(&item.sender.login).filter(|l| !l.is_empty()).cloned();
    nick.or_else(|| item.sender.display_name.clone())
        .or(login)
        .unwrap_or_else(|| item.sender.identity.clone())
}

/// An `invalidProperties` SetError naming `property`.
fn invalid(property: String) -> SetError {
    SetError::new(SetErrorType::InvalidProperties).with_properties([property])
}