iris-chat 0.1.57

Iris Chat command line client and shared encrypted chat core
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
use super::chats::chat_message_from_persisted;
use super::*;

const DELIVERED_RECEIPT_DEBOUNCE: Duration = Duration::from_millis(750);

impl AppCore {
    pub(super) fn set_chat_unread(&mut self, chat_id: &str, unread: bool) {
        let Some(normalized_chat_id) = self.normalize_chat_id(chat_id) else {
            return;
        };
        let Some(thread) = self.threads.get(&normalized_chat_id) else {
            return;
        };
        if unread {
            let Some(thread) = self.threads.get_mut(&normalized_chat_id) else {
                return;
            };
            thread.unread_count = thread.unread_count.max(1);
            self.persist_best_effort();
            self.rebuild_state();
            self.emit_state();
            return;
        }
        let mut message_ids = match self.app_store.incoming_message_ids(&normalized_chat_id) {
            Ok(ids) => ids,
            Err(error) => {
                self.push_debug_log("storage.messages.receipt.error", error.to_string());
                return;
            }
        };
        message_ids.extend(
            thread
                .messages
                .iter()
                .filter(|m| !m.is_outgoing)
                .map(|m| m.id.clone()),
        );
        message_ids.sort();
        message_ids.dedup();
        self.mark_messages_seen(&normalized_chat_id, &message_ids);
        // "Mark as read" is an explicit bulk action over currently known IDs.
        // It must also clear a manually added badge when every row was read.
        if let Some(thread) = self.threads.get_mut(&normalized_chat_id) {
            thread.unread_count = 0;
        }
        self.persist_best_effort();
        self.rebuild_state();
        self.emit_state();
    }

    pub(super) fn mark_messages_seen(&mut self, chat_id: &str, message_ids: &[String]) {
        if message_ids.len() > MAX_RECENT_READ_IDS {
            for ids in message_ids.chunks(MAX_RECENT_READ_IDS) {
                self.mark_messages_seen(chat_id, ids);
            }
            return;
        }
        if message_ids.is_empty() {
            return;
        }
        let Some(normalized_chat_id) = self.normalize_chat_id(chat_id) else {
            return;
        };
        let Some(stored_messages) =
            self.load_stored_receipt_messages(&normalized_chat_id, message_ids)
        else {
            return;
        };
        let Some(thread) = self.threads.get_mut(&normalized_chat_id) else {
            return;
        };
        let loaded_len = thread.messages.len();
        // Read-state persistence and group receipt routing need these rows only
        // while acknowledging them. They must not alter the UI's page cursor.
        thread.messages.extend(stored_messages);
        let receipt_ids = thread
            .messages
            .iter()
            .filter(|message| !message.is_outgoing && message_ids.contains(&message.id))
            .map(|message| message.id.clone())
            .collect::<Vec<_>>();
        let changed = !receipt_ids.is_empty()
            && self.record_local_chat_read_state(&normalized_chat_id, &receipt_ids, false);
        if changed {
            self.send_seen_updates(&normalized_chat_id, receipt_ids);
        }
        if let Some(thread) = self.threads.get_mut(&normalized_chat_id) {
            thread.messages.truncate(loaded_len);
        }
        if !changed {
            return;
        }
        self.persist_best_effort();
        self.rebuild_state();
        self.emit_state();
        self.broadcast_device_sync_snapshot();
    }

    // Inactive restored chats contain only their latest preview. Fetch missing
    // receipt targets separately: inserting sparse old rows into the visible
    // window would make pagination skip the history between them and the page.
    fn load_stored_receipt_messages(
        &mut self,
        chat_id: &str,
        message_ids: &[String],
    ) -> Option<Vec<ChatMessageSnapshot>> {
        let thread = self.threads.get(chat_id)?;
        let mut known_ids = thread
            .messages
            .iter()
            .map(|message| message.id.as_str())
            .collect::<HashSet<_>>();
        let missing = message_ids
            .iter()
            .filter(|id| known_ids.insert(id.as_str()))
            .collect::<Vec<_>>();
        let mut messages = Vec::new();
        for id in missing {
            match self.app_store.load_messages_around(chat_id, id, 0, 0) {
                Ok(stored) => messages.extend(stored.iter().map(chat_message_from_persisted)),
                Err(error) => {
                    self.push_debug_log("storage.messages.receipt.error", error.to_string());
                    return None;
                }
            }
        }
        Some(messages)
    }

