diff --git a/TODO.md b/TODO.md new file mode 100644 index 00000000..865ae160 --- /dev/null +++ b/TODO.md @@ -0,0 +1,3 @@ +# Todo + +- [ ] Add blocking instances and stuff diff --git a/package.json b/package.json index 94fa7b54..e36fe6ef 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "@vite-pwa/sveltekit": "^0.2.5", "autoprefixer": "^10.4.16", "jsdom": "^22.1.0", - "lemmy-js-client": "^0.18.1", + "lemmy-js-client": "^0.19.0-rc.13", "markdown-it-container": "^3.0.0", "markdown-it-sub": "^1.0.0", "markdown-it-sup": "^1.0.0", diff --git a/src/hooks.server.ts b/src/hooks.server.ts index edbac059..97bbad35 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,7 +1,9 @@ function buildUrl(inputUrl: URL): URL { let withoutCors = inputUrl.pathname.replace('/cors/', '') - if (!withoutCors.startsWith('https://')) { + if ( + !(withoutCors.startsWith('https://') || withoutCors.startsWith('http://')) + ) { withoutCors = 'https://' + withoutCors } diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 0fd799ca..0b92e4f7 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,15 +1,16 @@ import { amModOfAny } from '$lib/components/lemmy/moderation/moderation.js' import { toast } from 'mono-svelte' import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js' -import { getClient, site } from '$lib/lemmy.js' +import { client, getClient, site } from '$lib/lemmy.js' import { userSettings } from '$lib/settings.js' -import { moveItem } from '$lib/util.js' +import { instanceToURL, moveItem } from '$lib/util.js' import { LemmyHttp, type GetSiteResponse, type MyUserInfo, } from 'lemmy-js-client' import { get, writable } from 'svelte/store' +import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js' const getDefaultProfile = (): Profile => ({ id: -1, @@ -87,13 +88,21 @@ profile.subscribe(async (p) => { instance.set(p.instance) // fetch the user because p.user is undefined const user = await userFromJwt(p.jwt, p.instance) + .then((user) => { + if (!user?.user) + toast({ + content: 'Your login has expired. Re-login to fix this issue.', + type: 'warning', + }) + site.set(user?.site) - if (!user?.user) - toast({ - content: 'Your login has expired. Re-login to fix this issue.', - type: 'warning', + return user }) - site.set(user?.site) + .catch((err) => { + toast({ content: err as any, type: 'error' }) + }) + + if (!user?.user) return profile.update(() => ({ ...p, @@ -105,13 +114,17 @@ profile.subscribe(async (p) => { export async function setUser(jwt: string, inst: string, username: string) { try { - new URL(`https://${inst}`) + new URL(instanceToURL(inst)) } catch (err) { return } const user = await userFromJwt(jwt, inst) - if (!user) { + .then((u) => u) + .catch((err) => { + toast({ content: err as any, type: 'error' }) + }) + if (!user?.user) { toast({ content: 'Failed to fetch your user. Is your instance down?', type: 'error', @@ -152,9 +165,7 @@ async function userFromJwt( jwt: string, instance: string ): Promise<{ user: PersonData; site: GetSiteResponse } | undefined> { - const sitePromise = getClient(instance).getSite({ - auth: jwt, - }) + const sitePromise = client({ instanceURL: instance, auth: jwt }).getSite() let timer = setTimeout( () => @@ -170,6 +181,12 @@ async function userFromJwt( return r }) + if (!versionIsSupported(site.version, MINIMUM_VERSION)) { + throw new Error( + `This version of Photon only supports Lemmy instances with version ${MINIMUM_VERSION} or higher. This Lemmy instance is running: ${site.version}` + ) + } + const myUser = site.my_user if (!myUser) return undefined @@ -250,6 +267,10 @@ export async function setUserID(id: number) { if (prof?.jwt) { const user = await userFromJwt(prof.jwt, prof.instance) + .then((u) => u) + .catch((err) => { + toast({ content: err as any, type: 'error' }) + }) instance.set(prof.instance) prof.user = user?.user prof.avatar = user?.user.local_user_view.person.avatar @@ -276,16 +297,12 @@ export function moveProfile(id: number, up: boolean) { } const getNotificationCount = async (jwt: string, mod: boolean) => { - const unreads = await getClient().getUnreadCount({ - auth: jwt, - }) + const unreads = await getClient().getUnreadCount() let reports: number = 0 if (mod) { - const reportRes = await getClient().getReportCount({ - auth: jwt, - }) + const reportRes = await getClient().getReportCount({}) reports = reportRes.comment_reports + diff --git a/src/lib/components/lemmy/ObjectAutocomplete.svelte b/src/lib/components/lemmy/ObjectAutocomplete.svelte index 7d497666..d1bc4977 100644 --- a/src/lib/components/lemmy/ObjectAutocomplete.svelte +++ b/src/lib/components/lemmy/ObjectAutocomplete.svelte @@ -22,7 +22,6 @@ search={async (q) => { const results = await getClient().search({ q: q || ' ', - auth: jwt, type_: 'Communities', limit: 20, listing_type: listing_type, diff --git a/src/lib/components/lemmy/Sort.svelte b/src/lib/components/lemmy/Sort.svelte index 4cbef531..23192c3c 100644 --- a/src/lib/components/lemmy/Sort.svelte +++ b/src/lib/components/lemmy/Sort.svelte @@ -1,6 +1,8 @@ +{#if purgingCommunity} + + Purging Community +

