siggy 1.8.0

Terminal-based Signal messenger client with vim keybindings
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
//! Conversation, message, and contact storage extracted from [`crate::app::App`].
//!
//! [`ConversationStore`] owns the in-memory conversation map, ordered
//! sidebar list, contact-name and UUID lookups, and per-conversation read
//! markers. Mutations also persist via [`crate::db::Database`].

use chrono::{DateTime, Local, Utc};
use ratatui::text::Line;
use std::collections::{HashMap, HashSet};

use crate::db::Database;
use crate::signal::types::{
    Group, LinkPreview, Mention, MessageStatus, PollData, PollVote, Reaction, StyleType, TextStyle,
};

/// Log a database error via debug_log (no-op when --debug is off).
pub(crate) fn db_warn<T>(result: Result<T, impl std::fmt::Display>, context: &str) {
    if let Err(e) = result {
        crate::debug_log::logf(format_args!("db {context}: {e}"));
    }
}

/// Resolve U+FFFC placeholders in a message body using bodyRanges mentions against
/// a supplied `uuid_to_name` map. Returns (resolved_body, mention_byte_ranges).
/// Extracted as a free function so callers can re-resolve against a cloned or
/// borrowed map without conflicting with mutable iteration over conversations.
pub fn resolve_mentions_with(
    body: &str,
    mentions: &[Mention],
    uuid_to_name: &HashMap<String, String>,
) -> (String, Vec<(usize, usize)>) {
    if mentions.is_empty() {
        return (body.to_string(), Vec::new());
    }

    let lookup = |uuid: &str| -> String {
        uuid_to_name.get(uuid).cloned().unwrap_or_else(|| {
            // Truncated UUID fallback
            let short = if uuid.len() > 8 { &uuid[..8] } else { uuid };
            short.to_string()
        })
    };

    // Sort mentions by start descending so replacements don't shift earlier offsets
    let mut sorted: Vec<&Mention> = mentions.iter().collect();
    sorted.sort_by_key(|b| std::cmp::Reverse(b.start));

    // Convert body to UTF-16 for offset mapping
    let utf16: Vec<u16> = body.encode_utf16().collect();
    let mut result_utf16 = utf16.clone();
    for mention in &sorted {
        if mention.start >= result_utf16.len() {
            continue;
        }
        let name = lookup(&mention.uuid);
        let replacement = format!("@{name}");
        let replacement_utf16: Vec<u16> = replacement.encode_utf16().collect();
        let end = (mention.start + mention.length).min(result_utf16.len());
        result_utf16.splice(mention.start..end, replacement_utf16);
    }

    let resolved = String::from_utf16_lossy(&result_utf16);

    // Compute byte ranges for each @Name in the resolved string
    let mut ranges: Vec<(usize, usize)> = Vec::new();
    let mut sorted_fwd: Vec<&Mention> = mentions.iter().collect();
    sorted_fwd.sort_by_key(|m| m.start);

    // Re-build with forward pass to get accurate byte offsets
    let resolved_utf16: Vec<u16> = resolved.encode_utf16().collect();
    let mut byte_pos = 0;
    let resolved_bytes = resolved.as_bytes();

    // Build utf16_offset -> byte_offset mapping
    let mut utf16_to_byte: Vec<usize> = Vec::with_capacity(resolved_utf16.len() + 1);
    for ch in resolved.chars() {
        let utf16_len = ch.len_utf16();
        let utf8_len = ch.len_utf8();
        for _ in 0..utf16_len {
            utf16_to_byte.push(byte_pos);
        }
        byte_pos += utf8_len;
    }
    utf16_to_byte.push(byte_pos); // sentinel for end

    // Calculate where each mention ended up after all replacements
    let mut offset_shift: i64 = 0;
    for mention in &sorted_fwd {
        let adjusted_start = (mention.start as i64 + offset_shift) as usize;
        let name = lookup(&mention.uuid);
        let replacement_utf16_len = format!("@{name}").encode_utf16().count();
        let byte_start = utf16_to_byte
            .get(adjusted_start)
            .copied()
            .unwrap_or(resolved_bytes.len());
        let byte_end = utf16_to_byte
            .get(adjusted_start + replacement_utf16_len)
            .copied()
            .unwrap_or(resolved_bytes.len());
        ranges.push((byte_start, byte_end));
        // This mention replaced `mention.length` UTF-16 units with `replacement_utf16_len`
        offset_shift += replacement_utf16_len as i64 - mention.length as i64;
    }

    (resolved, ranges)
}

