// SPDX-License-Identifier: AGPL-3.0-or-later /// // 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); }, );