rusmes-jmap 0.1.2

JMAP server for RusMES — RFC 8620/8621 HTTP/JSON mail API with Email, Mailbox, Thread, Blob, EventSource push, and VacationResponse support
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
//! SearchSnippet method implementations for JMAP
//!
//! Implements:
//! - SearchSnippet/get - search result preview snippets with highlighting

use crate::methods::ensure_account_ownership;
use crate::types::Principal;
use rusmes_storage::MessageStore;
use serde::{Deserialize, Serialize};

/// SearchSnippet object
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchSnippet {
    /// Email ID
    pub email_id: String,
    /// Subject snippet with highlighting
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    /// Preview snippet with highlighting
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preview: Option<String>,
}

/// SearchSnippet/get request
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchSnippetGetRequest {
    pub account_id: String,
    /// Email IDs to get snippets for
    pub email_ids: Vec<String>,
    /// Filter from the Email/query that generated these results
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<crate::types::EmailFilterCondition>,
}

/// SearchSnippet/get response
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchSnippetGetResponse {
    pub account_id: String,
    pub list: Vec<SearchSnippet>,
    pub not_found: Vec<String>,
}

/// Handle SearchSnippet/get method
pub async fn search_snippet_get(
    request: SearchSnippetGetRequest,
    _message_store: &dyn MessageStore,
    principal: &Principal,
) -> anyhow::Result<SearchSnippetGetResponse> {
    ensure_account_ownership(&request.account_id, principal)?;
    let mut list = Vec::new();
    let mut not_found = Vec::new();

    // Extract search terms from filter
    let search_terms = extract_search_terms(&request.filter);

    for email_id in request.email_ids {
        // In production, would:
        // 1. Retrieve email by ID from message store
        // 2. Extract subject and body text
        // 3. Generate snippets with highlighted context around matches
        // 4. Truncate to reasonable length

        // For now, create a simple snippet or mark as not found
        if search_terms.is_empty() {
            // No search terms, return basic snippet
            list.push(SearchSnippet {
                email_id: email_id.clone(),
                subject: None,
                preview: None,
            });
        } else {
            // Would generate actual highlighted snippets in production
            not_found.push(email_id);
        }
    }

    Ok(SearchSnippetGetResponse {
        account_id: request.account_id,
        list,
        not_found,
    })
}

/// Extract search terms from email filter condition
fn extract_search_terms(filter: &Option<crate::types::EmailFilterCondition>) -> Vec<String> {
    let mut terms = Vec::new();

    if let Some(f) = filter {
        if let Some(text) = &f.text {
            terms.extend(text.split_whitespace().map(|s| s.to_string()));
        }
        if let Some(subject) = &f.subject {
            terms.extend(subject.split_whitespace().map(|s| s.to_string()));
        }
        if let Some(body) = &f.body {
            terms.extend(body.split_whitespace().map(|s| s.to_string()));
        }
        if let Some(from) = &f.from {
            terms.extend(from.split_whitespace().map(|s| s.to_string()));
        }
    }

    terms
}

/// Generate search snippet with highlighted terms
///
/// Extracts context around matching keywords and highlights matches with `<mark>` tags.
/// Configurable context size (default: 50 chars before/after).
pub fn generate_snippet(text: &str, search_terms: &[String], max_length: usize) -> String {
    if search_terms.is_empty() {
        // No search terms, just return truncated text
        if text.len() <= max_length {
            return text.to_string();
        }
        return format!("{}...", &text[..max_length.saturating_sub(3)]);
    }

    // Find first occurrence of any search term
    let mut best_pos = None;
    let text_lower = text.to_lowercase();

    for term in search_terms {
        let term_lower = term.to_lowercase();
        if let Some(pos) = text_lower.find(&term_lower) {
            if best_pos.map_or(true, |best| pos < best) {
                best_pos = Some(pos);
            }
        }
    }

    match best_pos {
        Some(pos) => {
            // Calculate context window around the match
            let context_before = 50;
            let context_after = max_length.saturating_sub(context_before).saturating_sub(6); // 6 for potential "..." on both sides

            let start = pos.saturating_sub(context_before);
            let end = (start + context_before + context_after).min(text.len());

            let mut snippet = String::new();
            if start > 0 {
                snippet.push_str("...");
            }
            snippet.push_str(&text[start..end]);
            if end < text.len() {
                snippet.push_str("...");
            }

            // Highlight the search terms
            highlight_snippet(&snippet, search_terms)
        }
        None => {
            // No match found, return beginning of text
            if text.len() <= max_length {
                text.to_string()
            } else {
                format!("{}...", &text[..max_length.saturating_sub(3)])
            }
        }
    }
}

