From a9fff5c9e81956aa944fc8cb48caa550d902fd79 Mon Sep 17 00:00:00 2001 From: James Xylight Date: Thu, 4 Sep 2025 18:40:17 -0700 Subject: [PATCH] stage3: add image uploads --- src/lib/auth.svelte.ts | 9 +- src/lib/client/base.ts | 13 ++- src/lib/client/lemmy.svelte.ts | 12 +-- src/lib/client/lemmy/lemmy.ts | 8 +- src/lib/client/piefed/piefed.ts | 97 +++++++++++++++---- src/lib/client/types/schemas/schemas.ts | 18 ++++ .../ui/sidebar/ProfileSelection.svelte | 2 +- src/lib/i18n/en.json | 4 +- src/routes/accounts/+page.svelte | 2 +- src/routes/login/+page.svelte | 9 +- src/routes/login/guest/+page.svelte | 7 +- 11 files changed, 124 insertions(+), 57 deletions(-) diff --git a/src/lib/auth.svelte.ts b/src/lib/auth.svelte.ts index 5d4bd699..73ebb0c1 100644 --- a/src/lib/auth.svelte.ts +++ b/src/lib/auth.svelte.ts @@ -5,13 +5,7 @@ import { isAdmin, } from '$lib/components/lemmy/moderation/moderation.js' import { DEFAULT_INSTANCE_URL } from '$lib/instance.svelte.js' -import { - client, - getClient, - site, - type ClientType, - DEFAULT_CLIENT_TYPE, -} from '$lib/client/lemmy.svelte' +import { client, getClient, site } from '$lib/client/lemmy.svelte' import { instanceToURL, moveItem } from '$lib/util.svelte' import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js' import { toast } from 'mono-svelte' @@ -20,6 +14,7 @@ import { t } from './i18n/translations' import { errorMessage } from './lemmy/error' import { publishedToDate } from './components/util/date' import type { Community, GetSiteResponse, MyUserInfo } from './client/types' +import { DEFAULT_CLIENT_TYPE, type ClientType } from './client/base' function getFromStorage(key: string): T | undefined { if (typeof localStorage == 'undefined') return undefined diff --git a/src/lib/client/base.ts b/src/lib/client/base.ts index 0765354d..92e14485 100644 --- a/src/lib/client/base.ts +++ b/src/lib/client/base.ts @@ -1,6 +1,14 @@ -import type { ClientType } from './lemmy.svelte' import * as types from './types' +export type ClientType = + | { name: 'lemmy'; baseUrl: '/api/v3' } + | { name: 'piefed'; baseUrl: '/api/alpha' } + +export const DEFAULT_CLIENT_TYPE: ClientType = { + name: 'lemmy', + baseUrl: '/api/v3', +} + export abstract class BaseClient { abstract type: ClientType @@ -203,4 +211,7 @@ export abstract class BaseClient { abstract blockInstance( form: types.BlockInstance, ): Promise + abstract uploadImage( + form: types.UploadImage, + ): Promise } diff --git a/src/lib/client/lemmy.svelte.ts b/src/lib/client/lemmy.svelte.ts index 2678c376..a8496039 100644 --- a/src/lib/client/lemmy.svelte.ts +++ b/src/lib/client/lemmy.svelte.ts @@ -5,6 +5,7 @@ import { error } from '@sveltejs/kit' import { type GetSiteResponse } from 'lemmy-js-client' import { LemmyClient } from './lemmy/lemmy' import { PiefedClient } from './piefed/piefed' +import { DEFAULT_CLIENT_TYPE, type ClientType } from './base' class SiteData { #data = $state() @@ -68,7 +69,7 @@ export function client({ instanceURL = profile.current.instance || DEFAULT_INSTANCE_URL if (!clientType) { - clientType = profile.current.client + clientType = profile.current.client ?? DEFAULT_CLIENT_TYPE } const jwt = auth ?? profile.current?.jwt @@ -144,12 +145,3 @@ export function mayBeIncompatible( return false } - -export type ClientType = - | { name: 'lemmy'; baseUrl: '/api/v3' } - | { name: 'piefed'; baseUrl: '/api/alpha' } - -export const DEFAULT_CLIENT_TYPE: ClientType = { - name: 'lemmy', - baseUrl: '/api/v3', -} diff --git a/src/lib/client/lemmy/lemmy.ts b/src/lib/client/lemmy/lemmy.ts index c2dfcfe6..0be24707 100644 --- a/src/lib/client/lemmy/lemmy.ts +++ b/src/lib/client/lemmy/lemmy.ts @@ -1,7 +1,6 @@ import { LemmyHttp } from 'lemmy-js-client' -import type { BaseClient } from '../base' +import type { BaseClient, ClientType } from '../base' import { fromGetPosts, toListingType } from './rewrite' -import type { ClientType } from '../lemmy.svelte' export class LemmyClient implements BaseClient { type: ClientType = { name: 'lemmy', baseUrl: '/api/v3' } @@ -472,4 +471,9 @@ export class LemmyClient implements BaseClient { ): ReturnType { return await this.#client.blockInstance(...params) } + async uploadImage( + ...params: Parameters + ): ReturnType { + return await this.#client.uploadImage(...params) + } } diff --git a/src/lib/client/piefed/piefed.ts b/src/lib/client/piefed/piefed.ts index 2aa10078..6374ed65 100644 --- a/src/lib/client/piefed/piefed.ts +++ b/src/lib/client/piefed/piefed.ts @@ -1,5 +1,6 @@ +/* eslint-disable */ import createClient from 'openapi-fetch' -import type { BaseClient } from '../base' +import type { BaseClient, ClientType } from '../base' import { fromCreateComment, fromCreatePost, @@ -23,7 +24,6 @@ import { toPrivateMessageView, } from './rewrite' import type { paths } from './schema' -import type { ClientType } from '../lemmy.svelte' export class PiefedClient implements BaseClient { type: ClientType = { name: 'piefed', baseUrl: '/api/alpha' } @@ -226,9 +226,15 @@ export class PiefedClient implements BaseClient { } } async createCommunity( - ...params: Parameters + params: Parameters[0], ): ReturnType { - throw new Error('unimplemented') + const response = (await this.#client.POST('/community', { body: params })) + .data! + + return { + ...response, + community_view: toCommunityView(response.community_view), + } } async getCommunity( params: Parameters[0], @@ -252,9 +258,18 @@ export class PiefedClient implements BaseClient { } } async editCommunity( - ...params: Parameters + params: Parameters[0], ): ReturnType { - throw new Error('unimplemented') + const response = ( + await this.#client.PUT('/community', { + body: { ...params, id: params.community_id }, + }) + ).data! + + return { + ...response, + community_view: toCommunityView(response.community_view), + } } async listCommunities( params: Parameters[0], @@ -297,28 +312,50 @@ export class PiefedClient implements BaseClient { } } async deleteCommunity( - ...params: Parameters + params: Parameters[0], ): ReturnType { - throw new Error('unimplemented') + const response = ( + await this.#client.POST('/community/delete', { body: params }) + ).data! + + return { + ...response, + community_view: toCommunityView(response.community_view), + } } - async hideCommunity( - ...params: Parameters - ): ReturnType { - throw new Error('unimplemented') + async hideCommunity(): ReturnType { + throw new Error('unsupported') } - async removeCommunity( - ...params: Parameters - ): ReturnType { - throw new Error('unimplemented') + async removeCommunity(): ReturnType { + throw new Error('unsupported') } async banFromCommunity( - ...params: Parameters + params: Parameters[0], ): ReturnType { - throw new Error('unimplemented') + const response = ( + await this.#client.POST('/community/moderate/ban', { + body: { + ...params, + expiredAt: new Date( + params.expires ?? '6767-06-07T00:00:00.000Z', + ).toISOString(), + user_id: params.person_id, + reason: params.reason ?? 'No reason provided.', + }, + }) + ).data! + + return { + banned: params.ban, + person_view: { + person: toPerson(response.bannedUser), + counts: { comment_count: -1, person_id: -1, post_count: -1 }, + is_admin: false, + }, + } } - async addModToCommunity( - ...params: Parameters - ): ReturnType { + async addModToCommunity() // ...params: Parameters + : ReturnType { throw new Error('unimplemented') } async markPostAsRead( @@ -714,4 +751,22 @@ export class PiefedClient implements BaseClient { return (await this.#client.POST('/site/block', { body: { ...params } }))! .data! } + async uploadImage( + params: Parameters[0], + ): ReturnType { + const formData = new FormData() + formData.append('file', params.image as File) + + const response = ( + await this.#client.POST('/upload/image', { + // @ts-expect-error this is the only working way to do this + body: formData, + }) + ).data! + + return { + ...response, + msg: 'yeah it worked i think', + } + } } diff --git a/src/lib/client/types/schemas/schemas.ts b/src/lib/client/types/schemas/schemas.ts index cd0cf68c..f41111f1 100644 --- a/src/lib/client/types/schemas/schemas.ts +++ b/src/lib/client/types/schemas/schemas.ts @@ -1966,3 +1966,21 @@ export interface VoteView { creator_banned_from_community: boolean score: number } + +export interface UploadImage { + image: File | Buffer +} +export interface UploadImageResponse { + msg: string + files?: ImageFile[] + url?: string + delete_url?: string +} +export interface ImageFile { + file: string + delete_token: string +} +export interface DeleteImage { + token: string + filename: string +} diff --git a/src/lib/components/ui/sidebar/ProfileSelection.svelte b/src/lib/components/ui/sidebar/ProfileSelection.svelte index 69c56ada..3770737b 100644 --- a/src/lib/components/ui/sidebar/ProfileSelection.svelte +++ b/src/lib/components/ui/sidebar/ProfileSelection.svelte @@ -12,7 +12,7 @@ import { goto } from '$app/navigation' import { page } from '$app/state' import { t } from '$lib/i18n/translations' - import { DEFAULT_CLIENT_TYPE } from '$lib/client/lemmy.svelte' + import { DEFAULT_CLIENT_TYPE } from '$lib/client/base' let { profiles }: { profiles: ProfileInfo[] } = $props() diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.json index 814b86b2..4f1c218e 100644 --- a/src/lib/i18n/en.json +++ b/src/lib/i18n/en.json @@ -893,10 +893,10 @@ "incorrect_login_12": "...", "registration_application_is_pending": "The registration application for that account hasn't been accepted yet.", "couldnt_find_post": "That post doesn't exist, isn't on this instance, or was removed.", - "cant_block_yourself": "You can't block yourself. You shouldn't either, you're a nice person!", + "cant_block_yourself": "You can't block yourself. You shouldn't either, you're (probably) a great person!", "email_not_verified": "Your account's email isn't verified.", "passwords_do_not_match": "Your input password and the verification password do not match.", - "honeypot_failed": "How did you- are you a bot? Beep bop bap boop. If you're not, the Honeypot check failed.", + "honeypot_failed": "Are you a bot? Beep bop bap boop. If you're not, the Honeypot check failed.", "downvotes_are_disabled": "Downvotes are disabled on this instance.", "email_already_exists": "An account already exists with that email.", "slurs": "Your submission contains a term banned in this instance.", diff --git a/src/routes/accounts/+page.svelte b/src/routes/accounts/+page.svelte index 9aa36eaa..56f28ed5 100644 --- a/src/routes/accounts/+page.svelte +++ b/src/routes/accounts/+page.svelte @@ -1,6 +1,6 @@