/// Shorten a phone number for display: +15551234567 -> +1***4567
pub(crate) fn short_name(number: &str) -> String {
    let chars: Vec<char> = number.chars().collect();
    if chars.len() > 6 {
        let prefix: String = chars[..2].iter().collect();
        let last4: String = chars[chars.len() - 4..].iter().collect();
        format!("{prefix}***{last4}")
    } else {
        number.to_string()
    }
}

/// Sentinel string used as `sender` / `sender_id` for outgoing messages and
/// reactions. Persisted to the DB as the literal string `"you"` for legacy
/// reasons, but new code should go through `DisplayMessage::is_outgoing` /
/// `Reaction::is_from_me` / the `OUTGOING_SENDER` const rather than comparing
/// the string directly. See issue #453 for the history.
pub const OUTGOING_SENDER: &str = "you";

/// Quoted reply context attached to a message.
#[derive(Debug, Clone)]
pub struct Quote {
    pub author: String,
    pub body: String,
    pub timestamp_ms: i64,
    /// Original phone number / account ID for wire protocol (not resolved to display name)
    pub author_id: String,
}

/// A single displayed message in a conversation
#[derive(Debug, Clone)]
pub struct DisplayMessage {
    pub sender: String,
    pub timestamp: DateTime<Utc>,
    pub body: String,
    pub is_system: bool,
    /// Pre-rendered halfblock image lines (for image attachments)
    pub image_lines: Option<Vec<Line<'static>>>,
    /// Local filesystem path for native protocol rendering (Kitty/iTerm2)
    pub image_path: Option<String>,
    /// Delivery/read status (Some for outgoing, None for incoming)
    pub status: Option<MessageStatus>,
    /// Millisecond epoch timestamp for receipt matching
    pub timestamp_ms: i64,
    /// Emoji reactions on this message
    pub reactions: Vec<Reaction>,
    /// Byte ranges of @mentions in body (for styling)
    pub mention_ranges: Vec<(usize, usize)>,
    /// Original body with U+FFFC placeholders, for lazy re-resolution of mentions
    /// when the contact list updates. `None` for legacy messages or messages with
    /// no mentions.
    pub body_raw: Option<String>,
    /// Raw mentions from signal-cli's bodyRanges array. Empty for messages with
    /// no mentions or legacy messages without a stored raw body.
    pub mentions: Vec<Mention>,
    /// Byte ranges + style type for text styling (bold, italic, etc.)
    pub style_ranges: Vec<(usize, usize, StyleType)>,
    /// Quoted reply context
    pub quote: Option<Quote>,
    /// Whether this message has been edited
    pub is_edited: bool,
    /// Whether this message has been remotely deleted
    pub is_deleted: bool,
    /// Whether this message is pinned
    pub is_pinned: bool,
    /// Phone number / ID of the sender (for wire protocol; "you" for outgoing)
    pub sender_id: String,
    /// Disappearing message timer (seconds, 0 = no expiration)
    pub expires_in_seconds: i64,
    /// When the expiration countdown started (epoch ms, 0 = not started)
    pub expiration_start_ms: i64,
    /// Poll data (for poll-create messages)
    pub poll_data: Option<PollData>,
    /// Votes received for this poll
    pub poll_votes: Vec<PollVote>,
    /// Link preview metadata
    pub preview: Option<LinkPreview>,
    /// Pre-rendered halfblock image lines for link preview thumbnail
    pub preview_image_lines: Option<Vec<Line<'static>>>,
    /// Local filesystem path for native protocol link preview thumbnail
    pub preview_image_path: Option<String>,
}

impl DisplayMessage {
    pub fn format_time(&self) -> String {
        let local: DateTime<Local> = self.timestamp.with_timezone(&Local);
        local.format("%H:%M").to_string()
    }