    pub(super) fn sync_open_chat_read_state(&mut self, chat_id: &str) {
        let Some(thread) = self.threads.get(chat_id) else {
            return;
        };
        let ids = thread
            .messages
            .iter()
            .filter(|message| !message.is_outgoing)
            .map(|message| message.id.clone())
            .collect::<Vec<_>>();
        if self.record_local_chat_read_state(chat_id, &ids, true) {
            self.send_seen_updates(chat_id, ids);
            self.broadcast_device_sync_snapshot();
        }
    }

    fn send_seen_updates(&mut self, chat_id: &str, mut receipt_ids: Vec<String>) {
        if receipt_ids.is_empty() {
            if let Some(state) = self.chat_read_states.get(chat_id) {
                receipt_ids.extend(state.exact_messages().into_keys());
            }
        }
        if receipt_ids.is_empty() {
            return;
        }
        self.cancel_pending_delivered_receipts(chat_id, &receipt_ids);
        for ids in receipt_ids.chunks(MAX_RECENT_READ_IDS) {
            self.send_seen_receipt_to_local_siblings(chat_id, ids.to_vec());
        }
        if self.preferences.send_read_receipts && !self.thread_is_message_request(chat_id) {
            self.send_receipt(chat_id, "seen", receipt_ids);
        }
    }

    fn send_seen_receipt_to_local_siblings(&mut self, chat_id: &str, message_ids: Vec<String>) {
        let Some(owner_pubkey) = self
            .logged_in
            .as_ref()
            .map(|logged_in| logged_in.owner_pubkey)
        else {
            return;
        };
        let (conversation_owner, group_id) = if is_group_chat_id(chat_id) {
            (owner_pubkey, parse_group_id_from_chat_id(chat_id))
        } else if let Ok((_, peer)) = parse_peer_input(chat_id) {
            (peer, None)
        } else {
            return;
        };
        let Some(mut unsigned) = receipt_unsigned_event(
            owner_pubkey,
            "seen",
            message_ids.clone(),
            group_id.as_deref(),
        ) else {
            return;
        };
        if let Some(state) = self.chat_read_states.get(chat_id) {
            let mut state = state.clone();
            state.seen_messages = message_ids
                .iter()
                .filter_map(|id| {
                    self.app_store
                        .message_read_state(chat_id, id)
                        .ok()
                        .flatten()
                        .map(|read| (id.clone(), read))
                })
                .collect();
            if let Ok(encoded) = serde_json::to_string(&state) {
                if let Ok(tag) = nostr::Tag::parse(["iris-read-state", &encoded]) {
                    unsigned.tags.push(tag);
                    unsigned.id = None;
                    unsigned.ensure_id();
                }
            }
        }
        self.send_protocol_engine_unsigned_event_to_local_siblings(
            conversation_owner,
            chat_id,
            unsigned,
            "receipt.self_sync",
        );
    }

    pub(super) fn send_receipt(
        &mut self,
        chat_id: &str,
        receipt_type: &str,
        message_ids: Vec<String>,
    ) {
        if message_ids.is_empty() {
            return;
        }
        // Within a batch (catch-up flurry, multi-action handling, …), queue
        // ids by (chat, receipt_type) and let exit_batch flush them all at
        // once. Outside a batch — e.g. a single `markMessagesSeen` from the
        // shell — fall through and send immediately, same as before.
        if self.batch_depth > 0 {
            self.pending_outgoing_receipts
                .entry((chat_id.to_string(), receipt_type.to_string()))
                .or_default()
                .extend(message_ids);
            return;
        }
        self.send_receipt_inner(chat_id, receipt_type, message_ids);
    }

    pub(super) fn queue_delivered_receipt(&mut self, chat_id: &str, message_id: String) {
        if message_id.is_empty() {
            return;
        }
        let due_at = Instant::now() + DELIVERED_RECEIPT_DEBOUNCE;
        self.pending_delivered_receipts
            .entry((chat_id.to_string(), message_id))
            .or_insert(due_at);
        self.schedule_pending_delivered_receipt_flush();
    }

