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