    /// Whether this message was sent from this account (vs. received from a
    /// contact). Three signals combine because they overlap in different
    /// data sources: `status.is_some()` is the canonical runtime marker
    /// (outgoing messages carry a [`MessageStatus`]), while `sender` /
    /// `sender_id == "you"` cover demo data and legacy DB rows respectively.
    pub fn is_outgoing(&self) -> bool {
        self.status.is_some() || self.sender == OUTGOING_SENDER || self.sender_id == OUTGOING_SENDER
    }

    /// Wire identifier suitable for routing replies / reactions / pin queries
    /// back to the original author. Resolves to `my_account` for outgoing
    /// messages or legacy rows with no `sender_id`; otherwise returns the
    /// sender's phone / UUID. The empty-sender_id fallback is defensive: a
    /// legacy row with neither signal is rarer than the alternative (an
    /// invalid empty recipient string sent to signal-cli).
    pub fn route_author<'a>(&'a self, my_account: &'a str) -> &'a str {
        if self.is_outgoing() || self.sender_id.is_empty() {
            my_account
        } else {
            &self.sender_id
        }
    }
}

impl crate::signal::types::Reaction {
    /// Whether this reaction was sent from this account.
    pub fn is_from_me(&self) -> bool {
        self.sender == OUTGOING_SENDER
    }
}

/// A conversation (1:1 or group)
#[derive(Debug, Clone)]
pub struct Conversation {
    /// Display name (contact name/number or group name)
    pub name: String,
    /// Unique key — phone number for 1:1, group ID for groups
    pub id: String,
    pub messages: Vec<DisplayMessage>,
    pub unread: usize,
    pub is_group: bool,
    /// Disappearing message timer in seconds (0 = off)
    pub expiration_timer: i64,
    /// Whether this conversation has been accepted (message requests are unaccepted)
    pub accepted: bool,
}

impl Conversation {
    /// Whether this conversation is stale and should be hidden from the default sidebar view.
    /// A conversation is stale if it has no messages AND has no meaningful name
    /// (e.g. empty/abandoned groups, or contacts with only a UUID hash).
    pub fn is_stale(&self) -> bool {
        if !self.messages.is_empty() {
            return false;
        }
        if self.is_group {
            // Group with no messages and no resolved name (name is the raw group ID)
            self.name.is_empty() || self.name == self.id
        } else {
            // 1:1 contact with no messages and no usable name:
            // keep if name is a phone number (+...), hide if name is just the raw ID
            // (a UUID hash or "..." with no real identity)
            !self.name.starts_with('+') && self.name == self.id
        }
    }

    /// Binary-search for a message by timestamp (messages are sorted by `timestamp_ms`).
    pub fn find_msg_idx(&self, ts: i64) -> Option<usize> {
        let end = self.messages.partition_point(|m| m.timestamp_ms <= ts);
        if end > 0 && self.messages[end - 1].timestamp_ms == ts {
            Some(end - 1)
        } else {
            None
        }
    }
}

/// Owns all conversation data: conversations, ordering, contact names, groups, and read markers.
pub struct ConversationStore {
    /// All conversations keyed by phone number (1:1) or group ID (groups).
    pub conversations: HashMap<String, Conversation>,
    /// Ordered list of conversation IDs for sidebar display.
    pub conversation_order: Vec<String>,
    /// Contact/group name lookup (number/id → display name).
    pub contact_names: HashMap<String, String>,
    /// UUID → display name mapping (built from contact list).
    pub uuid_to_name: HashMap<String, String>,
    /// Phone number → UUID mapping (for sending mentions).
    pub number_to_uuid: HashMap<String, String>,
    /// Last-read message index per conversation (for unread marker).
    pub last_read_index: HashMap<String, usize>,
    /// Groups indexed by group_id (with member lists for @mention autocomplete).
    pub groups: HashMap<String, Group>,
    /// Conversations that have more messages in the database to load.
    pub has_more_messages: HashSet<String>,
}

impl ConversationStore {
    pub fn new() -> Self {
        Self {
            conversations: HashMap::new(),
            conversation_order: Vec::new(),
            contact_names: HashMap::new(),
            uuid_to_name: HashMap::new(),
            number_to_uuid: HashMap::new(),
            last_read_index: HashMap::new(),
            groups: HashMap::new(),
            has_more_messages: HashSet::new(),
        }
    }

