| 1 | import { |
| 2 | Controller, |
| 3 | Delete, |
| 4 | Get, |
| 5 | Headers, |
| 6 | HttpCode, |
| 7 | HttpStatus, |
| 8 | Param, |
| 9 | Put, |
| 10 | Query, |
| 11 | Req, |
| 12 | Res, |
| 13 | Session, |
| 14 | StreamableFile, |
| 15 | } from '@nestjs/common'; |
| 16 | import { |
| 17 | ApiBadRequestResponse, |
| 18 | ApiBody, |
| 19 | ApiConsumes, |
| 20 | ApiNoContentResponse, |
| 21 | ApiNotFoundResponse, |
| 22 | ApiOkResponse, |
| 23 | ApiOperation, |
| 24 | ApiTags, |
| 25 | ApiUnsupportedMediaTypeResponse, |
| 26 | } from '@nestjs/swagger'; |
| 27 | import { OptionalAuth, type |
| 28 | import type { Request, |
| 29 | |
| 30 | import { ErrorResponseDTO } |
| 31 | import { AvatarStorageService } |
| 32 | import { UploadAvatarResponseDTO } |
| 33 | import { |
| 34 | GetUserContributionsQueryDTO, |
| 35 | GetUserContributionsResponseDTO, |
| 36 | } from './dto/contributions.dto.js'; |
| 37 | import { UserProfileResponseDTO } |
| 38 | import { UserService } |
| 39 | |
| 40 | @ApiTags('Users') |
| 41 | @Controller('users') |
| 42 | export class UserController |
| 43 | constructor( |
| 44 | private readonly users |
| 45 | private readonly avatars |
| 46 | ) {} |
| 47 | |
| 48 | @Put('avatar' |
| 49 | @ApiOperation({ |
| 50 | summary: 'Upload the signed-in user’s avatar', |
| 51 | description: |
| 52 | 'Takes the raw image bytes. Returns the URL to store on the user; writing `user.image` stays with Better Auth’s `update-user`.', |
| 53 | }) |
| 54 | @ApiConsumes('image/png' |
| 55 | @ApiBody({ schema |
| 56 | @ApiOkResponse({ type |
| 57 | @ApiBadRequestResponse({ type |
| 58 | @ApiUnsupportedMediaTypeResponse({ type |
| 59 | uploadAvatar( |
| 60 | @Req() request |
| 61 | @Headers('content-type' |
| 62 | @Session() session |
| 63 | ) { |
| 64 | return this.avatars |
| 65 | ownerId: session.user |
| 66 | contentType: contentType ?? |
| 67 | body: Buffer.isBuffer |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | @Delete('avatar' |
| 72 | @HttpCode(HttpStatus. |
| 73 | @ApiOperation({ summary |
| 74 | @ApiNoContentResponse() |
| 75 | deleteAvatar(@Session() |
| 76 | return this.avatars |
| 77 | } |
| 78 | |
| 79 | @Get('avatars/:userId/:name' |
| 80 | @OptionalAuth() |
| 81 | @ApiOperation({ summary |
| 82 | @ApiOkResponse({ schema |
| 83 | @ApiNotFoundResponse({ type |
| 84 | async getAvatar( |
| 85 | @Param('userId' |
| 86 | @Param('name' |
| 87 | @Res({ passthrough |
| 88 | ): Promise<StreamableFile |
| 89 | const avatar = |
| 90 | |
| 91 | response.set({ |
| 92 | 'Content-Type': avatar. |
| 93 | ...(avatar.size |
| 94 | 'X-Content-Type-Options': 'nosniff', |
| 95 | // A new upload gets a new name, so a hit can never be stale. |
| 96 | 'Cache-Control': 'public, max-age=31536000, immutable', |
| 97 | }); |
| 98 | |
| 99 | return new StreamableFile |
| 100 | } |
| 101 | |
| 102 | @Get(':username' |
| 103 | @OptionalAuth() |
| 104 | @ApiOperation({ summary |
| 105 | @ApiOkResponse({ type |
| 106 | @ApiNotFoundResponse({ type |
| 107 | getProfile(@Param( |
| 108 | return this.users |
| 109 | } |
| 110 | |
| 111 | @Get(':username/contributions' |
| 112 | @OptionalAuth() |
| 113 | @ApiOperation({ |
| 114 | summary: "Read a user's contribution calendar", |
| 115 | description: |
| 116 | 'Daily commit counts for a calendar year, across the repositories the requester may see that the user owns. Days are UTC, in order.', |
| 117 | }) |
| 118 | @ApiOkResponse({ type |
| 119 | @ApiNotFoundResponse({ type |
| 120 | getContributions( |
| 121 | @Param('username' |
| 122 | @Query() query |
| 123 | @Session() session |
| 124 | ) { |
| 125 | return this.users |
| 126 | } |
| 127 | } |