aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/messages/+page.server.ts
blob: 80d6f1a9a4b46d02d5e23d5a10e95f1519d4ba06 (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
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({ parent, url }) {
  const { user } = await parent();
  const username = url.searchParams.get("username");
  if (user.username !== username) redirect(307, `/${username}`);
  const since = url.searchParams.get("since") || 0;

  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 };
}