impr: restructure src/lib

This commit is contained in:
Xylight committed 2025-09-24 17:12:19 -07:00
1 parent d85b889409
commit 75f907743e
376 files changed
+1000 -1033

No files matched your search

+242
View File
@@ -0,0 +1,242 @@
import { env } from '$env/dynamic/public'
import * as types from './types'
export type ClientType =
| { name: 'lemmy'; baseUrl: '/api/v3' }
| { name: 'piefed'; baseUrl: '/api/alpha' }
export const DEFAULT_CLIENT_TYPE: ClientType =
env.PUBLIC_INSTANCE_TYPE == 'piefedalpha'
? {
name: 'piefed',
baseUrl: '/api/alpha',
}
: {
name: 'lemmy',
baseUrl: '/api/v3',
}
export abstract class BaseClient {
abstract type: ClientType
abstract getSite(): Promise<types.GetSiteResponse>
abstract editSite(form: types.EditSite): Promise<types.SiteResponse>
abstract generateTotpSecret(): Promise<types.GenerateTotpSecretResponse>
abstract listLogins(): Promise<types.LoginToken[]>
abstract listAllMedia(form: types.ListMedia): Promise<types.ListMediaResponse>
abstract updateTotp(form: types.UpdateTotp): Promise<types.UpdateTotpResponse>
abstract getModlog(form: types.GetModlog): Promise<types.GetModlogResponse>
abstract search(form: types.Search): Promise<types.SearchResponse>
abstract resolveObject(
form: types.ResolveObject,
): Promise<types.ResolveObjectResponse>
abstract createCommunity(
form: types.CreateCommunity,
): Promise<types.CommunityResponse>
abstract getCommunity(
form: types.GetCommunity,
): Promise<types.GetCommunityResponse>
abstract editCommunity(
form: types.EditCommunity,
): Promise<types.CommunityResponse>
abstract listCommunities(
form: types.ListCommunities,
): Promise<types.ListCommunitiesResponse>
abstract followCommunity(
form: types.FollowCommunity,
): Promise<types.CommunityResponse>
abstract blockCommunity(
form: types.BlockCommunity,
): Promise<types.BlockCommunityResponse>
abstract deleteCommunity(
form: types.DeleteCommunity,
): Promise<types.CommunityResponse>
abstract hideCommunity(
form: types.HideCommunity,
): Promise<types.SuccessResponse>
abstract removeCommunity(
form: types.RemoveCommunity,
): Promise<types.CommunityResponse>
abstract banFromCommunity(
form: types.BanFromCommunity,
): Promise<types.BanFromCommunityResponse>
abstract addModToCommunity(
form: types.AddModToCommunity,
): Promise<types.AddModToCommunityResponse>
abstract createPost(form: types.CreatePost): Promise<types.PostResponse>
abstract getPost(form: types.GetPost): Promise<types.GetPostResponse>
abstract editPost(form: types.EditPost): Promise<types.PostResponse>
abstract deletePost(form: types.DeletePost): Promise<types.PostResponse>
abstract removePost(form: types.RemovePost): Promise<types.PostResponse>
abstract markPostAsRead(
form: types.MarkPostAsRead,
): Promise<types.SuccessResponse>
abstract hidePost(form: types.HidePost): Promise<types.SuccessResponse>
abstract lockPost(form: types.LockPost): Promise<types.PostResponse>
abstract featurePost(form: types.FeaturePost): Promise<types.PostResponse>
abstract getPosts(form: types.GetPosts): Promise<types.GetPostsResponse>
abstract likePost(form: types.CreatePostLike): Promise<types.PostResponse>
abstract listPostLikes(
form: types.ListPostLikes,
): Promise<types.ListPostLikesResponse>
abstract savePost(form: types.SavePost): Promise<types.PostResponse>
// TODO should unify these reports probably
abstract createPostReport(
form: types.CreatePostReport,
): Promise<types.PostReportResponse>
abstract resolvePostReport(
form: types.ResolvePostReport,
): Promise<types.PostReportResponse>
abstract listPostReports(
form: types.ListPostReports,
): Promise<types.ListPostReportsResponse>
abstract getSiteMetadata(
form: types.GetSiteMetadata,
): Promise<types.GetSiteMetadataResponse>
abstract createComment(
form: types.CreateComment,
): Promise<types.CommentResponse>
abstract editComment(form: types.EditComment): Promise<types.CommentResponse>
abstract deleteComment(
form: types.DeleteComment,
): Promise<types.CommentResponse>
abstract removeComment(
form: types.RemoveComment,
): Promise<types.CommentResponse>
abstract markCommentReplyAsRead(
form: types.MarkCommentReplyAsRead,
): Promise<types.CommentReplyResponse | void>
abstract likeComment(
form: types.CreateCommentLike,
): Promise<types.CommentResponse>
abstract listCommentLikes(
form: types.ListCommentLikes,
): Promise<types.ListCommentLikesResponse>
// TODO unify saving with posts too
// i have options for unifying these since I'm making the API client myself now
abstract saveComment(form: types.SaveComment): Promise<types.CommentResponse>
abstract distinguishComment(
form: types.DistinguishComment,
): Promise<types.CommentResponse>
abstract getComments(
form: types.GetComments,
): Promise<types.GetCommentsResponse>
abstract getComment(form: types.GetComment): Promise<types.CommentResponse>
abstract createCommentReport(
form: types.CreateCommentReport,
): Promise<types.CommentReportResponse>
abstract resolveCommentReport(
form: types.ResolveCommentReport,
): Promise<types.CommentReportResponse>
abstract listCommentReports(
form: types.ListCommentReports,
): Promise<types.ListCommentReportsResponse>
abstract getPrivateMessages(
form: types.GetPrivateMessages,
): Promise<types.PrivateMessagesResponse>
abstract createPrivateMessage(
form: types.CreatePrivateMessage,
): Promise<types.PrivateMessageResponse>
abstract editPrivateMessage(
form: types.EditPrivateMessage,
): Promise<types.PrivateMessageResponse>
abstract deletePrivateMessage(
form: types.DeletePrivateMessage,
): Promise<types.PrivateMessageResponse>
abstract markPrivateMessageAsRead(
form: types.MarkPrivateMessageAsRead,
): Promise<types.PrivateMessageResponse>
abstract createPrivateMessageReport(
form: types.CreatePrivateMessageReport,
): Promise<types.PrivateMessageReportResponse>
abstract resolvePrivateMessageReport(
form: types.ResolvePrivateMessageReport,
): Promise<types.PrivateMessageReportResponse>
abstract listPrivateMessageReports(
form: types.ListPrivateMessageReports,
): Promise<types.ListPrivateMessageReportsResponse>
abstract register(form: types.Register): Promise<types.LoginResponse>
abstract login(form: types.Login): Promise<types.LoginResponse>
abstract logout(): Promise<types.SuccessResponse>
abstract getPersonDetails(
form: types.GetPersonDetails,
): Promise<types.GetPersonDetailsResponse>
// TODO unify these inbox things too
abstract getPersonMentions(
form: types.GetPersonMentions,
): Promise<types.GetPersonMentionsResponse>
abstract markPersonMentionAsRead(
form: types.MarkPersonMentionAsRead,
): Promise<types.PersonMentionResponse | void>
abstract getReplies(form: types.GetReplies): Promise<types.GetRepliesResponse>
abstract banPerson(form: types.BanPerson): Promise<types.BanPersonResponse>
abstract getBannedPersons(): Promise<types.BannedPersonsResponse>
abstract blockPerson(
form: types.BlockPerson,
): Promise<types.BlockPersonResponse>
abstract getCaptcha(): Promise<types.GetCaptchaResponse>
abstract deleteAccount(
form: types.DeleteAccount,
): Promise<types.SuccessResponse>
abstract passwordReset(
form: types.PasswordReset,
): Promise<types.SuccessResponse>
abstract passwordChangeAfterReset(
form: types.PasswordChangeAfterReset,
): Promise<types.SuccessResponse>
abstract markAllAsRead(): Promise<types.GetRepliesResponse>
abstract saveUserSettings(
form: types.SaveUserSettings,
): Promise<types.SuccessResponse>
abstract changePassword(
form: types.ChangePassword,
): Promise<types.LoginResponse>
abstract getReportCount(
form: types.GetReportCount,
): Promise<types.GetReportCountResponse>
abstract getUnreadCount(): Promise<types.GetUnreadCountResponse>
abstract verifyEmail(form: types.VerifyEmail): Promise<types.SuccessResponse>
abstract addAdmin(form: types.AddAdmin): Promise<types.AddAdminResponse>
abstract getUnreadRegistrationApplicationCount(): Promise<types.GetUnreadRegistrationApplicationCountResponse>
abstract listRegistrationApplications(
form: types.ListRegistrationApplications,
): Promise<types.ListRegistrationApplicationsResponse>
abstract approveRegistrationApplication(
form: types.ApproveRegistrationApplication,
): Promise<types.RegistrationApplicationResponse>
abstract getRegistrationApplication(
form: types.GetRegistrationApplication,
): Promise<types.RegistrationApplicationResponse>
abstract purgePerson(form: types.PurgePerson): Promise<types.SuccessResponse>
abstract purgeCommunity(
form: types.PurgeCommunity,
): Promise<types.SuccessResponse>
abstract purgePost(form: types.PurgePost): Promise<types.SuccessResponse>
abstract purgeComment(
form: types.PurgeComment,
): Promise<types.SuccessResponse>
abstract getFederatedInstances(): Promise<types.GetFederatedInstancesResponse>
abstract blockInstance(
form: types.BlockInstance,
): Promise<types.BlockInstanceResponse>
abstract uploadImage(
form: types.UploadImage,
): Promise<types.UploadImageResponse>
abstract deleteImage(form: types.DeleteImage): Promise<boolean>
abstract setFlair?(form: types.SetPersonFlair): Promise<types.PersonView>
abstract getFeeds?(form: types.GetFeeds): Promise<types.GetFeedsResponse>
abstract getTopics?(form: types.GetTopics): Promise<types.GetTopicsResponse>
abstract assignFlair?(form: types.AssignFlair): Promise<types.PostView>
}
export type NullableFnArg<T, Fallback = never> = T extends (
...args: infer A
) => any
? A
: Fallback
export type NullableFnReturn<T, Fallback = never> = T extends (
...args: any[]
) => infer A
? A
: Fallback
+121
View File
@@ -0,0 +1,121 @@
import { profile } from '$lib/app/auth.svelte'
import { DEFAULT_INSTANCE_URL } from '$lib/instance.svelte.js'
import { instanceToURL } from '$lib/util.svelte'
import { error } from '@sveltejs/kit'
import { type GetSiteResponse } from '$lib/client/types'
import { LemmyClient } from './lemmy/adapter'
import { PiefedClient } from './piefed/adapter'
import { DEFAULT_CLIENT_TYPE, type ClientType, BaseClient } from './base'
class SiteData {
#data = $state<GetSiteResponse>()
get data() {
return this.#data
}
set data(value) {
this.#data = value
}
}
export const site = new SiteData()
async function customFetch(
func:
| ((
input: RequestInfo | URL,
init?: RequestInit | undefined,
) => Promise<Response>)
| undefined,
input: RequestInfo | URL,
init?: RequestInit | undefined,
auth?: string,
): Promise<Response> {
const f = func ? func : fetch
if (init) {
init.headers = {
...init.headers,
'User-Agent': `Photon/${__VERSION__}`,
...(auth ? { authorization: `Bearer ${auth}` } : {}),
}
if (auth) {
init.cache = 'no-store'
}
}
const res = await f(input, init)
if (!res.ok) error(res.status, await res.text())
return res
}
export function client({
instanceURL,
func,
auth,
clientType,
}: {
instanceURL?: string
func?: (
input: RequestInfo | URL,
init?: RequestInit | undefined,
) => Promise<Response>
auth?: string
clientType?: ClientType
} = {}): BaseClient {
if (!instanceURL)
instanceURL = profile.current.instance || DEFAULT_INSTANCE_URL
if (!clientType) {
clientType = profile.current.client ?? DEFAULT_CLIENT_TYPE
}
// we use nullish coealsiaihsa something so that
// we can set auth = '' to remove it
const jwt = auth ?? profile.current?.jwt
// but not here, so that if jwt == '', it doesnt put a bearer
const headers = jwt ? { authorization: `Bearer ${jwt}` } : {}
return new (clientType.name == 'piefed' ? PiefedClient : LemmyClient)(
instanceToURL(instanceURL),
{
fetchFunction: (input, init) => customFetch(func, input, init, jwt),
headers: headers,
},
)
}
// here for parts where i forgor to switch
export function getClient(
instanceURL?: string,
func?: (
input: RequestInfo | URL,
init?: RequestInit | undefined,
) => Promise<Response>,
auth?: string,
) {
return client({ instanceURL, func, auth })
}
export async function validateInstance(
instance: string,
type: ClientType,
): Promise<boolean> {
if (instance == '') return false
try {
await client({
instanceURL: instance,
clientType: type,
auth: '',
}).getSite()
return true
} catch {
return false
}
}
+489
View File
@@ -0,0 +1,489 @@
import { LemmyHttp } from 'lemmy-js-client'
import type { BaseClient, ClientType } from '../base'
import { fromGetPosts, toListingType } from './rewrite'
export class LemmyClient implements BaseClient {
type: ClientType = { name: 'lemmy', baseUrl: '/api/v3' }
#client: LemmyHttp
constructor(
baseUrl: string,
args: {
fetchFunction: (input: any, init: any) => Promise<any>
headers: any
},
) {
this.#client = new LemmyHttp(baseUrl, args)
}
async getSite(
...params: Parameters<BaseClient['getSite']>
): ReturnType<BaseClient['getSite']> {
return await this.#client.getSite(...params)
}
async generateTotpSecret(
...params: Parameters<BaseClient['generateTotpSecret']>
): ReturnType<BaseClient['generateTotpSecret']> {
return await this.#client.generateTotpSecret(...params)
}
async listLogins(
...params: Parameters<BaseClient['listLogins']>
): ReturnType<BaseClient['listLogins']> {
return await this.#client.listLogins(...params)
}
async listAllMedia(
...params: Parameters<BaseClient['listAllMedia']>
): ReturnType<BaseClient['listAllMedia']> {
return await this.#client.listAllMedia(...params)
}
async updateTotp(
...params: Parameters<BaseClient['updateTotp']>
): ReturnType<BaseClient['updateTotp']> {
return await this.#client.updateTotp(...params)
}
async getModlog(
...params: Parameters<BaseClient['getModlog']>
): ReturnType<BaseClient['getModlog']> {
return await this.#client.getModlog(...params)
}
async search(
params: Parameters<BaseClient['search']>[0],
): ReturnType<BaseClient['search']> {
return await this.#client.search({
...params,
listing_type: toListingType(params.listing_type),
})
}
async resolveObject(
...params: Parameters<BaseClient['resolveObject']>
): ReturnType<BaseClient['resolveObject']> {
return await this.#client.resolveObject(...params)
}
async createCommunity(
...params: Parameters<BaseClient['createCommunity']>
): ReturnType<BaseClient['createCommunity']> {
return await this.#client.createCommunity(...params)
}
async getCommunity(
...params: Parameters<BaseClient['getCommunity']>
): ReturnType<BaseClient['getCommunity']> {
return await this.#client.getCommunity(...params)
}
async editCommunity(
...params: Parameters<BaseClient['editCommunity']>
): ReturnType<BaseClient['editCommunity']> {
return await this.#client.editCommunity(...params)
}
async listCommunities(
params: Parameters<BaseClient['listCommunities']>[0],
): ReturnType<BaseClient['listCommunities']> {
return await this.#client.listCommunities({
...params,
type_: toListingType(params.type_),
})
}
async followCommunity(
...params: Parameters<BaseClient['followCommunity']>
): ReturnType<BaseClient['followCommunity']> {
return await this.#client.followCommunity(...params)
}
async blockCommunity(
...params: Parameters<BaseClient['blockCommunity']>
): ReturnType<BaseClient['blockCommunity']> {
return await this.#client.blockCommunity(...params)
}
async deleteCommunity(
...params: Parameters<BaseClient['deleteCommunity']>
): ReturnType<BaseClient['deleteCommunity']> {
return await this.#client.deleteCommunity(...params)
}
async hideCommunity(
...params: Parameters<BaseClient['hideCommunity']>
): ReturnType<BaseClient['hideCommunity']> {
return await this.#client.hideCommunity(...params)
}
async removeCommunity(
...params: Parameters<BaseClient['removeCommunity']>
): ReturnType<BaseClient['removeCommunity']> {
return await this.#client.removeCommunity(...params)
}
async banFromCommunity(
...params: Parameters<BaseClient['banFromCommunity']>
): ReturnType<BaseClient['banFromCommunity']> {
return await this.#client.banFromCommunity(...params)
}
async addModToCommunity(
...params: Parameters<BaseClient['addModToCommunity']>
): ReturnType<BaseClient['addModToCommunity']> {
return await this.#client.addModToCommunity(...params)
}
async createPost(
...params: Parameters<BaseClient['createPost']>
): ReturnType<BaseClient['createPost']> {
return await this.#client.createPost(...params)
}
async getPost(
...params: Parameters<BaseClient['getPost']>
): ReturnType<BaseClient['getPost']> {
return await this.#client.getPost(...params)
}
async editPost(
...params: Parameters<BaseClient['editPost']>
): ReturnType<BaseClient['editPost']> {
return await this.#client.editPost(...params)
}
async deletePost(
...params: Parameters<BaseClient['deletePost']>
): ReturnType<BaseClient['deletePost']> {
return await this.#client.deletePost(...params)
}
async removePost(
...params: Parameters<BaseClient['removePost']>
): ReturnType<BaseClient['removePost']> {
return await this.#client.removePost(...params)
}
async markPostAsRead(
...params: Parameters<BaseClient['markPostAsRead']>
): ReturnType<BaseClient['markPostAsRead']> {
return await this.#client.markPostAsRead(...params)
}
async hidePost(
...params: Parameters<BaseClient['hidePost']>
): ReturnType<BaseClient['hidePost']> {
return await this.#client.hidePost(...params)
}
async lockPost(
...params: Parameters<BaseClient['lockPost']>
): ReturnType<BaseClient['lockPost']> {
return await this.#client.lockPost(...params)
}
async featurePost(
...params: Parameters<BaseClient['featurePost']>
): ReturnType<BaseClient['featurePost']> {
return await this.#client.featurePost(...params)
}
async getPosts(
params: Parameters<BaseClient['getPosts']>[0],
): ReturnType<BaseClient['getPosts']> {
return await this.#client.getPosts(fromGetPosts(params))
}
async likePost(
...params: Parameters<BaseClient['likePost']>
): ReturnType<BaseClient['likePost']> {
return await this.#client.likePost(...params)
}
async listPostLikes(
...params: Parameters<BaseClient['listPostLikes']>
): ReturnType<BaseClient['listPostLikes']> {
return await this.#client.listPostLikes(...params)
}
async savePost(
...params: Parameters<BaseClient['savePost']>
): ReturnType<BaseClient['savePost']> {
return await this.#client.savePost(...params)
}
async createPostReport(
...params: Parameters<BaseClient['createPostReport']>
): ReturnType<BaseClient['createPostReport']> {
return await this.#client.createPostReport(...params)
}
async resolvePostReport(
...params: Parameters<BaseClient['resolvePostReport']>
): ReturnType<BaseClient['resolvePostReport']> {
return await this.#client.resolvePostReport(...params)
}
async listPostReports(
...params: Parameters<BaseClient['listPostReports']>
): ReturnType<BaseClient['listPostReports']> {
return await this.#client.listPostReports(...params)
}
async getSiteMetadata(
...params: Parameters<BaseClient['getSiteMetadata']>
): ReturnType<BaseClient['getSiteMetadata']> {
return await this.#client.getSiteMetadata(...params)
}
async createComment(
...params: Parameters<BaseClient['createComment']>
): ReturnType<BaseClient['createComment']> {
return await this.#client.createComment(...params)
}
async editComment(
...params: Parameters<BaseClient['editComment']>
): ReturnType<BaseClient['editComment']> {
return await this.#client.editComment(...params)
}
async deleteComment(
...params: Parameters<BaseClient['deleteComment']>
): ReturnType<BaseClient['deleteComment']> {
return await this.#client.deleteComment(...params)
}
async removeComment(
...params: Parameters<BaseClient['removeComment']>
): ReturnType<BaseClient['removeComment']> {
return await this.#client.removeComment(...params)
}
async markCommentReplyAsRead(
...params: Parameters<BaseClient['markCommentReplyAsRead']>
): ReturnType<BaseClient['markCommentReplyAsRead']> {
return await this.#client.markCommentReplyAsRead(...params)
}
async likeComment(
...params: Parameters<BaseClient['likeComment']>
): ReturnType<BaseClient['likeComment']> {
return await this.#client.likeComment(...params)
}
async listCommentLikes(
...params: Parameters<BaseClient['listCommentLikes']>
): ReturnType<BaseClient['listCommentLikes']> {
return await this.#client.listCommentLikes(...params)
}
async saveComment(
...params: Parameters<BaseClient['saveComment']>
): ReturnType<BaseClient['saveComment']> {
return await this.#client.saveComment(...params)
}
async distinguishComment(
...params: Parameters<BaseClient['distinguishComment']>
): ReturnType<BaseClient['distinguishComment']> {
return await this.#client.distinguishComment(...params)
}
async getComments(
params: Parameters<BaseClient['getComments']>[0],
): ReturnType<BaseClient['getComments']> {
return await this.#client.getComments({
...params,
type_: toListingType(params.type_),
})
}
async getComment(
...params: Parameters<BaseClient['getComment']>
): ReturnType<BaseClient['getComment']> {
return await this.#client.getComment(...params)
}
async createCommentReport(
...params: Parameters<BaseClient['createCommentReport']>
): ReturnType<BaseClient['createCommentReport']> {
return await this.#client.createCommentReport(...params)
}
async resolveCommentReport(
...params: Parameters<BaseClient['resolveCommentReport']>
): ReturnType<BaseClient['resolveCommentReport']> {
return await this.#client.resolveCommentReport(...params)
}
async listCommentReports(
...params: Parameters<BaseClient['listCommentReports']>
): ReturnType<BaseClient['listCommentReports']> {
return await this.#client.listCommentReports(...params)
}
async getPrivateMessages(
...params: Parameters<BaseClient['getPrivateMessages']>
): ReturnType<BaseClient['getPrivateMessages']> {
return await this.#client.getPrivateMessages(...params)
}
async createPrivateMessage(
...params: Parameters<BaseClient['createPrivateMessage']>
): ReturnType<BaseClient['createPrivateMessage']> {
return await this.#client.createPrivateMessage(...params)
}
async editPrivateMessage(
...params: Parameters<BaseClient['editPrivateMessage']>
): ReturnType<BaseClient['editPrivateMessage']> {
return await this.#client.editPrivateMessage(...params)
}
async deletePrivateMessage(
...params: Parameters<BaseClient['deletePrivateMessage']>
): ReturnType<BaseClient['deletePrivateMessage']> {
return await this.#client.deletePrivateMessage(...params)
}
async markPrivateMessageAsRead(
...params: Parameters<BaseClient['markPrivateMessageAsRead']>
): ReturnType<BaseClient['markPrivateMessageAsRead']> {
return await this.#client.markPrivateMessageAsRead(...params)
}
async createPrivateMessageReport(
...params: Parameters<BaseClient['createPrivateMessageReport']>
): ReturnType<BaseClient['createPrivateMessageReport']> {
return await this.#client.createPrivateMessageReport(...params)
}
async resolvePrivateMessageReport(
...params: Parameters<BaseClient['resolvePrivateMessageReport']>
): ReturnType<BaseClient['resolvePrivateMessageReport']> {
return await this.#client.resolvePrivateMessageReport(...params)
}
async listPrivateMessageReports(
...params: Parameters<BaseClient['listPrivateMessageReports']>
): ReturnType<BaseClient['listPrivateMessageReports']> {
return await this.#client.listPrivateMessageReports(...params)
}
async register(
...params: Parameters<BaseClient['register']>
): ReturnType<BaseClient['register']> {
return await this.#client.register(...params)
}
async login(
...params: Parameters<BaseClient['login']>
): ReturnType<BaseClient['login']> {
return await this.#client.login(...params)
}
async logout(
...params: Parameters<BaseClient['logout']>
): ReturnType<BaseClient['logout']> {
return await this.#client.logout(...params)
}
async getPersonDetails(
...params: Parameters<BaseClient['getPersonDetails']>
): ReturnType<BaseClient['getPersonDetails']> {
return await this.#client.getPersonDetails(...params)
}
async getPersonMentions(
...params: Parameters<BaseClient['getPersonMentions']>
): ReturnType<BaseClient['getPersonMentions']> {
return await this.#client.getPersonMentions(...params)
}
async markPersonMentionAsRead(
...params: Parameters<BaseClient['markPersonMentionAsRead']>
): ReturnType<BaseClient['markPersonMentionAsRead']> {
return await this.#client.markPersonMentionAsRead(...params)
}
async getReplies(
...params: Parameters<BaseClient['getReplies']>
): ReturnType<BaseClient['getReplies']> {
return await this.#client.getReplies(...params)
}
async banPerson(
...params: Parameters<BaseClient['banPerson']>
): ReturnType<BaseClient['banPerson']> {
return await this.#client.banPerson(...params)
}
async getBannedPersons(
...params: Parameters<BaseClient['getBannedPersons']>
): ReturnType<BaseClient['getBannedPersons']> {
return await this.#client.getBannedPersons(...params)
}
async blockPerson(
...params: Parameters<BaseClient['blockPerson']>
): ReturnType<BaseClient['blockPerson']> {
return await this.#client.blockPerson(...params)
}
async getCaptcha(
...params: Parameters<BaseClient['getCaptcha']>
): ReturnType<BaseClient['getCaptcha']> {
return await this.#client.getCaptcha(...params)
}
async deleteAccount(
...params: Parameters<BaseClient['deleteAccount']>
): ReturnType<BaseClient['deleteAccount']> {
return await this.#client.deleteAccount(...params)
}
async passwordReset(
...params: Parameters<BaseClient['passwordReset']>
): ReturnType<BaseClient['passwordReset']> {
return await this.#client.passwordReset(...params)
}
async passwordChangeAfterReset(
...params: Parameters<BaseClient['passwordChangeAfterReset']>
): ReturnType<BaseClient['passwordChangeAfterReset']> {
return await this.#client.passwordChangeAfterReset(...params)
}
async markAllAsRead(
...params: Parameters<BaseClient['markAllAsRead']>
): ReturnType<BaseClient['markAllAsRead']> {
return await this.#client.markAllAsRead(...params)
}
async saveUserSettings(
...params: Parameters<BaseClient['saveUserSettings']>
): ReturnType<BaseClient['saveUserSettings']> {
return await this.#client.saveUserSettings(...params)
}
async changePassword(
...params: Parameters<BaseClient['changePassword']>
): ReturnType<BaseClient['changePassword']> {
return await this.#client.changePassword(...params)
}
async getReportCount(
...params: Parameters<BaseClient['getReportCount']>
): ReturnType<BaseClient['getReportCount']> {
return await this.#client.getReportCount(...params)
}
async getUnreadCount(
...params: Parameters<BaseClient['getUnreadCount']>
): ReturnType<BaseClient['getUnreadCount']> {
return await this.#client.getUnreadCount(...params)
}
async verifyEmail(
...params: Parameters<BaseClient['verifyEmail']>
): ReturnType<BaseClient['verifyEmail']> {
return await this.#client.verifyEmail(...params)
}
async addAdmin(
...params: Parameters<BaseClient['addAdmin']>
): ReturnType<BaseClient['addAdmin']> {
return await this.#client.addAdmin(...params)
}
async getUnreadRegistrationApplicationCount(
...params: Parameters<BaseClient['getUnreadRegistrationApplicationCount']>
): ReturnType<BaseClient['getUnreadRegistrationApplicationCount']> {
return await this.#client.getUnreadRegistrationApplicationCount(...params)
}
async listRegistrationApplications(
...params: Parameters<BaseClient['listRegistrationApplications']>
): ReturnType<BaseClient['listRegistrationApplications']> {
return await this.#client.listRegistrationApplications(...params)
}
async approveRegistrationApplication(
...params: Parameters<BaseClient['approveRegistrationApplication']>
): ReturnType<BaseClient['approveRegistrationApplication']> {
return await this.#client.approveRegistrationApplication(...params)
}
async getRegistrationApplication(
...params: Parameters<BaseClient['getRegistrationApplication']>
): ReturnType<BaseClient['getRegistrationApplication']> {
return await this.#client.getRegistrationApplication(...params)
}
async purgePerson(
...params: Parameters<BaseClient['purgePerson']>
): ReturnType<BaseClient['purgePerson']> {
return await this.#client.purgePerson(...params)
}
async purgeCommunity(
...params: Parameters<BaseClient['purgeCommunity']>
): ReturnType<BaseClient['purgeCommunity']> {
return await this.#client.purgeCommunity(...params)
}
async purgePost(
...params: Parameters<BaseClient['purgePost']>
): ReturnType<BaseClient['purgePost']> {
return await this.#client.purgePost(...params)
}
async purgeComment(
...params: Parameters<BaseClient['purgeComment']>
): ReturnType<BaseClient['purgeComment']> {
return await this.#client.purgeComment(...params)
}
async getFederatedInstances(
...params: Parameters<BaseClient['getFederatedInstances']>
): ReturnType<BaseClient['getFederatedInstances']> {
return await this.#client.getFederatedInstances(...params)
}
async blockInstance(
...params: Parameters<BaseClient['blockInstance']>
): ReturnType<BaseClient['blockInstance']> {
return await this.#client.blockInstance(...params)
}
async uploadImage(
...params: Parameters<BaseClient['uploadImage']>
): ReturnType<BaseClient['uploadImage']> {
return await this.#client.uploadImage(...params)
}
async editSite(
...params: Parameters<BaseClient['editSite']>
): ReturnType<BaseClient['editSite']> {
return await this.#client.editSite(...params)
}
async deleteImage(
...params: Parameters<BaseClient['deleteImage']>
): ReturnType<BaseClient['deleteImage']> {
return await this.#client.deleteImage(...params)
}
}
+16
View File
@@ -0,0 +1,16 @@
import * as lemmy from 'lemmy-js-client'
import type { GetPosts, ListingType } from '../types'
export function fromGetPosts(getPosts: GetPosts): lemmy.GetPosts {
return {
...getPosts,
type_: getPosts.type_ == 'Popular' ? 'All' : getPosts.type_,
}
}
export function toListingType(
listingType?: ListingType,
): lemmy.ListingType | undefined {
if (listingType == 'Popular') listingType = 'All'
return listingType
}
File diff suppressed because it is too large. Load diff
+330
View File
@@ -0,0 +1,330 @@
import type {
Comment,
CommentReplyView,
CommentSortType,
CommentView,
Community,
CommunityView,
CreateComment,
CreatePost,
FeedView,
GetComments,
GetPost,
GetPosts,
GetReplies,
ListCommunities,
LocalSite,
MyUserInfo,
Person,
PersonMentionView,
PersonView,
Post,
PostView,
PrivateMessageView,
Search,
SortType,
SubscribedType,
TopicView,
} from '../types'
import type { components } from './schema'
export function toPersonView(
personView: components['schemas']['PersonView'],
): PersonView {
return {
counts: personView.counts,
is_admin: personView.is_admin,
person: toPerson(personView.person),
}
}
export function toPerson(person: components['schemas']['Person']): Person {
return {
...person,
id: person.id,
name: person.user_name,
bot_account: person.bot,
}
}
export function toLocalSite(site: components['schemas']['Site']): LocalSite {
if (site) return {}
return {}
}
export function toCommunityView(
communityView: components['schemas']['CommunityView'],
): CommunityView {
return {
...communityView,
community: toCommunity(communityView.community),
banned_from_community: false,
counts: {
comments: communityView.counts.post_reply_count,
posts: communityView.counts.post_count,
subscribers: communityView.counts.total_subscriptions_count,
published: communityView.community.published,
community_id: communityView.community.id,
subscribers_local: communityView.counts.subscriptions_count,
users_active_day: communityView.counts.active_daily ?? -1,
users_active_half_year: communityView.counts.active_6monthly ?? -1,
users_active_month: communityView.counts.active_monthly ?? -1,
users_active_week: communityView.counts.active_weekly ?? -1,
},
}
}
export function toCommunity(
community: components['schemas']['Community'],
): Community {
return {
...community,
posting_restricted_to_mods: false,
visibility: 'Public',
}
}
export function toMyUser(
myUser: components['schemas']['MyUserInfo'],
): MyUserInfo {
return {
...myUser,
community_blocks: myUser.community_blocks.map((i) => ({
community: toCommunity(i.community),
person: toPerson(i.person),
})),
local_user_view: {
...myUser.local_user_view,
local_user: {
...myUser.local_user_view.local_user,
id: myUser.local_user_view.person.id,
person_id: myUser.local_user_view.person.id,
totp_2fa_enabled: false,
default_listing_type: 'All',
},
person: toPerson(myUser.local_user_view.person),
},
follows: myUser.follows.map((i) => ({
follower: toPerson(i.follower),
community: toCommunity(i.community),
})),
moderates: myUser.moderates.map((i) => ({
community: toCommunity(i.community),
moderator: toPerson(i.moderator),
})),
instance_blocks: myUser.instance_blocks.map((i) => ({
instance: i.instance,
person: toPerson(i.person),
})),
person_blocks: myUser.person_blocks.map((i) => ({
person: toPerson(i.person),
target: toPerson(i.target),
})),
discussion_languages: myUser.discussion_languages.map((i) => i.id!),
}
}
export function toPostView(
postView: components['schemas']['PostView'],
): PostView {
return {
...postView,
creator: toPerson(postView.creator),
creator_blocked: false,
post: toPost(postView.post),
community: toCommunity(postView.community),
}
}
export function toPost(post: components['schemas']['Post']): Post {
return {
...post,
creator_id: -1,
name: post.title,
featured_community: post.sticky,
featured_local: false,
}
}
export function toCommentView(
commentView: components['schemas']['CommentView'],
): CommentView {
return {
...commentView,
comment: toComment(commentView.comment),
creator: toPerson(commentView.creator),
post: toPost(commentView.post),
community: toCommunity(commentView.community),
subscribed: commentView.subscribed as SubscribedType,
}
}
export function toComment(comment: components['schemas']['Comment']): Comment {
return {
...comment,
content: comment.body,
creator_id: -1,
distinguished: comment.distinguished ?? false,
}
}
export function toCommentReplyView(
commentReplyView: components['schemas']['CommentReplyView'],
): CommentReplyView {
return {
...commentReplyView,
banned_from_community: false,
creator_banned_from_community: false,
comment: toComment(commentReplyView.comment),
creator: toPerson(commentReplyView.creator),
post: toPost(commentReplyView.post),
community: toCommunity(commentReplyView.community),
recipient: toPerson(commentReplyView.recipient),
}
}
export function toPrivateMessageView(
privateMessageView: components['schemas']['PrivateMessageView'],
): PrivateMessageView {
return {
...privateMessageView,
creator: toPerson(privateMessageView.creator),
recipient: toPerson(privateMessageView.recipient),
}
}
export function toPersonMentionView(
personMentionView: components['schemas']['CommentReplyView'],
): PersonMentionView {
return {
...personMentionView,
person_mention: personMentionView.comment_reply,
creator_banned_from_community: false,
banned_from_community: false,
comment: toComment(personMentionView.comment),
community: toCommunity(personMentionView.community),
post: toPost(personMentionView.post),
creator: toPerson(personMentionView.creator),
recipient: toPerson(personMentionView.recipient),
}
}
export function toTopicView(
topicView: components['schemas']['TopicView'],
): TopicView {
return {
...topicView,
communities: topicView.communities?.map(toCommunity),
children: topicView.children.map(toTopicView),
}
}
export function toFeedView(
feedView: components['schemas']['FeedView'],
): FeedView {
return {
...feedView,
communities: feedView.communities?.map(toCommunity),
children: feedView.children.map(toFeedView),
}
}
// Query calls
export function fromGetPosts(
getPosts: GetPosts,
): components['parameters']['GetPosts'] {
return {
...getPosts,
sort: toSortType(getPosts.sort),
page: Number(getPosts.page_cursor),
}
}
export function fromGetComments(
getComments: GetComments,
): components['schemas']['GetComments'] {
return {
...getComments,
sort: toCommentSortType(getComments.sort),
}
}
export function fromGetPost(
getPost: GetPost,
): components['schemas']['GetPost'] {
return getPost
}
export function fromGetReplies(
getReplies: GetReplies,
): components['schemas']['GetReplies'] {
return {
...getReplies,
sort: toCommentSortType(getReplies.sort),
}
}
export function fromSearch(search: Search): components['schemas']['Search'] {
return {
...(search as Omit<Search, 'type_'> & {
type_: 'Communities' | 'Users' | 'Url' | 'Posts'
}),
sort: toSortType(search.sort),
}
}
export function fromCreatePost(
createPost: CreatePost,
): components['schemas']['CreatePost'] {
return {
...createPost,
title: createPost.name,
}
}
export function fromListCommunities(
listCommunities: ListCommunities,
): components['schemas']['CommunityRequest'] {
return {
...listCommunities,
sort: toCommunitiesSortType(listCommunities.sort),
}
}
export function fromCreateComment(
createComment: CreateComment,
): components['schemas']['CreateComment'] {
return {
...createComment,
body: createComment.content,
}
}
// Misc
export function toSortType(
sortType?: SortType,
): components['schemas']['SortType'] | undefined {
if (
sortType == 'Old' ||
sortType == 'MostComments' ||
sortType == 'Controversial' ||
sortType == 'NewComments'
)
sortType = 'New'
return sortType
}
export function toCommentSortType(
commentSortType?: CommentSortType,
): components['schemas']['CommentSortType'] | undefined {
if (commentSortType == 'Controversial') commentSortType = undefined
return commentSortType
}
export function toCommunitiesSortType(
sortType?: SortType,
): 'Top' | 'Hot' | 'New' | undefined {
if (sortType == 'Hot' || sortType == 'New' || sortType == undefined)
return sortType
else return 'Top'
}
+5277
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+1
View File
@@ -0,0 +1 @@
export * from './generated'