| 1 | import { Inject, Injectable } from '@nestjs/common'; |
| 2 | import { type Database, |
| 3 | import { and, count |
| 4 | |
| 5 | import { |
| 6 | acceptedCollaboration, |
| 7 | organizationMembership, |
| 8 | readableBy, |
| 9 | } from '../../lib/git/repository-access/repository-access.js'; |
| 10 | import { DATABASE } |
| 11 | import { UsersService } |
| 12 | import type { |
| 13 | GetUserContributionsQueryDTO, |
| 14 | GetUserContributionsResponseDTO, |
| 15 | } from './dto/contributions.dto.js'; |
| 16 | import type { UserProfileResponseDTO |
| 17 | |
| 18 | @Injectable() |
| 19 | export class UserService |
| 20 | constructor( |
| 21 | @Inject(DATABASE) |
| 22 | private readonly users |
| 23 | ) {} |
| 24 | |
| 25 | /** The public profile: anything a signed-out visitor may see. */ |
| 26 | async getProfile(username |
| 27 | const user = |
| 28 | |
| 29 | // Repositories the user created in an organization are the organization's. |
| 30 | const publicRepositories = |
| 31 | eq(schema.repository |
| 32 | isNull(schema.repository |
| 33 | eq(schema.repository |
| 34 | ); |
| 35 | |
| 36 | const [[repositories], |
| 37 | this.db |
| 38 | .select({ value: |
| 39 | .from(schema.repository |
| 40 | .where(publicRepositories), |
| 41 | this.db |
| 42 | .select({ value: |
| 43 | .from(schema.stars |
| 44 | .innerJoin( |
| 45 | schema.repository, |
| 46 | eq(schema.repository |
| 47 | ) |
| 48 | .where(publicRepositories), |
| 49 | ]); |
| 50 | |
| 51 | return { |
| 52 | username: user.username |
| 53 | name: user.name |
| 54 | image: user.image |
| 55 | joinedAt: user.createdAt |
| 56 | repositoryCount: repositories?.value |
| 57 | starCount: stars?.value |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | /** Daily commit counts for the contribution graph, read from the index alone: rendering a profile must not materialize every repository. */ |
| 62 | async getContributions( |
| 63 | username: string, |
| 64 | query: GetUserContributionsQueryDTO |
| 65 | requesterId?: string |
| 66 | ): Promise<GetUserContributionsResponseDTO |
| 67 | const user = |
| 68 | const now = |
| 69 | const year = |
| 70 | Number.isFinite(Number |
| 71 | ? Math.trunc( |
| 72 | : now.getUTCFullYear() |
| 73 | |
| 74 | const from = |
| 75 | const to = |
| 76 | const counts = |
| 77 | |
| 78 | // Match on the account link or on a known author email, so adding an email never needs a reindex. Commits count wherever they landed, in any repository the viewer may read. |
| 79 | const emails = |
| 80 | const viewer = |
| 81 | const rows = |
| 82 | .select({ |
| 83 | day: schema.repositoryContribution |
| 84 | count: sum(schema |
| 85 | }) |
| 86 | .from(schema.repositoryContribution |
| 87 | .innerJoin( |
| 88 | schema.repository, |
| 89 | eq(schema.repository |
| 90 | ) |
| 91 | .leftJoin( |
| 92 | schema.repositoryCollaborator, |
| 93 | acceptedCollaboration(schema.repository |
| 94 | ) |
| 95 | .leftJoin( |
| 96 | schema.member, |
| 97 | organizationMembership(schema.repository |
| 98 | ) |
| 99 | .where( |
| 100 | and( |
| 101 | readableBy(viewer), |
| 102 | or( |
| 103 | eq(schema.repositoryContribution |
| 104 | inArray(schema.repositoryContribution |
| 105 | ), |
| 106 | gte(schema.repositoryContribution |
| 107 | lt(schema.repositoryContribution |
| 108 | ), |
| 109 | ) |
| 110 | .groupBy(schema.repositoryContribution |
| 111 | |
| 112 | for (const row |
| 113 | counts.set(row |
| 114 | } |
| 115 | |
| 116 | const days: |
| 117 | for ( |
| 118 | let day = |
| 119 | day < new |
| 120 | day = new |
| 121 | ) { |
| 122 | const date = |
| 123 | days.push({ |
| 124 | } |
| 125 | |
| 126 | return { |
| 127 | username: user.username |
| 128 | year, |
| 129 | totalContributions: days.reduce |
| 130 | days, |
| 131 | }; |
| 132 | } |
| 133 | } |