peppa/apps/bot/src/feedback.ts
Асель fec0a698e7 PEP-143: feedback table, core, feedback agent tick, bot mode and buttons
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 11:23:09 +05:00

45 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-143: «Сообщить о проблеме» in the bot. `/problem` opens a short window in which the next
// message (text, a photo with a caption, a voice note) becomes a report; photos sent right after
// it join the same report. A superadmin's «Не исправлять» waits for the reason the same way.
import { deleteSetting, getSetting, setSetting } from "@peppa/core";
import type { Db } from "@peppa/db";
/** How long `/problem` waits for the message. */
export const FEEDBACK_MODE_TTL_MS = 15 * 60_000;
/** How long after the report more photos still join it (an album arrives message by message). */
export const FEEDBACK_MORE_TTL_MS = 2 * 60_000;
/** How long «Не исправлять» waits for the reason. */
export const REJECT_TTL_MS = 10 * 60_000;
export interface FeedbackMode {
/** Set once the report exists: further photos are attached to it. */
feedbackId?: number;
until: number;
}
const modeKey = (userId: number) => `bot.feedback:${userId}`;
const rejectKey = (userId: number) => `bot.feedback.reject:${userId}`;
export async function feedbackMode(db: Db, userId: number, now = Date.now()): Promise<FeedbackMode | null> {
const m = await getSetting<FeedbackMode>(db, modeKey(userId));
if (!m) return null;
if (m.until > now) return m;
await deleteSetting(db, modeKey(userId));
return null;
}
export const openFeedbackMode = (db: Db, userId: number, now = Date.now()) => setSetting(db, modeKey(userId), { until: now + FEEDBACK_MODE_TTL_MS } satisfies FeedbackMode);
export const keepFeedbackMode = (db: Db, userId: number, feedbackId: number, now = Date.now()) => setSetting(db, modeKey(userId), { feedbackId, until: now + FEEDBACK_MORE_TTL_MS } satisfies FeedbackMode);
export const closeFeedbackMode = (db: Db, userId: number) => deleteSetting(db, modeKey(userId));
/** The report a superadmin is about to decline: the next message is the reason. */
export async function pendingReject(db: Db, userId: number, now = Date.now()): Promise<number | null> {
const r = await getSetting<{ feedbackId: number; until: number }>(db, rejectKey(userId));
if (!r) return null;
if (r.until > now) return r.feedbackId;
await deleteSetting(db, rejectKey(userId));
return null;
}
export const askRejectReason = (db: Db, userId: number, feedbackId: number, now = Date.now()) => setSetting(db, rejectKey(userId), { feedbackId, until: now + REJECT_TTL_MS });
export const clearReject = (db: Db, userId: number) => deleteSetting(db, rejectKey(userId));