aboutsummaryrefslogtreecommitdiffstats
path: root/routes/events.js
blob: 460d09a514fcf96b0b882ecb7adb2c0385296e4d (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
import app from "$app";
import redis from "$lib/redis";
import { pool } from "$lib/nostr";
import store from "$lib/store";
import { wait } from "$lib/utils";

app.get("/:pubkey/events", async (req, res) => {
  try {
    let { pubkey } = req.params;
    store.fetching[pubkey] = true;
    store.timeouts[pubkey] = setTimeout(() => (store.fetching[pubkey] = false), 500);

    pool.subscribe(pubkey, {
      limit: 500,
      kinds: [1],
      authors: [pubkey]
    });

    let events = [];
    await wait(() => !store.fetching[pubkey], 10, 10);
    for (let id of await redis.sMembers(pubkey)) {
      events.push(JSON.parse(await redis.get(`ev:${id}`)));
    }

    res.send(events);
  } catch (e) {
    console.log(e);
    res.code(500).send("problem fetching user events");
  }
});