Record sent chats in messages repo and blur gallery by default

Threads MessagesRepository through ChatService so first outgoing chat
messages are persisted for stats, and extends the repo + DB client to
support chat-room-keyed lookups. Adds tests covering the new behavior.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
jjbruntonandClaude Opus 4.7 committed 2026-05-22 18:48:14 +01:00
1 parent 2260388dc8
commit 05c95db6bd
10 files changed
+197 -6

No files matched your search

+43 -2
View File
@@ -15,7 +15,7 @@ export interface ProcessedPostRecord {
export interface SentMessageRecord {
id: number;
postId: string;
postId: string | null;
recipient: string;
subject: string;
message: string;
@@ -23,6 +23,7 @@ export interface SentMessageRecord {
gotResponse: boolean;
responseCheckedAt: number | null;
strategyId: string | null;
chatRoomId: string | null;
subreddit?: string;
postTitle?: string;
postImageUrl?: string;
@@ -240,7 +241,9 @@ export class PostsRepository {
export class MessagesRepository {
private insertStmt: Database.Statement;
private insertChatStmt: Database.Statement;
private getByPostIdStmt: Database.Statement;
private getByChatRoomIdStmt: Database.Statement;
private getLatestByRecipientStmt: Database.Statement;
private getRecentStmt: Database.Statement;
private getUncheckedStmt: Database.Statement;
@@ -254,6 +257,20 @@ export class MessagesRepository {
VALUES (?, ?, ?, ?, ?, ?)
`);
this.insertChatStmt = db.prepare(`
INSERT INTO sent_messages (post_id, recipient, subject, message, sent_at, chat_room_id)
VALUES (NULL, ?, ?, ?, ?, ?)
`);
this.getByChatRoomIdStmt = db.prepare(`
SELECT sm.*, pp.subreddit, pp.title AS post_title, pp.image_url AS post_image_url,
pp.image_analysis AS post_image_analysis
FROM sent_messages sm
LEFT JOIN processed_posts pp ON sm.post_id = pp.id
WHERE sm.chat_room_id = ?
LIMIT 1
`);
this.getByPostIdStmt = db.prepare(`SELECT * FROM sent_messages WHERE post_id = ?`);
this.getLatestByRecipientStmt = db.prepare(`
@@ -316,12 +333,35 @@ export class MessagesRepository {
return result.lastInsertRowid as number;
}
insertChat(message: {
recipient: string;
subject: string;
message: string;
sentAt: number;
chatRoomId: string;
}): number {
const result = this.insertChatStmt.run(
message.recipient,
message.subject,
message.message,
message.sentAt,
message.chatRoomId,
);
return result.lastInsertRowid as number;
}
getByPostId(postId: string): SentMessageRecord | undefined {
const row = this.getByPostIdStmt.get(postId) as Record<string, unknown> | undefined;
if (!row) return undefined;
return this.mapRow(row);
}
getByChatRoomId(chatRoomId: string): SentMessageRecord | undefined {
const row = this.getByChatRoomIdStmt.get(chatRoomId) as Record<string, unknown> | undefined;
if (!row) return undefined;
return this.mapRow(row);
}
getLatestByRecipient(recipient: string): SentMessageRecord | undefined {
const row = this.getLatestByRecipientStmt.get(recipient) as Record<string, unknown> | undefined;
if (!row) return undefined;
@@ -422,7 +462,7 @@ export class MessagesRepository {
private mapRow(row: Record<string, unknown>): SentMessageRecord {
return {
id: row.id as number,
postId: row.post_id as string,
postId: (row.post_id as string) ?? null,
recipient: row.recipient as string,
subject: row.subject as string,
message: row.message as string,
@@ -430,6 +470,7 @@ export class MessagesRepository {
gotResponse: Boolean(row.got_response),
responseCheckedAt: row.response_checked_at as number | null,
strategyId: (row.strategy_id as string) || null,
chatRoomId: (row.chat_room_id as string) ?? null,
subreddit: (row.subreddit as string) || undefined,
postTitle: (row.post_title as string) || undefined,
postImageUrl: (row.post_image_url as string) || undefined,