From 7e76b1684c6d616ba8bf1c6d937f42bb3db86f1b Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sun, 13 Aug 2023 12:09:53 -0700 Subject: [PATCH 1/4] feat: add community creation --- package.json | 2 +- src/lib/components/input/FileInput.svelte | 62 +++++---- .../lemmy/community/CommunityCard.svelte | 2 + .../components/markdown/MarkdownEditor.svelte | 2 +- src/lib/components/ui/Navbar.svelte | 9 ++ src/lib/lemmy/user.ts | 33 ++++- src/routes/communities/+page.svelte | 6 + src/routes/create/community/+page.svelte | 125 ++++++++++++++++++ 8 files changed, 210 insertions(+), 31 deletions(-) create mode 100644 src/routes/create/community/+page.svelte diff --git a/package.json b/package.json index 2ed184ad..d15a8bd4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "photon-lemmy", - "version": "0.7.8", + "version": "0.8.0", "private": true, "scripts": { "dev": "vite dev", diff --git a/src/lib/components/input/FileInput.svelte b/src/lib/components/input/FileInput.svelte index 613c25e3..3bc9fcdf 100644 --- a/src/lib/components/input/FileInput.svelte +++ b/src/lib/components/input/FileInput.svelte @@ -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 @@ } - + diff --git a/src/lib/components/lemmy/community/CommunityCard.svelte b/src/lib/components/lemmy/community/CommunityCard.svelte index d5cb1477..20ee40ea 100644 --- a/src/lib/components/lemmy/community/CommunityCard.svelte +++ b/src/lib/components/lemmy/community/CommunityCard.svelte @@ -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 } diff --git a/src/lib/components/markdown/MarkdownEditor.svelte b/src/lib/components/markdown/MarkdownEditor.svelte index 8702c327..ebe21b22 100644 --- a/src/lib/components/markdown/MarkdownEditor.svelte +++ b/src/lib/components/markdown/MarkdownEditor.svelte @@ -116,7 +116,7 @@
{label}
{/if}
{#if previewing} diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index ab21efcd..f22db2d5 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -28,6 +28,7 @@ InformationCircle, MagnifyingGlass, Moon, + Newspaper, PencilSquare, Plus, Sun, @@ -153,6 +154,14 @@ Post + + + Community + {#if !$profile?.jwt} Log in to create content. diff --git a/src/lib/lemmy/user.ts b/src/lib/lemmy/user.ts index e1350cf5..fea26b47 100644 --- a/src/lib/lemmy/user.ts +++ b/src/lib/lemmy/user.ts @@ -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) + } +} diff --git a/src/routes/communities/+page.svelte b/src/routes/communities/+page.svelte index b8fca01f..966885e8 100644 --- a/src/routes/communities/+page.svelte +++ b/src/routes/communities/+page.svelte @@ -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' diff --git a/src/routes/create/community/+page.svelte b/src/routes/create/community/+page.svelte new file mode 100644 index 00000000..32c9bffd --- /dev/null +++ b/src/routes/create/community/+page.svelte @@ -0,0 +1,125 @@ + + +
+

Create Community

+ { + formData.name = formData.name.toLowerCase().replaceAll(' ', '_') + }} + /> + + + + + + NSFW + + Only moderators can post + + + From f493f19c603cb01d62df6aaf871c06babb53f641 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sun, 13 Aug 2023 13:00:00 -0700 Subject: [PATCH 2/4] misc: add settings pages --- .../lemmy/community/CommunityCard.svelte | 16 ++ .../lemmy/community/CommunityForm.svelte | 142 ++++++++++++++++++ .../lemmy/community/CommunityTitle.svelte | 16 ++ src/lib/components/lemmy/post/PostForm.svelte | 12 +- src/routes/admin/config/+page.svelte | 2 +- src/routes/c/[name]/+layout.svelte | 9 ++ src/routes/c/[name]/{+page.ts => +layout.ts} | 10 +- src/routes/c/[name]/+page.svelte | 31 ++-- src/routes/c/[name]/settings/+layout.svelte | 27 ++++ src/routes/c/[name]/settings/+page.svelte | 27 ++++ .../c/[name]/settings/team/+page.svelte | 0 src/routes/create/community/+page.svelte | 126 +--------------- 12 files changed, 270 insertions(+), 148 deletions(-) create mode 100644 src/lib/components/lemmy/community/CommunityForm.svelte create mode 100644 src/lib/components/lemmy/community/CommunityTitle.svelte create mode 100644 src/routes/c/[name]/+layout.svelte rename src/routes/c/[name]/{+page.ts => +layout.ts} (80%) create mode 100644 src/routes/c/[name]/settings/+layout.svelte create mode 100644 src/routes/c/[name]/settings/+page.svelte create mode 100644 src/routes/c/[name]/settings/team/+page.svelte diff --git a/src/lib/components/lemmy/community/CommunityCard.svelte b/src/lib/components/lemmy/community/CommunityCard.svelte index 20ee40ea..c26f4221 100644 --- a/src/lib/components/lemmy/community/CommunityCard.svelte +++ b/src/lib/components/lemmy/community/CommunityCard.svelte @@ -1,6 +1,7 @@ + +
+ +

