1import { Client, simpleFetchHandler } from "@atcute/client";
2import { query } from "@solidjs/router";
3import { getRepoKnot, getUserRepo } from "../../util/get_repo";
4import { constellationUrl } from "../../util/microcosm";
5import type { DID } from "../../util/types";
6
7async function getKnotRpc(user: DID, repo: string) {
8 const service = await getRepoKnot(user, repo);
9 const handler = simpleFetchHandler({ service });
10 const rpc = new Client({ handler });
11
12 return rpc;
13}
14
15async function getConstellationRpc() {
16 const handler = simpleFetchHandler({ service: constellationUrl });
17 const rpc = new Client({ handler });
18
19 return rpc;
20}
21
22export const getRepoDefaultBranch = query(async (user: DID, repo: string) => {
23 const rpc = await getKnotRpc(user, repo);
24
25 return await rpc.get("sh.tangled.repo.getDefaultBranch", {
26 params: {
27 repo: `${user}/${repo}`,
28 },
29 });
30}, "RepoDefaultBranch");
31
32export const getRepoTree = query(
33 async (user: DID, repo: string, ref: string, path: string) => {
34 const rpc = await getKnotRpc(user, repo);
35
36 return await rpc.get("sh.tangled.repo.tree", {
37 params: {
38 repo: `${user}/${repo}`,
39 ref: ref,
40 path: path ?? "",
41 },
42 });
43 },
44 "RepoTreeData",
45);
46
47export const getRepoBlob = query(
48 async (user: DID, repo: string, ref: string, path: string) => {
49 const rpc = await getKnotRpc(user, repo);
50
51 return await rpc.get("sh.tangled.repo.blob", {
52 params: {
53 repo: `${user}/${repo}`,
54 ref,
55 path,
56 },
57 });
58 },
59 "RepoBlob",
60);
61
62export const getRepoBlobUrl = query(
63 async (user: DID, repo: string, ref: string, path: string) => {
64 const knot = await getRepoKnot(user, repo);
65
66 return `${knot}/xrpc/sh.tangled.repo.blob?repo=${user}/${repo}&ref=${ref}&path=${path}&raw=true`;
67 },
68 "RepoBlobUrl",
69);
70
71export const getRepoLog = query(
72 async (user: DID, repo: string, ref: string) => {
73 const rpc = await getKnotRpc(user, repo);
74
75 return await rpc.get("sh.tangled.repo.log", {
76 as: "json",
77 params: {
78 repo: `${user}/${repo}`,
79 ref,
80 },
81 });
82 },
83 "RepoBlob",
84);
85
86export const getRepoLanguages = query(
87 async (user: DID, repo: string, ref: string) => {
88 const rpc = await getKnotRpc(user, repo);
89
90 return await rpc.get("sh.tangled.repo.languages", {
91 params: {
92 repo: `${user}/${repo}`,
93 ref,
94 },
95 });
96 },
97 "RepoLanguages",
98);
99
100export const getRepoStars = query(async (user: DID, repo: string) => {
101 const request = new URL(`${constellationUrl}/links/count/distinct-dids`);
102 request.searchParams.set(
103 "target",
104 `at://${user}/sh.tangled.repo/${(await getUserRepo(user, repo))?.rkey}`,
105 );
106 request.searchParams.set("collection", "sh.tangled.feed.star");
107 request.searchParams.set("path", ".subject");
108
109 const res = await fetch(request).then((res) => res.json());
110 return (res?.total as number) || 0;
111}, "RepoStars");
112
113export const getRepoForks = query(async (user: DID, repo: string) => {
114 const request = new URL(`${constellationUrl}/links/count/distinct-dids`);
115 request.searchParams.set(
116 "target",
117 `at://${user}/sh.tangled.repo/${(await getUserRepo(user, repo))?.rkey}`,
118 );
119 request.searchParams.set("collection", "sh.tangled.repo");
120 request.searchParams.set("path", ".source");
121
122 const res = await fetch(request).then((res) => res.json());
123 return (res?.total as number) || 0;
124}, "RepoForks");
125
126export const getRepoCommit = query(
127 async (user: DID, repo: string, ref: string) => {
128 const rpc = await getKnotRpc(user, repo);
129
130 return await rpc.get("sh.tangled.repo.diff", {
131 as: "json",
132 params: {
133 repo: `${user}/${repo}`,
134 ref,
135 },
136 });
137 },
138 "RepoCommit",
139);
140
141export const getRepoCompare = query(
142 async (user: DID, repo: string, rev1: string, rev2: string) => {
143 const rpc = await getKnotRpc(user, repo);
144
145 return await rpc.get("sh.tangled.repo.compare", {
146 as: "json",
147 params: {
148 repo: `${user}/${repo}`,
149 rev1,
150 rev2,
151 },
152 });
153 },
154 "RepoCommit",
155);
156
157export const getRepoBranches = query(
158 async (user: DID, repo: string, ref: string) => {
159 const rpc = await getKnotRpc(user, repo);
160
161 return await rpc.get("sh.tangled.repo.branches", {
162 as: "json",
163 params: {
164 repo: `${user}/${repo}`,
165 ref,
166 },
167 });
168 },
169 "RepoBranches",
170);
171
172export const getRepoTags = query(
173 async (user: DID, repo: string, ref: string) => {
174 const rpc = await getKnotRpc(user, repo);
175
176 return await rpc.get("sh.tangled.repo.tags", {
177 as: "json",
178 params: {
179 repo: `${user}/${repo}`,
180 ref,
181 },
182 });
183 },
184 "RepoTags",
185);