| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { ConfigService } |
| 4 | import { eq } |
| 5 | |
| 6 | import { DATABASE } |
| 7 | import { isoTimestamp } |
| 8 | import { orNull } |
| 9 | import { |
| 10 | CouldNotResolveError, |
| 11 | GithubForbiddenError, |
| 12 | } from '../../../../lib/github/github.errors.js'; |
| 13 | import type { Loaders |
| 14 | import { decodeNodeId } |
| 15 | import { commentOf, issueRefOf |
| 16 | import { |
| 17 | toIssueCommentNode, |
| 18 | toLabelNode, |
| 19 | toOrganizationNode, |
| 20 | toUserNode, |
| 21 | } from '../../../../lib/github/nodes.js'; |
| 22 | import { githubOrigins } |
| 23 | import type { GithubViewer |
| 24 | import { IssueNodesService } |
| 25 | import { RepositoryNodesService } |
| 26 | |
| 27 | @Injectable() |
| 28 | export class ViewerService |
| 29 | constructor( |
| 30 | @Inject(DATABASE) |
| 31 | private readonly config |
| 32 | private readonly repositories |
| 33 | private readonly issueNodes |
| 34 | ) {} |
| 35 | |
| 36 | async viewer(viewer |
| 37 | if (!viewer) |
| 38 | throw new GithubForbiddenError( |
| 39 | 'This endpoint requires you to be authenticated.', |
| 40 | ); |
| 41 | const row = |
| 42 | if (!row) |
| 43 | throw new GithubForbiddenError( |
| 44 | 'This endpoint requires you to be authenticated.', |
| 45 | ); |
| 46 | return toUserNode(row |
| 47 | } |
| 48 | |
| 49 | async user(login |
| 50 | const row = |
| 51 | if (!row) |
| 52 | throw new CouldNotResolveError( |
| 53 | `Could not resolve to a User with the login of '${login}'.` |
| 54 | ); |
| 55 | return toUserNode(row |
| 56 | } |
| 57 | |
| 58 | async organization(login |
| 59 | const [row] |
| 60 | .select() |
| 61 | .from(schema.organization |
| 62 | .where(eq(schema |
| 63 | if (!row) |
| 64 | throw new CouldNotResolveError( |
| 65 | `Could not resolve to an Organization with the login of '${login}'.` |
| 66 | ); |
| 67 | return toOrganizationNode(row |
| 68 | } |
| 69 | |
| 70 | async repositoryOwner(login |
| 71 | const [organization] |
| 72 | .select() |
| 73 | .from(schema.organization |
| 74 | .where(eq(schema |
| 75 | if (organization) |
| 76 | return toOrganizationNode(organization |
| 77 | const user = |
| 78 | return user ? |
| 79 | } |
| 80 | |
| 81 | async node(id |
| 82 | const found = |
| 83 | if (!found) |
| 84 | throw new CouldNotResolveError( |
| 85 | `Could not resolve to a node with the global id of '${id}'` |
| 86 | ); |
| 87 | return found; |
| 88 | } |
| 89 | |
| 90 | nodes(ids: |
| 91 | return Promise.all |
| 92 | } |
| 93 | |
| 94 | /** A node the viewer cannot read resolves to null, as on GitHub. */ |
| 95 | private async lookup |
| 96 | id: string, |
| 97 | viewer: GithubViewer |
| 98 | loaders: Loaders |
| 99 | ) { |
| 100 | const decoded = |
| 101 | if (decoded?.type |
| 102 | const row = |
| 103 | return row ? |
| 104 | } |
| 105 | if (decoded?.type |
| 106 | const [row] |
| 107 | .select() |
| 108 | .from(schema.organization |
| 109 | .where(eq(schema |
| 110 | return row ? |
| 111 | } |
| 112 | const requesterId = |
| 113 | if (decoded?.type |
| 114 | return orNull(this |
| 115 | } |
| 116 | if (decoded?.type |
| 117 | const [label] |
| 118 | .select({ |
| 119 | id: schema.label |
| 120 | name: schema.label |
| 121 | description: schema.label |
| 122 | color: schema.label |
| 123 | createdAt: isoTimestamp(schema |
| 124 | updatedAt: isoTimestamp(schema |
| 125 | repositoryId: schema.label |
| 126 | }) |
| 127 | .from(schema.label |
| 128 | .where(eq(schema |
| 129 | if (!label) |
| 130 | const repository = |
| 131 | this.repositories.load |
| 132 | ); |
| 133 | return repository ? |
| 134 | } |
| 135 | if (decoded?.type |
| 136 | const found = |
| 137 | return found?.kind |
| 138 | } |
| 139 | if (decoded?.type |
| 140 | const comment = |
| 141 | // Loading the issue is what checks the viewer may read the comment. |
| 142 | const issue = |
| 143 | if (!issue) |
| 144 | const viewerLogin = |
| 145 | ? ((await loaders |
| 146 | : null; |
| 147 | return toIssueCommentNode(comment |
| 148 | } |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | private async issueById |
| 153 | const ref = |
| 154 | if (!ref) |
| 155 | const repository = |
| 156 | this.repositories.load |
| 157 | ); |
| 158 | return ( |
| 159 | repository && |
| 160 | orNull( |
| 161 | this.issueNodes.fromRepository |
| 162 | ) |
| 163 | ); |
| 164 | } |
| 165 | } |