    /// Ensure a conversation exists; create it if not. Returns a mutable ref.
    pub fn get_or_create_conversation(
        &mut self,
        id: &str,
        name: &str,
        is_group: bool,
        db: &Database,
    ) -> &mut Conversation {
        if !self.conversations.contains_key(id) {
            // New conversation — always persist
            db_warn(
                db.upsert_conversation(id, name, is_group),
                "upsert_conversation",
            );
            self.conversations.insert(
                id.to_string(),
                Conversation {
                    name: name.to_string(),
                    id: id.to_string(),
                    messages: Vec::new(),
                    unread: 0,
                    is_group,
                    expiration_timer: 0,
                    accepted: true,
                },
            );
            self.conversation_order.push(id.to_string());
        } else if name != id {
            // Existing conversation — only update if we have a real display name
            let conv = self.conversations.get_mut(id).unwrap();
            if conv.name != name {
                conv.name = name.to_string();
                db_warn(
                    db.upsert_conversation(id, name, is_group),
                    "upsert_conversation",
                );
            }
        }
        self.conversations.get_mut(id).unwrap()
    }

    /// Remember a contact's display name if not already known.
    /// No-op when `name` is `None`. Used by signal-event handlers to learn
    /// names from envelopes (typing indicators, reactions, edits, pins, votes).
    pub fn remember_contact_name(&mut self, id: &str, name: Option<&str>) {
        if let Some(name) = name {
            self.contact_names
                .entry(id.to_string())
                .or_insert_with(|| name.to_string());
        }
    }

    /// Move a conversation to the top of the sidebar order.
    /// Returns `true` if the conversation was actually reordered.
    pub fn move_conversation_to_top(&mut self, id: &str) -> bool {
        let pos = match self.conversation_order.iter().position(|c| c == id) {
            Some(pos) => pos,
            None => return false,
        };

        self.conversation_order.remove(pos);
        self.conversation_order.insert(0, id.to_string());
        true
    }

    /// Total unread count across all conversations.
    pub fn total_unread(&self) -> usize {
        self.conversations.values().map(|c| c.unread).sum()
    }

    /// Resolve U+FFFC placeholders in a message body using bodyRanges mentions.
    /// Returns (resolved_body, mention_byte_ranges) where mention_byte_ranges are
    /// (start, end) byte offsets of each `@Name` in the resolved body.
    pub fn resolve_mentions(
        &self,
        body: &str,
        mentions: &[Mention],
    ) -> (String, Vec<(usize, usize)>) {
        resolve_mentions_with(body, mentions, &self.uuid_to_name)
    }

    /// Re-resolve @mentions across all stored messages using the current
    /// `uuid_to_name` map. Intended to be called after a contact or group list
    /// update populates previously-unknown UUIDs. Persists updated bodies to
    /// the database.
    ///
    /// Fix for #283: before this, the first render of a message with mentions
    /// that arrived before the contact list would bake the truncated UUID into
    /// the display body forever.
    pub fn rebuild_mention_display(&mut self, db: &Database) {
        let uuid_to_name = self.uuid_to_name.clone();
        for (conv_id, conv) in self.conversations.iter_mut() {
            for msg in conv.messages.iter_mut() {
                if msg.mentions.is_empty() {
                    continue;
                }
                let body_raw = match &msg.body_raw {
                    Some(b) => b.clone(),
                    None => continue,
                };
                let (resolved, ranges) =
                    resolve_mentions_with(&body_raw, &msg.mentions, &uuid_to_name);
                if resolved != msg.body {
                    msg.body = resolved.clone();
                    msg.mention_ranges = ranges;
                    db_warn(
                        db.update_message_body(conv_id, msg.timestamp_ms, &resolved),
                        "update_message_body (rebuild_mentions)",
                    );
                }
            }
        }
    }

