| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | |
| 3 | import { pageOf, RestPage |
| 4 | import { IssuesService } |
| 5 | import type { |
| 6 | GithubRepositoryTarget, |
| 7 | GithubViewer, |
| 8 | } from '../../auth/github-request.js'; |
| 9 | import type { RepositoryNode |
| 10 | import type { |
| 11 | GithubCommentDTO, |
| 12 | GithubListCommentsQueryDTO, |
| 13 | } from './dto/issue.dto.js'; |
| 14 | import { GithubIssuesService } |
| 15 | |
| 16 | /** GitHub's REST issue comments over IssuesService, by GitHub's integer comment ids. */ |
| 17 | @Injectable() |
| 18 | export class GithubCommentsService |
| 19 | constructor( |
| 20 | private readonly github |
| 21 | private readonly issues |
| 22 | ) {} |
| 23 | |
| 24 | /** GitHub ignores `direction` without `sort`, listing oldest first. */ |
| 25 | async listForRepository( |
| 26 | { owner, repo |
| 27 | query: GithubListCommentsQueryDTO |
| 28 | ) { |
| 29 | const repository = |
| 30 | return this.page |
| 31 | sort: query.sort |
| 32 | direction: query.sort |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | async list( |
| 37 | { owner, repo |
| 38 | number: number, |
| 39 | query: GithubListCommentsQueryDTO |
| 40 | ) { |
| 41 | const repository = |
| 42 | return this.page |
| 43 | number, |
| 44 | sort: 'created', |
| 45 | direction: 'asc', |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | async get({ |
| 50 | const repository = |
| 51 | const comment = |
| 52 | const [rest] |
| 53 | return rest; |
| 54 | } |
| 55 | |
| 56 | async create( |
| 57 | { owner, repo |
| 58 | number: number, |
| 59 | body: GithubCommentDTO |
| 60 | ) { |
| 61 | const { repository, |
| 62 | owner, |
| 63 | repo, |
| 64 | viewer, |
| 65 | 'POST /repos/:owner/:repo/issues/:number/comments', |
| 66 | ); |
| 67 | const created = |
| 68 | ...this.github. |
| 69 | requesterId: signedIn.userId |
| 70 | number, |
| 71 | body: body.body |
| 72 | }); |
| 73 | const comment = |
| 74 | const [rest] |
| 75 | return rest; |
| 76 | } |
| 77 | |
| 78 | /** Only the comment's author may, as Ghost allows. */ |
| 79 | async update( |
| 80 | { owner, repo |
| 81 | id: number, |
| 82 | body: GithubCommentDTO |
| 83 | ) { |
| 84 | const { repository, |
| 85 | owner, |
| 86 | repo, |
| 87 | viewer, |
| 88 | 'PATCH /repos/:owner/:repo/issues/comments/:comment_id', |
| 89 | ); |
| 90 | const comment = |
| 91 | const updated = |
| 92 | ...this.github. |
| 93 | requesterId: signedIn.userId |
| 94 | number: comment.issueNumber |
| 95 | commentId: comment.id |
| 96 | body: body.body |
| 97 | }); |
| 98 | const [rest] |
| 99 | { ...comment, |
| 100 | ]); |
| 101 | return rest; |
| 102 | } |
| 103 | |
| 104 | async delete({ |
| 105 | const { repository, |
| 106 | owner, |
| 107 | repo, |
| 108 | viewer, |
| 109 | 'DELETE /repos/:owner/:repo/issues/comments/:comment_id', |
| 110 | ); |
| 111 | const comment = |
| 112 | await this.issues |
| 113 | ...this.github. |
| 114 | requesterId: signedIn.userId |
| 115 | number: comment.issueNumber |
| 116 | commentId: comment.id |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | private async page |
| 121 | repository: RepositoryNode |
| 122 | viewer: GithubViewer |
| 123 | query: GithubListCommentsQueryDTO |
| 124 | order: { |
| 125 | number?: number |
| 126 | sort: 'created |
| 127 | direction: 'asc |
| 128 | }, |
| 129 | ) { |
| 130 | const page = |
| 131 | const { comments, |
| 132 | ...this.github. |
| 133 | ...order, |
| 134 | since: query.since |
| 135 | offset: page.offset |
| 136 | limit: page.perPage |
| 137 | }); |
| 138 | return new RestPage |
| 139 | await this.github |
| 140 | page, |
| 141 | total, |
| 142 | ); |
| 143 | } |
| 144 | } |