| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { and, count |
| 4 | import { alias } |
| 5 | |
| 6 | import { DATABASE } |
| 7 | import { publishEvent } |
| 8 | import { |
| 9 | acceptedCollaboration, |
| 10 | organizationMembership, |
| 11 | ownerNameOf, |
| 12 | readableBy, |
| 13 | } from '../../lib/git/repository-access/repository-access.js'; |
| 14 | import { RepositoryAccessService } |
| 15 | import { decodeCursor, encodeCursor |
| 16 | import { IssuesService } |
| 17 | import { InvalidCursorError } |
| 18 | import type { |
| 19 | GetNotificationsQueryDTO, |
| 20 | GetNotificationsResponseDTO, |
| 21 | WatchLevel, |
| 22 | } from './dto/notification.dto.js'; |
| 23 | import { NotificationNotFoundError } |
| 24 | |
| 25 | const DEFAULT_PAGE_SIZE = 25 |
| 26 | |
| 27 | const actor = alias |
| 28 | |
| 29 | type IssueRef |
| 30 | username: string |
| 31 | repo: string |
| 32 | number: number |
| 33 | requesterId: string |
| 34 | }; |
| 35 | type RepositoryRef |
| 36 | |
| 37 | /** The inbox, and what decides what lands in it: thread subscriptions and repository watching. */ |
| 38 | @Injectable() |
| 39 | export class NotificationsService |
| 40 | constructor( |
| 41 | @Inject(DATABASE) |
| 42 | private readonly access |
| 43 | private readonly issues |
| 44 | ) {} |
| 45 | |
| 46 | async list( |
| 47 | userId: string, |
| 48 | query: GetNotificationsQueryDTO |
| 49 | ): Promise<GetNotificationsResponseDTO |
| 50 | const decoded = |
| 51 | if (query.cursor |
| 52 | const pageSize = |
| 53 | |
| 54 | const rows = |
| 55 | userId, |
| 56 | and( |
| 57 | query.unread ? |
| 58 | decoded |
| 59 | ? or( |
| 60 | lt(schema.notification |
| 61 | and( |
| 62 | eq(schema.notification |
| 63 | lt(schema.notification |
| 64 | ), |
| 65 | ) |
| 66 | : undefined, |
| 67 | ), |
| 68 | ) |
| 69 | .orderBy( |
| 70 | desc(schema.notification |
| 71 | desc(schema.notification |
| 72 | ) |
| 73 | .limit(pageSize + |
| 74 | |
| 75 | const page = |
| 76 | const last = |
| 77 | return { |
| 78 | notifications: page, |
| 79 | nextCursor: |
| 80 | rows.length > pageSize |
| 81 | ? encodeCursor({ date |
| 82 | : null, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | async unreadCount(userId |
| 87 | const [row] |
| 88 | .select({ count: |
| 89 | .from( |
| 90 | this.inbox(userId |
| 91 | ); |
| 92 | return { count: |
| 93 | } |
| 94 | |
| 95 | async setUnread(userId |
| 96 | const updated = |
| 97 | .update(schema.notification |
| 98 | .set({ unread |
| 99 | .where( |
| 100 | and( |
| 101 | eq(schema.notification |
| 102 | eq(schema.notification |
| 103 | ), |
| 104 | ) |
| 105 | .returning({ id: |
| 106 | if (updated.length |
| 107 | } |
| 108 | |
| 109 | async markAllRead(userId |
| 110 | await this.db |
| 111 | .update(schema.notification |
| 112 | .set({ unread: |
| 113 | .where( |
| 114 | and( |
| 115 | eq(schema.notification |
| 116 | eq(schema.notification |
| 117 | ), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | async getSubscription(target |
| 122 | const { issue |
| 123 | const [row] |
| 124 | .select({ subscribed: |
| 125 | .from(schema.issueSubscription |
| 126 | .where( |
| 127 | and( |
| 128 | eq(schema.issueSubscription |
| 129 | eq(schema.issueSubscription |
| 130 | ), |
| 131 | ); |
| 132 | return { subscribed: |
| 133 | } |
| 134 | |
| 135 | async setSubscription(target |
| 136 | const { issue |
| 137 | await this.db |
| 138 | .insert(schema.issueSubscription |
| 139 | .values({ |
| 140 | userId: target.requesterId |
| 141 | issueId: issue.id |
| 142 | subscribed: target.subscribed |
| 143 | }) |
| 144 | .onConflictDoUpdate({ |
| 145 | target: [ |
| 146 | schema.issueSubscription.userId |
| 147 | schema.issueSubscription.issueId |
| 148 | ], |
| 149 | set: { subscribed: target |
| 150 | }); |
| 151 | return { subscribed: |
| 152 | } |
| 153 | |
| 154 | async getWatch(target |
| 155 | const repository = |
| 156 | const [row] |
| 157 | .select({ level: |
| 158 | .from(schema.repositoryWatch |
| 159 | .where(watchOf(repository |
| 160 | return { level: |
| 161 | } |
| 162 | |
| 163 | async setWatch(target |
| 164 | const repository = |
| 165 | if (target.level |
| 166 | await this.db |
| 167 | .delete(schema.repositoryWatch |
| 168 | .where(watchOf(repository |
| 169 | } else { |
| 170 | const level = |
| 171 | await this.db |
| 172 | // Returns a row only when the level changed, so re-saving `all` starts no second watch. |
| 173 | const changed = |
| 174 | .insert(schema.repositoryWatch |
| 175 | .values({ |
| 176 | userId: target.requesterId |
| 177 | repositoryId: repository.id |
| 178 | level, |
| 179 | }) |
| 180 | .onConflictDoUpdate({ |
| 181 | target: [ |
| 182 | schema.repositoryWatch.userId |
| 183 | schema.repositoryWatch.repositoryId |
| 184 | ], |
| 185 | set: { level |
| 186 | setWhere: ne(schema |
| 187 | }) |
| 188 | .returning({ level: |
| 189 | if (changed.length |
| 190 | await publishEvent(tx |
| 191 | type: 'watch.started', |
| 192 | repositoryId: repository.id |
| 193 | actorId: target.requesterId |
| 194 | payload: {}, |
| 195 | }); |
| 196 | }); |
| 197 | } |
| 198 | return { level: |
| 199 | } |
| 200 | |
| 201 | /** The user's notifications on threads they can still read: losing access to a repository hides what it sent them. */ |
| 202 | private inbox(userId |
| 203 | const viewer = |
| 204 | return this.db |
| 205 | .select({ |
| 206 | id: schema.notification |
| 207 | reason: schema.notification |
| 208 | eventType: schema.notification |
| 209 | actorUsername: actor.username |
| 210 | unread: schema.notification |
| 211 | updatedAt: isoTimestamp(schema |
| 212 | repository: { |
| 213 | owner: ownerNameOf(schema |
| 214 | slug: schema.repository |
| 215 | }, |
| 216 | thread: { |
| 217 | number: schema.issue |
| 218 | title: schema.issue |
| 219 | isPullRequest: schema.issue |
| 220 | state: sql< |
| 221 | 'open' | 'closed' |
| 222 | >`case when ${schema.pullRequest |
| 223 | }, |
| 224 | }) |
| 225 | .from(schema.notification |
| 226 | .innerJoin(schema.issue |
| 227 | .innerJoin( |
| 228 | schema.repository, |
| 229 | eq(schema.repository |
| 230 | ) |
| 231 | .innerJoin(schema.user |
| 232 | .leftJoin( |
| 233 | schema.organization, |
| 234 | eq(schema.organization |
| 235 | ) |
| 236 | .leftJoin( |
| 237 | schema.repositoryCollaborator, |
| 238 | acceptedCollaboration(schema.repository |
| 239 | ) |
| 240 | .leftJoin( |
| 241 | schema.member, |
| 242 | organizationMembership(schema.repository |
| 243 | ) |
| 244 | .leftJoin(actor, |
| 245 | .leftJoin( |
| 246 | schema.pullRequest, |
| 247 | eq(schema.pullRequest |
| 248 | ) |
| 249 | .where( |
| 250 | and(eq(schema |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | private authorize({ |
| 255 | return this.access |
| 256 | username, |
| 257 | repo, |
| 258 | actor: { userId: requesterId |
| 259 | operation: 'read', |
| 260 | }); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | function watchOf(repositoryId: |
| 265 | return and( |
| 266 | eq(schema.repositoryWatch |
| 267 | eq(schema.repositoryWatch |
| 268 | ); |
| 269 | } |