    /// Convert text style ranges from UTF-16 offsets (on the original body) to byte offsets
    /// on the resolved body (after mention replacement). Mentions may change the body length,
    /// so we need to account for the offset shift caused by mention replacements.
    pub fn resolve_text_styles(
        &self,
        resolved_body: &str,
        text_styles: &[TextStyle],
        mentions: &[Mention],
    ) -> Vec<(usize, usize, StyleType)> {
        if text_styles.is_empty() {
            return Vec::new();
        }

        // Calculate how mention replacements shift UTF-16 offsets.
        let mut mention_shifts: Vec<(usize, i64)> = Vec::new();
        if !mentions.is_empty() {
            let mut sorted_mentions: Vec<&Mention> = mentions.iter().collect();
            sorted_mentions.sort_by_key(|m| m.start);
            let mut cumulative: i64 = 0;
            for m in &sorted_mentions {
                let name = self.uuid_to_name.get(&m.uuid).cloned().unwrap_or_else(|| {
                    let short = if m.uuid.len() > 8 {
                        &m.uuid[..8]
                    } else {
                        &m.uuid
                    };
                    short.to_string()
                });
                let replacement_utf16_len = format!("@{name}").encode_utf16().count() as i64;
                let original_len = m.length as i64;
                cumulative += replacement_utf16_len - original_len;
                mention_shifts.push((m.start + m.length, cumulative));
            }
        }

        // For a given original UTF-16 offset, compute the shifted offset after mention replacements
        let shift_offset = |orig: usize| -> usize {
            let mut shift: i64 = 0;
            for &(boundary, cum_shift) in &mention_shifts {
                if orig >= boundary {
                    shift = cum_shift;
                } else {
                    break;
                }
            }
            (orig as i64 + shift) as usize
        };

        // Build UTF-16 to byte offset mapping for the resolved body
        let mut utf16_to_byte: Vec<usize> = Vec::new();
        let mut byte_pos = 0;
        for ch in resolved_body.chars() {
            for _ in 0..ch.len_utf16() {
                utf16_to_byte.push(byte_pos);
            }
            byte_pos += ch.len_utf8();
        }
        utf16_to_byte.push(byte_pos); // sentinel

        let body_byte_len = resolved_body.len();

        text_styles
            .iter()
            .filter_map(|ts| {
                let shifted_start = shift_offset(ts.start);
                let shifted_end = shift_offset(ts.start + ts.length);
                let byte_start = utf16_to_byte
                    .get(shifted_start)
                    .copied()
                    .unwrap_or(body_byte_len);
                let byte_end = utf16_to_byte
                    .get(shifted_end)
                    .copied()
                    .unwrap_or(body_byte_len);
                if byte_start < byte_end && byte_end <= body_byte_len {
                    Some((byte_start, byte_end, ts.style))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Re-resolve reaction sender names and quote authors using the latest contact_names.
    pub fn resolve_stored_names(&mut self, account: &str) {
        // Build phone→name lookup from message sender_id fields
        let mut phone_to_name: HashMap<String, String> = HashMap::new();
        for conv in self.conversations.values() {
            for msg in &conv.messages {
                if !msg.sender_id.is_empty()
                    && msg.sender_id != "you"
                    && !msg.sender.is_empty()
                    && msg.sender != msg.sender_id
                {
                    phone_to_name.insert(msg.sender_id.clone(), msg.sender.clone());
                }
            }
        }

        // Merge contact_names on top (takes priority)
        for (phone, name) in &self.contact_names {
            phone_to_name.insert(phone.clone(), name.clone());
        }

        // Resolve reaction senders and quote authors
        for conv in self.conversations.values_mut() {
            for msg in &mut conv.messages {
                // Resolve reaction senders
                for reaction in &mut msg.reactions {
                    if reaction.is_from_me() {
                        continue;
                    }
                    if reaction.sender == account {
                        reaction.sender = OUTGOING_SENDER.to_string();
                    } else if let Some(name) = phone_to_name.get(&reaction.sender) {
                        reaction.sender = name.clone();
                    }
                }
                // Resolve quote author
                if let Some(ref mut quote) = msg.quote {
                    if quote.author == account {
                        quote.author = "you".to_string();
                    } else if let Some(name) = phone_to_name.get(&quote.author) {
                        quote.author = name.clone();
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn remember_contact_name_respects_existing_entry() {
        let mut s = ConversationStore::new();
        s.remember_contact_name("+1", None);
        assert!(!s.contact_names.contains_key("+1"));
        s.remember_contact_name("+1", Some("Alice"));
        assert_eq!(s.contact_names["+1"], "Alice");
        s.remember_contact_name("+1", Some("Overwrite?"));
        assert_eq!(s.contact_names["+1"], "Alice");
    }
}