synpad 0.1.0

A full-featured Matrix chat client built with Dioxus
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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// A single indexed message entry.
#[derive(Clone, Debug, Serialize, Deserialize)]
struct IndexedMessage {
    room_id: String,
    event_id: String,
    sender: String,
    body: String,
    /// Lowercase body for case-insensitive matching.
    body_lower: String,
    timestamp: u64,
}

/// A search result returned from the local search index.
#[derive(Clone, Debug, PartialEq)]
pub struct SearchResult {
    pub room_id: String,
    pub event_id: String,
    pub sender: String,
    pub body: String,
    pub timestamp: u64,
    /// A snippet of the message body with the matched portion wrapped in <mark> tags.
    pub snippet: String,
}

/// In-memory full-text search index for locally decrypted messages.
///
/// Encrypted messages are decrypted client-side and never sent to the server
/// in plaintext, so server-side search cannot find them. This index allows
/// users to search through messages that have already been decrypted and
/// displayed in the timeline.
///
/// The index supports file-backed persistence so that it survives app restarts.
/// Use `save_to_file` / `load_from_file` for explicit persistence, or set
/// `auto_save_threshold` and check `needs_save()` to trigger saves automatically
/// after a certain number of new messages have been indexed.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SearchIndex {
    /// All indexed messages keyed by event_id for deduplication.
    messages: HashMap<String, IndexedMessage>,
    /// Secondary index: room_id -> list of event_ids in that room.
    room_index: HashMap<String, Vec<String>>,
    /// When message count crosses this threshold since last save, a save is needed.
    /// Set to 0 to disable auto-save hinting.
    #[serde(default = "default_auto_save_threshold")]
    pub auto_save_threshold: usize,
    /// Tracks the message count at the time of the last save.
    #[serde(default)]
    last_save_count: usize,
    /// Whether the index has been modified since the last save.
    #[serde(skip)]
    dirty: bool,
}

fn default_auto_save_threshold() -> usize {
    100
}

impl Default for SearchIndex {
    fn default() -> Self {
        Self::new()
    }
}

impl SearchIndex {
    /// Create a new empty search index.
    pub fn new() -> Self {
        Self {
            messages: HashMap::new(),
            room_index: HashMap::new(),
            auto_save_threshold: default_auto_save_threshold(),
            last_save_count: 0,
            dirty: false,
        }
    }

    /// Add a message to the search index.
    ///
    /// If a message with the same event_id already exists, it will be replaced.
    pub fn index_message(
        &mut self,
        room_id: &str,
        event_id: &str,
        sender: &str,
        body: &str,
        timestamp: u64,
    ) {
        // Skip empty bodies
        if body.trim().is_empty() {
            return;
        }

        let entry = IndexedMessage {
            room_id: room_id.to_string(),
            event_id: event_id.to_string(),
            sender: sender.to_string(),
            body: body.to_string(),
            body_lower: body.to_lowercase(),
            timestamp,
        };

        // Remove from old room index if updating
        if let Some(old) = self.messages.get(event_id) {
            let old_room = old.room_id.clone();
            if let Some(ids) = self.room_index.get_mut(&old_room) {
                ids.retain(|id| id != event_id);
            }
        }

        // Insert into room index
        self.room_index
            .entry(room_id.to_string())
            .or_default()
            .push(event_id.to_string());

        // Insert into main index
        self.messages.insert(event_id.to_string(), entry);

        // Mark as dirty since we modified the index
        self.dirty = true;
    }

    /// Search indexed messages with case-insensitive substring matching.
    ///
    /// If `room_id` is provided, only messages from that room are searched.
    /// Results are sorted by timestamp (most recent first).
    pub fn search(&self, query: &str, room_id: Option<&str>) -> Vec<SearchResult> {
        if query.trim().is_empty() {
            return Vec::new();
        }

        let query_lower = query.to_lowercase();

        let candidates: Box<dyn Iterator<Item = &IndexedMessage> + '_> = match room_id {
            Some(rid) => {
                let event_ids = self.room_index.get(rid);
                match event_ids {
                    Some(ids) => Box::new(
                        ids.iter().filter_map(|eid| self.messages.get(eid)),
                    ),
                    None => return Vec::new(),
                }
            }
            None => Box::new(self.messages.values()),
        };

