~james/core

core/web/src/lib/api/records.ts -rw-r--r-- 6.0 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import type { BobbinContext, XrpcRequestInit } from "#lib/api/client.ts";
import { jsonGet } from "#lib/api/_request.ts";
import type * as OrgTangledActorGetProfileActivity from "#lib/api/lexicons/types/org/tangled/actor/getProfileActivity.ts";
import type * as OrgTangledActorProfile from "#lib/api/lexicons/types/org/tangled/actor/profile.ts";
import type * as OrgTangledFeedStar from "#lib/api/lexicons/types/org/tangled/feed/star.ts";
import type * as OrgTangledGraphVouch from "#lib/api/lexicons/types/org/tangled/graph/vouch.ts";
import type * as OrgTangledRepoDefs from "#lib/api/lexicons/types/org/tangled/repo/defs.ts";
import type * as OrgTangledRepoManifest from "#lib/api/lexicons/types/org/tangled/repo/manifest.ts";
import type * as OrgTangledReviewPatch from "#lib/api/lexicons/types/org/tangled/review/patch.ts";
import type * as OrgTangledSnippetString from "#lib/api/lexicons/types/org/tangled/snippet/string.ts";
import type * as OrgTangledTrackGetTicketView from "#lib/api/lexicons/types/org/tangled/track/getTicketView.ts";
import type * as OrgTangledTrackTicket from "#lib/api/lexicons/types/org/tangled/track/ticket.ts";
export interface RecordView<V> {
	uri: string;
	cid?: string;
	value: V;
}

export interface RecordList<V> {
	items: RecordView<V>[];
}

export type RepoRecord = OrgTangledRepoManifest.Main;
export type ProfileRecord = OrgTangledActorProfile.Main;
export type TicketRecord = OrgTangledTrackTicket.Main;
export type TicketState = OrgTangledTrackTicket.Main["state"];

export type StringRecord = OrgTangledSnippetString.Main;
export type StarRecord = OrgTangledFeedStar.Main;
export type VouchRecord = OrgTangledGraphVouch.Main;
export type ProfileActivityOutput = OrgTangledActorGetProfileActivity.$output;
export type ProfileActivityRepoRef = OrgTangledActorGetProfileActivity.RepoRef;

export const BULK_LIMIT = 50;

// absent when the repo doesn't support git at all
export interface GitInfo {
	defaultBranch: string;
	isEmpty: boolean;
	languages?: OrgTangledRepoDefs.Language[];
}

export type RepoView = RecordView<RepoRecord> & { knot?: string; git?: GitInfo };

export const getRepo = (ctx: BobbinContext, repo: string, init?: XrpcRequestInit) =>
	jsonGet<RepoView>(ctx, "org.tangled.repo.getRepo", { repo }, init);

export const getRepoByRepoDid = (ctx: BobbinContext, repoDid: string, init?: XrpcRequestInit) =>
	jsonGet<RepoView>(ctx, "org.tangled.repo.getRepoByRepoDid", { repoDid }, init);

export const getRepoByName = (
	ctx: BobbinContext,
	owner: string,
	name: string,
	init?: XrpcRequestInit
) => jsonGet<RepoView>(ctx, "org.tangled.temp.repo.getRepoByName", { owner, name }, init);

export interface RepoListPage {
	cursor?: string | null;
	items: RecordView<RepoRecord>[];
}

export const listRepos = (
	ctx: BobbinContext,
	subject: string,
	filter: { limit?: number; cursor?: string; order?: "asc" | "desc" } = {},
	init?: XrpcRequestInit
) => jsonGet<RepoListPage>(ctx, "org.tangled.repo.listRepos", { subject, ...filter }, init);

export const getProfile = (ctx: BobbinContext, did: string, init?: XrpcRequestInit) =>
	jsonGet<RecordView<ProfileRecord>>(
		ctx,
		"org.tangled.actor.getProfile",
		{ actor: `at://${did}/org.tangled.actor.profile/self` },
		init
	);

export type TicketViewDetailed = OrgTangledTrackGetTicketView.$output;

export type PatchRecord = OrgTangledReviewPatch.Main;

export const listPatches = (ctx: BobbinContext, ticket: string, init?: XrpcRequestInit) =>
	jsonGet<RecordList<PatchRecord>>(
		ctx,
		"org.tangled.temp.review.listPatches",
		{ ticket, order: "asc", limit: 500 },
		init
	);

export interface PullListItem {
	cid?: string;
	commentCount: number;
	state: TicketState;
	stateUpdatedAt?: string;
	uri: string;
	value: TicketRecord;
}

export interface PullListPage {
	cursor?: string | null;
	total?: number;
	items: PullListItem[];
}

export interface ListPullsFilter {
	status?: TicketState;
	author?: string;
	limit?: number;
	cursor?: string;
	order?: "asc" | "desc";
}

export const listPulls = (
	ctx: BobbinContext,
	subject: string,
	filter: ListPullsFilter = {},
	init?: XrpcRequestInit
) => jsonGet<PullListPage>(ctx, "org.tangled.repo.listPulls", { subject, ...filter }, init);

export const getRepos = (ctx: BobbinContext, repos: readonly string[], init?: XrpcRequestInit) =>
	jsonGet<RecordList<RepoRecord>>(ctx, "org.tangled.repo.getRepos", { repos }, init);

export const getReposByRepoDids = (
	ctx: BobbinContext,
	dids: readonly string[],
	init?: XrpcRequestInit
) => jsonGet<RecordList<RepoRecord>>(ctx, "org.tangled.repo.getReposByRepoDids", { dids }, init);

export const getProfileActivity = (
	ctx: BobbinContext,
	actor: string,
	months = 7,
	init?: XrpcRequestInit
) =>
	jsonGet<ProfileActivityOutput>(
		ctx,
		"org.tangled.actor.getProfileActivity",
		{ actor, months },
		init
	);

export const getProfiles = (
	ctx: BobbinContext,
	actors: readonly string[],
	init?: XrpcRequestInit
) => jsonGet<RecordList<ProfileRecord>>(ctx, "org.tangled.actor.getProfiles", { actors }, init);

// one uri outside org.tangled.track.ticket fails the whole batch with a 400
export const getTickets = async (
	ctx: BobbinContext,
	uris: readonly string[],
	init?: XrpcRequestInit
): Promise<RecordList<TicketRecord>> => {
	const pages = await Promise.all(
		Array.from({ length: Math.ceil(uris.length / BULK_LIMIT) }, (_, index) =>
			jsonGet<RecordList<TicketRecord>>(
				ctx,
				"org.tangled.track.getTickets",
				{ uris: uris.slice(index * BULK_LIMIT, (index + 1) * BULK_LIMIT) },
				init
			)
		)
	);
	return { items: pages.flatMap((page) => page.items) };
};

export interface StarListPage {
	cursor?: string | null;
	total?: number;
	items: RecordView<StarRecord>[];
}

export const listStars = (
	ctx: BobbinContext,
	subject: string,
	filter: { limit?: number; cursor?: string; offset?: number; order?: "asc" | "desc" } = {},
	init?: XrpcRequestInit
) => jsonGet<StarListPage>(ctx, "org.tangled.feed.listStars", { subject, ...filter }, init);

export const getString = (ctx: BobbinContext, uri: string, init?: XrpcRequestInit) =>
	jsonGet<RecordView<StringRecord>>(ctx, "org.tangled.string.getString", { uri }, init);