this repo has no description
1import { threadTimestamps } from "./db";
2
3const THREAD_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes
4
5/**
6 * Generate a short 5-character thread ID from thread_ts
7 */
8export function generateThreadId(threadTs: string): string {
9 let hash = 0;
10 for (let i = 0; i < threadTs.length; i++) {
11 hash = (hash << 5) - hash + threadTs.charCodeAt(i);
12 hash = hash & hash;
13 }
14 // Convert to base36 and take first 5 characters
15 return Math.abs(hash).toString(36).substring(0, 5);
16}
17
18/**
19 * Check if this is the first message in a thread (thread doesn't exist in DB yet)
20 */
21export function isFirstThreadMessage(threadTs: string): boolean {
22 const thread = threadTimestamps.get(threadTs);
23 return !thread;
24}
25
26/**
27 * Get thread info by thread ID
28 */
29export function getThreadByThreadId(threadId: string) {
30 return threadTimestamps.getByThreadId(threadId);
31}
32
33/**
34 * Update the last message time for a thread
35 */
36export function updateThreadTimestamp(
37 threadTs: string,
38 slackChannelId: string,
39): string {
40 const threadId = generateThreadId(threadTs);
41 threadTimestamps.update(threadTs, threadId, slackChannelId, Date.now());
42 return threadId;
43}
44
45/**
46 * Clean up old thread entries (optional, for memory management)
47 */
48export function cleanupOldThreads(): void {
49 const cutoff = Date.now() - THREAD_TIMEOUT_MS * 2;
50 threadTimestamps.cleanup(cutoff);
51}