| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { ConfigService } |
| 4 | import { and, asc |
| 5 | import { alias } |
| 6 | |
| 7 | import { DATABASE } |
| 8 | import { mailConfigured } |
| 9 | import { MailService } |
| 10 | import { RepositoryAccessService } |
| 11 | import { |
| 12 | type CollaboratorRole, |
| 13 | ownerNameOf, |
| 14 | } from '../../lib/git/repository-access/repository-access.js'; |
| 15 | import { UsersService } |
| 16 | import { isoTimestamp } |
| 17 | import type { CollaboratorStatus |
| 18 | import { |
| 19 | CannotInviteOwnerError, |
| 20 | CollaboratorNotFoundError, |
| 21 | InvitationNotFoundError, |
| 22 | RepositoryNotInOrganizationError, |
| 23 | TeamNotFoundError, |
| 24 | } from './collaborators.errors.js'; |
| 25 | |
| 26 | const owner = alias |
| 27 | const inviter = alias |
| 28 | |
| 29 | // An invitation lapses a week after it was sent. The row stays, so an admin sees it lapsed and can send it again. |
| 30 | const expiresAt = sql |
| 31 | |
| 32 | interface RepositoryRef { |
| 33 | username: string; |
| 34 | repo: string; |
| 35 | requesterId: string; |
| 36 | } |
| 37 | |
| 38 | @Injectable() |
| 39 | export class CollaboratorsService |
| 40 | private readonly logger |
| 41 | private readonly mailEnabled |
| 42 | |
| 43 | constructor( |
| 44 | @Inject(DATABASE) |
| 45 | private readonly access |
| 46 | private readonly users |
| 47 | private readonly mail |
| 48 | config: ConfigService |
| 49 | ) { |
| 50 | this.mailEnabled = |
| 51 | } |
| 52 | |
| 53 | /** Accepted collaborators and pending invitations, oldest first. */ |
| 54 | async list(ref |
| 55 | const repository = |
| 56 | return { collaborators: |
| 57 | } |
| 58 | |
| 59 | /** Invites `collaborator`, or changes the role of someone already invited or collaborating. Only a new invitation sends an email. */ |
| 60 | async invite( |
| 61 | ref: RepositoryRef |
| 62 | ) { |
| 63 | const repository = |
| 64 | const user = |
| 65 | if (!repository.organizationId |
| 66 | throw new CannotInviteOwnerError() |
| 67 | } |
| 68 | |
| 69 | // A lapsed invitation is sent again as if new, restarting its week. |
| 70 | const [renewed] |
| 71 | .update(schema.repositoryCollaborator |
| 72 | .set({ |
| 73 | role: ref.role |
| 74 | invitedById: ref.requesterId |
| 75 | createdAt: new Date |
| 76 | }) |
| 77 | .where( |
| 78 | and( |
| 79 | this.membership(repository |
| 80 | isNull(schema.repositoryCollaborator |
| 81 | sql`${expiresAt} <= now() |
| 82 | ), |
| 83 | ) |
| 84 | .returning({ id: |
| 85 | |
| 86 | const [invited] |
| 87 | ? [renewed] |
| 88 | : await this. |
| 89 | .insert(schema.repositoryCollaborator |
| 90 | .values({ |
| 91 | repositoryId: repository.id |
| 92 | userId: user.id |
| 93 | role: ref.role |
| 94 | invitedById: ref.requesterId |
| 95 | }) |
| 96 | .onConflictDoNothing() |
| 97 | .returning({ id: |
| 98 | |
| 99 | if (invited) { |
| 100 | await this.notify |
| 101 | } else { |
| 102 | await this.db |
| 103 | .update(schema.repositoryCollaborator |
| 104 | .set({ role: |
| 105 | .where(this.membership |
| 106 | } |
| 107 | |
| 108 | const [collaborator] |
| 109 | return collaborator; |
| 110 | } |
| 111 | |
| 112 | /** Removes a collaborator or withdraws an invitation. A collaborator may also remove themselves, which is leaving. */ |
| 113 | async remove(ref |
| 114 | const user = |
| 115 | const repository = |
| 116 | username: ref.username |
| 117 | repo: ref.repo |
| 118 | actor: { userId: ref |
| 119 | operation: user.id |
| 120 | }); |
| 121 | |
| 122 | const removed = |
| 123 | .delete(schema.repositoryCollaborator |
| 124 | .where(this.membership |
| 125 | .returning({ id: |
| 126 | if (removed.length |
| 127 | throw new CollaboratorNotFoundError( |
| 128 | } |
| 129 | |
| 130 | /** Every team in the repository's organization, with the role each holds here, if any. */ |
| 131 | async listTeams(ref |
| 132 | const repository = |
| 133 | const teams = |
| 134 | .select({ |
| 135 | id: schema.team |
| 136 | name: schema.team |
| 137 | memberCount: schema.team |
| 138 | role: schema.repositoryTeam |
| 139 | }) |
| 140 | .from(schema.team |
| 141 | .leftJoin( |
| 142 | schema.repositoryTeam, |
| 143 | and( |
| 144 | eq(schema.repositoryTeam |
| 145 | eq(schema.repositoryTeam |
| 146 | ), |
| 147 | ) |
| 148 | .where(eq(schema |
| 149 | .orderBy(asc(schema |
| 150 | return { teams |
| 151 | } |
| 152 | |
| 153 | async setTeamRole( |
| 154 | ref: RepositoryRef |
| 155 | ) { |
| 156 | const repository = |
| 157 | const [team] |
| 158 | .select({ id: |
| 159 | .from(schema.team |
| 160 | .where( |
| 161 | and( |
| 162 | eq(schema.team |
| 163 | eq(schema.team |
| 164 | ), |
| 165 | ); |
| 166 | if (!team) |
| 167 | |
| 168 | await this.db |
| 169 | .insert(schema.repositoryTeam |
| 170 | .values({ repositoryId: |
| 171 | .onConflictDoUpdate({ |
| 172 | target: [ |
| 173 | schema.repositoryTeam.repositoryId |
| 174 | schema.repositoryTeam.teamId |
| 175 | ], |
| 176 | set: { role: ref |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | async removeTeam(ref |
| 181 | const repository = |
| 182 | const removed = |
| 183 | .delete(schema.repositoryTeam |
| 184 | .where( |
| 185 | and( |
| 186 | eq(schema.repositoryTeam |
| 187 | eq(schema.repositoryTeam |
| 188 | ), |
| 189 | ) |
| 190 | .returning({ teamId: |
| 191 | if (removed.length |
| 192 | } |
| 193 | |
| 194 | async listInvitations(userId |
| 195 | const invitations = |
| 196 | .select({ |
| 197 | id: schema.repositoryCollaborator |
| 198 | repository: { |
| 199 | username: sql<string |
| 200 | slug: schema.repository |
| 201 | name: schema.repository |
| 202 | }, |
| 203 | role: schema.repositoryCollaborator |
| 204 | invitedByUsername: inviter.username |
| 205 | invitedAt: isoTimestamp(schema |
| 206 | expiresAt: isoTimestamp(expiresAt |
| 207 | }) |
| 208 | .from(schema.repositoryCollaborator |
| 209 | .innerJoin( |
| 210 | schema.repository, |
| 211 | eq(schema.repository |
| 212 | ) |
| 213 | .innerJoin(owner, |
| 214 | .leftJoin( |
| 215 | schema.organization, |
| 216 | eq(schema.organization |
| 217 | ) |
| 218 | .leftJoin( |
| 219 | inviter, |
| 220 | eq(inviter.id |
| 221 | ) |
| 222 | .where(this.pendingFor |
| 223 | .orderBy(asc(schema |
| 224 | |
| 225 | return { invitations |
| 226 | } |
| 227 | |
| 228 | async acceptInvitation(id |
| 229 | const accepted = |
| 230 | .update(schema.repositoryCollaborator |
| 231 | .set({ acceptedAt: |
| 232 | .where( |
| 233 | and(eq(schema |
| 234 | ) |
| 235 | .returning({ id: |
| 236 | if (accepted.length |
| 237 | } |
| 238 | |
| 239 | async declineInvitation(id |
| 240 | const declined = |
| 241 | .delete(schema.repositoryCollaborator |
| 242 | .where( |
| 243 | and(eq(schema |
| 244 | ) |
| 245 | .returning({ id: |
| 246 | if (declined.length |
| 247 | } |
| 248 | |
| 249 | private rows(repositoryId |
| 250 | return this.db |
| 251 | .select({ |
| 252 | username: sql<string |
| 253 | name: schema.user |
| 254 | image: schema.user |
| 255 | role: schema.repositoryCollaborator |
| 256 | status: sql<CollaboratorStatus |
| 257 | invitedAt: isoTimestamp(schema |
| 258 | expiresAt: sql< |
| 259 | string | null |
| 260 | >`case when ${schema.repositoryCollaborator |
| 261 | }) |
| 262 | .from(schema.repositoryCollaborator |
| 263 | .innerJoin( |
| 264 | schema.user, |
| 265 | eq(schema.user |
| 266 | ) |
| 267 | .where( |
| 268 | userId |
| 269 | ? this.membership( |
| 270 | : eq(schema. |
| 271 | ) |
| 272 | .orderBy(asc(schema |
| 273 | } |
| 274 | |
| 275 | private membership(repositoryId |
| 276 | return and( |
| 277 | eq(schema.repositoryCollaborator |
| 278 | eq(schema.repositoryCollaborator |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | /** Invitations `userId` can still act on: neither accepted nor lapsed. */ |
| 283 | private pendingFor(userId |
| 284 | return and( |
| 285 | eq(schema.repositoryCollaborator |
| 286 | isNull(schema.repositoryCollaborator |
| 287 | sql`${expiresAt} > now() |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | private async authorizeTeams |
| 292 | const repository = |
| 293 | if (!repository.organizationId |
| 294 | throw new RepositoryNotInOrganizationError() |
| 295 | } |
| 296 | return { ...repository |
| 297 | } |
| 298 | |
| 299 | private authorizeAdmin({ |
| 300 | return this.access |
| 301 | username, |
| 302 | repo, |
| 303 | actor: { userId: requesterId |
| 304 | operation: 'admin', |
| 305 | }); |
| 306 | } |
| 307 | |
| 308 | /** The invitation stands whether or not the email goes out: the invitee also finds it on their dashboard. */ |
| 309 | private async notify |
| 310 | user: { |
| 311 | ref: RepositoryRef |
| 312 | ) { |
| 313 | if (!this.mailEnabled |
| 314 | |
| 315 | const sender = |
| 316 | await this.mail |
| 317 | .sendRepositoryInvitationEmail(user.email |
| 318 | name: user.name |
| 319 | inviter: sender.username |
| 320 | repository: `${ref. |
| 321 | role: ref.role |
| 322 | }) |
| 323 | .catch((error: |
| 324 | this.logger.warn |
| 325 | `Invitation email to ${user.email |
| 326 | ), |
| 327 | ); |
| 328 | } |
| 329 | } |