Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 54x 54x 54x 37x 37x 37x 37x 54x 7x 42x 42x 42x 42x 42x 42x 42x 26x 26x 42x 5x 5x 42x 4x 4x 42x 21x 5x 5x 5x 1x 1x 1x 2x 2x 2x 3x 3x 3x 5x 2x 2x 1x 1x 2x 2x 5x 2x 2x 2x 2x 2x 2x 1x 1x 1x 42x 42x 26x 26x 26x 26x 16x 1x 15x 42x 42x 42x 2x | import type Database from "better-sqlite3-multiple-ciphers";
export interface SearchResult {
id: number;
subject: string;
from_address: string;
from_name: string;
date: string;
snippet: string;
folder_path: string;
rank: number;
}
export interface SearchOptions {
inboundConnectorId?: number;
folderId?: number;
limit?: number;
offset?: number;
}
interface ParsedQuery {
ftsQuery: string;
filters: SearchFilter[];
}
interface SearchFilter {
type: "from" | "to" | "subject" | "has" | "is" | "before" | "after" | "label";
value: string;
}
/**
* Parse Gmail-style search operators from a query string.
*
* Supported operators:
* from:user@example.com — match sender address or name
* to:user@example.com — match recipient address
* subject:hello — match subject (use quotes for phrases: subject:"hello world")
* has:attachment — messages with attachments
* is:unread — unread messages
* is:starred — starred/flagged messages
* is:read — read messages
* before:2024-01-15 — messages before a date (YYYY-MM-DD)
* after:2024-01-15 — messages after a date (YYYY-MM-DD)
* label:inbox — messages with a specific label
*
* Remaining text after operator extraction is passed to FTS5.
*/
export function parseSearchQuery(raw: string): ParsedQuery {
const filters: SearchFilter[] = [];
// Match operator:value or operator:"quoted value"
const operatorRegex = /\b(from|to|subject|has|is|before|after|label):((?:"[^"]*")|(?:\S+))/gi;
const ftsQuery = raw
.replace(operatorRegex, (_, op: string, val: string) => {
const cleanVal = val.replace(/^"|"$/g, "");
const type = op.toLowerCase() as SearchFilter["type"];
filters.push({ type, value: cleanVal });
return "";
})
.replace(/\s+/g, " ")
.trim();
return { ftsQuery, filters };
}
/**
* Full-text search across synced messages using SQLite FTS5.
*/
export class MessageSearch {
private db: Database.Database;
constructor(db: Database.Database) {
this.db = db;
}
/**
* Search messages by query string.
* Supports FTS5 query syntax (AND, OR, NOT, phrase matching with quotes)
* and Gmail-style operators (from:, to:, subject:, has:, is:, before:, after:, label:).
*/
search(query: string, options: SearchOptions = {}): SearchResult[] {
const { inboundConnectorId, folderId, limit = 50, offset = 0 } = options;
const { ftsQuery, filters } = parseSearchQuery(query);
const conditions: string[] = [];
const params: (string | number)[] = [];
const joins: string[] = [];
let useFts = false;
// Only MATCH on FTS if there's remaining text
if (ftsQuery) {
useFts = true;
params.push(ftsQuery);
}
if (inboundConnectorId) {
conditions.push("m.inbound_connector_id = ?");
params.push(inboundConnectorId);
}
if (folderId) {
conditions.push("m.folder_id = ?");
params.push(folderId);
}
for (const filter of filters) {
switch (filter.type) {
case "from":
conditions.push(
"(m.from_address LIKE ? COLLATE NOCASE OR m.from_name LIKE ? COLLATE NOCASE)",
);
params.push(`%${filter.value}%`, `%${filter.value}%`);
break;
case "to":
conditions.push(
"(m.to_addresses LIKE ? COLLATE NOCASE OR m.cc_addresses LIKE ? COLLATE NOCASE)",
);
params.push(`%${filter.value}%`, `%${filter.value}%`);
break;
case "subject":
conditions.push("m.subject LIKE ? COLLATE NOCASE");
params.push(`%${filter.value}%`);
break;
case "has":
Eif (filter.value.toLowerCase() === "attachment") {
conditions.push("m.has_attachments > 0");
}
break;
case "is":
switch (filter.value.toLowerCase()) {
case "unread":
conditions.push("(m.flags IS NULL OR m.flags NOT LIKE '%\\Seen%')");
break;
case "read":
conditions.push("m.flags LIKE '%\\Seen%'");
break;
case "starred":
conditions.push("m.flags LIKE '%\\Flagged%'");
break;
}
break;
case "before":
conditions.push("m.date < ?");
params.push(filter.value);
break;
case "after":
conditions.push("m.date > ?");
params.push(filter.value);
break;
case "label":
joins.push(
"JOIN message_labels ml_filter ON ml_filter.message_id = m.id JOIN labels l_filter ON l_filter.id = ml_filter.label_id AND l_filter.name LIKE ? COLLATE NOCASE",
);
params.push(filter.value);
break;
}
}
params.push(limit, offset);
if (useFts) {
// FTS query with optional filters
const whereClause = conditions.length > 0 ? `AND ${conditions.join(" AND ")}` : "";
const joinClause = joins.join(" ");
const stmt = this.db.prepare(`
SELECT
m.id,
m.subject,
m.from_address,
m.from_name,
m.date,
snippet(messages_fts, -1, '<mark>', '</mark>', '...', 40) as snippet,
f.path as folder_path,
rank
FROM messages_fts
JOIN messages m ON m.id = messages_fts.rowid
JOIN folders f ON f.id = m.folder_id
${joinClause}
WHERE messages_fts MATCH ?
${whereClause}
ORDER BY rank
LIMIT ? OFFSET ?
`);
return stmt.all(...params) as SearchResult[];
}
// Filter-only query (no FTS text) — search by conditions only
if (conditions.length === 0 && joins.length === 0) {
return [];
}
const whereClause = conditions.length > 0 ? conditions.join(" AND ") : "1=1";
const joinClause = joins.join(" ");
const stmt = this.db.prepare(`
SELECT
m.id,
m.subject,
m.from_address,
m.from_name,
m.date,
SUBSTR(m.text_body, 1, 200) as snippet,
f.path as folder_path,
0 as rank
FROM messages m
JOIN folders f ON f.id = m.folder_id
${joinClause}
WHERE ${whereClause}
ORDER BY m.date DESC
LIMIT ? OFFSET ?
`);
return stmt.all(...params) as SearchResult[];
}
/**
* Rebuild the FTS index from scratch.
* Useful after bulk imports or schema changes.
*/
rebuildIndex(): void {
this.db.exec("INSERT INTO messages_fts(messages_fts) VALUES ('rebuild')");
}
}
|