Coves frontend - a photon fork
1import { env } from '$env/dynamic/public'
2import * as types from './types'
3
4export type ClientType =
5 | { name: 'lemmy'; baseUrl: '/api/v3' }
6 | { name: 'piefed'; baseUrl: '/api/alpha' }
7
8export const DEFAULT_CLIENT_TYPE: ClientType =
9 env.PUBLIC_INSTANCE_TYPE == 'piefedalpha'
10 ? {
11 name: 'piefed',
12 baseUrl: '/api/alpha',
13 }
14 : {
15 name: 'lemmy',
16 baseUrl: '/api/v3',
17 }
18
19export abstract class BaseClient {
20 abstract type: ClientType
21
22 static constants: {
23 password: {
24 minLength: number
25 maxLength: number
26 }
27 }
28
29 static async fetchInfo(
30 base: string | URL,
31 ): Promise<{ type: ClientType; version: string } | null> {
32 try {
33 const res = await fetch(new URL('/nodeinfo/2.1', base))
34
35 if (!res.ok) return null
36
37 const software: { name: string; version: string } = (await res.json())
38 .software
39
40 // should probably extract this into not a static function but its ok
41 switch (software.name) {
42 case 'lemmy': {
43 return {
44 type: { baseUrl: '/api/v3', name: 'lemmy' },
45 version: software.version,
46 }
47 }
48 case 'piefed': {
49 return {
50 type: { baseUrl: '/api/alpha', name: 'piefed' },
51 version: software.version,
52 }
53 }
54 default:
55 return null
56 }
57 } catch (err) {
58 console.error(err)
59 return null
60 }
61 }
62
63 abstract getSite(): Promise<types.GetSiteResponse>
64 abstract editSite(form: types.EditSite): Promise<types.SiteResponse>
65 abstract generateTotpSecret(): Promise<types.GenerateTotpSecretResponse>
66 abstract listLogins(): Promise<types.LoginToken[]>
67 abstract listAllMedia(form: types.ListMedia): Promise<types.ListMediaResponse>
68 abstract updateTotp(form: types.UpdateTotp): Promise<types.UpdateTotpResponse>
69 abstract getModlog(form: types.GetModlog): Promise<types.GetModlogResponse>
70 abstract search(form: types.Search): Promise<types.SearchResponse>
71 abstract resolveObject(
72 form: types.ResolveObject,
73 ): Promise<types.ResolveObjectResponse>
74 abstract createCommunity(
75 form: types.CreateCommunity,
76 ): Promise<types.CommunityResponse>
77 abstract getCommunity(
78 form: types.GetCommunity,
79 ): Promise<types.GetCommunityResponse>
80 abstract editCommunity(
81 form: types.EditCommunity,
82 ): Promise<types.CommunityResponse>
83 abstract listCommunities(
84 form: types.ListCommunities,
85 ): Promise<types.ListCommunitiesResponse>
86 abstract followCommunity(
87 form: types.FollowCommunity,
88 ): Promise<types.CommunityResponse>
89 abstract blockCommunity(
90 form: types.BlockCommunity,
91 ): Promise<types.BlockCommunityResponse>
92 abstract deleteCommunity(
93 form: types.DeleteCommunity,
94 ): Promise<types.CommunityResponse>
95 abstract hideCommunity(
96 form: types.HideCommunity,
97 ): Promise<types.SuccessResponse>
98 abstract removeCommunity(
99 form: types.RemoveCommunity,
100 ): Promise<types.CommunityResponse>
101 abstract banFromCommunity(
102 form: types.BanFromCommunity,
103 ): Promise<types.BanFromCommunityResponse>
104 abstract addModToCommunity(
105 form: types.AddModToCommunity,
106 ): Promise<types.AddModToCommunityResponse>
107 abstract createPost(form: types.CreatePost): Promise<types.PostResponse>
108 abstract getPost(form: types.GetPost): Promise<types.GetPostResponse>
109 abstract editPost(form: types.EditPost): Promise<types.PostResponse>
110 abstract deletePost(form: types.DeletePost): Promise<types.PostResponse>
111 abstract removePost(form: types.RemovePost): Promise<types.PostResponse>
112 abstract markPostAsRead(
113 form: types.MarkPostAsRead,
114 ): Promise<types.SuccessResponse>
115 abstract hidePost(form: types.HidePost): Promise<types.SuccessResponse>
116 abstract lockPost(form: types.LockPost): Promise<types.PostResponse>
117 abstract featurePost(form: types.FeaturePost): Promise<types.PostResponse>
118 abstract getPosts(form: types.GetPosts): Promise<types.GetPostsResponse>
119 abstract likePost(form: types.CreatePostLike): Promise<types.PostResponse>
120 abstract listPostLikes(
121 form: types.ListPostLikes,
122 ): Promise<types.ListPostLikesResponse>
123 abstract savePost(form: types.SavePost): Promise<types.PostResponse>
124 // TODO should unify these reports probably
125 abstract createPostReport(
126 form: types.CreatePostReport,
127 ): Promise<types.PostReportResponse>
128 abstract resolvePostReport(
129 form: types.ResolvePostReport,
130 ): Promise<types.PostReportResponse>
131 abstract listPostReports(
132 form: types.ListPostReports,
133 ): Promise<types.ListPostReportsResponse>
134 abstract getSiteMetadata(
135 form: types.GetSiteMetadata,
136 ): Promise<types.GetSiteMetadataResponse>
137 abstract createComment(
138 form: types.CreateComment,
139 ): Promise<types.CommentResponse>
140 abstract editComment(form: types.EditComment): Promise<types.CommentResponse>
141 abstract deleteComment(
142 form: types.DeleteComment,
143 ): Promise<types.CommentResponse>
144 abstract removeComment(
145 form: types.RemoveComment,
146 ): Promise<types.CommentResponse>
147 abstract markCommentReplyAsRead(
148 form: types.MarkCommentReplyAsRead,
149 ): Promise<types.CommentReplyResponse | void>
150 abstract likeComment(
151 form: types.CreateCommentLike,
152 ): Promise<types.CommentResponse>
153 abstract listCommentLikes(
154 form: types.ListCommentLikes,
155 ): Promise<types.ListCommentLikesResponse>
156 // TODO unify saving with posts too
157 // i have options for unifying these since I'm making the API client myself now
158 abstract saveComment(form: types.SaveComment): Promise<types.CommentResponse>
159 abstract distinguishComment(
160 form: types.DistinguishComment,
161 ): Promise<types.CommentResponse>
162 abstract getComments(
163 form: types.GetComments,
164 ): Promise<types.GetCommentsResponse>
165 abstract getComment(form: types.GetComment): Promise<types.CommentResponse>
166 abstract createCommentReport(
167 form: types.CreateCommentReport,
168 ): Promise<types.CommentReportResponse>
169 abstract resolveCommentReport(
170 form: types.ResolveCommentReport,
171 ): Promise<types.CommentReportResponse>
172 abstract listCommentReports(
173 form: types.ListCommentReports,
174 ): Promise<types.ListCommentReportsResponse>
175 abstract getPrivateMessages(
176 form: types.GetPrivateMessages,
177 ): Promise<types.PrivateMessagesResponse>
178 abstract createPrivateMessage(
179 form: types.CreatePrivateMessage,
180 ): Promise<types.PrivateMessageResponse>
181 abstract editPrivateMessage(
182 form: types.EditPrivateMessage,
183 ): Promise<types.PrivateMessageResponse>
184 abstract deletePrivateMessage(
185 form: types.DeletePrivateMessage,
186 ): Promise<types.PrivateMessageResponse>
187 abstract markPrivateMessageAsRead(
188 form: types.MarkPrivateMessageAsRead,
189 ): Promise<types.PrivateMessageResponse>
190 abstract createPrivateMessageReport(
191 form: types.CreatePrivateMessageReport,
192 ): Promise<types.PrivateMessageReportResponse>
193 abstract resolvePrivateMessageReport(
194 form: types.ResolvePrivateMessageReport,
195 ): Promise<types.PrivateMessageReportResponse>
196 abstract listPrivateMessageReports(
197 form: types.ListPrivateMessageReports,
198 ): Promise<types.ListPrivateMessageReportsResponse>
199 abstract register(form: types.Register): Promise<types.LoginResponse>
200 abstract login(form: types.Login): Promise<types.LoginResponse>
201 abstract logout(): Promise<types.SuccessResponse>
202 abstract getPersonDetails(
203 form: types.GetPersonDetails,
204 ): Promise<types.GetPersonDetailsResponse>
205 // TODO unify these inbox things too
206 abstract getPersonMentions(
207 form: types.GetPersonMentions,
208 ): Promise<types.GetPersonMentionsResponse>
209 abstract markPersonMentionAsRead(
210 form: types.MarkPersonMentionAsRead,
211 ): Promise<types.PersonMentionResponse | void>
212 abstract getReplies(form: types.GetReplies): Promise<types.GetRepliesResponse>
213 abstract banPerson(form: types.BanPerson): Promise<types.BanPersonResponse>
214 abstract getBannedPersons(): Promise<types.BannedPersonsResponse>
215 abstract blockPerson(
216 form: types.BlockPerson,
217 ): Promise<types.BlockPersonResponse>
218 abstract getCaptcha(): Promise<types.GetCaptchaResponse>
219 abstract deleteAccount(
220 form: types.DeleteAccount,
221 ): Promise<types.SuccessResponse>
222 abstract passwordReset(
223 form: types.PasswordReset,
224 ): Promise<types.SuccessResponse>
225 abstract passwordChangeAfterReset(
226 form: types.PasswordChangeAfterReset,
227 ): Promise<types.SuccessResponse>
228 abstract markAllAsRead(): Promise<types.GetRepliesResponse>
229 abstract saveUserSettings(
230 form: types.SaveUserSettings,
231 ): Promise<types.SuccessResponse>
232 abstract changePassword(
233 form: types.ChangePassword,
234 ): Promise<types.LoginResponse>
235 abstract getReportCount(
236 form: types.GetReportCount,
237 ): Promise<types.GetReportCountResponse>
238 abstract getUnreadCount(): Promise<types.GetUnreadCountResponse>
239 abstract verifyEmail(form: types.VerifyEmail): Promise<types.SuccessResponse>
240 abstract addAdmin(form: types.AddAdmin): Promise<types.AddAdminResponse>
241 abstract getUnreadRegistrationApplicationCount(): Promise<types.GetUnreadRegistrationApplicationCountResponse>
242 abstract listRegistrationApplications(
243 form: types.ListRegistrationApplications,
244 ): Promise<types.ListRegistrationApplicationsResponse>
245 abstract approveRegistrationApplication(
246 form: types.ApproveRegistrationApplication,
247 ): Promise<types.RegistrationApplicationResponse>
248 abstract getRegistrationApplication(
249 form: types.GetRegistrationApplication,
250 ): Promise<types.RegistrationApplicationResponse>
251 abstract purgePerson(form: types.PurgePerson): Promise<types.SuccessResponse>
252 abstract purgeCommunity(
253 form: types.PurgeCommunity,
254 ): Promise<types.SuccessResponse>
255 abstract purgePost(form: types.PurgePost): Promise<types.SuccessResponse>
256 abstract purgeComment(
257 form: types.PurgeComment,
258 ): Promise<types.SuccessResponse>
259 abstract getFederatedInstances(): Promise<types.GetFederatedInstancesResponse>
260 abstract blockInstance(
261 form: types.BlockInstance,
262 ): Promise<types.BlockInstanceResponse>
263 abstract uploadImage(
264 form: types.UploadImage,
265 ): Promise<types.UploadImageResponse>
266 abstract deleteImage(form: types.DeleteImage): Promise<boolean>
267 abstract setFlair?(form: types.SetPersonFlair): Promise<types.PersonView>
268 abstract getFeeds?(form: types.GetFeeds): Promise<types.GetFeedsResponse>
269 abstract getTopics?(form: types.GetTopics): Promise<types.GetTopicsResponse>
270 abstract assignFlair?(form: types.AssignFlair): Promise<types.PostView>
271 abstract listMedia(form: types.ListMedia): Promise<types.ListMediaResponse>
272 abstract voteOnPoll?(form: types.PollVote): Promise<types.PostView>
273 abstract setNote?(form: types.SetNote): Promise<types.PersonView>
274}
275
276export type NullableFnArg<T, Fallback = never> = T extends (
277 ...args: infer A
278) => any
279 ? A
280 : Fallback
281
282export type NullableFnReturn<T, Fallback = never> = T extends (
283 ...args: any[]
284) => infer A
285 ? A
286 : Fallback