Files
RedditBot/src/web/event-reporting.ts
T

54 lines
1.8 KiB
TypeScript

interface DeliveryEvents {
messageSent(input: { id: string; username: string; conversationId?: string }): unknown;
messageFailed(input: { username: string; error: unknown; messageId?: string; conversationId?: string }): unknown;
}
interface ConversationEvents {
conversation(input: { id: string; state: 'replied'; unread?: number }): unknown;
reply(input: { conversationId: string; id: string; username: string; body?: string }): unknown;
}
interface RateLimitEvents {
rateLimit(input: { blocked: boolean; resetAt?: number; reason?: string }): unknown;
}
interface DeliveryResult {
success: boolean;
messageId: string;
username: string;
conversationId?: string;
error?: unknown;
}
export function reportDeliveryResult(events: DeliveryEvents, result: DeliveryResult): void {
if (result.success) {
events.messageSent({ id: result.messageId, username: result.username, conversationId: result.conversationId });
return;
}
events.messageFailed({
messageId: result.messageId,
username: result.username,
conversationId: result.conversationId,
error: result.error ?? 'Message delivery failed',
});
}
export function reportReply(
events: ConversationEvents,
reply: { conversationId: string; replyId: string; username: string; body?: string },
): void {
events.conversation({ id: reply.conversationId, state: 'replied', unread: 1 });
events.reply({ conversationId: reply.conversationId, id: reply.replyId, username: reply.username, body: reply.body });
}
export function reportInvitationCooldown(
events: RateLimitEvents,
cooldown: { until: number; reason: string } | null,
): void {
if (cooldown) {
events.rateLimit({ blocked: true, resetAt: cooldown.until, reason: cooldown.reason });
return;
}
events.rateLimit({ blocked: false });
}