feat: [1/2]: add flairs

This commit is contained in:
James Xylight authored and xylight committed 2025-09-17 00:27:11 +00:00
1 parent fda8e5fb37
commit 50c0b83511
15 files changed
+160 -96

No files matched your search

+1 -2
View File
@@ -1,7 +1,6 @@
// See https://kit.svelte.dev/docs/types#app
import type { Action } from '$lib/components/ui/navbar/commands/actions.svelte'
import type { ComponentType } from 'svelte'
// for information about these interfaces
declare global {
@@ -13,7 +12,7 @@ declare global {
interface PageData {
slots?: {
sidebar?: {
component?: ComponentType
component?: Component<Props, unknown, 'community_view'>
props?: any
}
}
+1
View File
@@ -226,6 +226,7 @@ export abstract class BaseClient {
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 (
+12
View File
@@ -1002,4 +1002,16 @@ export class PiefedClient implements BaseClient {
topics: response.topics.map(toTopicView),
}
}
async assignFlair(
params: NullableFnArg<BaseClient['assignFlair']>[0],
): NullableFnReturn<BaseClient['assignFlair']> {
const response = (
await this.#client.POST('/api/alpha/post/assign_flair', {
body: params,
})
).data!
return toPostView(response)
}
}
+6
View File
@@ -263,6 +263,7 @@ export interface CommunityView {
blocked: boolean
counts: CommunityAggregates
banned_from_community: boolean
flair_list?: CommunityFlair[]
}
export type CommunityVisibility = 'Public' | 'LocalOnly'
@@ -2057,3 +2058,8 @@ export interface TopicView {
export interface GetTopicsResponse {
topics: TopicView[]
}
export interface AssignFlair {
post_id: number
flair_id_list: number[]
}
@@ -44,9 +44,10 @@
sort: 'TopAll',
})
return results.communities.map((c) => c.community)
return results.communities
}}
extractName={(c) => `${c.title}@${new URL(c.actor_id).hostname}`}
extractName={(c) =>
`${c.community.name}@${new URL(c.community.actor_id).hostname}`}
bind:query={q}
{...rest}
>
@@ -67,11 +68,15 @@
{#snippet children({ item, select })}
<div in:fly|global={{ y: -4, opacity: 0 }}>
<MenuButton onclick={() => select(item)}>
<Avatar url={item.icon} alt={item.title} width={24} />
<Avatar
url={item.community.icon}
alt={item.community.title}
width={24}
/>
<div class="flex flex-col text-left">
<span>{item.title}</span>
<span>{item.community.title}</span>
<span class="text-xs opacity-80">
{new URL(item.actor_id).hostname}
{new URL(item.community.actor_id).hostname}
</span>
</div>
</MenuButton>
@@ -1,9 +1,9 @@
<script lang="ts">
import Avatar from '$lib/components/ui/Avatar.svelte'
import { communityLink } from '$lib/util.svelte'
import { settings } from '$lib/settings.svelte.js'
import { t } from '$lib/i18n/translations'
import type { Community } from '$lib/client/types'
import Avatar from '$lib/components/ui/Avatar.svelte'
import { t } from '$lib/i18n/translations'
import { settings } from '$lib/settings.svelte.js'
import { communityLink } from '$lib/util.svelte'
import { ExclamationTriangle, Icon } from 'svelte-hero-icons'
import type { HTMLAnchorAttributes } from 'svelte/elements'
@@ -39,17 +39,15 @@
<a
{...rest}
class="items-center flex flex-row gap-2 hover:underline max-w-full min-w-0 {clazz}"
class={[
'items-center inline-flex flex-row gap-2 hover:underline max-w-full min-w-0',
clazz,
]}
href={communityLink(community)}
data-sveltekit-preload-data="tap"
>
{#if avatar && !badges.nsfw}
<Avatar
url={community.icon ??
`https://api.dicebear.com/6.x/initials/svg?seed=${community.name}`}
alt={community.name}
width={avatarSize}
/>
<Avatar url={community.icon} alt={community.name} width={avatarSize} />
{/if}
{#if name}
@@ -62,6 +62,7 @@
color: i.background_color,
icon: null,
type: 'flair' as 'flair' | 'custom',
textColor: i.text_color,
})) ?? []),
],
}
@@ -428,7 +428,7 @@
}
:global(.badge-tag-color) {
background-color: var(--tag-color) !important;
background-color: var(--tag-color, #fff) !important;
color: var(--tag-text-color, #000) !important;
}
</style>
@@ -1,5 +1,15 @@
<script lang="ts">
import { profile } from '$lib/auth.svelte.js'
// This file is genuinely atrocious. It was here since the start of Photon,
// 2 years ago. I'm very sorry about this. please don't think this is
// my peak of development.
import { client, site } from '$lib/client/lemmy.svelte'
import type {
CommunityView,
Post,
PostResponse,
PostView,
} from '$lib/client/types'
import ErrorContainer, {
clearErrorScope,
pushError,
@@ -10,34 +20,36 @@
import Avatar from '$lib/components/ui/Avatar.svelte'
import { Header } from '$lib/components/ui/layout'
import { t } from '$lib/i18n/translations'
import { client, site } from '$lib/client/lemmy.svelte'
import { errorMessage } from '$lib/lemmy/error'
import { getSessionStorage, setSessionStorage } from '$lib/session.js'
import { placeholders } from '$lib/util.svelte.js'
import type { Community, Post, PostView } from '$lib/client/types'
import {
Badge,
Button,
Modal,
Material,
modal,
Option,
Select,
Spinner,
Switch,
TextArea,
TextInput,
toast,
Option,
} from 'mono-svelte'
import { onDestroy, onMount } from 'svelte'
import {
ArrowPath,
ChatBubbleBottomCenterText,
Check,
Icon,
Language,
Link,
Photo,
Plus,
Sparkles,
Tag,
XMark,
} from 'svelte-hero-icons'
import { slide } from 'svelte/transition'
interface Props {
edit?: boolean
@@ -45,9 +57,9 @@
* The post to edit
*/
editingPost?: Post | undefined
passedCommunity?: Community | undefined
passedCommunity?: CommunityView
passedData?: {
community: Community | null
community: CommunityView | null
title: string
body: string
image: FileList | null
@@ -57,6 +69,7 @@
loading: boolean
alt_text?: string
language_id?: number | string
flair_list: number[]
}
formtitle?: import('svelte').Snippet
onsubmit?: (post: PostView) => void
@@ -71,12 +84,9 @@
title: '',
body: '',
image: null,
thumbnail: undefined,
url: undefined,
nsfw: false,
loading: false,
alt_text: undefined,
language_id: undefined,
flair_list: [],
},
formtitle,
onsubmit,
@@ -90,24 +100,14 @@
})
let saveDraft = edit ? false : true
let communitySearch = $state(passedCommunity?.name ?? '')
let communities: Community[] = $state([])
let communitySearch = $state(passedCommunity?.community.name ?? '')
onMount(async () => {
if (editingPost) Object.assign(data, editingPost)
if (passedCommunity) {
data.community = passedCommunity
communitySearch = passedCommunity.name
} else {
const list = await client().listCommunities({
type_: 'All',
sort: 'Active',
limit: 40,
})
communities = list.communities.map((c) => c.community)
communitySearch = passedCommunity.community.name
}
})
@@ -139,12 +139,13 @@
data.loading = true
try {
let post: PostResponse
if (edit) {
if (!editingPost) {
throw new Error('Post is being edited but editingPost is null')
}
const post = await client().editPost({
post = await client().editPost({
name: data.title,
body: data.body,
url: data.url || undefined,
@@ -156,11 +157,9 @@
})
if (!post) throw new Error('Failed to edit post')
onsubmit?.(post.post_view)
} else {
const post = await client().createPost({
community_id: data.community!.id,
post = await client().createPost({
community_id: data.community!.community.id,
name: data.title,
body: data.body,
url: data.url || undefined,
@@ -173,8 +172,19 @@
if (!post) throw new Error('Failed to upload post')
saveDraft = false
onsubmit?.(post.post_view)
}
if (data.flair_list.length > 0) {
const piefed = client()
if (piefed.assignFlair) {
await piefed.assignFlair({
flair_id_list: data.flair_list,
post_id: post.post_view.post.id,
})
}
}
onsubmit?.(post.post_view)
} catch (err) {
pushError({ message: errorMessage(err as string), scope: 'post-form' })
data.loading = false
@@ -232,11 +242,13 @@
title: '',
})
let addAltText = $state(false)
$effect(() => {
generation.generatable = canGenerateTitle(data.url)
})
$effect(() => {
if (data.community) data.flair_list = []
})
</script>
{#if uploadingImage}
@@ -248,22 +260,39 @@
{/await}
{/if}
{#if addAltText}
<Modal title={$t('form.post.altText')} bind:open={addAltText}>
<form
onsubmit={(e) => {
e.preventDefault()
addAltText = !addAltText
}}
class="space-y-4 w-full"
>
<TextArea bind:value={data.alt_text} />
<Button size="lg" submit class="w-full">
{$t('common.back')}
</Button>
</form>
</Modal>
{/if}
{#snippet altText()}
<TextArea bind:value={data.alt_text} />
{/snippet}
{#snippet flairs()}
<Material color="uniform" class="flex flex-row gap-4 flex-wrap">
{#if data.community?.flair_list}
{#each data.community.flair_list as flair}
{@const selected = data.flair_list?.some((i) => i == flair.id)}
<button
class="rounded-full cursor-pointer hover:brightness-100"
style="background-color: {flair.background_color};"
onclick={() => {
if (selected) {
const index = data.flair_list.findIndex((i) => i == flair.id)
if (index != -1) data.flair_list.splice(index, 1)
} else data.flair_list.push(flair.id)
}}
>
<Badge
color={selected ? 'gray-subtle' : 'custom'}
class="ring-white/20"
>
{#snippet icon()}
<Icon src={selected ? Check : Plus} size="16" micro />
{/snippet}
{flair.flair_title}
</Badge>
</button>
{/each}
{/if}
</Material>
{/snippet}
<form
onsubmit={(e) => {
@@ -282,11 +311,8 @@
{#if !data.community}
<ObjectAutocomplete
bind:q={communitySearch}
items={communities}
jwt={profile.current?.jwt}
listing_type="All"
label={$t('form.post.community')}
required
onselect={(e) => {
const c = e
if (!c) {
@@ -311,15 +337,15 @@
>
{#snippet prefix()}
<Avatar
url={data?.community?.icon}
alt={data.community?.name}
url={data?.community?.community.icon}
alt={data.community?.community.name}
width={24}
/>
{/snippet}
<div class="flex flex-col gap-0">
<span class="text-sm">{data.community.name}</span>
<span class="text-sm">{data.community?.community.name}</span>
<span class="text-[10px] leading-3">
{new URL(data.community.actor_id).hostname}
{new URL(data.community.community.actor_id).hostname}
</span>
</div>
</Button>
@@ -349,13 +375,15 @@
<div class="flex items-center gap-2 actions">
{#if data.url}
<Button
onclick={() => (addAltText = !addAltText)}
onclick={() =>
modal({ title: $t('form.post.altText'), snippet: altText })}
rounding="pill"
size="sm"
color={data.alt_text ? 'primary' : 'ghost'}
class="text-xs"
icon={ChatBubbleBottomCenterText}
>{$t('form.post.altText')}
>
{$t('form.post.altText')}
</Button>
{/if}
<Button
@@ -399,8 +427,8 @@
}}
size="sm"
rounding="pill"
icon={Link}
>
<Icon src={Link} size="16" micro />
{$t('form.post.addUrl')}
</Button>
<Button
@@ -410,23 +438,33 @@
}}
size="sm"
rounding="pill"
icon={Photo}
>
<Icon src={Photo} size="16" micro />
{$t('form.post.uploadImage')}
</Button>
{/if}
{#if data.language_id === undefined}
<Button size="sm" rounding="pill" onclick={() => (data.language_id = 0)}>
<Icon src={Language} size="16" micro />
<Button
size="sm"
rounding="pill"
onclick={() => (data.language_id = 0)}
icon={Language}
>
{$t('form.post.setLanguage')}
</Button>
{/if}
{#if (data.community?.flair_list?.length ?? 0) > 0}
<Button
onclick={() => modal({ title: $t('form.post.flair'), snippet: flairs })}
size="sm"
rounding="pill"
icon={Tag}
color={(data.flair_list?.length ?? 0) > 0 ? 'primary' : 'secondary'}
>
{$t('form.post.flair')}
</Button>
{/if}
</div>
{#if addAltText}
<div transition:slide={{ axis: 'y', duration: 150 }} class="w-full">
<TextInput label={$t('form.post.altText')} bind:value={data.alt_text} />
</div>
{/if}
<Switch bind:checked={data.nsfw}>{$t('form.post.nsfw')}</Switch>
{#if data.language_id !== undefined}
{#if site.data}
@@ -88,7 +88,7 @@
</script>
<a
class="items-center flex flex-row gap-1 hover:underline max-w-full min-w-0 {clazz}"
class="items-center inline-flex flex-row gap-1 hover:underline max-w-full min-w-0 {clazz}"
href="/u/{user.name}@{new URL(user.actor_id).hostname}"
data-sveltekit-preload-data="tap"
>
+2 -1
View File
@@ -144,7 +144,8 @@
"addUrl": "Add URL",
"setLanguage": "Set language",
"unset": "Unset",
"readMore": "Read more"
"readMore": "Read more",
"flair": "Add flair"
},
"profile": {
"displayName": "Display name",
+6 -1
View File
@@ -140,7 +140,12 @@
{:else if prefix}
{@render prefix?.()}
{:else if icon}
<Icon src={icon} size="16" mini />
<Icon
src={icon}
size="16"
mini
class={[color == 'secondary' && 'text-slate-600 dark:text-zinc-400']}
/>
{/if}
{@render children?.()}
{@render suffix?.()}
+2 -1
View File
@@ -1,3 +1,4 @@
import { t } from '$lib/i18n/translations'
import { type Snippet } from 'svelte'
import type { IconSource } from 'svelte-hero-icons'
import { writable } from 'svelte/store'
@@ -41,7 +42,7 @@ export const action = (action?: {
}
: () => shownModal.set(undefined),
type: action?.type ?? 'secondary',
content: action?.content || 'OK',
content: action?.content || t.get('common.back'),
close: action?.close ?? true,
})
+3 -3
View File
@@ -1,10 +1,10 @@
import { browser } from '$app/environment'
import type { Community } from '$lib/client/types'
import type { CommunityView } from '$lib/client/types'
interface SessionStorage {
lastSeenCommunity?: Community
lastSeenCommunity?: CommunityView
postDraft?: {
community: Community | null
community: CommunityView | null
title: string
body: string
image: FileList | null
+1 -4
View File
@@ -16,10 +16,7 @@
onMount(() => {
if (browser)
setSessionStorage(
'lastSeenCommunity',
data.community.community_view.community,
)
setSessionStorage('lastSeenCommunity', data.community.community_view)
resumables.add({
name: data.community.community_view.community.title,