        let mut results: Vec<SearchResult> = candidates
            .filter(|msg| msg.body_lower.contains(&query_lower))
            .map(|msg| {
                let snippet = build_snippet(&msg.body, &query_lower);
                SearchResult {
                    room_id: msg.room_id.clone(),
                    event_id: msg.event_id.clone(),
                    sender: msg.sender.clone(),
                    body: msg.body.clone(),
                    timestamp: msg.timestamp,
                    snippet,
                }
            })
            .collect();

        // Sort by timestamp descending (most recent first)
        results.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

        results
    }

    /// Clear all indexed messages for a specific room.
    pub fn clear_room(&mut self, room_id: &str) {
        if let Some(event_ids) = self.room_index.remove(room_id) {
            for eid in &event_ids {
                self.messages.remove(eid);
            }
            self.dirty = true;
        }
    }

    /// Returns the total number of indexed messages.
    pub fn message_count(&self) -> usize {
        self.messages.len()
    }

    /// Returns a list of room IDs that have indexed messages.
    pub fn indexed_room_ids(&self) -> Vec<String> {
        self.room_index
            .iter()
            .filter(|(_, ids)| !ids.is_empty())
            .map(|(rid, _)| rid.clone())
            .collect()
    }

    /// Save the search index to a JSON file on disk.
    pub fn save_to_file(&mut self, path: &std::path::Path) -> Result<(), String> {
        let json = serde_json::to_string(self).map_err(|e| {
            let err = e.to_string();
            format!("Failed to serialize search index: {}", err)
        })?;

        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| {
                let err = e.to_string();
                format!("Failed to create directory for search index: {}", err)
            })?;
        }

        std::fs::write(path, json).map_err(|e| {
            let err = e.to_string();
            format!("Failed to write search index file: {}", err)
        })?;

        // Reset dirty tracking after successful save
        self.last_save_count = self.messages.len();
        self.dirty = false;

        tracing::debug!("Search index saved to {:?} ({} messages)", path, self.last_save_count);

        Ok(())
    }

    /// Load a search index from a JSON file on disk.
    ///
    /// If the file does not exist, returns a new empty index.
    pub fn load_from_file(path: &std::path::Path) -> Result<Self, String> {
        if !path.exists() {
            tracing::debug!("No search index file at {:?}, starting fresh", path);
            return Ok(Self::new());
        }

        let json = std::fs::read_to_string(path).map_err(|e| {
            let err = e.to_string();
            format!("Failed to read search index file: {}", err)
        })?;

        let mut index: SearchIndex = serde_json::from_str(&json).map_err(|e| {
            let err = e.to_string();
            format!("Failed to deserialize search index: {}", err)
        })?;

        // After loading, the index is clean (matches what's on disk)
        index.last_save_count = index.messages.len();
        index.dirty = false;

        tracing::debug!("Search index loaded from {:?} ({} messages)", path, index.messages.len());

        Ok(index)
    }

    /// Mark the index as dirty (modified since last save).
    pub fn mark_dirty(&mut self) {
        self.dirty = true;
    }

    /// Check whether the index needs to be saved.
    ///
    /// Returns `true` if the index has been modified and either:
    /// - `auto_save_threshold` is 0 (every change triggers save hint), or
    /// - The number of messages added since the last save exceeds the threshold.
    pub fn needs_save(&self) -> bool {
        if !self.dirty {
            return false;
        }

        if self.auto_save_threshold == 0 {
            return true;
        }

        let messages_since_save = self.messages.len().saturating_sub(self.last_save_count);
        messages_since_save >= self.auto_save_threshold
    }
}

