Merge pull request #99 from Xyphyn/communities

feat: Add community management
This commit is contained in:
Xylight authored and GitHub committed 2023-08-13 20:41:58 +00:00
commit 2463bf3740
16 files changed
+492 -50

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "photon-lemmy",
"version": "0.7.8",
"version": "0.8.0",
"private": true,
"scripts": {
"dev": "vite dev",
+34 -28
View File
@@ -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>
@@ -1,6 +1,7 @@
<script lang="ts">
import { profile } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import { amMod } from '$lib/components/lemmy/moderation/moderation.js'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import Card from '$lib/components/ui/Card.svelte'
@@ -9,10 +10,13 @@
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 { fullCommunityName } from '$lib/util.js'
import type { CommunityView } from 'lemmy-js-client'
import {
Calendar,
ChatBubbleOvalLeftEllipsis,
Cog6Tooth,
Icon,
PencilSquare,
UserGroup,
@@ -43,6 +47,7 @@
}
community_view.subscribed = subscribed ? 'NotSubscribed' : 'Subscribed'
addSubscription(community_view.community, !subscribed)
loading.subscribing = false
}
@@ -125,6 +130,19 @@
>
{community_view.blocked ? 'Unblock' : 'Block'}
</Button>
{#if $profile.user && amMod($profile.user, community_view.community)}
<div class="flex flex-row gap-2 ml-auto">
<Button
href="/c/{fullCommunityName(
community_view.community.name,
community_view.community.actor_id
)}/settings"
size="square-md"
>
<Icon src={Cog6Tooth} mini size="16" slot="icon" />
</Button>
</div>
{/if}
</div>
{/if}
<Markdown source={community_view.community.description} />
@@ -0,0 +1,142 @@
<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'
/**
* The community ID to edit.
*/
export let edit: number | undefined = undefined
export 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 ((!edit && 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 = edit
? await getClient().editCommunity({
auth: $profile.jwt,
title: formData.displayName,
description: formData.sidebar,
nsfw: formData.nsfw,
posting_restricted_to_mods: formData.postsLockedToModerators,
icon: icon,
banner: banner,
community_id: edit,
})
: 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 ${edit ? 'saved' : '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)
}
if (!edit)
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">
<slot name="formtitle">
<h1 class="text-2xl font-bold">Create Community</h1>
</slot>
<TextInput
required
label="Name"
bind:value={formData.name}
on:input={() => {
formData.name = formData.name.toLowerCase().replaceAll(' ', '_')
}}
disabled={edit}
/>
<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}
>
{edit ? 'Save' : 'Create'}
</Button>
</form>
@@ -0,0 +1,16 @@
<script lang="ts">
import Avatar from '$lib/components/ui/Avatar.svelte'
import type { Community } from 'lemmy-js-client'
export let community: Community
</script>
<div class="flex flex-row gap-3 items-center">
<Avatar width={48} url={community.icon} alt={community.name} />
<div class="flex flex-col gap-0">
<h1 class="font-bold text-xl">{community.title}</h1>
<span class="dark:text-zinc-400 text-slate-600 text-sm">
!{community.name}@{new URL(community.actor_id).hostname}
</span>
</div>
</div>
@@ -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}
+9
View File
@@ -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
View File
@@ -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)
}
}
+9
View File
@@ -0,0 +1,9 @@
<script lang="ts">
export let data
</script>
<svelte:head>
<title>{data.community.community_view.community.title}</title>
</svelte:head>
<slot />
@@ -21,11 +21,9 @@ export async function load(req: any) {
sort: sort,
auth: get(profile)?.jwt,
}),
community: (
await getClient(undefined, req.fetch).getCommunity({
name: req.params.name,
auth: get(profile)?.jwt,
})
).community_view,
community: await getClient(undefined, req.fetch).getCommunity({
name: req.params.name,
auth: get(profile)?.jwt,
}),
}
}
+18 -13
View File
@@ -25,10 +25,10 @@
onMount(() => {
setSessionStorage('lastSeenCommunity', {
id: data.community.community.id,
id: data.community.community_view.community.id,
name: fullCommunityName(
data.community.community.name,
data.community.community.actor_id
data.community.community_view.community.name,
data.community.community_view.community.actor_id
),
})
})
@@ -36,11 +36,14 @@
<svelte:head>
<title>{data.posts.posts[0]?.community.name}</title>
<meta name="og:title" content={data.community.community.name} />
{#if data.community.community.description}
<meta
name="og:title"
content={data.community.community_view.community.name}
/>
{#if data.community.community_view.community.description}
<meta
name="og:description"
content={data.community.community.description}
content={data.community.community_view.community.description}
/>
{/if}
<meta name="og:url" content={$page.url.toString()} />
@@ -49,7 +52,7 @@
<Modal bind:open={sidebar}>
<span slot="title">About</span>
<div class="mx-auto">
<CommunityCard community_view={data.community} />
<CommunityCard community_view={data.community.community_view} />
</div>
</Modal>
@@ -58,14 +61,16 @@
<div class="flex flex-row gap-3 items-center">
<Avatar
width={48}
url={data.community.community.icon}
alt={data.community.community.name}
url={data.community.community_view.community.icon}
alt={data.community.community_view.community.name}
/>
<div class="flex flex-col gap-0">
<h1 class="font-bold text-xl">{data.community.community.title}</h1>
<h1 class="font-bold text-xl">
{data.community.community_view.community.title}
</h1>
<span class="dark:text-zinc-400 text-slate-600 text-sm">
!{data.community.community.name}@{new URL(
data.community.community.actor_id
!{data.community.community_view.community.name}@{new URL(
data.community.community_view.community.actor_id
).hostname}
</span>
</div>
@@ -85,6 +90,6 @@
/>
</div>
<div class="hidden xl:block">
<CommunityCard community_view={data.community} />
<CommunityCard community_view={data.community.community_view} />
</div>
</div>
@@ -0,0 +1,27 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { page } from '$app/stores'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import { fullCommunityName } from '$lib/util.js'
import type { PageData } from './$types.js'
export let data: PageData
$: communityUrl = `/c/${fullCommunityName(
data.community.community_view.community.name,
data.community.community_view.community.actor_id
)}`
</script>
<div class="flex flex-col gap-4 h-full">
<MultiSelect
headless
options={[`${communityUrl}/settings`, `${communityUrl}/settings/team`]}
optionNames={['Settings', 'Team']}
selected={$page.url.pathname}
on:select={(e) => {
goto(e.detail)
}}
/>
<slot />
</div>
+27
View File
@@ -0,0 +1,27 @@
<script lang="ts">
import CommunityForm from '$lib/components/lemmy/community/CommunityForm.svelte'
import CommunityTitle from '$lib/components/lemmy/community/CommunityTitle.svelte'
export let data
</script>
<div class="flex flex-col gap-4">
<CommunityTitle community={data.community.community_view.community} />
<h1 class="font-bold text-2xl">Settings</h1>
<CommunityForm
edit={data.community.community_view.community.id}
formData={{
name: data.community.community_view.community.name,
displayName: data.community.community_view.community.title,
nsfw: data.community.community_view.community.nsfw,
postsLockedToModerators:
data.community.community_view.community.posting_restricted_to_mods,
sidebar: data.community.community_view.community.description ?? '',
icon: null,
banner: null,
submitting: false,
}}
>
<svelte:fragment slot="formtitle">{''}</svelte:fragment>
</CommunityForm>
</div>
@@ -0,0 +1,141 @@
<script lang="ts">
import Button from '$lib/components/input/Button.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import EditableList from '$lib/components/ui/list/EditableList.svelte'
import { Icon, Plus, Trash } from 'svelte-hero-icons'
import type { PageData } from './$types.js'
import { profile } from '$lib/auth.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js'
import { flip } from 'svelte/animate'
export let data: PageData
let formData = {
newModerator: '',
addingModerator: false,
}
async function addModerator() {
if (!$profile?.jwt) return
formData.addingModerator = true
try {
const res = await getClient().resolveObject({
auth: $profile.jwt,
q: formData.newModerator,
})
if (res.person) {
const addModRes = await getClient().addModToCommunity({
auth: $profile.jwt,
added: true,
person_id: res.person.person.id,
community_id: data.community.community_view.community.id,
})
data.community.moderators = addModRes.moderators
toast({
content: 'Added that user as a moderator.',
type: 'success',
})
formData.newModerator = ''
} else {
toast({
content: 'Could not find that user.',
type: 'warning',
})
}
} catch (err) {
toast({
content: err as any,
type: 'error',
})
}
formData.addingModerator = false
}
async function removeMod(id: number) {
if (!$profile?.jwt) return
try {
const res = await getClient().addModToCommunity({
added: false,
auth: $profile.jwt,
community_id: data.community.community_view.community.id,
person_id: id,
})
data.community.moderators = res.moderators
toast({
content: 'Successfully updated community moderators.',
type: 'success',
})
} catch (err) {
toast({
content: err as any,
type: 'error',
})
}
}
</script>
<h1 class="text-2xl font-bold">Moderators</h1>
<EditableList
let:action
on:action={(e) => {
toast({
content: `Are you sure you want to remove ${e.detail.name} as a moderator?`,
action: () => removeMod(e.detail.id),
})
}}
>
{#each data.community.moderators as moderator (moderator.moderator.id)}
<div
class="py-4 flex items-center gap-2 justify-between"
animate:flip={{ duration: 300 }}
>
<div class="flex gap-2 items-center">
<Avatar
width={28}
url={moderator.moderator.avatar}
alt={moderator.moderator.name}
/>
<div class="flex flex-col gap-0">
<UserLink user={moderator.moderator} showInstance={false} />
<span class="text-xs text-slate-600 dark:text-zinc-400">
{new URL(moderator.moderator.actor_id).hostname}
</span>
</div>
</div>
{#if moderator.moderator.id != $profile?.user?.local_user_view.person.id}
<Button size="square-md" on:click={() => action(moderator.moderator)}>
<Icon src={Trash} size="16" mini slot="icon" />
</Button>
{/if}
</div>
{/each}
</EditableList>
<div class="mt-auto flex gap-2 w-full">
<TextInput
bind:value={formData.newModerator}
class="w-full"
placeholder="@user@example.com"
/>
<Button
loading={formData.addingModerator}
disabled={formData.addingModerator}
size="lg"
class="w-max flex-shrink-0"
on:click={addModerator}
>
<Icon slot="icon" src={Plus} mini size="16" />Add moderator
</Button>
</div>
+6
View File
@@ -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'
+7
View File
@@ -0,0 +1,7 @@
<script lang="ts">
import CommunityForm from '$lib/components/lemmy/community/CommunityForm.svelte'
</script>
<div class=" max-w-2xl mx-auto">
<CommunityForm />
</div>