    fn schedule_pending_delivered_receipt_flush(&mut self) {
        let Some(due_at) = self.pending_delivered_receipts.values().min().copied() else {
            self.pending_delivered_receipt_flush_due_at = None;
            return;
        };
        if self
            .pending_delivered_receipt_flush_due_at
            .is_some_and(|existing| existing <= due_at)
        {
            return;
        }
        self.pending_delivered_receipt_flush_due_at = Some(due_at);
        self.pending_delivered_receipt_token = self.pending_delivered_receipt_token.wrapping_add(1);
        let token = self.pending_delivered_receipt_token;
        let tx = self.core_sender.clone();
        self.runtime.spawn(async move {
            sleep_until(due_at).await;
            let _ = tx.send(CoreMsg::Internal(Box::new(
                InternalEvent::FlushPendingDeliveredReceipts { token },
            )));
        });
    }

    pub(super) fn handle_pending_delivered_receipt_flush(&mut self, token: u64) {
        if token != self.pending_delivered_receipt_token {
            return;
        }
        self.pending_delivered_receipt_flush_due_at = None;
        self.flush_due_pending_delivered_receipts(Instant::now());
        self.schedule_pending_delivered_receipt_flush();
    }

    fn flush_due_pending_delivered_receipts(&mut self, now: Instant) {
        let pending = std::mem::take(&mut self.pending_delivered_receipts);
        let mut due = Vec::new();
        for ((chat_id, message_id), due_at) in pending {
            if due_at <= now {
                due.push((chat_id, message_id));
            } else {
                self.pending_delivered_receipts
                    .insert((chat_id, message_id), due_at);
            }
        }
        self.send_pending_delivered_receipts(due);
    }

    #[cfg(test)]
    pub(super) fn flush_all_pending_delivered_receipts_for_test(&mut self) {
        let pending = std::mem::take(&mut self.pending_delivered_receipts);
        self.pending_delivered_receipt_flush_due_at = None;
        self.pending_delivered_receipt_token = self.pending_delivered_receipt_token.wrapping_add(1);
        self.send_pending_delivered_receipts(pending.into_keys());
    }

    fn cancel_pending_delivered_receipts(&mut self, chat_id: &str, message_ids: &[String]) {
        if message_ids.is_empty() {
            return;
        }
        let ids = message_ids
            .iter()
            .map(String::as_str)
            .collect::<HashSet<_>>();
        let mut removed = false;
        self.pending_delivered_receipts
            .retain(|(pending_chat_id, pending_message_id), _| {
                let keep = pending_chat_id != chat_id || !ids.contains(pending_message_id.as_str());
                removed |= !keep;
                keep
            });
        let delivered_key = (chat_id.to_string(), "delivered".to_string());
        let mut remove_batch_key = false;
        if let Some(batch_ids) = self.pending_outgoing_receipts.get_mut(&delivered_key) {
            batch_ids.retain(|id| !ids.contains(id.as_str()));
            remove_batch_key = batch_ids.is_empty();
        }
        if remove_batch_key {
            self.pending_outgoing_receipts.remove(&delivered_key);
        }
        if removed && self.pending_delivered_receipts.is_empty() {
            self.pending_delivered_receipt_flush_due_at = None;
            self.pending_delivered_receipt_token =
                self.pending_delivered_receipt_token.wrapping_add(1);
        }
    }

    fn send_pending_delivered_receipts(
        &mut self,
        pending: impl IntoIterator<Item = (String, String)>,
    ) {
        let mut by_chat = BTreeMap::<String, Vec<String>>::new();
        for (chat_id, message_id) in pending {
            if self.should_send_pending_delivered_receipt(&chat_id, &message_id) {
                by_chat.entry(chat_id).or_default().push(message_id);
            }
        }
        for (chat_id, mut ids) in by_chat {
            ids.sort();
            ids.dedup();
            if !ids.is_empty() {
                self.send_receipt(&chat_id, "delivered", ids);
            }
        }
    }

