aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/messages/+page.server.ts
blob: b64dfb42d2874efd386c5145864082f66c344a53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { get } from "$lib/utils";
import { redirect } from "@sveltejs/kit";

interface Message {
	created_at: number;
}

interface Note {
	created_at: number;
	seen: number;
}

export async function load({ params, parent }) {
	const { user } = await parent();
	if (user.username !== params.username) redirect(307, `/${params.username}`);
	const { since = 0 } = params;

	let messages = [];
	let notes = [];

	if (user?.pubkey) {
		const { pubkey } = user;
		try {
			messages = await get(`/${pubkey}/${since}/messages`);
			messages = messages.sort(
				(a: Message, b: Message) => b.created_at - a.created_at,
			);
		} catch (e) {
			console.log("failed to fetch nostr messages", e);
		}

		try {
			notes = await get(`/${pubkey}/notes`);
		} catch (e) {
			console.log(`failed to fetch nostr notes for ${pubkey}`, e);
		}

		notes.map((e: Note) => {
			e.seen = e.created_at;
		});
	}

	return { messages, notes };
}