feat: p2 batch 28-31 reminders lists comments bang stats
Calendar events support multiple reminder offsets with presets and per-user defaults. Lists index adds inline task entry and list property editing. Generic entity comments on list detail. New bangs.stats dashboard widget. Closes Gitea #28, #29, #30, #31. Migrations 0022 and 0023.
This commit is contained in:
+115
-17
@@ -1,11 +1,19 @@
|
||||
import { and, eq, inArray, isNull, lte, sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
import logger from "@/lib/logger";
|
||||
import { fireAtForEventStart } from "@/lib/reminder-offsets";
|
||||
import { reminders } from "./schema";
|
||||
import { notify } from "./notify";
|
||||
|
||||
const REMINDER_LOCK_KEY = 7_777_777;
|
||||
|
||||
type ReminderInput = {
|
||||
fireAt: Date;
|
||||
offsetMinutes?: number | null;
|
||||
title?: string;
|
||||
body?: string;
|
||||
};
|
||||
|
||||
export async function scheduleReminder(input: {
|
||||
householdId: string;
|
||||
entityType: string;
|
||||
@@ -15,30 +23,92 @@ export async function scheduleReminder(input: {
|
||||
channel?: string;
|
||||
title?: string;
|
||||
body?: string;
|
||||
offsetMinutes?: number | null;
|
||||
}) {
|
||||
await db
|
||||
.insert(reminders)
|
||||
.values({
|
||||
.delete(reminders)
|
||||
.where(
|
||||
and(
|
||||
eq(reminders.entityType, input.entityType),
|
||||
eq(reminders.entityId, input.entityId),
|
||||
isNull(reminders.firedAt),
|
||||
),
|
||||
);
|
||||
|
||||
await db.insert(reminders).values({
|
||||
householdId: input.householdId,
|
||||
entityType: input.entityType,
|
||||
entityId: input.entityId,
|
||||
fireAt: input.fireAt,
|
||||
offsetMinutes: input.offsetMinutes ?? null,
|
||||
channel: input.channel ?? "auto",
|
||||
title: input.title ?? null,
|
||||
body: input.body ?? null,
|
||||
createdBy: input.createdBy,
|
||||
firedAt: null,
|
||||
});
|
||||
}
|
||||
|
||||
export async function syncRemindersForEntity(input: {
|
||||
householdId: string;
|
||||
entityType: string;
|
||||
entityId: string;
|
||||
createdBy: string;
|
||||
channel?: string;
|
||||
reminders: ReminderInput[];
|
||||
}) {
|
||||
await db
|
||||
.delete(reminders)
|
||||
.where(
|
||||
and(
|
||||
eq(reminders.entityType, input.entityType),
|
||||
eq(reminders.entityId, input.entityId),
|
||||
isNull(reminders.firedAt),
|
||||
),
|
||||
);
|
||||
|
||||
const now = new Date();
|
||||
const rows = input.reminders.filter((r) => r.fireAt > now);
|
||||
if (rows.length === 0) return;
|
||||
|
||||
await db.insert(reminders).values(
|
||||
rows.map((row) => ({
|
||||
householdId: input.householdId,
|
||||
entityType: input.entityType,
|
||||
entityId: input.entityId,
|
||||
fireAt: input.fireAt,
|
||||
fireAt: row.fireAt,
|
||||
offsetMinutes: row.offsetMinutes ?? null,
|
||||
channel: input.channel ?? "auto",
|
||||
title: input.title ?? null,
|
||||
body: input.body ?? null,
|
||||
title: row.title ?? null,
|
||||
body: row.body ?? null,
|
||||
createdBy: input.createdBy,
|
||||
firedAt: null,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [reminders.entityType, reminders.entityId],
|
||||
set: {
|
||||
fireAt: input.fireAt,
|
||||
title: input.title ?? null,
|
||||
body: input.body ?? null,
|
||||
firedAt: null,
|
||||
createdBy: input.createdBy,
|
||||
},
|
||||
});
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
export async function syncCalendarEventReminders(input: {
|
||||
householdId: string;
|
||||
eventId: string;
|
||||
eventTitle: string;
|
||||
startAt: Date;
|
||||
createdBy: string;
|
||||
offsetMinutes: number[];
|
||||
}) {
|
||||
const remindersToSchedule = input.offsetMinutes.map((offset) => ({
|
||||
fireAt: fireAtForEventStart(input.startAt, offset),
|
||||
offsetMinutes: offset,
|
||||
title: input.eventTitle,
|
||||
body: formatReminderOffsetBody(offset, input.eventTitle),
|
||||
}));
|
||||
|
||||
await syncRemindersForEntity({
|
||||
householdId: input.householdId,
|
||||
entityType: "calendar.event",
|
||||
entityId: input.eventId,
|
||||
createdBy: input.createdBy,
|
||||
reminders: remindersToSchedule,
|
||||
});
|
||||
}
|
||||
|
||||
export async function cancelReminder(entityType: string, entityId: string) {
|
||||
@@ -54,6 +124,29 @@ export async function listReminders(entityType: string, entityId: string) {
|
||||
.where(and(eq(reminders.entityType, entityType), eq(reminders.entityId, entityId)));
|
||||
}
|
||||
|
||||
export async function listReminderOffsets(entityType: string, entityId: string): Promise<number[]> {
|
||||
const rows = await listReminders(entityType, entityId);
|
||||
return rows
|
||||
.filter((row) => row.offsetMinutes != null && row.firedAt == null)
|
||||
.map((row) => row.offsetMinutes!)
|
||||
.toSorted((a, b) => b - a);
|
||||
}
|
||||
|
||||
function formatReminderOffsetBody(offsetMinutes: number, title: string): string {
|
||||
if (offsetMinutes === 0) return `${title} is starting now`;
|
||||
if (offsetMinutes % 1440 === 0) {
|
||||
const days = offsetMinutes / 1440;
|
||||
return days === 1 ? `${title} starts in 1 day` : `${title} starts in ${days} days`;
|
||||
}
|
||||
if (offsetMinutes % 60 === 0) {
|
||||
const hours = offsetMinutes / 60;
|
||||
return hours === 1 ? `${title} starts in 1 hour` : `${title} starts in ${hours} hours`;
|
||||
}
|
||||
return offsetMinutes === 1
|
||||
? `${title} starts in 1 minute`
|
||||
: `${title} starts in ${offsetMinutes} minutes`;
|
||||
}
|
||||
|
||||
async function tickReminders() {
|
||||
let dueReminders: (typeof reminders.$inferSelect)[] = [];
|
||||
|
||||
@@ -94,7 +187,12 @@ async function tickReminders() {
|
||||
await notify(reminder.createdBy, {
|
||||
title: reminder.title ?? "Reminder",
|
||||
body: reminder.body ?? "You have a reminder",
|
||||
url: reminder.entityType === "notes.note" ? `/notes/${reminder.entityId}` : "/",
|
||||
url:
|
||||
reminder.entityType === "notes.note"
|
||||
? `/notes/${reminder.entityId}`
|
||||
: reminder.entityType === "calendar.event"
|
||||
? "/calendar"
|
||||
: "/",
|
||||
channels: ["push", "inapp"],
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user