    fn should_send_pending_delivered_receipt(&self, chat_id: &str, message_id: &str) -> bool {
        if !self.preferences.send_read_receipts || self.thread_is_message_request(chat_id) {
            return false;
        }
        let Some(thread) = self.threads.get(chat_id) else {
            return false;
        };
        thread.messages.iter().any(|message| {
            !message.is_outgoing
                && message.id == message_id
                && !matches!(message.delivery, DeliveryState::Seen)
        })
    }

    pub(super) fn send_receipt_inner(
        &mut self,
        chat_id: &str,
        receipt_type: &str,
        message_ids: Vec<String>,
    ) {
        let Some(owner_pubkey) = self
            .logged_in
            .as_ref()
            .map(|logged_in| logged_in.owner_pubkey)
        else {
            return;
        };
        if is_group_chat_id(chat_id) {
            self.send_group_author_receipts(owner_pubkey, chat_id, receipt_type, message_ids);
        } else if let Ok((_, peer)) = parse_peer_input(chat_id) {
            self.send_pairwise_receipt(
                owner_pubkey,
                peer,
                chat_id,
                receipt_type,
                message_ids,
                None,
            );
        }
    }

    fn send_group_author_receipts(
        &mut self,
        owner_pubkey: PublicKey,
        chat_id: &str,
        receipt_type: &str,
        message_ids: Vec<String>,
    ) {
        let Some(group_id) = parse_group_id_from_chat_id(chat_id) else {
            return;
        };
        let local_owner_hex = owner_pubkey.to_hex();
        for (author_hex, author_message_ids) in
            self.group_receipt_message_ids_by_author(chat_id, message_ids)
        {
            if author_hex == local_owner_hex {
                continue;
            }
            let Ok(author) = PublicKey::parse(&author_hex) else {
                continue;
            };
            self.send_pairwise_receipt(
                owner_pubkey,
                author,
                chat_id,
                receipt_type,
                author_message_ids,
                Some(&group_id),
            );
        }
    }

    fn group_receipt_message_ids_by_author(
        &mut self,
        chat_id: &str,
        message_ids: Vec<String>,
    ) -> BTreeMap<String, Vec<String>> {
        let mut by_author = BTreeMap::<String, Vec<String>>::new();
        // A batch can flush after explicitly acknowledged rows have left the
        // visible window. Resolve their authors without changing pagination.
        let Some(stored_messages) = self.load_stored_receipt_messages(chat_id, &message_ids) else {
            return by_author;
        };
        let requested = message_ids.into_iter().collect::<HashSet<_>>();
        let Some(thread) = self.threads.get(chat_id) else {
            return by_author;
        };
        for message in thread.messages.iter().chain(&stored_messages) {
            if message.is_outgoing || !requested.contains(&message.id) {
                continue;
            }
            let Some(author_hex) = group_message_author_hex(message) else {
                continue;
            };
            by_author
                .entry(author_hex.to_string())
                .or_default()
                .push(message.id.clone());
        }
        by_author
    }

    fn send_pairwise_receipt(
        &mut self,
        owner_pubkey: PublicKey,
        peer: PublicKey,
        chat_id: &str,
        receipt_type: &str,
        message_ids: Vec<String>,
        group_id: Option<&str>,
    ) {
        if message_ids.is_empty() {
            return;
        }
        let Some(unsigned) =
            receipt_unsigned_event(owner_pubkey, receipt_type, message_ids, group_id)
        else {
            return;
        };
        self.send_protocol_engine_unsigned_event_to_peer_only(peer, chat_id, unsigned, "receipt");
    }

