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:
@@ -0,0 +1,107 @@
|
||||
"use server";
|
||||
|
||||
import { and, asc, eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { z } from "zod";
|
||||
import { db } from "@/lib/db";
|
||||
import { getCurrentSession } from "@/lib/session";
|
||||
import { comments, users } from "./schema";
|
||||
|
||||
export type CommentDto = {
|
||||
id: string;
|
||||
entityType: string;
|
||||
entityId: string;
|
||||
body: string;
|
||||
authorId: string;
|
||||
authorName: string | null;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
const commentInput = z.object({
|
||||
entityType: z.string().trim().min(1).max(80),
|
||||
entityId: z.string().uuid(),
|
||||
body: z.string().trim().min(1).max(2000),
|
||||
});
|
||||
|
||||
export async function listComments(entityType: string, entityId: string): Promise<CommentDto[]> {
|
||||
const { household } = await getCurrentSession();
|
||||
const parsedEntityId = z.string().uuid().parse(entityId);
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: comments.id,
|
||||
entityType: comments.entityType,
|
||||
entityId: comments.entityId,
|
||||
body: comments.body,
|
||||
authorId: comments.authorId,
|
||||
authorName: users.name,
|
||||
createdAt: comments.createdAt,
|
||||
})
|
||||
.from(comments)
|
||||
.innerJoin(users, eq(comments.authorId, users.id))
|
||||
.where(
|
||||
and(
|
||||
eq(comments.householdId, household.id),
|
||||
eq(comments.entityType, entityType),
|
||||
eq(comments.entityId, parsedEntityId),
|
||||
),
|
||||
)
|
||||
.orderBy(asc(comments.createdAt));
|
||||
|
||||
return rows.map((row) => ({
|
||||
id: row.id,
|
||||
entityType: row.entityType,
|
||||
entityId: row.entityId,
|
||||
body: row.body,
|
||||
authorId: row.authorId,
|
||||
authorName: row.authorName,
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
}));
|
||||
}
|
||||
|
||||
export async function addComment(input: z.input<typeof commentInput>): Promise<CommentDto> {
|
||||
const parsed = commentInput.parse(input);
|
||||
const { user, household } = await getCurrentSession();
|
||||
|
||||
const [row] = await db
|
||||
.insert(comments)
|
||||
.values({
|
||||
householdId: household.id,
|
||||
entityType: parsed.entityType,
|
||||
entityId: parsed.entityId,
|
||||
authorId: user.id,
|
||||
body: parsed.body,
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (!row) throw new Error("Comment was not created");
|
||||
|
||||
revalidatePath("/lists");
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
entityType: row.entityType,
|
||||
entityId: row.entityId,
|
||||
body: row.body,
|
||||
authorId: row.authorId,
|
||||
authorName: user.name,
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
export async function deleteComment(input: { id: string }) {
|
||||
const parsed = z.object({ id: z.string().uuid() }).parse(input);
|
||||
const { user, household } = await getCurrentSession();
|
||||
|
||||
const [existing] = await db
|
||||
.select({ id: comments.id, authorId: comments.authorId })
|
||||
.from(comments)
|
||||
.where(and(eq(comments.id, parsed.id), eq(comments.householdId, household.id)))
|
||||
.limit(1);
|
||||
|
||||
if (!existing) throw new Error("Comment not found");
|
||||
if (existing.authorId !== user.id) throw new Error("Forbidden");
|
||||
|
||||
await db.delete(comments).where(eq(comments.id, parsed.id));
|
||||
revalidatePath("/lists");
|
||||
}
|
||||
@@ -24,4 +24,13 @@ export { createShareLink, resolveShareToken, revokeShareLink } from "./share";
|
||||
export type { ShareLinkCapabilities, CreateShareLinkResult } from "./share";
|
||||
export { sendPush, sendPushToEndpoint } from "./push";
|
||||
export { notify } from "./notify";
|
||||
export { scheduleReminder, cancelReminder, listReminders, startReminderWorker } from "./reminders";
|
||||
export {
|
||||
scheduleReminder,
|
||||
cancelReminder,
|
||||
listReminders,
|
||||
listReminderOffsets,
|
||||
syncRemindersForEntity,
|
||||
syncCalendarEventReminders,
|
||||
startReminderWorker,
|
||||
} from "./reminders";
|
||||
export { listComments, addComment, deleteComment, type CommentDto } from "./comments";
|
||||
|
||||
+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) {
|
||||
|
||||
@@ -36,6 +36,10 @@ export const users = pgTable("users", {
|
||||
notifInApp: boolean("notif_inapp").notNull().default(true),
|
||||
notifNtfy: boolean("notif_ntfy").notNull().default(false),
|
||||
assistantEnabled: boolean("assistant_enabled").notNull().default(false),
|
||||
defaultEventReminderOffsets: jsonb("default_event_reminder_offsets")
|
||||
.notNull()
|
||||
.$type<number[]>()
|
||||
.default([30]),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||
});
|
||||
|
||||
@@ -170,12 +174,13 @@ export const reminders = pgTable(
|
||||
channel: text("channel").notNull().default("auto"),
|
||||
title: text("title"),
|
||||
body: text("body"),
|
||||
offsetMinutes: integer("offset_minutes"),
|
||||
firedAt: timestamp("fired_at", { withTimezone: true }),
|
||||
createdBy: uuid("created_by").references(() => users.id, { onDelete: "set null" }),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||
},
|
||||
(t) => [
|
||||
uniqueIndex("reminders_entity_unique").on(t.entityType, t.entityId),
|
||||
index("reminders_entity_idx").on(t.entityType, t.entityId),
|
||||
index("reminders_household_fire_at_idx").on(t.householdId, t.fireAt),
|
||||
],
|
||||
);
|
||||
@@ -212,6 +217,25 @@ export const notifications = pgTable(
|
||||
(t) => [index("notifications_user_read_idx").on(t.userId, t.readAt)],
|
||||
);
|
||||
|
||||
export const comments = pgTable(
|
||||
"comments",
|
||||
{
|
||||
id: uuid("id").primaryKey().defaultRandom(),
|
||||
householdId: uuid("household_id")
|
||||
.notNull()
|
||||
.references(() => households.id, { onDelete: "cascade" }),
|
||||
entityType: text("entity_type").notNull(),
|
||||
entityId: uuid("entity_id").notNull(),
|
||||
authorId: uuid("author_id")
|
||||
.notNull()
|
||||
.references(() => users.id, { onDelete: "cascade" }),
|
||||
body: text("body").notNull(),
|
||||
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||
},
|
||||
(t) => [index("comments_entity_idx").on(t.entityType, t.entityId, t.createdAt)],
|
||||
);
|
||||
|
||||
export const householdApiTokens = pgTable(
|
||||
"household_api_tokens",
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user