+ Purging community + {community_view.community.title} + +

+

+ Are you sure you want to do this? (The button will enable in 3 seconds.) +

+
+ + +
+
+{/if} +
@@ -167,17 +234,8 @@ ? 'Unsubscribe' : 'Subscribe'} - - {#if $profile.user && amMod($profile.user, community_view.community)} -
+
+ {#if $profile.user && amMod($profile.user, community_view.community)} -
- {/if} + {/if} + + + + + {community_view.blocked ? 'Unblock' : 'Block'} + + {#if $profile.user && isAdmin($profile.user)} + (purgingCommunity = !purgingCommunity)} + > + + Purge + + {/if} + +
{/if} diff --git a/src/lib/components/lemmy/community/CommunityForm.svelte b/src/lib/components/lemmy/community/CommunityForm.svelte index c02d86c8..8d7aa724 100644 --- a/src/lib/components/lemmy/community/CommunityForm.svelte +++ b/src/lib/components/lemmy/community/CommunityForm.svelte @@ -49,7 +49,6 @@ const res = edit ? await getClient().editCommunity({ - auth: $profile.jwt, title: formData.displayName, description: formData.sidebar, nsfw: formData.nsfw, @@ -59,7 +58,6 @@ community_id: edit, }) : await getClient().createCommunity({ - auth: $profile.jwt, name: formData.name, title: formData.displayName, description: formData.sidebar, diff --git a/src/lib/components/lemmy/modal/PrivateMessageModal.svelte b/src/lib/components/lemmy/modal/PrivateMessageModal.svelte index f4adb6f2..fba0e753 100644 --- a/src/lib/components/lemmy/modal/PrivateMessageModal.svelte +++ b/src/lib/components/lemmy/modal/PrivateMessageModal.svelte @@ -20,7 +20,6 @@ try { await getClient().createPrivateMessage({ - auth: $profile.jwt, content: message, recipient_id: user.id, }) diff --git a/src/lib/components/lemmy/moderation/BanModal.svelte b/src/lib/components/lemmy/moderation/BanModal.svelte index 5235cee8..43d31c54 100644 --- a/src/lib/components/lemmy/moderation/BanModal.svelte +++ b/src/lib/components/lemmy/moderation/BanModal.svelte @@ -62,7 +62,6 @@ if (community) { await getClient().banFromCommunity({ - auth: $profile.jwt, ban: !banned, community_id: community.id, person_id: item.id, @@ -72,7 +71,6 @@ }) } else { await getClient().banPerson({ - auth: $profile.jwt, ban: !banned, person_id: item.id, reason: reason || undefined, diff --git a/src/lib/components/lemmy/moderation/ModerationMenu.svelte b/src/lib/components/lemmy/moderation/ModerationMenu.svelte index a591a692..733f01d3 100644 --- a/src/lib/components/lemmy/moderation/ModerationMenu.svelte +++ b/src/lib/components/lemmy/moderation/ModerationMenu.svelte @@ -29,7 +29,6 @@ try { await getClient().lockPost({ - auth: $profile.jwt, locked: lock, post_id: item.post.id, }) @@ -60,7 +59,6 @@ try { await getClient().featurePost({ feature_type: toInstance ? 'Local' : 'Community', - auth: $profile.jwt, featured: pinned, post_id: item.post.id, }) diff --git a/src/lib/components/lemmy/moderation/RemoveModal.svelte b/src/lib/components/lemmy/moderation/RemoveModal.svelte index 6ac84bfc..4989d8cc 100644 --- a/src/lib/components/lemmy/moderation/RemoveModal.svelte +++ b/src/lib/components/lemmy/moderation/RemoveModal.svelte @@ -57,13 +57,11 @@ if (purge) { if (isCommentView(item)) { await getClient(undefined, fetch).purgeComment({ - auth: $profile.jwt, comment_id: item.comment.id, reason: reason, }) } else { await getClient(undefined, fetch).purgePost({ - auth: $profile.jwt, post_id: item.post.id, reason: reason, }) @@ -91,7 +89,6 @@ if (privateMessage) { await getClient() .createPrivateMessage({ - auth: $profile.jwt, content: replyReason, recipient_id: isCommentView(item) ? item.comment.creator_id @@ -106,7 +103,6 @@ } else { await getClient() .createComment({ - auth: $profile.jwt, content: replyReason, post_id: item.post.id, parent_id: isCommentView(item) ? item.comment.id : undefined, @@ -122,7 +118,6 @@ if (isCommentView(item)) { await getClient().removeComment({ - auth: $profile.jwt, comment_id: item.comment.id, removed: !removed, reason: reason || undefined, @@ -130,7 +125,6 @@ item.comment.removed = !removed } else if (isPostView(item)) { await getClient().removePost({ - auth: $profile.jwt, post_id: item.post.id, removed: !removed, reason: reason || undefined, diff --git a/src/lib/components/lemmy/moderation/ReportModal.svelte b/src/lib/components/lemmy/moderation/ReportModal.svelte index 7103bf3c..5b19eaea 100644 --- a/src/lib/components/lemmy/moderation/ReportModal.svelte +++ b/src/lib/components/lemmy/moderation/ReportModal.svelte @@ -27,13 +27,11 @@ try { if (isComment(item)) { await getClient().createCommentReport({ - auth: $profile.jwt, comment_id: item.comment.id, reason: reason, }) } else if (isPost(item)) { await getClient().createPostReport({ - auth: $profile.jwt, post_id: item.post.id, reason: reason, }) diff --git a/src/lib/components/lemmy/moderation/moderation.ts b/src/lib/components/lemmy/moderation/moderation.ts index df6f7237..d458446c 100644 --- a/src/lib/components/lemmy/moderation/moderation.ts +++ b/src/lib/components/lemmy/moderation/moderation.ts @@ -78,7 +78,10 @@ export const amMod = (me: MyUserInfo, community: Community) => export const amModOfAny = (me?: MyUserInfo) => me && (me.moderates.length > 0 || isAdmin(me)) -export const isAdmin = (me: MyUserInfo) => me.local_user_view.person.admin +export const isAdmin = (me: MyUserInfo) => + // backwards compatibility hack + // @ts-ignore + me.local_user_view.local_user.admin ?? me.local_user_view.person?.admin export const removalTemplate = ( input: string, diff --git a/src/lib/components/lemmy/post/PostForm.svelte b/src/lib/components/lemmy/post/PostForm.svelte index d8c46520..3ba03898 100644 --- a/src/lib/components/lemmy/post/PostForm.svelte +++ b/src/lib/components/lemmy/post/PostForm.svelte @@ -63,7 +63,6 @@ communitySearch = passedCommunity.name } else { const list = await getClient().listCommunities({ - auth: $profile?.jwt, type_: 'All', sort: 'Active', limit: 40, @@ -107,7 +106,6 @@ } const post = await getClient().editPost({ - auth: $profile.jwt, name: data.title, body: data.body, url: data.url || undefined, @@ -121,10 +119,11 @@ dispatcher('submit', post.post_view) } else { - let image = data.image ? await uploadImage(data.image[0], $profile.instance, $profile.jwt) : undefined + let image = data.image + ? await uploadImage(data.image[0], $profile.instance, $profile.jwt) + : undefined data.url = image || data.url || undefined const post = await getClient().createPost({ - auth: $profile.jwt, community_id: data.community!, name: data.title, body: data.body, diff --git a/src/lib/components/lemmy/post/helpers.ts b/src/lib/components/lemmy/post/helpers.ts index d573c008..be361239 100644 --- a/src/lib/components/lemmy/post/helpers.ts +++ b/src/lib/components/lemmy/post/helpers.ts @@ -1,9 +1,6 @@ import { getInstance } from '$lib/lemmy.js' import type { CommentView, PersonView, Post, PostView } from 'lemmy-js-client' -export const isMutable = (post: PostView, me: PersonView) => - (me.person.admin && post.post.local) || me.person.id == post.creator.id - export const isCommentMutable = (comment: CommentView, me: PersonView) => me.person.id == comment.creator.id @@ -13,8 +10,8 @@ export const bestImageURL = ( width: number = 1024 ) => { if (width > 1024) { - if (post.url) return `${post.url}?format=webp` - else if (post.thumbnail_url) return `${post.thumbnail_url}?format=webp` + if (post.url) return `${post.url}?format=webp` + else if (post.thumbnail_url) return `${post.thumbnail_url}?format=webp` } if (compact && post.thumbnail_url) diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index 86b8e011..04a3fb6c 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -113,7 +113,7 @@ + {#if cursor?.back} + + {/if} + +
+ +{/if} +
@@ -130,7 +183,7 @@
- {#each data.items as item (item.counts.id)} + {#each data.items as item} {#if isCommentView(item) && (data.type == 'all' || data.type == 'comments')} {:else if !isCommentView(item) && (data.type == 'all' || data.type == 'posts')} @@ -223,34 +276,48 @@ {/if}
- +
+ {#if isAdmin($profile?.user)} + + + + ban( + data.person_view.person.banned, + data.person_view.person + )} + > + + {data.person_view.person.banned ? 'Unban' : 'Ban'} + + (purgingUser = !purgingUser)} + > + + Purge + + + {/if} + + + blockUser(data.person_view.person.id)} + > + + {isBlocked($profile.user, data.person_view.person.id) + ? 'Unblock' + : 'Block'} + + +
- {#if isAdmin($profile?.user)} - - - - ban(data.person_view.person.banned, data.person_view.person)} - > - - {data.person_view.person.banned ? 'Unban' : 'Ban'} - - - {/if} {/if}

diff --git a/src/routes/u/[name]/+page.ts b/src/routes/u/[name]/+page.ts index 2e3a9d3f..2376afbc 100644 --- a/src/routes/u/[name]/+page.ts +++ b/src/routes/u/[name]/+page.ts @@ -15,7 +15,6 @@ export async function load({ params, url, fetch }) { page: page, username: params.name, sort: sort, - auth: get(profile)?.jwt, }) const items = [...user.posts, ...user.comments] diff --git a/tests/version.test.ts b/tests/version.test.ts new file mode 100644 index 00000000..713cb4da --- /dev/null +++ b/tests/version.test.ts @@ -0,0 +1,21 @@ +import { versionIsSupported } from '../src/lib/version.js' +import { describe, expect, it } from 'vitest' + +describe('versionIsSupported()', () => { + it('checks minimum correctly', () => { + expect(versionIsSupported('0.1.0', '0.0.0')).toBeTruthy() + expect(versionIsSupported('0.0.0', '0.1.0')).toBeFalsy() + }) + it('checks weird minimum versions correctly', () => { + expect(versionIsSupported('0.19.0-rc.2', '0.19.0')).toBeTruthy() + expect(versionIsSupported('0.19.0.rc.9', '0.19.1')).toBeFalsy() + }) + it('checks major and patch versions correctly', () => { + expect(versionIsSupported('2.0.0', '1.0.0')).toBeTruthy() + expect(versionIsSupported('0.0.2', '0.0.1')).toBeTruthy() + }) + it('handles other cases', () => { + expect(versionIsSupported('2.0.2', '1.0.5')).toBeTruthy() + expect(versionIsSupported('9.5.2', '7.6.3')).toBeTruthy() + }) +}) diff --git a/yarn.lock b/yarn.lock index 987b4c04..07edabf7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2821,10 +2821,10 @@ kleur@^4.1.5: resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.5.tgz#95106101795f7050c6c650f350c683febddb1780" integrity sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ== -lemmy-js-client@^0.18.1: - version "0.18.1" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.18.1.tgz#28df266eded0ef4a72b05ee6eabebddadfd81a27" - integrity sha512-maafUZ9ZJkXthRZmM0+limJmymF2kmN/8SiKufS3V4OVNM72s+wUFFiyhKmA370g23iZRW6SUagE7jNKnqNyYQ== +lemmy-js-client@^0.19.0-rc.13: + version "0.19.0-rc.13" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.19.0-rc.13.tgz#e0e15ba6fe3a08cb85130eea7eec4bd2773999f9" + integrity sha512-JP9oEh1+Wfttqx5O5EMAVIR/hFVS66iVKmEo8/Uxw8fJfyUeQo7BhKvG8LTYegBE39Womgyu3KxXb7Jy9DRI5A== dependencies: cross-fetch "^3.1.5" form-data "^4.0.0"