Open History to see the last commit
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 /** Empty means the host confirms deletion; null means no baseline, so the mark stays cleared until replaced. */
29 revision: Schema.NullOr(Schema.String),
30});
31export type PullRequestFileViewedMark = typeof PullRequestFileViewedMark.Type;
32
33export interface SetPullRequestFilesViewedInput extends PullRequestFilesViewedScope {
34 readonly files: ReadonlyArray<PullRequestFileViewedMark & { readonly viewed: boolean }>;
35 readonly viewedAt: string;
36}
37
38export const MAX_FILES_VIEWED_ROWS = 500;
39
40export interface PullRequestFilesViewedPage {
41 readonly files: ReadonlyArray<PullRequestFileViewedMark>;
42 readonly truncated: boolean;
43}
44
45export class PullRequestFilesViewedRepository extends Context.Service<
46 PullRequestFilesViewedRepository,
47 {
48 readonly list: (
49 input: PullRequestFilesViewedScope,
50 ) => Effect.Effect<PullRequestFilesViewedPage, PullRequestFilesViewedRepositoryError>;
51 readonly set: (
52 input: SetPullRequestFilesViewedInput,
53 ) => Effect.Effect<void, PullRequestFilesViewedRepositoryError>;
54 }
55>()("t3/persistence/PullRequestFilesViewed/PullRequestFilesViewedRepository") {}
56
57function toSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
58 return (cause: unknown): PullRequestFilesViewedRepositoryError =>
59 Schema.isSchemaError(cause)
60 ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
61 : new PersistenceSqlError({ operation: sqlOperation, cause });
62}
63
64const make = Effect.gen(function* () {
65 const sql = yield* SqlClient.SqlClient;
66
67 const listRows = SqlSchema.findAll({
68 Request: PullRequestFilesViewedScope,
69 Result: PullRequestFileViewedMark,
70 execute: ({ provider, host, repository, number, viewer }) =>
71 sql`
72 SELECT
73 path AS "path",
74 revision AS "revision"
75 FROM pull_request_files_viewed
76 WHERE provider = ${provider}
77 AND host = ${host}
78 AND repository = ${repository}
79 AND number = ${number}
80 AND viewer = ${viewer}
81 ORDER BY path
82 LIMIT ${MAX_FILES_VIEWED_ROWS + 1}
83 `,
84 });
85
86 return PullRequestFilesViewedRepository.of({
87 list: (input) =>
88 listRows(input).pipe(
89 NodeSqliteClient.readOnly,
90 Effect.map((rows) => ({
91 files: rows.slice(0, MAX_FILES_VIEWED_ROWS),
92 truncated: rows.length > MAX_FILES_VIEWED_ROWS,
93 })),
94 Effect.mapError(toSqlOrDecodeError("listPullRequestFilesViewed", "PullRequestFileViewed")),
95 ),
96
97 set: (input) =>
98 sql
99 .withTransaction(
100 Effect.forEach(
101 input.files,
102 (file) =>
103 file.viewed
104 ? sql`
105 INSERT INTO pull_request_files_viewed (
106 provider,
107 host,
108 repository,
109 number,
110 viewer,
111 path,
112 revision,
113 viewed_at
114 )
115 VALUES (
116 ${input.provider},
117 ${input.host},
118 ${input.repository},
119 ${input.number},
120 ${input.viewer},
121 ${file.path},
122 ${file.revision},
123 ${input.viewedAt}
124 )
125 ON CONFLICT (provider, host, repository, number, viewer, path)
126 DO UPDATE SET revision = excluded.revision, viewed_at = excluded.viewed_at
127 `
128 : sql`
129 DELETE FROM pull_request_files_viewed
130 WHERE provider = ${input.provider}
131 AND host = ${input.host}
132 AND repository = ${input.repository}
133 AND number = ${input.number}
134 AND viewer = ${input.viewer}
135 AND path = ${file.path}
136 `,
137 { discard: true },
138 ),
139 )
140 .pipe(
141 Effect.mapError(
142 (cause) => new PersistenceSqlError({ operation: "setPullRequestFilesViewed", cause }),
143 ),
144 ),
145 });
146});
147
148export const layer = Layer.effect(PullRequestFilesViewedRepository, make);