chore(server): improve gql types (#11441)

This commit is contained in:
darkskygit
2025-04-03 07:30:51 +00:00
parent 70a318f1c4
commit b4c643e8bc
5 changed files with 61 additions and 67 deletions
@@ -1,6 +1,6 @@
import { randomUUID } from 'node:crypto';
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { SessionCache } from '../../base';
import { SubmittedMessage, SubmittedMessageSchema } from './types';
@@ -10,26 +10,18 @@ const CHAT_MESSAGE_TTL = 3600 * 1 * 1000; // 1 hours
@Injectable()
export class ChatMessageCache {
private readonly logger = new Logger(ChatMessageCache.name);
constructor(private readonly cache: SessionCache) {}
async get(id: string): Promise<SubmittedMessage | undefined> {
return await this.cache.get(`${CHAT_MESSAGE_KEY}:${id}`);
}
async set(message: SubmittedMessage): Promise<string | undefined> {
try {
const parsed = SubmittedMessageSchema.safeParse(message);
if (parsed.success) {
const id = randomUUID();
await this.cache.set(`${CHAT_MESSAGE_KEY}:${id}`, parsed.data, {
ttl: CHAT_MESSAGE_TTL,
});
return id;
}
} catch (e: any) {
this.logger.error(`Failed to get chat message from cache: ${e.message}`);
}
return undefined;
async set(message: SubmittedMessage): Promise<string> {
const parsedMessage = SubmittedMessageSchema.parse(message);
const id = randomUUID();
await this.cache.set(`${CHAT_MESSAGE_KEY}:${id}`, parsedMessage, {
ttl: CHAT_MESSAGE_TTL,
});
return id;
}
}