stage3: add image uploads

This commit is contained in:
James Xylight committed 2025-09-04 18:45:36 -07:00
1 parent f177fab454
commit a9fff5c9e8
11 files changed
+124 -57

No files matched your search

+2 -7
View File
@@ -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<T>(key: string): T | undefined {
if (typeof localStorage == 'undefined') return undefined
+12 -1
View File
@@ -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<types.BlockInstanceResponse>
abstract uploadImage(
form: types.UploadImage,
): Promise<types.UploadImageResponse>
}
+2 -10
View File
@@ -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<GetSiteResponse>()
@@ -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',
}
+6 -2
View File
@@ -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<BaseClient['blockInstance']> {
return await this.#client.blockInstance(...params)
}
async uploadImage(
...params: Parameters<BaseClient['uploadImage']>
): ReturnType<BaseClient['uploadImage']> {
return await this.#client.uploadImage(...params)
}
}
+76 -21
View File
@@ -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<BaseClient['createCommunity']>
params: Parameters<BaseClient['createCommunity']>[0],
): ReturnType<BaseClient['createCommunity']> {
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<BaseClient['getCommunity']>[0],
@@ -252,9 +258,18 @@ export class PiefedClient implements BaseClient {
}
}
async editCommunity(
...params: Parameters<BaseClient['editCommunity']>
params: Parameters<BaseClient['editCommunity']>[0],
): ReturnType<BaseClient['editCommunity']> {
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<BaseClient['listCommunities']>[0],
@@ -297,28 +312,50 @@ export class PiefedClient implements BaseClient {
}
}
async deleteCommunity(
...params: Parameters<BaseClient['deleteCommunity']>
params: Parameters<BaseClient['deleteCommunity']>[0],
): ReturnType<BaseClient['deleteCommunity']> {
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<BaseClient['hideCommunity']>
): ReturnType<BaseClient['hideCommunity']> {
throw new Error('unimplemented')
async hideCommunity(): ReturnType<BaseClient['hideCommunity']> {
throw new Error('unsupported')
}
async removeCommunity(
...params: Parameters<BaseClient['removeCommunity']>
): ReturnType<BaseClient['removeCommunity']> {
throw new Error('unimplemented')
async removeCommunity(): ReturnType<BaseClient['removeCommunity']> {
throw new Error('unsupported')
}
async banFromCommunity(
...params: Parameters<BaseClient['banFromCommunity']>
params: Parameters<BaseClient['banFromCommunity']>[0],
): ReturnType<BaseClient['banFromCommunity']> {
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<BaseClient['addModToCommunity']>
): ReturnType<BaseClient['addModToCommunity']> {
async addModToCommunity() // ...params: Parameters<BaseClient['addModToCommunity']>
: ReturnType<BaseClient['addModToCommunity']> {
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<BaseClient['uploadImage']>[0],
): ReturnType<BaseClient['uploadImage']> {
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',
}
}
}
+18
View File
@@ -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
}
@@ -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()
+2 -2
View File
@@ -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.",
+1 -1
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import { profile, type ProfileInfo } from '$lib/auth.svelte.js'
import { DEFAULT_CLIENT_TYPE } from '$lib/client/lemmy.svelte'
import { DEFAULT_CLIENT_TYPE } from '$lib/client/base'
import CommonList from '$lib/components/ui/layout/CommonList.svelte'
import Header from '$lib/components/ui/layout/pages/Header.svelte'
import DebugObject from '$lib/components/util/debug/DebugObject.svelte'
+2 -7
View File
@@ -4,13 +4,7 @@
import { goto } from '$app/navigation'
import { page } from '$app/state'
import { profile } from '$lib/auth.svelte'
import {
client,
DEFAULT_CLIENT_TYPE,
mayBeIncompatible,
site,
type ClientType,
} from '$lib/client/lemmy.svelte'
import { client, mayBeIncompatible, site } from '$lib/client/lemmy.svelte'
import ErrorContainer, {
clearErrorScope,
pushError,
@@ -31,6 +25,7 @@
QuestionMarkCircle,
UserCircle,
} from 'svelte-hero-icons'
import { DEFAULT_CLIENT_TYPE, type ClientType } from '$lib/client/base'
interface Props {
ref?: string
+2 -5
View File
@@ -6,16 +6,13 @@
import Header from '$lib/components/ui/layout/pages/Header.svelte'
import { t } from '$lib/i18n/translations'
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
import {
DEFAULT_CLIENT_TYPE,
mayBeIncompatible,
type ClientType,
} from '$lib/client/lemmy.svelte'
import { mayBeIncompatible } from '$lib/client/lemmy.svelte'
import { site, validateInstance } from '$lib/client/lemmy.svelte'
import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte'
import { MINIMUM_VERSION } from '$lib/version'
import { Button, Note, Option, Select, TextInput, toast } from 'mono-svelte'
import { profile } from '$lib/auth.svelte'
import { DEFAULT_CLIENT_TYPE, type ClientType } from '$lib/client/base'
interface Props {
ref?: string