/// Build a snippet string with the matched portion wrapped in <mark> tags.
///
/// Shows up to `context_chars` characters around the first match.
fn build_snippet(body: &str, query_lower: &str) -> String {
    let body_lower = body.to_lowercase();
    let match_start = match body_lower.find(query_lower) {
        Some(pos) => pos,
        None => return body.to_string(),
    };

    let context_chars: usize = 40;
    let match_end = match_start + query_lower.len();

    // Compute snippet window around the match
    let snippet_start = if match_start > context_chars {
        // Find a word boundary near the desired start
        let desired = match_start - context_chars;
        body[desired..match_start]
            .find(' ')
            .map(|pos| desired + pos + 1)
            .unwrap_or(desired)
    } else {
        0
    };

    let snippet_end = if match_end + context_chars < body.len() {
        let desired = match_end + context_chars;
        body[match_end..desired]
            .rfind(' ')
            .map(|pos| match_end + pos)
            .unwrap_or(desired)
    } else {
        body.len()
    };

    let prefix = if snippet_start > 0 { "..." } else { "" };
    let suffix = if snippet_end < body.len() { "..." } else { "" };

    let before = &body[snippet_start..match_start];
    let matched = &body[match_start..match_end];
    let after = &body[match_end..snippet_end];

    format!(
        "{}{}<mark>{}</mark>{}{}",
        prefix, before, matched, after, suffix
    )
}

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

    #[test]
    fn test_index_and_search() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello world", 1000);
        index.index_message("!room1:example.com", "$evt2", "@bob:example.com", "Goodbye world", 2000);
        index.index_message("!room2:example.com", "$evt3", "@alice:example.com", "Hello again", 3000);

        assert_eq!(index.message_count(), 3);

        let results = index.search("hello", None);
        assert_eq!(results.len(), 2);
        // Most recent first
        assert_eq!(results[0].event_id, "$evt3");
        assert_eq!(results[1].event_id, "$evt1");
    }

    #[test]
    fn test_search_by_room() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello world", 1000);
        index.index_message("!room2:example.com", "$evt2", "@bob:example.com", "Hello there", 2000);

        let results = index.search("hello", Some("!room1:example.com"));
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].room_id, "!room1:example.com");
    }

    #[test]
    fn test_clear_room() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello", 1000);
        index.index_message("!room1:example.com", "$evt2", "@bob:example.com", "World", 2000);
        index.index_message("!room2:example.com", "$evt3", "@alice:example.com", "Test", 3000);

        assert_eq!(index.message_count(), 3);
        index.clear_room("!room1:example.com");
        assert_eq!(index.message_count(), 1);
    }

    #[test]
    fn test_empty_query_returns_nothing() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello", 1000);

        let results = index.search("", None);
        assert!(results.is_empty());
    }

    #[test]
    fn test_empty_body_not_indexed() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "", 1000);
        index.index_message("!room1:example.com", "$evt2", "@alice:example.com", "   ", 2000);
        assert_eq!(index.message_count(), 0);
    }

    #[test]
    fn test_snippet_has_mark_tags() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello world", 1000);

        let results = index.search("world", None);
        assert_eq!(results.len(), 1);
        assert!(results[0].snippet.contains("<mark>world</mark>"));
    }

    #[test]
    fn test_case_insensitive() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello World", 1000);

        let results = index.search("HELLO", None);
        assert_eq!(results.len(), 1);
        // The snippet should preserve original casing
        assert!(results[0].snippet.contains("<mark>Hello</mark>"));
    }

    #[test]
    fn test_deduplication() {
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello v1", 1000);
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello v2", 2000);

        assert_eq!(index.message_count(), 1);
        let results = index.search("Hello", None);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].body, "Hello v2");
    }

    #[test]
    fn test_dirty_tracking() {
        let mut index = SearchIndex::new();
        assert!(!index.needs_save());

        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello", 1000);
        assert!(index.dirty);

        // With default threshold of 100, one message isn't enough
        assert!(!index.needs_save());

        // Set a low threshold
        index.auto_save_threshold = 1;
        assert!(index.needs_save());
    }

    #[test]
    fn test_mark_dirty() {
        let mut index = SearchIndex::new();
        assert!(!index.dirty);
        index.mark_dirty();
        assert!(index.dirty);
    }

    #[test]
    fn test_save_and_load() {
        let dir = std::env::temp_dir().join("synpad_test_search_index");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("test_index.json");

        // Create and populate an index
        let mut index = SearchIndex::new();
        index.index_message("!room1:example.com", "$evt1", "@alice:example.com", "Hello world", 1000);
        index.index_message("!room2:example.com", "$evt2", "@bob:example.com", "Test message", 2000);

        // Save it
        index.save_to_file(&path).expect("save should succeed");
        assert!(!index.needs_save());
        assert!(!index.dirty);

        // Load it
        let loaded = SearchIndex::load_from_file(&path).expect("load should succeed");
        assert_eq!(loaded.message_count(), 2);

        // Search should work on loaded index
        let results = loaded.search("hello", None);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].event_id, "$evt1");

        // Clean up
        let _ = std::fs::remove_file(&path);
        let _ = std::fs::remove_dir(&dir);
    }

    #[test]
    fn test_load_nonexistent_file() {
        let path = std::path::Path::new("/tmp/nonexistent_synpad_search_index_xyz.json");
        let index = SearchIndex::load_from_file(path).expect("should return empty index");
        assert_eq!(index.message_count(), 0);
    }
}