| 1 | package calendar |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // Models is every model the schema generator manages. Keep it in step |
| 6 | // with the structs below and in models_event.go: `rastrillo migration |
| 7 | // generate` reads it to work out what the database should look like, |
| 8 | // and `rastrillo migration check` fails CI when it and the migrations |
| 9 | // disagree. |
| 10 | var Models = []any{ |
| 11 | &Person{}, |
| 12 | &Calendar{}, |
| 13 | &Grant{}, |
| 14 | &Event{}, |
| 15 | &Attendee{}, |
| 16 | &Reminder{}, |
| 17 | &ReminderQueue{}, |
| 18 | &ReminderNotice{}, |
| 19 | &Feed{}, |
| 20 | &Meta{}, |
| 21 | &BookingPage{}, |
| 22 | &BookingWindow{}, |
| 23 | &Booking{}, |
| 24 | &BookingMail{}, |
| 25 | } |
| 26 | |
| 27 | // Person is an address that has signed in here, and the row everything |
| 28 | // owned is scoped to. |
| 29 | // |
| 30 | // The indirection is not ceremony. Sign-in is a magic link, so the |
| 31 | // session Subject is a verified email address, not a numeric id — which |
| 32 | // is why sessions.UserID is unusable under this plugin and returns |
| 33 | // (0, false). Owning rows by an address column instead would scatter a |
| 34 | // piece of personal data across every table and make a change of |
| 35 | // address a migration; owning them by this row's id keeps the address |
| 36 | // in one place. |
| 37 | // |
| 38 | // Named Person rather than the brainstorm's Member, matching docs. |
| 39 | // Sheets calls the same row Member. Nothing turns on it, and the table |
| 40 | // shipped before the difference was noticed, so it stays: a rename is |
| 41 | // indistinguishable from a drop plus an add, and this one buys nothing. |
| 42 | type Person struct { |
| 43 | ID int64 `gorm:"primaryKey"` |
| 44 | |
| 45 | // Address is the verified sign-in address, normalised (trimmed, |
| 46 | // lowercased) by normalizeAddress before it is ever written or |
| 47 | // compared, so two spellings of one person cannot become two rows. |
| 48 | // |
| 49 | // Plaintext, deliberately. The team already knows it, a reminder |
| 50 | // email needs it to be deliverable, and the leak list says so. |
| 51 | Address string `gorm:"column:address;uniqueIndex;not null"` |
| 52 | |
| 53 | // NameSealed is a display name, if they set one. Sealed: it is |
| 54 | // content, and sheets seals the equivalent. |
| 55 | NameSealed []byte `gorm:"column:name_sealed"` |
| 56 | |
| 57 | // Zone is the IANA zone this person reads their calendar in |
| 58 | // ("Europe/Dublin"). Stated, never sniffed — see the settings page. |
| 59 | // A browser's zone is a guess about a device, not a statement about |
| 60 | // a person, and a calendar that silently re-times everything |
| 61 | // because somebody opened it on holiday is a calendar that lies. |
| 62 | Zone string `gorm:"column:zone;not null"` |
| 63 | |
| 64 | // Handle is the plaintext name in this person's public booking |
| 65 | // URLs, /book/{handle}/{code}: [a-z0-9-]{2,32}, lowercased on save, |
| 66 | // NULL until they choose one. Plaintext because it is in a URL the |
| 67 | // person hands to strangers, and unique because two people cannot |
| 68 | // share a URL prefix. Nullable so the unique index admits any |
| 69 | // number of people who never set one. |
| 70 | Handle *string `gorm:"column:handle;uniqueIndex"` |
| 71 | |
| 72 | // WeekStart is 1 for Monday (the default), 7 for Sunday, 6 for |
| 73 | // Saturday — time.Weekday's numbering with Sunday as 7 rather than |
| 74 | // 0, so "unset" is distinguishable from "Sunday". |
| 75 | WeekStart int `gorm:"column:week_start;not null"` |
| 76 | |
| 77 | // The defaults applied to events this person creates. Separate for |
| 78 | // timed and all-day because "10 minutes before" is useless for a |
| 79 | // birthday and "the evening before" is useless for a standup. |
| 80 | DefaultReminderMin int `gorm:"column:default_reminder_min;not null"` |
| 81 | DefaultAllDayMin int `gorm:"column:default_all_day_min;not null"` |
| 82 | |
| 83 | // ReminderChannel is the channel this person's NEW reminder rules |
| 84 | // take: nil or "email" (everybody before in-app reminders), "inapp", |
| 85 | // or "both" (spec §7 "In-app reminders"). A Home-only member's is |
| 86 | // always in-app whatever this says (reminderChannelFor). |
| 87 | ReminderChannel *string `gorm:"column:reminder_channel"` |
| 88 | |
| 89 | CreatedAt time.Time `gorm:"column:created_at;not null"` |
| 90 | UpdatedAt time.Time `gorm:"column:updated_at;not null"` |
| 91 | |
| 92 | // homeOnly is, in team mode, the Home subject of a person who has no |
| 93 | // address here (a Home-only member: team_access.go), and "" for |
| 94 | // everybody else. Never a column: unexported, so gorm and the |
| 95 | // migration check never see it. |
| 96 | homeOnly string |
| 97 | } |
| 98 | |
| 99 | // Calendar is a container of events: one team calendar created with the |
| 100 | // instance, plus a personal calendar per person. |
| 101 | type Calendar struct { |
| 102 | ID int64 `gorm:"primaryKey"` |
| 103 | |
| 104 | // NameSealed is content, like a sheet's title. |
| 105 | NameSealed []byte `gorm:"column:name_sealed"` |
| 106 | |
| 107 | // Kind is "team" or "personal". The team calendar is the one every |
| 108 | // person is granted editor on at creation; a personal one is |
| 109 | // private until shared. |
| 110 | Kind string `gorm:"column:kind;not null"` |
| 111 | |
| 112 | OwnerID int64 `gorm:"column:owner_id;not null;index"` |
| 113 | |
| 114 | // Hue is where this calendar's colour sits on the wheel, 0-360. An |
| 115 | // intent rather than a hex value: the theme resolves it to a pair |
| 116 | // that clears the contrast floor in both light and dark, which a |
| 117 | // stored hex cannot do. |
| 118 | // |
| 119 | // Colour never carries meaning alone here — every chip prints its |
| 120 | // own title and time, and the calendar's name is on the page. |
| 121 | Hue int `gorm:"column:hue;not null"` |
| 122 | |
| 123 | CreatedAt time.Time `gorm:"column:created_at;not null"` |
| 124 | ArchivedAt *time.Time `gorm:"column:archived_at;index"` |
| 125 | } |
| 126 | |
| 127 | // Grant is the calendar-to-person join, and the one place authorisation |
| 128 | // is decided. |
| 129 | // |
| 130 | // Explicit rows even for the team calendar, where every person gets one |
| 131 | // at creation. One query shape then serves every calendar, and |
| 132 | // "a row that isn't yours 404s" has exactly one home rather than a |
| 133 | // special case that someone will later forget. |
| 134 | type Grant struct { |
| 135 | ID int64 `gorm:"primaryKey"` |
| 136 | CalendarID int64 `gorm:"column:calendar_id;not null;uniqueIndex:idx_grant_cal_person"` |
| 137 | PersonID int64 `gorm:"column:person_id;not null;uniqueIndex:idx_grant_cal_person"` |
| 138 | |
| 139 | // Role is "owner", "editor" or "viewer". |
| 140 | Role string `gorm:"column:role;not null"` |
| 141 | |
| 142 | GrantedBy int64 `gorm:"column:granted_by;not null"` |
| 143 | CreatedAt time.Time `gorm:"column:created_at;not null"` |
| 144 | } |
| 145 | |
| 146 | // The roles, as constants, because a role compared against a literal is |
| 147 | // a role that will eventually be compared against a typo. |
| 148 | const ( |
| 149 | RoleOwner = "owner" |
| 150 | RoleEditor = "editor" |
| 151 | RoleViewer = "viewer" |
| 152 | ) |
| 153 | |
| 154 | // CanWrite reports whether a role may change what it names. |
| 155 | func CanWrite(role string) bool { |
| 156 | return role == RoleOwner || role == RoleEditor |
| 157 | } |
| 158 | |
| 159 | // Week start, using time.Weekday's numbering except that Sunday is 7 |
| 160 | // rather than 0 — so a zero column means "never set" and is |
| 161 | // distinguishable from a deliberate Sunday. |
| 162 | const ( |
| 163 | WeekStartMonday = 1 |
| 164 | WeekStartSaturday = 6 |
| 165 | WeekStartSunday = 7 |
| 166 | ) |
| 167 | |
| 168 | // The reminder defaults a new person gets. |
| 169 | // |
| 170 | // Ten minutes before a timed event is enough to walk to a room and not |
| 171 | // so much that it arrives while you are still in the previous thing. |
| 172 | // An all-day event is reminded the evening before at 18:00 — fifteen |
| 173 | // hours ahead of its midnight — because a birthday you learn about at |
| 174 | // midnight is a birthday you have already half-missed. |
| 175 | const ( |
| 176 | DefaultReminderMin = 10 |
| 177 | DefaultAllDayMin = 6 * 60 |
| 178 | ) |
| 179 | |
| 180 | // Meta is the instance's own key/value scratch: the tzdata stamp the |
| 181 | // re-derive sweep compares against, and anything else that is a fact |
| 182 | // about this database rather than about anybody's calendar. |
| 183 | type Meta struct { |
| 184 | Key string `gorm:"primaryKey"` |
| 185 | Value string `gorm:"column:value;not null"` |
| 186 | } |
| 187 | |
| 188 | // The Meta keys, spelled once. |
| 189 | const ( |
| 190 | MetaTzdata = "tzdata_version" |
| 191 | ) |
| 192 | |