/// Highlight search terms in text with HTML mark tags
///
/// Example: "important message" with term "important" becomes "<mark>important</mark> message"
pub fn highlight_snippet(text: &str, search_terms: &[String]) -> String {
    if search_terms.is_empty() {
        return text.to_string();
    }

    let mut result = text.to_string();
    let text_lower = text.to_lowercase();

    // Collect all match positions
    let mut matches: Vec<(usize, usize, String)> = Vec::new();

    for term in search_terms {
        let term_lower = term.to_lowercase();
        let mut pos = 0;
        while let Some(found_pos) = text_lower[pos..].find(&term_lower) {
            let actual_pos = pos + found_pos;
            let end_pos = actual_pos + term.len();

            // Get the actual text (preserve case)
            let matched_text = text[actual_pos..end_pos].to_string();
            matches.push((actual_pos, end_pos, matched_text));

            pos = end_pos;
        }
    }

    // Sort by position (reverse order for replacement)
    matches.sort_by_key(|b| std::cmp::Reverse(b.0));

    // Remove overlapping matches
    let mut non_overlapping: Vec<(usize, usize, String)> = Vec::new();
    for m in matches {
        let overlaps = non_overlapping.iter().any(|existing| {
            (m.0 >= existing.0 && m.0 < existing.1) || (m.1 > existing.0 && m.1 <= existing.1)
        });
        if !overlaps {
            non_overlapping.push(m);
        }
    }

    // Apply highlights in reverse order to preserve positions
    for (start, end, matched_text) in non_overlapping {
        let highlighted = format!("<mark>{}</mark>", matched_text);
        result.replace_range(start..end, &highlighted);
    }

    result
}

#[cfg(test)]
mod tests {

    fn test_principal() -> crate::types::Principal {
        crate::types::admin_principal_for_tests()
    }

    use super::*;
    use rusmes_storage::backends::filesystem::FilesystemBackend;
    use rusmes_storage::StorageBackend;
    use std::path::PathBuf;

    fn create_test_store() -> std::sync::Arc<dyn MessageStore> {
        let backend = FilesystemBackend::new(PathBuf::from("/tmp/rusmes-test-storage"));
        backend.message_store()
    }

    #[tokio::test]
    async fn test_search_snippet_get() {
        let store = create_test_store();
        let request = SearchSnippetGetRequest {
            account_id: "acc1".to_string(),
            email_ids: vec!["email1".to_string()],
            filter: None,
        };

        let response = search_snippet_get(request, store.as_ref(), &test_principal())
            .await
            .unwrap();
        assert_eq!(response.account_id, "acc1");
        assert_eq!(response.list.len(), 1);
    }

    #[tokio::test]
    async fn test_search_snippet_multiple_emails() {
        let store = create_test_store();
        let request = SearchSnippetGetRequest {
            account_id: "acc1".to_string(),
            email_ids: vec![
                "email1".to_string(),
                "email2".to_string(),
                "email3".to_string(),
            ],
            filter: None,
        };

        let response = search_snippet_get(request, store.as_ref(), &test_principal())
            .await
            .unwrap();
        assert_eq!(response.list.len(), 3);
    }

