Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestFilesViewed.ts

153 lines5.1 KB

1import * as Context from "effect/Context";
2import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Schema from "effect/Schema";
6import * as SqlClient from "effect/sql/SqlClient";
7import * as SqlSchema from "effect/sql/SqlSchema";
8​
9import { SourceControlProviderKind } from "@t3tools/contracts";
10​
11import {
12 PersistenceDecodeError,
13 PersistenceSqlError,
14 type PullRequestFilesViewedRepositoryError,
15} from "./Errors.ts";
16​
17export const PullRequestFilesViewedScope = Schema.Struct({
18 provider: SourceControlProviderKind,
19 host: Schema.String,
20 repository: Schema.String,
21 number: Schema.Int,
22 viewer: Schema.String,
23});
24export type PullRequestFilesViewedScope = typeof PullRequestFilesViewedScope.Type;
25​
26export const PullRequestFileViewedMark = Schema.Struct({
27 path: Schema.String,
28 /**
29 * Empty where the host said it had none to give, which is an answer rather than a gap: a file
30 * with no version at the head is one the change request deletes. Null where the host could
31 * not say at all, which is no baseline rather than an empty one, and such a mark stays
32 * cleared until a press replaces it.
33 */
34 revision: Schema.NullOr(Schema.String),
35});
36export type PullRequestFileViewedMark = typeof PullRequestFileViewedMark.Type;
37​
38export interface SetPullRequestFilesViewedInput extends PullRequestFilesViewedScope {
39 readonly files: ReadonlyArray<PullRequestFileViewedMark & { readonly viewed: boolean }>;
40 readonly viewedAt: string;
42​
43export const MAX_FILES_VIEWED_ROWS = 500;
44​
45export interface PullRequestFilesViewedPage {
46 readonly files: ReadonlyArray<PullRequestFileViewedMark>;
47 readonly truncated: boolean;
49​
50export class PullRequestFilesViewedRepository extends Context.Service<
51 PullRequestFilesViewedRepository,
52 {
53 readonly list: (
54 input: PullRequestFilesViewedScope,
55 ) => Effect.Effect<PullRequestFilesViewedPage, PullRequestFilesViewedRepositoryError>;
56 readonly set: (
57 input: SetPullRequestFilesViewedInput,
58 ) => Effect.Effect<void, PullRequestFilesViewedRepositoryError>;
59 }
60>()("t3/persistence/PullRequestFilesViewed/PullRequestFilesViewedRepository") {}
61​
62function toSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
63 return (cause: unknown): PullRequestFilesViewedRepositoryError =>
64 Schema.isSchemaError(cause)
65 ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
66 : new PersistenceSqlError({ operation: sqlOperation, cause });
68​
69const make = Effect.gen(function* () {
70 const sql = yield* SqlClient.SqlClient;
71​
72 const listRows = SqlSchema.findAll({
73 Request: PullRequestFilesViewedScope,
74 Result: PullRequestFileViewedMark,
75 execute: ({ provider, host, repository, number, viewer }) =>
76 sql`
77 SELECT
78 path AS "path",
79 revision AS "revision"
80 FROM pull_request_files_viewed
81 WHERE provider = ${provider}
82 AND host = ${host}
83 AND repository = ${repository}
84 AND number = ${number}
85 AND viewer = ${viewer}
86 ORDER BY path
87 LIMIT ${MAX_FILES_VIEWED_ROWS + 1}
88 `,
89 });
90​
91 return PullRequestFilesViewedRepository.of({
92 list: (input) =>
93 listRows(input).pipe(
94 NodeSqliteClient.readOnly,
95 Effect.map((rows) => ({
96 files: rows.slice(0, MAX_FILES_VIEWED_ROWS),
97 truncated: rows.length > MAX_FILES_VIEWED_ROWS,
98 })),
99 Effect.mapError(toSqlOrDecodeError("listPullRequestFilesViewed", "PullRequestFileViewed")),
100 ),
101​
102 set: (input) =>
103 sql
104 .withTransaction(
105 Effect.forEach(
106 input.files,
107 (file) =>
108 file.viewed
109 ? sql`
110 INSERT INTO pull_request_files_viewed (
111 provider,
112 host,
113 repository,
114 number,
115 viewer,
116 path,
117 revision,
118 viewed_at
119 )
120 VALUES (
121 ${input.provider},
122 ${input.host},
123 ${input.repository},
124 ${input.number},
125 ${input.viewer},
126 ${file.path},
127 ${file.revision},
128 ${input.viewedAt}
129 )
130 ON CONFLICT (provider, host, repository, number, viewer, path)
131 DO UPDATE SET revision = excluded.revision, viewed_at = excluded.viewed_at
132 `
133 : sql`
134 DELETE FROM pull_request_files_viewed
135 WHERE provider = ${input.provider}
136 AND host = ${input.host}
137 AND repository = ${input.repository}
138 AND number = ${input.number}
139 AND viewer = ${input.viewer}
140 AND path = ${file.path}
141 `,
142 { discard: true },
143 ),
144 )
145 .pipe(
146 Effect.mapError(
147 (cause) => new PersistenceSqlError({ operation: "setPullRequestFilesViewed", cause }),
148 ),
149 ),
150 });
151});
152​
153export const layer = Layer.effect(PullRequestFilesViewedRepository, make);