Create Community

+
+ { + formData.name = formData.name.toLowerCase().replaceAll(' ', '_') + }} + disabled={edit} + /> + + + + + + NSFW + + Only moderators can post + + + diff --git a/src/lib/components/lemmy/community/CommunityTitle.svelte b/src/lib/components/lemmy/community/CommunityTitle.svelte new file mode 100644 index 00000000..2410c20f --- /dev/null +++ b/src/lib/components/lemmy/community/CommunityTitle.svelte @@ -0,0 +1,16 @@ + + +
+ +
+

{community.title}

+ + !{community.name}@{new URL(community.actor_id).hostname} + +
+
diff --git a/src/lib/components/lemmy/post/PostForm.svelte b/src/lib/components/lemmy/post/PostForm.svelte index 0e8435a9..f557900b 100644 --- a/src/lib/components/lemmy/post/PostForm.svelte +++ b/src/lib/components/lemmy/post/PostForm.svelte @@ -61,7 +61,7 @@ } if (passedCommunity) { - data.community = passedCommunity.id + data.community.community_view = passedCommunity.id communitySearch = passedCommunity.name } else { const list = await getClient().listCommunities({ @@ -88,7 +88,7 @@ }) async function submit() { - if ((!data.community || communitySearch == '') && !edit) { + if ((!data.community.community_view || communitySearch == '') && !edit) { toast({ type: 'warning', content: 'You need to set a community.', @@ -135,7 +135,7 @@ data.url = image || data.url || undefined const post = await getClient().createPost({ auth: $profile.jwt, - community_id: data.community!, + community_id: data.community.community_view!, name: data.title, body: data.body, url: data.url, @@ -183,7 +183,7 @@ Community * - {#if data.community} + {#if data.community.community_view} { if (!c) { - data.community = null + data.community.community_view = null return } - data.community = c.id + data.community.community_view = c.id communitySearch = `${c.name}@${new URL(c.actor_id).hostname}` }} /> diff --git a/src/routes/admin/config/+page.svelte b/src/routes/admin/config/+page.svelte index c951eff2..ffed7e20 100644 --- a/src/routes/admin/config/+page.svelte +++ b/src/routes/admin/config/+page.svelte @@ -86,7 +86,7 @@ }} /> Only admins can create communities diff --git a/src/routes/c/[name]/+layout.svelte b/src/routes/c/[name]/+layout.svelte new file mode 100644 index 00000000..2b51e0da --- /dev/null +++ b/src/routes/c/[name]/+layout.svelte @@ -0,0 +1,9 @@ + + + + {data.community.community_view.community.title} + + + diff --git a/src/routes/c/[name]/+page.ts b/src/routes/c/[name]/+layout.ts similarity index 80% rename from src/routes/c/[name]/+page.ts rename to src/routes/c/[name]/+layout.ts index 1aca1dc8..657eb25b 100644 --- a/src/routes/c/[name]/+page.ts +++ b/src/routes/c/[name]/+layout.ts @@ -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, + }), } } diff --git a/src/routes/c/[name]/+page.svelte b/src/routes/c/[name]/+page.svelte index 4eaaebeb..3957727a 100644 --- a/src/routes/c/[name]/+page.svelte +++ b/src/routes/c/[name]/+page.svelte @@ -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 @@ {data.posts.posts[0]?.community.name} - - {#if data.community.community.description} + + {#if data.community.community_view.community.description} {/if} @@ -49,7 +52,7 @@ About
- +
@@ -58,14 +61,16 @@
-

{data.community.community.title}

+

+ {data.community.community_view.community.title} +

- !{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}
@@ -85,6 +90,6 @@ />
diff --git a/src/routes/c/[name]/settings/+layout.svelte b/src/routes/c/[name]/settings/+layout.svelte new file mode 100644 index 00000000..1686dc7d --- /dev/null +++ b/src/routes/c/[name]/settings/+layout.svelte @@ -0,0 +1,27 @@ + + +
+ { + goto(e.detail) + }} + /> + +
diff --git a/src/routes/c/[name]/settings/+page.svelte b/src/routes/c/[name]/settings/+page.svelte new file mode 100644 index 00000000..8b62049b --- /dev/null +++ b/src/routes/c/[name]/settings/+page.svelte @@ -0,0 +1,27 @@ + + +
+ +

Settings

+ + {''} + +
diff --git a/src/routes/c/[name]/settings/team/+page.svelte b/src/routes/c/[name]/settings/team/+page.svelte new file mode 100644 index 00000000..e69de29b diff --git a/src/routes/create/community/+page.svelte b/src/routes/create/community/+page.svelte index 32c9bffd..12f93ff3 100644 --- a/src/routes/create/community/+page.svelte +++ b/src/routes/create/community/+page.svelte @@ -1,125 +1,7 @@ -
-

Create Community

- { - formData.name = formData.name.toLowerCase().replaceAll(' ', '_') - }} - /> - - - - - - NSFW - - Only moderators can post - - - +
+ +
From 600a50d9c8f6df9b9563b66e8e730189970815f3 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sun, 13 Aug 2023 13:27:33 -0700 Subject: [PATCH 3/4] feat: add moderator management --- src/routes/admin/config/+page.svelte | 2 +- .../c/[name]/settings/team/+page.svelte | 141 ++++++++++++++++++ 2 files changed, 142 insertions(+), 1 deletion(-) diff --git a/src/routes/admin/config/+page.svelte b/src/routes/admin/config/+page.svelte index ffed7e20..c951eff2 100644 --- a/src/routes/admin/config/+page.svelte +++ b/src/routes/admin/config/+page.svelte @@ -86,7 +86,7 @@ }} /> Only admins can create communities diff --git a/src/routes/c/[name]/settings/team/+page.svelte b/src/routes/c/[name]/settings/team/+page.svelte index e69de29b..dd383b6d 100644 --- a/src/routes/c/[name]/settings/team/+page.svelte +++ b/src/routes/c/[name]/settings/team/+page.svelte @@ -0,0 +1,141 @@ + + +

Moderators

+ { + 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)} +
+
+ +
+ + + {new URL(moderator.moderator.actor_id).hostname} + +
+
+ {#if moderator.moderator.id != $profile?.user?.local_user_view.person.id} + + {/if} +
+ {/each} +
+
+ + +
From 720ae81aa8c3a84b5ba599870a33694c37df148e Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sun, 13 Aug 2023 13:39:20 -0700 Subject: [PATCH 4/4] fix: post creation and editing not working --- src/lib/components/lemmy/post/PostForm.svelte | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib/components/lemmy/post/PostForm.svelte b/src/lib/components/lemmy/post/PostForm.svelte index f557900b..0e8435a9 100644 --- a/src/lib/components/lemmy/post/PostForm.svelte +++ b/src/lib/components/lemmy/post/PostForm.svelte @@ -61,7 +61,7 @@ } if (passedCommunity) { - data.community.community_view = passedCommunity.id + data.community = passedCommunity.id communitySearch = passedCommunity.name } else { const list = await getClient().listCommunities({ @@ -88,7 +88,7 @@ }) async function submit() { - if ((!data.community.community_view || communitySearch == '') && !edit) { + if ((!data.community || communitySearch == '') && !edit) { toast({ type: 'warning', content: 'You need to set a community.', @@ -135,7 +135,7 @@ data.url = image || data.url || undefined const post = await getClient().createPost({ auth: $profile.jwt, - community_id: data.community.community_view!, + community_id: data.community!, name: data.title, body: data.body, url: data.url, @@ -183,7 +183,7 @@ Community * - {#if data.community.community_view} + {#if data.community} { if (!c) { - data.community.community_view = null + data.community = null return } - data.community.community_view = c.id + data.community = c.id communitySearch = `${c.name}@${new URL(c.actor_id).hostname}` }} />