| 1 | import { |
| 2 | Body, |
| 3 | Controller, |
| 4 | Delete, |
| 5 | Get, |
| 6 | HttpCode, |
| 7 | HttpStatus, |
| 8 | Param, |
| 9 | Post, |
| 10 | Query, |
| 11 | Redirect, |
| 12 | Session, |
| 13 | } from '@nestjs/common'; |
| 14 | import { |
| 15 | ApiBadRequestResponse, |
| 16 | ApiConflictResponse, |
| 17 | ApiCreatedResponse, |
| 18 | ApiNoContentResponse, |
| 19 | ApiNotFoundResponse, |
| 20 | ApiOkResponse, |
| 21 | ApiOperation, |
| 22 | ApiTags, |
| 23 | } from '@nestjs/swagger'; |
| 24 | import { OptionalAuth, type |
| 25 | |
| 26 | import { ErrorResponseDTO } |
| 27 | import { |
| 28 | AddEmailDTO, |
| 29 | ListEmailsResponseDTO, |
| 30 | UserEmailDTO, |
| 31 | } from './dto/email.dto.js'; |
| 32 | import { EmailsService } |
| 33 | |
| 34 | @ApiTags('Emails') |
| 35 | @Controller('emails') |
| 36 | export class EmailsController |
| 37 | constructor(private readonly |
| 38 | |
| 39 | @Get() |
| 40 | @ApiOperation({ summary |
| 41 | @ApiOkResponse({ type |
| 42 | list(@Session() |
| 43 | return this.emails |
| 44 | } |
| 45 | |
| 46 | @Post() |
| 47 | @ApiOperation({ |
| 48 | summary: 'Add an address to the signed-in account', |
| 49 | description: |
| 50 | 'Mails a verification link. The address counts for nothing - sign-in, commit attribution - until that link is followed.', |
| 51 | }) |
| 52 | @ApiCreatedResponse({ type |
| 53 | @ApiConflictResponse({ type |
| 54 | add(@Session() |
| 55 | return this.emails |
| 56 | } |
| 57 | |
| 58 | @Get('verify' |
| 59 | @OptionalAuth() |
| 60 | @Redirect() |
| 61 | @ApiOperation({ |
| 62 | summary: 'Follow an address verification link', |
| 63 | description: 'Redirects to the web app either way; the link is single use.', |
| 64 | }) |
| 65 | async verify(@ |
| 66 | try { |
| 67 | await this.emails |
| 68 | return { url: |
| 69 | } catch { |
| 70 | return { url: |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Post(':id/resend' |
| 75 | @HttpCode(HttpStatus. |
| 76 | @ApiOperation({ |
| 77 | summary: 'Send the verification link again', |
| 78 | description: |
| 79 | 'For the mail that never arrived or expired. The previous link stops working, and a resend is refused for a minute after the last one.', |
| 80 | }) |
| 81 | @ApiNoContentResponse() |
| 82 | @ApiConflictResponse({ type |
| 83 | @ApiNotFoundResponse({ type |
| 84 | resend(@Session() |
| 85 | return this.emails |
| 86 | } |
| 87 | |
| 88 | @Post(':id/primary' |
| 89 | @ApiOperation({ |
| 90 | summary: 'Make a verified address the account address', |
| 91 | description: |
| 92 | 'Swaps it with the current primary, which stays on the account as an extra so sign-in with it keeps working.', |
| 93 | }) |
| 94 | @ApiOkResponse({ type |
| 95 | @ApiBadRequestResponse({ type |
| 96 | @ApiNotFoundResponse({ type |
| 97 | makePrimary(@Session() |
| 98 | return this.emails |
| 99 | } |
| 100 | |
| 101 | @Delete(':id' |
| 102 | @HttpCode(HttpStatus. |
| 103 | @ApiOperation({ summary |
| 104 | @ApiNoContentResponse() |
| 105 | @ApiNotFoundResponse({ type |
| 106 | remove(@Session() |
| 107 | return this.emails |
| 108 | } |
| 109 | } |