| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { sql } |
| 3 | import { |
| 4 | boolean, |
| 5 | index, |
| 6 | integer, |
| 7 | jsonb, |
| 8 | pgEnum, |
| 9 | pgTable, |
| 10 | primaryKey, |
| 11 | text, |
| 12 | timestamp, |
| 13 | uniqueIndex, |
| 14 | } from "drizzle-orm/pg-core"; |
| 15 | import { user } |
| 16 | import { issue } |
| 17 | import { repository } |
| 18 | |
| 19 | /** |
| 20 | * Something that happened in a repository, written in the same transaction as the change itself and handed to every consumer once it commits. |
| 21 | * |
| 22 | * `type` is text rather than an enum so a new kind of event needs no migration; the API types what each one carries in `payload`. |
| 23 | */ |
| 24 | export const outboxEvent = |
| 25 | "outbox_event", |
| 26 | { |
| 27 | id: text() |
| 28 | .primaryKey() |
| 29 | .$defaultFn(() => |
| 30 | type: text().notNull |
| 31 | repositoryId: text() |
| 32 | .references(() => |
| 33 | .notNull(), |
| 34 | actorId: text().references |
| 35 | onDelete: "set null", |
| 36 | }), |
| 37 | payload: jsonb().$type |
| 38 | |
| 39 | attempts: integer().notNull |
| 40 | lastError: text(), |
| 41 | |
| 42 | createdAt: timestamp({ |
| 43 | processedAt: timestamp({ |
| 44 | }, |
| 45 | (t) => |
| 46 | index("outbox_event_pending_idx") |
| 47 | .on(t.createdAt |
| 48 | .where(sql`${ |
| 49 | ], |
| 50 | ); |
| 51 | |
| 52 | export const notificationReason = |
| 53 | "assigned", |
| 54 | "mentioned", |
| 55 | "team_mentioned", |
| 56 | "author", |
| 57 | "subscribed", |
| 58 | "watching", |
| 59 | ]); |
| 60 | |
| 61 | /** One row per user and thread; later activity on the thread bumps the same row back to unread. */ |
| 62 | export const notification = |
| 63 | "notification", |
| 64 | { |
| 65 | id: text() |
| 66 | .primaryKey() |
| 67 | .$defaultFn(() => |
| 68 | userId: text() |
| 69 | .references(() => |
| 70 | .notNull(), |
| 71 | issueId: text() |
| 72 | .references(() => |
| 73 | .notNull(), |
| 74 | reason: notificationReason().notNull |
| 75 | // the latest event on the thread, and who caused it |
| 76 | eventType: text().notNull |
| 77 | actorId: text().references |
| 78 | onDelete: "set null", |
| 79 | }), |
| 80 | unread: boolean().notNull |
| 81 | |
| 82 | // milliseconds, the precision of the inbox cursor that pages on it |
| 83 | updatedAt: timestamp({ |
| 84 | .notNull() |
| 85 | .defaultNow(), |
| 86 | }, |
| 87 | (t) => |
| 88 | uniqueIndex("notification_user_issue_idx"). |
| 89 | index("notification_user_updated_idx"). |
| 90 | ], |
| 91 | ); |
| 92 | |
| 93 | /** `subscribed = false` is an explicit unsubscribe, which outlasts the automatic subscription that commenting would otherwise add back. */ |
| 94 | export const issueSubscription = |
| 95 | "issue_subscription", |
| 96 | { |
| 97 | userId: text() |
| 98 | .references(() => |
| 99 | .notNull(), |
| 100 | issueId: text() |
| 101 | .references(() => |
| 102 | .notNull(), |
| 103 | subscribed: boolean().notNull |
| 104 | |
| 105 | createdAt: timestamp({ |
| 106 | }, |
| 107 | (t) => |
| 108 | primaryKey({ columns: |
| 109 | index("issue_subscription_issue_idx"). |
| 110 | ], |
| 111 | ); |
| 112 | |
| 113 | // No row means participating: notified only about threads the user takes part in. |
| 114 | export const repositoryWatchLevel = |
| 115 | "all", |
| 116 | "ignore", |
| 117 | ]); |
| 118 | |
| 119 | export const repositoryWatch = |
| 120 | "repository_watch", |
| 121 | { |
| 122 | userId: text() |
| 123 | .references(() => |
| 124 | .notNull(), |
| 125 | repositoryId: text() |
| 126 | .references(() => |
| 127 | .notNull(), |
| 128 | level: repositoryWatchLevel().notNull |
| 129 | |
| 130 | createdAt: timestamp({ |
| 131 | }, |
| 132 | (t) => |
| 133 | primaryKey({ columns: |
| 134 | index("repository_watch_repository_idx"). |
| 135 | ], |
| 136 | ); |