    pub(super) fn apply_receipt_to_messages(
        &mut self,
        chat_id: &str,
        message_ids: &[String],
        delivery: DeliveryState,
        is_from_local_owner: bool,
        receipt_author_hex: Option<&str>,
    ) {
        if message_ids.is_empty() {
            return;
        }
        let Some(mut stored_messages) = self.load_stored_receipt_messages(chat_id, message_ids)
        else {
            return;
        };
        stored_messages.retain_mut(|message| {
            apply_message_receipt(message, &delivery, is_from_local_owner, receipt_author_hex)
        });
        // Save unloaded rows directly even inside a relay-event batch. A deferred
        // full snapshot contains only the visible window and cannot save them.
        let unread_thread = (is_from_local_owner && matches!(delivery, DeliveryState::Seen))
            .then(|| self.threads.get(chat_id))
            .flatten();
        let mut newly_seen_incoming =
            match self
                .app_store
                .save_message_receipts(chat_id, &stored_messages, unread_thread)
            {
                Ok(count) => count,
                Err(error) => {
                    self.push_debug_log("storage.messages.receipt.error", error.to_string());
                    return;
                }
            };
        let Some(thread) = self.threads.get_mut(chat_id) else {
            return;
        };
        let unread_ids = thread.unread_message_ids();
        let mut changed = !stored_messages.is_empty();
        for message in &mut thread.messages {
            if !message_ids.contains(&message.id) {
                continue;
            }
            if apply_message_receipt(message, &delivery, is_from_local_owner, receipt_author_hex) {
                if !message.is_outgoing && matches!(delivery, DeliveryState::Seen) {
                    newly_seen_incoming += u64::from(unread_ids.contains(&message.id));
                }
                changed = true;
            }
        }
        if is_from_local_owner && matches!(delivery, DeliveryState::Seen) {
            // An older receipt must not clear newer, unread messages.
            let next_unread = thread.unread_count.saturating_sub(newly_seen_incoming);
            changed |= thread.unread_count != next_unread;
            thread.unread_count = next_unread;
        }
        if changed {
            self.persist_best_effort();
        }
    }
}

fn apply_message_receipt(
    message: &mut ChatMessageSnapshot,
    delivery: &DeliveryState,
    is_from_local_owner: bool,
    receipt_author_hex: Option<&str>,
) -> bool {
    if is_from_local_owner == message.is_outgoing {
        return false;
    }
    let mut changed = false;
    if should_advance_delivery(&message.delivery, delivery) {
        message.delivery = delivery.clone();
        changed = true;
    }
    if message.is_outgoing {
        if let Some(author) = receipt_author_hex {
            if let Some(recipient) = message
                .recipient_deliveries
                .iter_mut()
                .find(|recipient| recipient.owner_pubkey_hex == author)
            {
                if should_advance_delivery(&recipient.delivery, delivery) {
                    recipient.delivery = delivery.clone();
                    recipient.updated_at_secs = unix_now().get();
                    changed = true;
                }
            } else {
                message
                    .recipient_deliveries
                    .push(MessageRecipientDeliverySnapshot {
                        owner_pubkey_hex: author.to_string(),
                        display_name: String::new(),
                        picture_url: None,
                        delivery: delivery.clone(),
                        updated_at_secs: unix_now().get(),
                    });
                changed = true;
            }
        }
    }
    changed
}

fn receipt_unsigned_event(
    owner_pubkey: PublicKey,
    receipt_type: &str,
    message_ids: Vec<String>,
    group_id: Option<&str>,
) -> Option<UnsignedEvent> {
    let now = unix_now();
    let receipt_type_for_pairwise = match receipt_type {
        "seen" => pairwise_codec::ReceiptType::Seen,
        _ => pairwise_codec::ReceiptType::Delivered,
    };
    let mut unsigned = pairwise_codec::receipt_event(
        owner_pubkey,
        receipt_type_for_pairwise,
        message_ids,
        pairwise_codec::EncodeOptions::new(now.get(), now.get().saturating_mul(1000)),
    )
    .ok()?;
    if let Some(group_id) = group_id {
        if let Ok(group_tag) = nostr::Tag::parse(["l", group_id]) {
            unsigned.tags.push(group_tag);
            unsigned.id = None;
            unsigned.ensure_id();
        }
    }
    Some(unsigned)
}

fn should_advance_delivery(current: &DeliveryState, next: &DeliveryState) -> bool {
    delivery_rank(next) > delivery_rank(current)
}

fn delivery_rank(state: &DeliveryState) -> u8 {
    match state {
        DeliveryState::Queued => 0,
        DeliveryState::Pending => 1,
        DeliveryState::Sent => 2,
        DeliveryState::Received => 3,
        DeliveryState::Seen => 4,
        DeliveryState::Failed => 0,
    }
}

fn group_message_author_hex(message: &ChatMessageSnapshot) -> Option<&str> {
    message.author_owner_pubkey_hex.as_deref().or_else(|| {
        PublicKey::parse(&message.author)
            .ok()
            .map(|_| message.author.as_str())
    })
}