feat: Add instance changing
This commit is contained in:
1 parent
f201123291
commit
3159583654
17 files changed
+142
-35
No files matched your search
@@ -0,0 +1,26 @@
|
||||
import { instance_url } from '$lib/lemmy.js'
|
||||
|
||||
export async function handle({ event, resolve }) {
|
||||
// annoying hack to fix lemmy's CORS
|
||||
if (event.url.pathname.startsWith('/cors')) {
|
||||
const instance = `${event.url.pathname.split('/')[2]}`
|
||||
const url = `${event.url.pathname
|
||||
.split('/')
|
||||
.slice(3)
|
||||
.join('/')}?${event.url.searchParams.toString()}`
|
||||
|
||||
event.request.headers.delete('origin')
|
||||
event.request.headers.delete('host')
|
||||
|
||||
const data = await fetch(`https://${instance}/${url.toString()}`, {
|
||||
method: event.request.method,
|
||||
body: event.request.body,
|
||||
headers: event.request.headers,
|
||||
})
|
||||
|
||||
return new Response(JSON.stringify(await data.json()))
|
||||
}
|
||||
|
||||
const response = await resolve(event)
|
||||
return response
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
<script lang="ts">
|
||||
import Link from '$lib/components/Link.svelte'
|
||||
import Logo from '$lib/components/Logo.svelte'
|
||||
|
||||
export let instance_url: string | undefined
|
||||
</script>
|
||||
|
||||
<nav
|
||||
@@ -10,7 +12,18 @@
|
||||
<div class="flex flex-row gap-2 items-center mr-auto">
|
||||
<a href="/" class="flex flex-row items-center gap-2">
|
||||
<Logo width={40} />
|
||||
<span class="font-bold">Xylemmy</span>
|
||||
<div class="flex flex-col">
|
||||
<span class="font-bold">Xylemmy</span>
|
||||
{#if instance_url}
|
||||
<a
|
||||
class="text-xs opacity-50 hover:opacity-100 hover:underline
|
||||
transition-opacity"
|
||||
href="/instance"
|
||||
>
|
||||
{new URL(`https://${instance_url}`).hostname}
|
||||
</a>
|
||||
{/if}
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
<Link href="/communities">Communities</Link>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<script lang="ts">
|
||||
import SvelteMarkdown from 'svelte-markdown'
|
||||
|
||||
export let source: string
|
||||
</script>
|
||||
|
||||
<SvelteMarkdown {source} />
|
||||
+6
-1
@@ -1,5 +1,10 @@
|
||||
import { LemmyHttp, type LoginResponse } from 'lemmy-js-client'
|
||||
import { writable } from 'svelte/store'
|
||||
import { PUBLIC_PROXY_URL } from '$env/static/public'
|
||||
|
||||
export let instance_url = 'programming.dev'
|
||||
export function getClient(instance: string = `programming.dev`): LemmyHttp {
|
||||
return new LemmyHttp(`${PUBLIC_PROXY_URL}/cors/${instance}`)
|
||||
}
|
||||
|
||||
export let lemmy = new LemmyHttp(`https://lemmy.world`)
|
||||
export const token = writable<LoginResponse>()
|
||||
@@ -0,0 +1,2 @@
|
||||
export const isImage = (url: string | undefined) =>
|
||||
/\.(jpeg|jpg|gif|png|svg|bmp|webp)$/i.test(url ?? '')
|
||||
@@ -0,0 +1,7 @@
|
||||
import { instance_url } from '$lib/lemmy.js'
|
||||
|
||||
export function load({ cookies, locals }) {
|
||||
return {
|
||||
instance_url: cookies.get('instance_url') || instance_url,
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,11 @@
|
||||
nProgress.done()
|
||||
}
|
||||
}
|
||||
|
||||
export let data
|
||||
</script>
|
||||
|
||||
<Navbar />
|
||||
<Navbar instance_url={data.instance_url} />
|
||||
<div class="mx-auto max-w-5xl p-4">
|
||||
<slot />
|
||||
</div>
|
||||
@@ -1,15 +1,14 @@
|
||||
import { lemmy } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||
|
||||
export function load(req) {
|
||||
const page = Number(req.url.searchParams.get('page') || 1) || 1
|
||||
export function load({ url, cookies }) {
|
||||
const page = Number(url.searchParams.get('page') || 1) || 1
|
||||
|
||||
const sort: SortType =
|
||||
(req.url.searchParams.get('sort') as SortType) || 'Active'
|
||||
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'Active'
|
||||
const listingType: ListingType =
|
||||
(req.url.searchParams.get('type') as ListingType) || 'Local'
|
||||
(url.searchParams.get('type') as ListingType) || 'Local'
|
||||
|
||||
return lemmy.getPosts({
|
||||
return getClient(cookies.get('instance_url')).getPosts({
|
||||
limit: 40,
|
||||
page: page,
|
||||
sort: sort,
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { lemmy } from '$lib/lemmy.js';
|
||||
|
||||
export function load({ params }) {
|
||||
return lemmy.getCommunity({ name: params.name })
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
|
||||
export function load({ params }) {
|
||||
return getClient().getCommunity({ name: params.name })
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { lemmy } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||
|
||||
export function load(req: any) {
|
||||
@@ -7,7 +7,7 @@ export function load(req: any) {
|
||||
const sort: SortType =
|
||||
(req.url.searchParams.get('sort') as SortType) || 'Active'
|
||||
|
||||
return lemmy.getPosts({
|
||||
return getClient(req.cookies.get('instance_url')).getPosts({
|
||||
limit: 40,
|
||||
community_name: req.params.name,
|
||||
page: page,
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { lemmy } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { ListingType } from 'lemmy-js-client'
|
||||
|
||||
export function load(req) {
|
||||
const type = req.url.searchParams.get('listingType') as ListingType
|
||||
const page = Number(req.url.searchParams.get('page')) || 1
|
||||
const query = req.url.searchParams.get('q')
|
||||
export function load({ url, cookies }) {
|
||||
const type = url.searchParams.get('listingType') as ListingType
|
||||
const page = Number(url.searchParams.get('page')) || 1
|
||||
const query = url.searchParams.get('q')
|
||||
|
||||
if (query) {
|
||||
return lemmy.search({
|
||||
return getClient(cookies.get('instance_url')).search({
|
||||
limit: 40,
|
||||
page: page,
|
||||
sort: 'Active',
|
||||
@@ -16,7 +16,7 @@ export function load(req) {
|
||||
q: query,
|
||||
})
|
||||
} else {
|
||||
return lemmy.listCommunities({
|
||||
return getClient(cookies.get('instance_url')).listCommunities({
|
||||
limit: 40,
|
||||
page: page,
|
||||
sort: 'Active',
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/Button.svelte'
|
||||
|
||||
let url = ''
|
||||
|
||||
async function changeInstance() {
|
||||
try {
|
||||
new URL(`https://${url}`)
|
||||
} catch (error) {
|
||||
return
|
||||
}
|
||||
|
||||
await fetch('/instance_url', {
|
||||
method: 'POST',
|
||||
body: url,
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<form
|
||||
class="bg-slate-100 border border-slate-200 dark:border-zinc-800
|
||||
dark:bg-zinc-900 p-8 flex flex-col gap-4 rounded-md"
|
||||
>
|
||||
<h1 class="font-bold text-lg">Change instance</h1>
|
||||
<p class="text-xs">(Must be exact URL )</p>
|
||||
<input bind:value={url} type="text" />
|
||||
<Button on:click={changeInstance}>change</Button>
|
||||
</form>
|
||||
@@ -0,0 +1,15 @@
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { error } from '@sveltejs/kit'
|
||||
|
||||
export async function POST(event) {
|
||||
try {
|
||||
const text = await event.request.text()
|
||||
const instance = new URL(`https://${text}`)
|
||||
|
||||
event.cookies.set('instance_url', instance.hostname)
|
||||
|
||||
return new Response(instance.hostname)
|
||||
} catch (err) {
|
||||
throw error(400, 'Invalid URL')
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import { lemmy } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
|
||||
export async function load({ params }) {
|
||||
export async function load({ params, cookies }) {
|
||||
return {
|
||||
post: await lemmy.getPost({ id: Number(params.id) }),
|
||||
comments: await lemmy.getComments({
|
||||
post: await getClient(cookies.get('instance_url')).getPost({
|
||||
id: Number(params.id),
|
||||
}),
|
||||
comments: await getClient(cookies.get('instance_url')).getComments({
|
||||
post_id: Number(params.id),
|
||||
type_: 'All',
|
||||
limit: 250,
|
||||
|
||||
@@ -14,9 +14,6 @@
|
||||
<li class="py-3">
|
||||
<details bind:open class="flex flex-col gap-1">
|
||||
<summary class="flex flex-row cursor-pointer arrow gap-2 items-center">
|
||||
<!-- <button class="-z-10">
|
||||
<Icon src={open ? Minus : Plus} width={12} mini />
|
||||
</button> -->
|
||||
<Avatar
|
||||
url={node.comment_view.creator.avatar ??
|
||||
`https://api.dicebear.com/6.x/initials/svg?seed=${node.comment_view.creator.name}`}
|
||||
@@ -38,7 +35,9 @@
|
||||
class="flex flex-col gap-1 whitespace-pre-wrap
|
||||
max-w-full"
|
||||
>
|
||||
<SvelteMarkdown source={node.comment_view.comment.content} />
|
||||
<div class="max-w-full break-words">
|
||||
<SvelteMarkdown source={node.comment_view.comment.content} />
|
||||
</div>
|
||||
<div class="flex flex-row gap-2">
|
||||
<button>
|
||||
<Icon src={ArrowUp} width={16} mini />
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
import type { CommentNodeI } from './comments.js'
|
||||
import RelativeDate from '$lib/components/RelativeDate.svelte'
|
||||
import CommunityLink from '$lib/components/CommunityLink.svelte'
|
||||
import { isImage } from '$lib/ui/image.js'
|
||||
|
||||
let postRes: PostView
|
||||
export { postRes as post }
|
||||
@@ -45,12 +46,13 @@
|
||||
{postRes.post.url}
|
||||
</a>
|
||||
{/if}
|
||||
{#if postRes.post.thumbnail_url}
|
||||
{#if isImage(postRes.post.url)}
|
||||
<a href="/post/{postRes.post.id}" class="inline self-start">
|
||||
<img
|
||||
src={postRes.post.thumbnail_url}
|
||||
src={postRes.post.url}
|
||||
alt={postRes.post.name}
|
||||
class="rounded-md max-h-64 max-w-max"
|
||||
class="rounded-md max-h-64 max-w-full"
|
||||
loading="lazy"
|
||||
/>
|
||||
</a>
|
||||
{/if}
|
||||
|
||||
Reference in new issue
Block a user