feat: add flairsettings

This commit is contained in:
James Xylight committed 2025-09-05 16:14:11 -07:00
1 parent 43654ca2d8
commit ab00f53cac
10 files changed
+125 -11

No files matched your search

+14
View File
@@ -214,4 +214,18 @@ export abstract class BaseClient {
abstract uploadImage(
form: types.UploadImage,
): Promise<types.UploadImageResponse>
abstract setFlair?(form: types.SetPersonFlair): Promise<types.PersonView>
}
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
+2 -2
View File
@@ -5,7 +5,7 @@ import { error } from '@sveltejs/kit'
import { type GetSiteResponse } from '$lib/client/types'
import { LemmyClient } from './lemmy/lemmy'
import { PiefedClient } from './piefed/piefed'
import { DEFAULT_CLIENT_TYPE, type ClientType } from './base'
import { DEFAULT_CLIENT_TYPE, type ClientType, BaseClient } from './base'
class SiteData {
#data = $state<GetSiteResponse>()
@@ -64,7 +64,7 @@ export function client({
) => Promise<Response>
auth?: string
clientType?: ClientType
} = {}) {
} = {}): BaseClient {
if (!instanceURL)
instanceURL = profile.current.instance || DEFAULT_INSTANCE_URL
+17 -1
View File
@@ -1,5 +1,10 @@
import createClient from 'openapi-fetch'
import type { BaseClient, ClientType } from '../base'
import type {
BaseClient,
ClientType,
NullableFnArg,
NullableFnReturn,
} from '../base'
import {
fromCreateComment,
fromCreatePost,
@@ -26,6 +31,7 @@ import {
toSortType,
} from './rewrite'
import type { paths } from './schema'
import type { SetPersonFlair, PersonView } from '../types'
export class PiefedClient implements BaseClient {
type: ClientType = { name: 'piefed', baseUrl: '/api/alpha' }
@@ -893,4 +899,14 @@ export class PiefedClient implements BaseClient {
msg: 'yeah it worked i think',
}
}
async setFlair(
params: NullableFnArg<BaseClient['setFlair']>[0],
): NullableFnReturn<BaseClient['setFlair']> {
const response = (
await this.#client.POST('/user/set_flair', { body: params })
).data!
return toPersonView(response.person_view)
}
}
+5
View File
@@ -1985,3 +1985,8 @@ export interface DeleteImage {
token: string
filename: string
}
export interface SetPersonFlair {
community_id: number
flair_text: string
}
@@ -67,28 +67,31 @@
<script lang="ts">
import { profile } from '$lib/auth.svelte.js'
import { client, getClient } from '$lib/client/lemmy.svelte'
import type { CommunityModeratorView, CommunityView } from '$lib/client/types'
import {
amMod,
isAdmin,
} from '$lib/components/lemmy/moderation/moderation.js'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
import Entity from '$lib/components/ui/Entity.svelte'
import Expandable from '$lib/components/ui/Expandable.svelte'
import LabelStat from '$lib/components/ui/LabelStat.svelte'
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
import { t } from '$lib/i18n/translations'
import { client, getClient } from '$lib/client/lemmy.svelte'
import { errorMessage } from '$lib/lemmy/error'
import { userLink } from '$lib/lemmy/generic'
import { addSubscription } from '$lib/lemmy/user.js'
import { settings } from '$lib/settings.svelte'
import { fullCommunityName } from '$lib/util.svelte.js'
import type { CommunityModeratorView, CommunityView } from '$lib/client/types'
import {
action,
Button,
Menu,
MenuButton,
Modal,
modal,
removeToast,
Spinner,
@@ -105,10 +108,10 @@
NoSymbol,
Plus,
ShieldCheck,
Tag,
} from 'svelte-hero-icons'
import ItemList from '../generic/ItemList.svelte'
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
import CommunityFlair from './CommunityFlair.svelte'
let loading = $state({
blocking: false,
@@ -135,6 +138,10 @@
loading.subscribing = false
}
let setFlair = $state(false)
interface Props {
community_view: CommunityView | Promise<CommunityView>
moderators?: CommunityModeratorView[]
@@ -148,6 +155,7 @@
}: Props = $props()
</script>
{#await community_view}
<div
class="w-full h-full grid place-items-center"
@@ -157,6 +165,9 @@
<Spinner width={24} />
</div>
{:then community_view}
<Modal title={$t('cards.community.flair')} bind:open={setFlair}>
<CommunityFlair community={community_view.community.id} onsubmit={() => setFlair = !setFlair} />
</Modal>
<aside
class={[
'min-w-full pt-0 text-slate-600 dark:text-zinc-400 flex flex-col gap-1',
@@ -210,6 +221,14 @@
? $t('cards.community.subscribed')
: $t('cards.community.subscribe')}
</Button>
{#if client().setFlair}
<SidebarButton
onclick={() => setFlair = !setFlair}
rounding="xl"
icon={Tag}
label={$t('cards.community.flair')}
/>
{/if}
{/if}
{#if profile.current?.user && amMod(profile.current.user, community_view.community)}
<SidebarButton
@@ -0,0 +1,46 @@
<script lang="ts">
import { client } from '$lib/client/lemmy.svelte'
import { t } from '$lib/i18n/translations'
import { errorMessage } from '$lib/lemmy/error'
import { Button, TextInput, toast } from 'mono-svelte'
interface Props {
flair?: string
community: number
onsubmit?: (flair: string) => void
}
let { flair: passedFlair = '', community, onsubmit }: Props = $props()
let flair = $state(passedFlair)
let loading = $state(false)
async function submit() {
try {
loading = true
if (!client().setFlair) throw new Error('unsupported')
await client().setFlair!({ community_id: community, flair_text: flair })
onsubmit?.(flair)
} catch (err) {
toast({
content: errorMessage(err as string),
type: 'error',
})
}
loading = false
}
</script>
<form
onsubmit={e => {
e.preventDefault()
submit()
}}
class="contents"
>
<TextInput bind:value={flair} label={$t('nav.commands.content')} required />
<Button {loading} submit color="primary" size="lg">
{$t('form.submit')}
</Button>
</form>
@@ -14,7 +14,7 @@
CommunityModeratorView,
SubscribedType,
} from '$lib/client/types'
import { action, Button, Menu, MenuButton, modal, toast } from 'mono-svelte'
import { action, Button, Menu, MenuButton, Modal, modal, toast } from 'mono-svelte'
import {
BuildingOffice2,
Check,
@@ -26,11 +26,13 @@
NoSymbol,
Plus,
ShieldCheck,
Tag,
} from 'svelte-hero-icons'
import Subscribe from '../../../../routes/communities/Subscribe.svelte'
import ItemList from '../generic/ItemList.svelte'
import { amMod, isAdmin } from '../moderation/moderation'
import { block, blockInstance, purgeCommunity } from './CommunityCard.svelte'
import CommunityFlair from './CommunityFlair.svelte'
interface Props {
community: Community
@@ -55,8 +57,15 @@
compact,
...rest
}: Props = $props()
let setFlair = $state(false)
</script>
<Modal title={$t('cards.community.flair')} bind:open={setFlair}>
<CommunityFlair community={community.id} onsubmit={() => setFlair = !setFlair} />
</Modal>
<EntityHeader
{...rest}
{compact}
@@ -157,6 +166,10 @@
</Button>
{/snippet}
</Subscribe>
<Button size="custom" class="h-9 aspect-square" rounding="pill" onclick={() => setFlair = !setFlair} aria-label={$t('cards.community.flair')}>
<Icon src={Tag} size="16" micro />
</Button>
{/if}
{#if profile.current?.user && profile.current.user.moderates
@@ -142,7 +142,7 @@
{/if}
{/if}
{#if user.flair}
<Badge>{user.flair}</Badge>
<Badge color="blue-subtle" class="px-1.5! py-0.5!">{user.flair}</Badge>
{/if}
{@render extraBadges?.()}
{/if}
+2 -1
View File
@@ -498,7 +498,8 @@
"block": "Block",
"unblock": "Unblock",
"blockInstance": "Block instance",
"activeDay": "Active Today"
"activeDay": "Active Today",
"flair": "Set my flair"
}
},
"admin": {
+1 -1
View File
@@ -1,4 +1,4 @@
import type { Snippet } from 'svelte'
import { type Snippet } from 'svelte'
import type { IconSource } from 'svelte-hero-icons'
import { writable } from 'svelte/store'