| 1 | import { Inject, Injectable } from '@nestjs/common'; |
| 2 | import { ConfigService } |
| 3 | import { type Database, |
| 4 | import { and, count |
| 5 | import { nanoid } |
| 6 | import type { Readable |
| 7 | |
| 8 | import { DATABASE } |
| 9 | import { S3Service } |
| 10 | import { UsersService } |
| 11 | import { |
| 12 | AVATAR_CONTENT_TYPES, |
| 13 | AVATAR_PREFIX, |
| 14 | type AvatarContentType, |
| 15 | } from './avatar.constants.js'; |
| 16 | import type { UploadAvatarResponseDTO |
| 17 | import type { |
| 18 | GetUserContributionsQueryDTO, |
| 19 | GetUserContributionsResponseDTO, |
| 20 | } from './dto/contributions.dto.js'; |
| 21 | import type { UserProfileResponseDTO |
| 22 | import { |
| 23 | AvatarNotFoundError, |
| 24 | EmptyAvatarError, |
| 25 | UnsupportedAvatarTypeError, |
| 26 | } from './user.errors.js'; |
| 27 | |
| 28 | export type AvatarObject |
| 29 | stream: Readable |
| 30 | contentType: string |
| 31 | size?: number |
| 32 | }; |
| 33 | |
| 34 | @Injectable() |
| 35 | export class UserService |
| 36 | private readonly publicUrl |
| 37 | |
| 38 | constructor( |
| 39 | @Inject(DATABASE) |
| 40 | private readonly users |
| 41 | private readonly s3 |
| 42 | configService: ConfigService |
| 43 | ) { |
| 44 | this.publicUrl = |
| 45 | .getOrThrow<string>( |
| 46 | .replace(/\/+ |
| 47 | } |
| 48 | |
| 49 | /** The public profile: anything a signed-out visitor may see. */ |
| 50 | async getProfile(username |
| 51 | const user = |
| 52 | |
| 53 | const publicRepositories = |
| 54 | eq(schema.repository |
| 55 | eq(schema.repository |
| 56 | ); |
| 57 | |
| 58 | const [[repositories], |
| 59 | this.db |
| 60 | .select({ value: |
| 61 | .from(schema.repository |
| 62 | .where(publicRepositories), |
| 63 | this.db |
| 64 | .select({ value: |
| 65 | .from(schema.stars |
| 66 | .innerJoin( |
| 67 | schema.repository, |
| 68 | eq(schema.repository |
| 69 | ) |
| 70 | .where(publicRepositories), |
| 71 | ]); |
| 72 | |
| 73 | return { |
| 74 | username: user.username |
| 75 | name: user.name |
| 76 | image: user.image |
| 77 | joinedAt: user.createdAt |
| 78 | repositoryCount: repositories?.value |
| 79 | starCount: stars?.value |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | async uploadAvatar({ |
| 84 | userId, |
| 85 | contentType, |
| 86 | body, |
| 87 | }: { |
| 88 | userId: string |
| 89 | contentType: string |
| 90 | body: Buffer |
| 91 | }): Promise |
| 92 | const extension = |
| 93 | AVATAR_CONTENT_TYPES[ |
| 94 | contentType.split('; |
| 95 | ]; |
| 96 | |
| 97 | if (!extension) |
| 98 | if (!body?.length |
| 99 | |
| 100 | const name = |
| 101 | |
| 102 | await this.s3 |
| 103 | Bucket: this.s3 |
| 104 | Key: this.avatarKey |
| 105 | Body: body, |
| 106 | ContentType: contentType, |
| 107 | }); |
| 108 | |
| 109 | await this.deleteAvatarObjects |
| 110 | |
| 111 | return { url: |
| 112 | } |
| 113 | |
| 114 | async deleteAvatar(userId |
| 115 | await this.deleteAvatarObjects |
| 116 | } |
| 117 | |
| 118 | async getAvatar(userId |
| 119 | try { |
| 120 | const object = |
| 121 | Bucket: this.s3 |
| 122 | Key: this.avatarKey |
| 123 | }); |
| 124 | |
| 125 | if (!object.Body |
| 126 | |
| 127 | return { |
| 128 | stream: object.Body |
| 129 | contentType: object.ContentType |
| 130 | size: object.ContentLength |
| 131 | }; |
| 132 | } catch (error) |
| 133 | if (error instanceof |
| 134 | throw new AvatarNotFoundError() |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private avatarKey(userId |
| 139 | return `${AVATAR_PREFIX} |
| 140 | } |
| 141 | |
| 142 | private async deleteAvatarObjects |
| 143 | const listed = |
| 144 | Bucket: this.s3 |
| 145 | Prefix: `${AVATAR_PREFIX} |
| 146 | }); |
| 147 | |
| 148 | const keepKey = |
| 149 | const stale = |
| 150 | .map((object) |
| 151 | .filter((key) |
| 152 | |
| 153 | if (!stale.length |
| 154 | |
| 155 | await this.s3 |
| 156 | Bucket: this.s3 |
| 157 | Delete: { Objects: stale |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Daily commit counts for the contribution graph, read straight from the |
| 163 | * contribution index. This endpoint never touches git: indexing happens when |
| 164 | * repositories are pushed or browsed, so rendering a profile cannot |
| 165 | * materialize every repository the user owns. |
| 166 | */ |
| 167 | async getContributions( |
| 168 | username: string, |
| 169 | query: GetUserContributionsQueryDTO |
| 170 | requesterId?: string |
| 171 | ): Promise<GetUserContributionsResponseDTO |
| 172 | const user = |
| 173 | const now = |
| 174 | const year = |
| 175 | Number.isFinite(Number |
| 176 | ? Math.trunc( |
| 177 | : now.getUTCFullYear() |
| 178 | |
| 179 | const from = |
| 180 | const to = |
| 181 | const counts = |
| 182 | |
| 183 | // A row counts when it is linked to the account, or when its author email |
| 184 | // is one of the account's emails (a commit indexed before the author |
| 185 | // registered, or before the email was added to the account). Matching on |
| 186 | // both means adding an email never needs a reindex. |
| 187 | // Private repositories are the owner's business: anyone else only sees |
| 188 | // the public ones in the graph. |
| 189 | const emails = |
| 190 | const rows = |
| 191 | .select({ |
| 192 | day: schema.repositoryContribution |
| 193 | count: sum(schema |
| 194 | }) |
| 195 | .from(schema.repositoryContribution |
| 196 | .innerJoin( |
| 197 | schema.repository, |
| 198 | eq(schema.repository |
| 199 | ) |
| 200 | .where( |
| 201 | and( |
| 202 | eq(schema.repository |
| 203 | inArray( |
| 204 | schema.repository.visibility |
| 205 | user.id === |
| 206 | ), |
| 207 | or( |
| 208 | eq(schema.repositoryContribution |
| 209 | inArray(schema.repositoryContribution |
| 210 | ), |
| 211 | gte(schema.repositoryContribution |
| 212 | lt(schema.repositoryContribution |
| 213 | ), |
| 214 | ) |
| 215 | .groupBy(schema.repositoryContribution |
| 216 | |
| 217 | for (const row |
| 218 | counts.set(row |
| 219 | } |
| 220 | |
| 221 | const days: |
| 222 | for ( |
| 223 | let day = |
| 224 | day < new |
| 225 | day = new |
| 226 | ) { |
| 227 | const date = |
| 228 | days.push({ |
| 229 | } |
| 230 | |
| 231 | return { |
| 232 | username: user.username |
| 233 | year, |
| 234 | totalContributions: days.reduce |
| 235 | days, |
| 236 | }; |
| 237 | } |
| 238 | } |