Files
Docwell/Database/pb_migrations/1762900000_threads_notifications.js
T
tajniak81andClaude Opus 5.5 2b78c167e2 Add comment threads, reactions, notifications and page watching
Comments are now threads: replies hang off a thread's first comment,
which alone points at text and can be resolved. Authors can edit their
own comments, and anyone who can comment can react with an emoji.
Resolved threads stop lighting text in the editor and fold away in the
panel. Comments can @mention people.

People watch the pages they create or comment on, and can watch or mute
a page from its menu. Muting is remembered. The inbox collects comments
on watched pages, replies in your threads, mentions (in comments and in
page text), pages shared with you and resolved threads. It shows an
unread badge in the sidebar, polled every minute, and links straight to
the thread. Nobody hears about their own actions or about pages they
cannot open, and a notification hides its text once access is lost.

Unread notifications are emailed as a digest every few minutes unless
the person turns that off. Read ones are pruned after 90 days. The jobs
registry can now run jobs on a timer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 22:27:55 +01:00

111 lines
4.8 KiB
JavaScript

// SPDX-License-Identifier: AGPL-3.0-or-later
/// <reference path="../pb_data/types.d.ts" />
// Comment threads, reactions, page watching and notifications.
//
// A reply is a comment with a parent (always the thread's first comment,
// which alone carries the anchor and can be resolved). reactions maps an
// emoji to the ids of the people who reacted with it.
//
// page_watchers.watching false means the person muted the page, so taking
// part in it later does not start watching it again.
//
// pages.mentions remembers who the page already mentions, so a save only
// notifies people who were newly mentioned.
//
// users.notify_email is "digest" (default when empty) or "off".
migrate(
(app) => {
const users = app.findCollectionByNameOrId("users");
const pages = app.findCollectionByNameOrId("pages");
const workspaces = app.findCollectionByNameOrId("workspaces");
const comments = app.findCollectionByNameOrId("comments");
comments.fields.add(new RelationField({ name: "parent", collectionId: comments.id, maxSelect: 1, cascadeDelete: true }));
comments.fields.add(new DateField({ name: "resolved_at" }));
comments.fields.add(new RelationField({ name: "resolved_by", collectionId: users.id, maxSelect: 1, cascadeDelete: false }));
comments.fields.add(new JSONField({ name: "reactions", maxSize: 20000 }));
comments.fields.add(new DateField({ name: "edited_at" }));
comments.indexes = [
"CREATE INDEX idx_comments_page ON comments (page, created)",
"CREATE INDEX idx_comments_parent ON comments (parent)",
];
app.save(comments);
pages.fields.add(new JSONField({ name: "mentions", maxSize: 20000 }));
app.save(pages);
users.fields.add(new SelectField({ name: "notify_email", values: ["digest", "off"], maxSelect: 1 }));
app.save(users);
app.save(
new Collection({
type: "base",
name: "page_watchers",
fields: [
{ name: "page", type: "relation", collectionId: pages.id, maxSelect: 1, required: true, cascadeDelete: true },
{ name: "user", type: "relation", collectionId: users.id, maxSelect: 1, required: true, cascadeDelete: true },
{ name: "watching", type: "bool" },
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
],
indexes: [
"CREATE UNIQUE INDEX idx_page_watchers_unique ON page_watchers (page, user)",
"CREATE INDEX idx_page_watchers_user ON page_watchers (user)",
],
}),
);
app.save(
new Collection({
type: "base",
name: "notifications",
fields: [
{ name: "user", type: "relation", collectionId: users.id, maxSelect: 1, required: true, cascadeDelete: true },
{ name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, cascadeDelete: true },
{ name: "page", type: "relation", collectionId: pages.id, maxSelect: 1, cascadeDelete: true },
{ name: "comment", type: "relation", collectionId: comments.id, maxSelect: 1, cascadeDelete: true },
{ name: "actor", type: "relation", collectionId: users.id, maxSelect: 1, cascadeDelete: false },
{
name: "type",
type: "select",
values: ["comment", "reply", "mention", "share", "resolved"],
maxSelect: 1,
required: true,
},
// What the notification says beyond its links, e.g. a comment excerpt.
{ name: "excerpt", type: "text", max: 300 },
{ name: "read_at", type: "date" },
{ name: "emailed_at", type: "date" },
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
],
indexes: [
"CREATE INDEX idx_notifications_user ON notifications (user, created)",
"CREATE INDEX idx_notifications_unread ON notifications (user, read_at)",
"CREATE INDEX idx_notifications_mail ON notifications (emailed_at, read_at)",
],
}),
);
},
(app) => {
for (const name of ["notifications", "page_watchers"]) {
try {
app.delete(app.findCollectionByNameOrId(name));
} catch (_) {}
}
const users = app.findCollectionByNameOrId("users");
users.fields.removeByName("notify_email");
app.save(users);
const pages = app.findCollectionByNameOrId("pages");
pages.fields.removeByName("mentions");
app.save(pages);
const comments = app.findCollectionByNameOrId("comments");
for (const f of ["edited_at", "reactions", "resolved_by", "resolved_at", "parent"]) comments.fields.removeByName(f);
comments.indexes = ["CREATE INDEX idx_comments_page ON comments (page, created)"];
app.save(comments);
},
);