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