Skip to content

Commit

Permalink
Merge pull request #162 from Xyphyn/v0.19.0
Browse files Browse the repository at this point in the history
feat: v0.19.0 support
  • Loading branch information
Xyphyn authored Oct 2, 2023
2 parents 3bc3258 + 2f01e6b commit 9785306
Show file tree
Hide file tree
Showing 66 changed files with 583 additions and 285 deletions.
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Todo

- [ ] Add blocking instances and stuff
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
55 changes: 36 additions & 19 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)

if (!user?.user)
toast({
content: 'Your login has expired. Re-login to fix this issue.',
type: 'warning',
.then((user) => {
if (!user?.user)
toast({
content: 'Your login has expired. Re-login to fix this issue.',
type: 'warning',
})
site.set(user?.site)

return user
})
.catch((err) => {
toast({ content: err as any, type: 'error' })
})
site.set(user?.site)

if (!user?.user) return

profile.update(() => ({
...p,
Expand All @@ -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',
Expand Down Expand Up @@ -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(
() =>
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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 +
Expand Down
1 change: 0 additions & 1 deletion src/lib/components/lemmy/ObjectAutocomplete.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
search={async (q) => {
const results = await getClient().search({
q: q || ' ',
auth: jwt,
type_: 'Communities',
limit: 20,
listing_type: listing_type,
Expand Down
10 changes: 9 additions & 1 deletion src/lib/components/lemmy/Sort.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script lang="ts">
import { page } from '$app/stores'
import { site } from '$lib/lemmy.js'
import { searchParam } from '$lib/util.js'
import { feature } from '$lib/version.js'
import { Select } from 'mono-svelte'
import { ChartBar, Clock, Icon } from 'svelte-hero-icons'
import { fly } from 'svelte/transition'
Expand All @@ -19,7 +21,7 @@
bind:value={selected}
on:change={(e) => {
sort = 'TopAll'
if (navigate) searchParam($page.url, 'sort', selected, 'page')
if (navigate) searchParam($page.url, 'sort', selected, 'page', 'cursor')
}}
>
<span slot="label" class="flex items-center gap-1">
Expand Down Expand Up @@ -52,8 +54,14 @@
</span>
<option value="Active">Active</option>
<option value="Hot">Hot</option>
{#if feature('scaledSort', $site?.version)}
<option value="Scaled">Rising</option>
{/if}
<option value="TopAll">Top</option>
<option value="New">New</option>
{#if feature('controversialSort', $site?.version)}
<option value="Controversial">Controversial</option>
{/if}
<option value="Old">Old</option>
<option value="MostComments">Most Comments</option>
<option value="NewComments">New Comments</option>
Expand Down
1 change: 0 additions & 1 deletion src/lib/components/lemmy/comment/Comment.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

try {
await getClient().editComment({
auth: $profile.jwt,
comment_id: node.comment_view.comment.id,
content: newComment,
})
Expand Down
1 change: 0 additions & 1 deletion src/lib/components/lemmy/comment/CommentForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
try {
const response = await getClient().createComment({
auth: $profile.jwt,
content: value,
post_id: postId,
parent_id: parentId,
Expand Down
2 changes: 0 additions & 2 deletions src/lib/components/lemmy/comment/CommentVote.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
await getClient()
.likeComment({
score: upvoted ? 0 : 1,
auth: $profile.jwt,
comment_id: commentId,
})
.catch((_) => undefined)
Expand All @@ -59,7 +58,6 @@
await getClient()
.likeComment({
score: upvoted ? 0 : -1,
auth: $profile.jwt,
comment_id: commentId,
})
.catch((_) => undefined)
Expand Down
1 change: 0 additions & 1 deletion src/lib/components/lemmy/comment/Comments.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
parent.loading = true
const newComments = await getClient($page.params.instance).getComments({
auth: $profile?.jwt,
max_depth: 5,
parent_id: parent.comment_view.comment.id,
type_: 'All',
Expand Down
112 changes: 94 additions & 18 deletions src/lib/components/lemmy/community/CommunityCard.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<script lang="ts">
import { profile } from '$lib/auth.js'
import { amMod } from '$lib/components/lemmy/moderation/moderation.js'
import {
amMod,
isAdmin,
} from '$lib/components/lemmy/moderation/moderation.js'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import StickyCard from '$lib/components/ui/StickyCard.svelte'
import { Popover, toast } from 'mono-svelte'
import {
Menu,
MenuButton,
Modal,
Popover,
removeToast,
toast,
} from 'mono-svelte'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import { getClient } from '$lib/lemmy.js'
import { client, getClient } from '$lib/lemmy.js'
import { addSubscription } from '$lib/lemmy/user.js'
import { fullCommunityName } from '$lib/util.js'
import type { CommunityModeratorView, CommunityView } from 'lemmy-js-client'
Expand All @@ -16,8 +26,11 @@
Calendar,
ChatBubbleOvalLeftEllipsis,
Cog6Tooth,
EllipsisHorizontal,
Fire,
Icon,
Minus,
NoSymbol,
PencilSquare,
Plus,
UserGroup,
Expand All @@ -41,7 +54,6 @@
try {
await getClient().followCommunity({
auth: $profile.jwt,
community_id: community_view.community.id,
follow: !subscribed,
})
Expand All @@ -61,7 +73,6 @@
try {
await getClient().blockCommunity({
auth: $profile.jwt,
community_id: community_view.community.id,
block: !blocked,
})
Expand All @@ -72,8 +83,64 @@
community_view.blocked = !blocked
loading.blocking = false
}
let purgingCommunity = false
let purgeEnabled = false
$: if (purgingCommunity) {
purgeEnabled = false
setTimeout(() => {
purgeEnabled = true
}, 3000)
}
async function purgeCommunity() {
purgingCommunity = false
const purgeToast = toast({ content: 'Purging community...', loading: true })
try {
await client().purgeCommunity({
community_id: community_view.community.id,
})
removeToast(purgeToast)
toast({ content: 'Purged that community.', type: 'success' })
} catch (e) {
toast({ content: e as any, type: 'error' })
}
}
</script>

{#if purgingCommunity}
<Modal bind:open={purgingCommunity}>
<svelte:fragment slot="title">Purging Community</svelte:fragment>
<p>
Purging community <span class="font-bold">
{community_view.community.title}
</span>
</p>
<p>
Are you sure you want to do this? (The button will enable in 3 seconds.)
</p>
<div class="flex flex-row gap-2">
<Button
size="lg"
on:click={() => (purgingCommunity = false)}
class="flex-1"
>
Cancel
</Button>
<Button
size="lg"
color="danger"
on:click={purgeCommunity}
disabled={!purgeEnabled}
class="flex-1"
>
Purge
</Button>
</div>
</Modal>
{/if}

<StickyCard>
<div class="flex flex-row gap-3 items-center">
<div class="flex-shrink-0">
Expand Down Expand Up @@ -167,17 +234,8 @@
? 'Unsubscribe'
: 'Subscribe'}
</Button>
<Button
disabled={loading.blocking}
loading={loading.blocking}
color="danger"
size="lg"
on:click={block}
>
{community_view.blocked ? 'Unblock' : 'Block'}
</Button>
{#if $profile.user && amMod($profile.user, community_view.community)}
<div class="flex flex-row gap-2 ml-auto">
<div class="flex flex-row gap-2 ml-auto">
{#if $profile.user && amMod($profile.user, community_view.community)}
<Button
href="/c/{fullCommunityName(
community_view.community.name,
Expand All @@ -187,8 +245,26 @@
>
<Icon src={Cog6Tooth} mini size="16" slot="prefix" />
</Button>
</div>
{/if}
{/if}
<Menu origin="bottom-right">
<Button size="square-md" slot="target">
<Icon src={EllipsisHorizontal} size="16" mini slot="prefix" />
</Button>
<MenuButton color="danger-subtle" size="lg" on:click={block}>
<Icon src={NoSymbol} size="16" mini slot="prefix" />
{community_view.blocked ? 'Unblock' : 'Block'}
</MenuButton>
{#if $profile.user && isAdmin($profile.user)}
<MenuButton
color="danger-subtle"
on:click={() => (purgingCommunity = !purgingCommunity)}
>
<Icon src={Fire} size="16" mini slot="prefix" />
Purge
</MenuButton>
{/if}
</Menu>
</div>
</div>
{/if}
<Markdown source={community_view.community.description} />
Expand Down
Loading

0 comments on commit 9785306

Please sign in to comment.