feat: add community creation
This commit is contained in:
1 parent
98a5a2f6f2
commit
7e76b1684c
8 files changed
+210
-31
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "photon-lemmy",
|
||||
"version": "0.7.8",
|
||||
"version": "0.8.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
export let multiple = false
|
||||
export let files: FileList | null | undefined = null
|
||||
export let image = false
|
||||
export let label: string | undefined = undefined
|
||||
|
||||
let previewURL = ''
|
||||
let dragover = false
|
||||
@@ -14,34 +15,39 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
<label
|
||||
class="flex flex-col items-center px-8 py-4 mx-auto w-full rounded-lg
|
||||
<div>
|
||||
{#if label}
|
||||
<span class="block my-1 text-sm font-bold">{label}</span>
|
||||
{/if}
|
||||
<label
|
||||
class="flex flex-col items-center px-8 py-4 mx-auto w-full rounded-lg
|
||||
border border-slate-300 dark:border-zinc-700 bg-white dark:bg-black
|
||||
cursor-pointer min-h-36 transition-colors {dragover
|
||||
? 'border-sky-500 text-sky-500'
|
||||
: ''}
|
||||
? 'border-sky-500 text-sky-500'
|
||||
: ''}
|
||||
"
|
||||
on:drop|preventDefault={(event) => (files = event.dataTransfer?.files)}
|
||||
on:dragover|preventDefault={(event) => {
|
||||
if (event.dataTransfer) {
|
||||
event.dataTransfer.dropEffect = 'copy'
|
||||
dragover = true
|
||||
}
|
||||
}}
|
||||
on:dragleave|preventDefault={() => (dragover = false)}
|
||||
>
|
||||
{#if image && files}
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src={previewURL}
|
||||
on:load={() => {
|
||||
if (previewURL) URL.revokeObjectURL(previewURL)
|
||||
}}
|
||||
class="w-full max-w-sm h-full rounded-lg"
|
||||
/>
|
||||
{:else}
|
||||
<Icon src={DocumentPlus} class="opacity-50" size="36" />
|
||||
<p class="text-sm opacity-50">Attach a file</p>
|
||||
{/if}
|
||||
<input type="file" bind:files {accept} class="hidden" {multiple} />
|
||||
</label>
|
||||
on:drop|preventDefault={(event) => (files = event.dataTransfer?.files)}
|
||||
on:dragover|preventDefault={(event) => {
|
||||
if (event.dataTransfer) {
|
||||
event.dataTransfer.dropEffect = 'copy'
|
||||
dragover = true
|
||||
}
|
||||
}}
|
||||
on:dragleave|preventDefault={() => (dragover = false)}
|
||||
>
|
||||
{#if image && files}
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src={previewURL}
|
||||
on:load={() => {
|
||||
if (previewURL) URL.revokeObjectURL(previewURL)
|
||||
}}
|
||||
class="w-full max-w-sm h-full rounded-lg"
|
||||
/>
|
||||
{:else}
|
||||
<Icon src={DocumentPlus} class="opacity-50" size="36" />
|
||||
<p class="text-sm opacity-50">Attach a file</p>
|
||||
{/if}
|
||||
<input type="file" bind:files {accept} class="hidden" {multiple} />
|
||||
</label>
|
||||
</div>
|
||||
@@ -9,6 +9,7 @@
|
||||
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
||||
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { addSubscription } from '$lib/lemmy/user.js'
|
||||
import type { CommunityView } from 'lemmy-js-client'
|
||||
import {
|
||||
Calendar,
|
||||
@@ -43,6 +44,7 @@
|
||||
}
|
||||
|
||||
community_view.subscribed = subscribed ? 'NotSubscribed' : 'Subscribed'
|
||||
addSubscription(community_view.community, !subscribed)
|
||||
|
||||
loading.subscribing = false
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@
|
||||
<div class="block my-1 font-bold text-sm">{label}</div>
|
||||
{/if}
|
||||
<div
|
||||
class="flex flex-col border border-slate-200 dark:border-zinc-800 rounded-md
|
||||
class="flex flex-col border border-slate-300 dark:border-zinc-800 rounded-md
|
||||
overflow-hidden focus-within:border-black focus-within:dark:border-white transition-colors"
|
||||
>
|
||||
{#if previewing}
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
InformationCircle,
|
||||
MagnifyingGlass,
|
||||
Moon,
|
||||
Newspaper,
|
||||
PencilSquare,
|
||||
Plus,
|
||||
Sun,
|
||||
@@ -153,6 +154,14 @@
|
||||
<Icon src={PencilSquare} mini width={16} />
|
||||
Post
|
||||
</MenuButton>
|
||||
<MenuButton
|
||||
link
|
||||
href="/create/community"
|
||||
disabled={$profile?.jwt == undefined}
|
||||
>
|
||||
<Icon src={Newspaper} mini width={16} />
|
||||
Community
|
||||
</MenuButton>
|
||||
{#if !$profile?.jwt}
|
||||
<span class="text-sm mx-4 my-1 py-1">
|
||||
<Link highlight href="/login">Log in</Link> to create content.
|
||||
|
||||
+32
-1
@@ -1,6 +1,6 @@
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { MyUserInfo, PersonView } from 'lemmy-js-client'
|
||||
import type { Community, MyUserInfo, PersonView } from 'lemmy-js-client'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
export const blockUser = async (block: boolean, id: number) => {
|
||||
@@ -17,3 +17,34 @@ export const blockUser = async (block: boolean, id: number) => {
|
||||
|
||||
export const isBlocked = (me: MyUserInfo, user: number) =>
|
||||
me.person_blocks.map((b) => b.target.id).includes(user)
|
||||
|
||||
export const addSubscription = (
|
||||
community: Community,
|
||||
subscribe: boolean = true
|
||||
) => {
|
||||
const p = get(profile)
|
||||
|
||||
if (!p?.user) return
|
||||
|
||||
if (subscribe) {
|
||||
profile.set({
|
||||
...p,
|
||||
user: {
|
||||
...p.user,
|
||||
follows: [
|
||||
...p.user.follows,
|
||||
{
|
||||
community: community,
|
||||
follower: p.user.local_user_view.person,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
} else {
|
||||
p.user.follows.splice(
|
||||
p.user.follows.findIndex((i) => i.community.id == community.id),
|
||||
1
|
||||
)
|
||||
profile.set(p)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||
import { searchParam } from '$lib/util.js'
|
||||
import { addSubscription } from '$lib/lemmy/user.js'
|
||||
|
||||
export let data
|
||||
|
||||
@@ -96,6 +97,11 @@
|
||||
res.community_view.subscribed != 'NotSubscribed'
|
||||
? 'Subscribed'
|
||||
: 'NotSubscribed'
|
||||
|
||||
addSubscription(
|
||||
community.community,
|
||||
res.community_view.subscribed == 'Subscribed'
|
||||
)
|
||||
}
|
||||
}}
|
||||
color={community.subscribed == 'Subscribed'
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import Checkbox from '$lib/components/input/Checkbox.svelte'
|
||||
import FileInput from '$lib/components/input/FileInput.svelte'
|
||||
import TextArea from '$lib/components/input/TextArea.svelte'
|
||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
|
||||
import { toast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import { getClient, uploadImage } from '$lib/lemmy.js'
|
||||
import { addSubscription } from '$lib/lemmy/user.js'
|
||||
|
||||
let formData: {
|
||||
name: string
|
||||
displayName: string
|
||||
icon: FileList | null
|
||||
banner: FileList | null
|
||||
sidebar: string
|
||||
nsfw: boolean
|
||||
postsLockedToModerators: boolean
|
||||
submitting: boolean
|
||||
} = {
|
||||
name: '',
|
||||
displayName: '',
|
||||
icon: null,
|
||||
banner: null,
|
||||
sidebar: '',
|
||||
nsfw: false,
|
||||
postsLockedToModerators: false,
|
||||
submitting: false,
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
if (!$profile?.jwt) return
|
||||
if (formData.name == '' || formData.displayName == '') return
|
||||
|
||||
formData.submitting = true
|
||||
|
||||
try {
|
||||
let icon = formData.icon ? await uploadImage(formData.icon[0]) : undefined
|
||||
let banner = formData.banner
|
||||
? await uploadImage(formData.banner[0])
|
||||
: undefined
|
||||
|
||||
const res = await getClient().createCommunity({
|
||||
auth: $profile.jwt,
|
||||
name: formData.name,
|
||||
title: formData.displayName,
|
||||
description: formData.sidebar,
|
||||
nsfw: formData.nsfw,
|
||||
posting_restricted_to_mods: formData.postsLockedToModerators,
|
||||
icon: icon,
|
||||
banner: banner,
|
||||
})
|
||||
|
||||
toast({
|
||||
content: 'Your community was created.',
|
||||
type: 'success',
|
||||
})
|
||||
|
||||
if ($profile.user) {
|
||||
$profile.user = {
|
||||
...$profile.user,
|
||||
moderates: [
|
||||
...$profile.user.moderates,
|
||||
{
|
||||
community: res.community_view.community,
|
||||
moderator: $profile.user.local_user_view.person,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
addSubscription(res.community_view.community, true)
|
||||
}
|
||||
|
||||
goto(
|
||||
`/c/${res.community_view.community.name}@${
|
||||
new URL(res.community_view.community.actor_id).hostname
|
||||
}`
|
||||
)
|
||||
} catch (err) {
|
||||
toast({
|
||||
content: err as any,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
|
||||
formData.submitting = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<form
|
||||
on:submit|preventDefault={submit}
|
||||
class="flex flex-col gap-4 h-full max-w-2xl mx-auto"
|
||||
>
|
||||
<h1 class="text-2xl font-bold">Create Community</h1>
|
||||
<TextInput
|
||||
required
|
||||
label="Name"
|
||||
bind:value={formData.name}
|
||||
on:input={() => {
|
||||
formData.name = formData.name.toLowerCase().replaceAll(' ', '_')
|
||||
}}
|
||||
/>
|
||||
<TextInput required label="Display name" bind:value={formData.displayName} />
|
||||
<FileInput label="Icon" bind:files={formData.icon} image />
|
||||
<FileInput label="Banner" bind:files={formData.banner} image />
|
||||
<MarkdownEditor previewButton label="Sidebar" bind:value={formData.sidebar} />
|
||||
|
||||
<Checkbox bind:checked={formData.nsfw}>NSFW</Checkbox>
|
||||
<Checkbox bind:checked={formData.postsLockedToModerators}>
|
||||
Only moderators can post
|
||||
</Checkbox>
|
||||
<Button
|
||||
submit
|
||||
color="primary"
|
||||
size="lg"
|
||||
class="mt-auto"
|
||||
loading={formData.submitting}
|
||||
disabled={formData.submitting}
|
||||
>
|
||||
Create
|
||||
</Button>
|
||||
</form>
|
||||
Reference in new issue
Block a user