forked from nkuntz1934/matrix-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.ts
More file actions
430 lines (368 loc) · 12.1 KB
/
search.ts
File metadata and controls
430 lines (368 loc) · 12.1 KB
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
// Search API
// Implements: https://spec.matrix.org/v1.12/client-server-api/#searching
//
// Provides full-text search for room messages
import { Hono } from 'hono';
import type { AppEnv } from '../types';
import { Errors } from '../utils/errors';
import { requireAuth } from '../middleware/auth';
const app = new Hono<AppEnv>();
// ============================================
// Types
// ============================================
interface SearchRequest {
search_categories: {
room_events?: {
search_term: string;
keys?: string[];
filter?: {
rooms?: string[];
not_rooms?: string[];
senders?: string[];
not_senders?: string[];
types?: string[];
not_types?: string[];
};
order_by?: 'recent' | 'rank';
event_context?: {
before_limit?: number;
after_limit?: number;
include_profile?: boolean;
};
include_state?: boolean;
groupings?: {
group_by: Array<{ key: string }>;
};
};
};
}
interface SearchResult {
event_id: string;
rank: number;
result: {
event_id: string;
type: string;
room_id: string;
sender: string;
origin_server_ts: number;
content: Record<string, any>;
};
context?: {
events_before: any[];
events_after: any[];
profile_info?: Record<string, { displayname?: string; avatar_url?: string }>;
start?: string;
end?: string;
};
}
// ============================================
// Endpoints
// ============================================
// POST /_matrix/client/v3/search - Search room events
app.post('/_matrix/client/v3/search', requireAuth(), async (c) => {
const userId = c.get('userId');
const db = c.env.DB;
// Parse pagination
const nextBatch = c.req.query('next_batch');
let body: SearchRequest;
try {
body = await c.req.json();
} catch {
return Errors.badJson().toResponse();
}
const roomEvents = body.search_categories?.room_events;
if (!roomEvents) {
return c.json({
search_categories: {
room_events: {
results: [],
count: 0,
highlights: [],
},
},
});
}
const searchTerm = roomEvents.search_term;
if (!searchTerm || searchTerm.trim().length === 0) {
return c.json({
search_categories: {
room_events: {
results: [],
count: 0,
highlights: [],
},
},
});
}
const filter = roomEvents.filter || {};
const orderBy = roomEvents.order_by || 'recent';
const eventContext = roomEvents.event_context;
const includeState = roomEvents.include_state || false;
// Get rooms the user is a member of
const userRooms = await db.prepare(`
SELECT room_id FROM room_memberships
WHERE user_id = ? AND membership IN ('join', 'leave')
`).bind(userId).all<{ room_id: string }>();
const userRoomIds = new Set(userRooms.results.map(r => r.room_id));
// Apply room filters
let searchRoomIds = Array.from(userRoomIds);
if (filter.rooms && filter.rooms.length > 0) {
searchRoomIds = searchRoomIds.filter(r => filter.rooms!.includes(r));
}
if (filter.not_rooms && filter.not_rooms.length > 0) {
searchRoomIds = searchRoomIds.filter(r => !filter.not_rooms!.includes(r));
}
if (searchRoomIds.length === 0) {
return c.json({
search_categories: {
room_events: {
results: [],
count: 0,
highlights: [],
},
},
});
}
// Build the search query using FTS5 for ranked full-text search
const limit = 50;
let offset = 0;
if (nextBatch) {
try {
offset = parseInt(nextBatch, 10);
} catch {}
}
// Use FTS5 MATCH for full-text search with BM25 ranking
// Escape FTS5 special characters in search term
const ftsSearchTerm = searchTerm.replace(/['"*()]/g, ' ').trim();
let query = `
SELECT e.event_id, e.event_type, e.room_id, e.sender, e.origin_server_ts, e.content,
bm25(events_fts) as rank
FROM events_fts fts
JOIN events e ON fts.event_id = e.event_id
WHERE fts.body MATCH ?
AND e.room_id IN (${searchRoomIds.map(() => '?').join(',')})
`;
const params: any[] = [ftsSearchTerm, ...searchRoomIds];
// Apply sender filter
if (filter.senders && filter.senders.length > 0) {
query += ` AND e.sender IN (${filter.senders.map(() => '?').join(',')})`;
params.push(...filter.senders);
}
if (filter.not_senders && filter.not_senders.length > 0) {
query += ` AND e.sender NOT IN (${filter.not_senders.map(() => '?').join(',')})`;
params.push(...filter.not_senders);
}
// Apply type filter (though we're already filtering for m.room.message)
if (filter.types && filter.types.length > 0) {
query += ` AND e.event_type IN (${filter.types.map(() => '?').join(',')})`;
params.push(...filter.types);
}
if (filter.not_types && filter.not_types.length > 0) {
query += ` AND e.event_type NOT IN (${filter.not_types.map(() => '?').join(',')})`;
params.push(...filter.not_types);
}
// Order by
if (orderBy === 'rank') {
query += ` ORDER BY rank ASC`; // BM25 returns negative values, lower = better
} else {
query += ` ORDER BY e.origin_server_ts DESC`;
}
query += ` LIMIT ? OFFSET ?`;
params.push(limit + 1, offset);
const results = await db.prepare(query).bind(...params).all<{
event_id: string;
event_type: string;
room_id: string;
sender: string;
origin_server_ts: number;
content: string;
rank: number;
}>();
// Check if there are more results
const hasMore = results.results.length > limit;
const searchResults = results.results.slice(0, limit);
// Get count (approximate for performance)
const countQuery = `
SELECT COUNT(*) as total
FROM events_fts fts
JOIN events e ON fts.event_id = e.event_id
WHERE fts.body MATCH ?
AND e.room_id IN (${searchRoomIds.map(() => '?').join(',')})
`;
const countResult = await db.prepare(countQuery).bind(ftsSearchTerm, ...searchRoomIds).first<{ total: number }>();
const totalCount = countResult?.total || 0;
// Build response
const formattedResults: SearchResult[] = [];
for (const event of searchResults) {
let content: Record<string, any> = {};
try {
content = JSON.parse(event.content);
} catch {}
const result: SearchResult = {
event_id: event.event_id,
rank: Math.abs(event.rank || 0),
result: {
event_id: event.event_id,
type: event.event_type,
room_id: event.room_id,
sender: event.sender,
origin_server_ts: event.origin_server_ts,
content,
},
};
// Add context if requested
if (eventContext) {
const beforeLimit = eventContext.before_limit || 5;
const afterLimit = eventContext.after_limit || 5;
// Get events before
const eventsBefore = await db.prepare(`
SELECT event_id, event_type, sender, origin_server_ts, content
FROM events
WHERE room_id = ? AND origin_server_ts < ?
ORDER BY origin_server_ts DESC
LIMIT ?
`).bind(event.room_id, event.origin_server_ts, beforeLimit).all<{
event_id: string;
event_type: string;
sender: string;
origin_server_ts: number;
content: string;
}>();
// Get events after
const eventsAfter = await db.prepare(`
SELECT event_id, event_type, sender, origin_server_ts, content
FROM events
WHERE room_id = ? AND origin_server_ts > ?
ORDER BY origin_server_ts ASC
LIMIT ?
`).bind(event.room_id, event.origin_server_ts, afterLimit).all<{
event_id: string;
event_type: string;
sender: string;
origin_server_ts: number;
content: string;
}>();
result.context = {
events_before: eventsBefore.results.reverse().map(e => ({
event_id: e.event_id,
type: e.event_type,
sender: e.sender,
origin_server_ts: e.origin_server_ts,
content: JSON.parse(e.content),
room_id: event.room_id,
})),
events_after: eventsAfter.results.map(e => ({
event_id: e.event_id,
type: e.event_type,
sender: e.sender,
origin_server_ts: e.origin_server_ts,
content: JSON.parse(e.content),
room_id: event.room_id,
})),
};
// Add profile info if requested
if (eventContext.include_profile) {
const senders = new Set<string>();
senders.add(event.sender);
eventsBefore.results.forEach(e => senders.add(e.sender));
eventsAfter.results.forEach(e => senders.add(e.sender));
const profiles: Record<string, { displayname?: string; avatar_url?: string }> = {};
for (const senderId of senders) {
const profile = await db.prepare(`
SELECT display_name, avatar_url FROM users WHERE user_id = ?
`).bind(senderId).first<{ display_name: string | null; avatar_url: string | null }>();
if (profile) {
profiles[senderId] = {
displayname: profile.display_name || undefined,
avatar_url: profile.avatar_url || undefined,
};
}
}
result.context.profile_info = profiles;
}
}
formattedResults.push(result);
}
// Extract highlights (words that matched)
const highlights = extractHighlights(searchTerm);
// Build response
const response: any = {
search_categories: {
room_events: {
results: formattedResults,
count: totalCount,
highlights,
},
},
};
// Add pagination token if there are more results
if (hasMore) {
response.search_categories.room_events.next_batch = String(offset + limit);
}
// Add room state if requested
if (includeState && formattedResults.length > 0) {
const roomIds = new Set(formattedResults.map(r => r.result.room_id));
const state: Record<string, any[]> = {};
for (const roomId of roomIds) {
const roomState = await db.prepare(`
SELECT e.event_type, e.state_key, e.sender, e.content, e.origin_server_ts
FROM room_state rs
JOIN events e ON rs.event_id = e.event_id
WHERE rs.room_id = ?
`).bind(roomId).all<{
event_type: string;
state_key: string;
sender: string;
content: string;
origin_server_ts: number;
}>();
state[roomId] = roomState.results.map(s => ({
type: s.event_type,
state_key: s.state_key,
sender: s.sender,
content: JSON.parse(s.content),
origin_server_ts: s.origin_server_ts,
room_id: roomId,
}));
}
response.search_categories.room_events.state = state;
}
// Add groupings if requested
if (roomEvents.groupings?.group_by) {
const groups: Record<string, any> = {};
for (const groupBy of roomEvents.groupings.group_by) {
if (groupBy.key === 'room_id') {
const roomGroups: Record<string, { results: string[]; order: number; next_batch?: string }> = {};
for (const result of formattedResults) {
const roomId = result.result.room_id;
if (!roomGroups[roomId]) {
roomGroups[roomId] = { results: [], order: 0 };
}
roomGroups[roomId].results.push(result.event_id);
}
groups.room_id = roomGroups;
} else if (groupBy.key === 'sender') {
const senderGroups: Record<string, { results: string[]; order: number; next_batch?: string }> = {};
for (const result of formattedResults) {
const sender = result.result.sender;
if (!senderGroups[sender]) {
senderGroups[sender] = { results: [], order: 0 };
}
senderGroups[sender].results.push(result.event_id);
}
groups.sender = senderGroups;
}
}
if (Object.keys(groups).length > 0) {
response.search_categories.room_events.groups = groups;
}
}
return c.json(response);
});
// Helper function to extract highlight terms
function extractHighlights(searchTerm: string): string[] {
// Split search term into words and return unique terms
const words = searchTerm.toLowerCase().split(/\s+/).filter(w => w.length > 0);
return [...new Set(words)];
}
export default app;