    #[tokio::test]
    async fn test_search_snippet_with_filter() {
        let store = create_test_store();
        let filter = crate::types::EmailFilterCondition {
            in_mailbox: None,
            in_mailbox_other_than: None,
            before: None,
            after: None,
            min_size: None,
            max_size: None,
            all_in_thread_have_keyword: None,
            some_in_thread_have_keyword: None,
            none_in_thread_have_keyword: None,
            has_keyword: None,
            not_keyword: None,
            has_attachment: None,
            text: Some("search term".to_string()),
            from: None,
            to: None,
            cc: None,
            bcc: None,
            subject: None,
            body: None,
            header: None,
        };

        let request = SearchSnippetGetRequest {
            account_id: "acc1".to_string(),
            email_ids: vec!["email1".to_string()],
            filter: Some(filter),
        };

        let response = search_snippet_get(request, store.as_ref(), &test_principal())
            .await
            .unwrap();
        assert_eq!(response.account_id, "acc1");
    }

    #[tokio::test]
    async fn test_search_snippet_empty_email_ids() {
        let store = create_test_store();
        let request = SearchSnippetGetRequest {
            account_id: "acc1".to_string(),
            email_ids: vec![],
            filter: None,
        };

        let response = search_snippet_get(request, store.as_ref(), &test_principal())
            .await
            .unwrap();
        assert_eq!(response.list.len(), 0);
        assert_eq!(response.not_found.len(), 0);
    }

    #[tokio::test]
    async fn test_generate_snippet_no_search_terms() {
        let text = "This is a test message with some content.";
        let snippet = generate_snippet(text, &[], 100);
        assert_eq!(snippet, text);
    }

    #[tokio::test]
    async fn test_generate_snippet_with_match() {
        let text = "This is a test message with important information that we need to find.";
        let terms = vec!["important".to_string()];
        let snippet = generate_snippet(text, &terms, 100);

        assert!(snippet.contains("important"));
        assert!(snippet.contains("<mark>"));
    }

    #[tokio::test]
    async fn test_generate_snippet_truncate_long_text() {
        let text = "A".repeat(200);
        let snippet = generate_snippet(&text, &[], 50);

        assert!(snippet.len() <= 53); // 50 + "..."
        assert!(snippet.ends_with("..."));
    }

    #[tokio::test]
    async fn test_generate_snippet_match_at_start() {
        let text = "Important information at the beginning of the message.";
        let terms = vec!["Important".to_string()];
        let snippet = generate_snippet(text, &terms, 100);

        assert!(snippet.contains("<mark>Important</mark>"));
    }

    #[tokio::test]
    async fn test_generate_snippet_match_at_end() {
        let text = "The message ends with important information.";
        let terms = vec!["important".to_string()];
        let snippet = generate_snippet(text, &terms, 100);

        assert!(snippet.contains("<mark>important</mark>"));
    }

    #[tokio::test]
    async fn test_generate_snippet_multiple_terms() {
        let text = "This message contains both urgent and important information.";
        let terms = vec!["urgent".to_string(), "important".to_string()];
        let snippet = generate_snippet(text, &terms, 100);

        // Should find first matching term
        assert!(
            snippet.contains("<mark>urgent</mark>") || snippet.contains("<mark>important</mark>")
        );
    }

    #[tokio::test]
    async fn test_generate_snippet_case_insensitive() {
        let text = "This message contains IMPORTANT information.";
        let terms = vec!["important".to_string()];
        let snippet = generate_snippet(text, &terms, 100);

        assert!(snippet.to_lowercase().contains("<mark>important</mark>"));
    }

    #[tokio::test]
    async fn test_generate_snippet_exact_max_length() {
        let text = "Exactly fifty characters for testing purposes!";
        let snippet = generate_snippet(text, &[], 47);

        assert_eq!(snippet, text);
    }

    #[tokio::test]
    async fn test_generate_snippet_context_window() {
        let text = "A".repeat(50) + "IMPORTANT" + &"Z".repeat(50);
        let terms = vec!["IMPORTANT".to_string()];
        let snippet = generate_snippet(&text, &terms, 80);

        assert!(snippet.contains("<mark>IMPORTANT</mark>"));
        assert!(snippet.contains("..."));
    }

    #[tokio::test]
    async fn test_highlight_snippet_basic() {
        let text = "This is an important message";
        let terms = vec!["important".to_string()];
        let highlighted = highlight_snippet(text, &terms);

        assert!(highlighted.contains("<mark>important</mark>"));
    }

