File size: 1,446 Bytes
2299bb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Formatter utilities - clean and normalize messages.
"""
import re
from typing import Optional
from html import escape


def clean_message(text: str, max_length: int = 4000) -> str:
    """
    Clean and sanitize message text.
    - Remove excessive whitespace
    - Limit length
    - Escape HTML
    """
    if not text:
        return ""
    
    # Strip leading/trailing whitespace
    text = text.strip()
    
    # Remove excessive internal whitespace (more than 2 spaces)
    text = re.sub(r' {2,}', ' ', text)
    
    # Remove excessive newlines (more than 2 consecutive)
    text = re.sub(r'\n{3,}', '\n\n', text)
    
    # Limit length
    if len(text) > max_length:
        text = text[:max_length]
    
    # Escape HTML to prevent XSS
    text = escape(text)
    
    return text


def normalize_content(content: str) -> str:
    """Normalize message content for storage/search."""
    # Lowercase for search indexing (optional)
    # Remove special characters for search (optional)
    return content.strip()


def format_timestamp(timestamp) -> str:
    """Format a timestamp for display."""
    from datetime import datetime
    if isinstance(timestamp, datetime):
        return timestamp.strftime("%Y-%m-%d %H:%M")
    return str(timestamp)


def truncate_text(text: str, max_len: int = 50) -> str:
    """Truncate text for previews."""
    if len(text) <= max_len:
        return text
    return text[:max_len - 3] + "..."