-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
322 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function dateToUnixTimestamp(date: Date): bigint { | ||
return BigInt(Math.round((date?.getTime() || Date.now()) / 1000)); | ||
} | ||
|
||
export function dateFromUnixTimestamp(timestamp: number | bigint): Date { | ||
return new Date(Number(timestamp) * 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/routes/(app)/[username]/[slug]/revisions/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { PageServerLoad } from './$types'; | ||
import { appendSubpath, profileLinkByUsername } from '$lib/leaf/profile'; | ||
import { error, redirect } from '@sveltejs/kit'; | ||
import { env } from '$env/dynamic/public'; | ||
import { parseUsername } from '$lib/utils/username'; | ||
import { leafClient } from '$lib/leaf'; | ||
|
||
export const load: PageServerLoad = async ({ | ||
params | ||
}): Promise<{ revisions: (number | bigint)[] }> => { | ||
const username = parseUsername(params.username); | ||
if (username.domain == env.PUBLIC_DOMAIN) { | ||
return redirect(302, `/${username.name}/${params.slug}`); | ||
} | ||
const fullUsername = `${username.name}@${username.domain || env.PUBLIC_DOMAIN}`; | ||
const profileLink = await profileLinkByUsername(fullUsername); | ||
|
||
if (!profileLink) return error(404, `User not found: ${fullUsername}`); | ||
const pageLink = appendSubpath(profileLink, params.slug); | ||
|
||
const links = await leafClient.list_entities(pageLink); | ||
const revisions = []; | ||
for (const link of links) { | ||
const last = link.path[link.path.length - 1]; | ||
if ('Uint' in last) { | ||
revisions.push(last.Uint); | ||
} | ||
} | ||
|
||
revisions.sort(); | ||
// Remove the last revision because it's the same as the current one | ||
revisions.pop(); | ||
revisions.reverse(); | ||
|
||
return { | ||
revisions | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<script lang="ts"> | ||
import type { PageData } from './$types'; | ||
import { page } from '$app/stores'; | ||
import { dateFromUnixTimestamp } from '$lib/utils/time'; | ||
const { data }: { data: PageData } = $props(); | ||
</script> | ||
|
||
<main class="flex flex-col items-center"> | ||
<h1 class="mb-2 mt-3 text-4xl font-bold">Revisions</h1> | ||
|
||
<h2 class="font-mono"> | ||
{$page.params.username} / {$page.params.slug} | ||
</h2> | ||
|
||
<ul class="my-5 flex flex-col items-center gap-3"> | ||
<li> | ||
<a class="variant-ghost btn" href={`/${$page.params.username}/${$page.params.slug}`}> | ||
Current | ||
</a> | ||
</li> | ||
{#each data.revisions as revision} | ||
<li> | ||
<a | ||
class="variant-ghost btn" | ||
href={`/${$page.params.username}/${$page.params.slug}/revisions/${revision}`} | ||
> | ||
{new Intl.DateTimeFormat(undefined, { | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit' | ||
}).format(dateFromUnixTimestamp(revision))} | ||
</a> | ||
</li> | ||
{/each} | ||
</ul> | ||
</main> |
70 changes: 70 additions & 0 deletions
70
src/routes/(app)/[username]/[slug]/revisions/[revision]/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { Actions, PageServerLoad } from './$types'; | ||
import { | ||
Username, | ||
WebLinks, | ||
WeirdWikiPage, | ||
WeirdWikiRevisionAuthor, | ||
appendSubpath, | ||
getProfileById, | ||
profileLinkById, | ||
profileLinkByUsername | ||
} from '$lib/leaf/profile'; | ||
import { error, fail, redirect } from '@sveltejs/kit'; | ||
import { env } from '$env/dynamic/public'; | ||
import { parseUsername } from '$lib/utils/username'; | ||
import { leafClient } from '$lib/leaf'; | ||
import { CommonMark, Name } from 'leaf-proto/components'; | ||
import { Page } from '../../../types'; | ||
|
||
export const load: PageServerLoad = async ({ | ||
params | ||
}): Promise<{ page: Page; revisionAuthor: string }> => { | ||
const username = parseUsername(params.username); | ||
if (username.domain == env.PUBLIC_DOMAIN) { | ||
return redirect(302, `/${username.name}/${params.slug}`); | ||
} | ||
const fullUsername = `${username.name}@${username.domain || env.PUBLIC_DOMAIN}`; | ||
const profileLink = await profileLinkByUsername(fullUsername); | ||
|
||
if (!profileLink) return error(404, `User not found: ${fullUsername}`); | ||
const revisionLink = appendSubpath(profileLink, params.slug, { Uint: BigInt(params.revision!) }); | ||
|
||
const ent = await leafClient.get_components( | ||
revisionLink, | ||
CommonMark, | ||
WebLinks, | ||
Name, | ||
WeirdWikiRevisionAuthor | ||
); | ||
if (!ent) return error(404, 'Revision not found'); | ||
|
||
let display_name = ent.get(Name)?.value; | ||
let links = ent.get(WebLinks)?.value; | ||
|
||
const commonMark = ent.get(CommonMark)?.value; | ||
|
||
if (!display_name) { | ||
display_name = env.PUBLIC_INSTANCE_NAME; | ||
} | ||
|
||
const authorId = ent.get(WeirdWikiRevisionAuthor)?.value; | ||
const revisionAuthor = | ||
(await (async () => { | ||
if (!authorId) return; | ||
const profileLink = profileLinkById(authorId); | ||
const ent = await leafClient.get_components(profileLink, Username); | ||
const username = ent?.get(Username)?.value; | ||
return username; | ||
})()) || ''; | ||
|
||
return { | ||
page: { | ||
slug: params.slug, | ||
display_name, | ||
markdown: commonMark || '', | ||
links: links || [], | ||
wiki: false | ||
}, | ||
revisionAuthor | ||
}; | ||
}; |
Oops, something went wrong.