    #[tokio::test]
    async fn test_highlight_snippet_multiple_occurrences() {
        let text = "test message with test data";
        let terms = vec!["test".to_string()];
        let highlighted = highlight_snippet(text, &terms);

        // Should highlight both occurrences
        let count = highlighted.matches("<mark>test</mark>").count();
        assert_eq!(count, 2);
    }

    #[tokio::test]
    async fn test_highlight_snippet_case_preservation() {
        let text = "IMPORTANT message is Important";
        let terms = vec!["important".to_string()];
        let highlighted = highlight_snippet(text, &terms);

        assert!(highlighted.contains("<mark>IMPORTANT</mark>"));
        assert!(highlighted.contains("<mark>Important</mark>"));
    }

    #[tokio::test]
    async fn test_highlight_snippet_no_terms() {
        let text = "No highlighting needed";
        let highlighted = highlight_snippet(text, &[]);

        assert_eq!(highlighted, text);
        assert!(!highlighted.contains("<mark>"));
    }

    #[tokio::test]
    async fn test_highlight_snippet_overlapping_terms() {
        let text = "important information";
        let terms = vec!["important".to_string(), "info".to_string()];
        let highlighted = highlight_snippet(text, &terms);

        // Should highlight both without breaking
        assert!(highlighted.contains("<mark>"));
    }

    #[tokio::test]
    async fn test_extract_search_terms_text() {
        let filter = crate::types::EmailFilterCondition {
            in_mailbox: None,
            in_mailbox_other_than: None,
            before: None,
            after: None,
            min_size: None,
            max_size: None,
            all_in_thread_have_keyword: None,
            some_in_thread_have_keyword: None,
            none_in_thread_have_keyword: None,
            has_keyword: None,
            not_keyword: None,
            has_attachment: None,
            text: Some("search term".to_string()),
            from: None,
            to: None,
            cc: None,
            bcc: None,
            subject: None,
            body: None,
            header: None,
        };

        let terms = extract_search_terms(&Some(filter));
        assert_eq!(terms.len(), 2);
        assert!(terms.contains(&"search".to_string()));
        assert!(terms.contains(&"term".to_string()));
    }

    #[tokio::test]
    async fn test_extract_search_terms_multiple_fields() {
        let filter = crate::types::EmailFilterCondition {
            in_mailbox: None,
            in_mailbox_other_than: None,
            before: None,
            after: None,
            min_size: None,
            max_size: None,
            all_in_thread_have_keyword: None,
            some_in_thread_have_keyword: None,
            none_in_thread_have_keyword: None,
            has_keyword: None,
            not_keyword: None,
            has_attachment: None,
            text: Some("search".to_string()),
            from: Some("user".to_string()),
            to: None,
            cc: None,
            bcc: None,
            subject: Some("important".to_string()),
            body: Some("message".to_string()),
            header: None,
        };

        let terms = extract_search_terms(&Some(filter));
        assert!(terms.len() >= 4);
    }

    #[tokio::test]
    async fn test_extract_search_terms_empty() {
        let terms = extract_search_terms(&None);
        assert_eq!(terms.len(), 0);
    }

    #[tokio::test]
    async fn test_search_snippet_object_structure() {
        let snippet = SearchSnippet {
            email_id: "email1".to_string(),
            subject: Some("Test Subject".to_string()),
            preview: Some("This is a preview...".to_string()),
        };

        let json = serde_json::to_string(&snippet).unwrap();
        assert!(json.contains("email1"));
        assert!(json.contains("Test Subject"));
    }

    #[tokio::test]
    async fn test_generate_snippet_very_short_text() {
        let text = "Hi";
        let snippet = generate_snippet(text, &[], 100);
        assert_eq!(snippet, "Hi");
    }

    #[tokio::test]
    async fn test_generate_snippet_no_match_no_terms() {
        let text = "This is a longer message that should be truncated";
        let snippet = generate_snippet(text, &[], 20);

        assert!(snippet.len() <= 20);
        assert!(snippet.ends_with("..."));
    }
}