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