aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-06 23:02:01 -0800
committerAdam Soltys <[email protected]>2023-01-06 23:02:01 -0800
commit018732b4544fd95b7f1830db8b43dce26ebec1aa (patch)
tree1829685a50906b7f21434e64bfad92d876466697
parent6df7873c907024285504f6ad684ecc1d1f9b7141 (diff)
downloadcoinos-server-018732b4544fd95b7f1830db8b43dce26ebec1aa.tar.gz
coinos-server-018732b4544fd95b7f1830db8b43dce26ebec1aa.zip
event endpoint, send uuid with contacts
-rw-r--r--lib/nostr.js17
-rw-r--r--routes/events.js33
-rw-r--r--routes/users.js10
3 files changed, 37 insertions, 23 deletions
diff --git a/lib/nostr.js b/lib/nostr.js
index 4ee5cb13..e1d2f68e 100644
--- a/lib/nostr.js
+++ b/lib/nostr.js
@@ -25,7 +25,11 @@ export let coinos;
let now = () => Math.round(Date.now() / 1000);
let timeouts = {};
-export let q = async (sub, query, timeout = 20000, since = 3600, eager = 200) =>
+export let q = async (
+ sub,
+ query,
+ { timeout = 20000, since = 3600, eager = 200 }
+) =>
new Promise(async (r, j) => {
let start = Date.now();
let seen = [];
@@ -157,16 +161,7 @@ pool.on("event", async (relay, sub, ev) => {
coinos.send(ev);
} else if (sub.includes("notes")) {
let pubkey = sub.split(":")[0];
- ev.user = await getUser(pubkey);
-
- if (!ev.user.updated) {
- relay.subscribe(`${pubkey}:${ev.id}:profile`, {
- limit: 1,
- kinds: [0],
- authors: [pubkey]
- });
- }
-
+ if (relay.url === config.relay) console.log(ev)
await redis.sAdd(pubkey, ev.id);
await redis.set(`ev:${ev.id}`, JSON.stringify(ev));
}
diff --git a/routes/events.js b/routes/events.js
index 554be5d8..e52b8a7c 100644
--- a/routes/events.js
+++ b/routes/events.js
@@ -5,6 +5,25 @@ import { coinos, pool, q } from "$lib/nostr";
import store from "$lib/store";
import { nada, wait } from "$lib/utils";
+app.get("/event/:id", async (req, res) => {
+ try {
+ let event = JSON.parse(await redis.get(`ev:${req.params.id}`));
+ let { pubkey } = event;
+
+ event.user = JSON.parse(await redis.get(`user:${pubkey}`)) || {
+ username: pubkey.substr(0, 6),
+ pubkey,
+ anon: true,
+ follows: [],
+ followers: []
+ };
+
+ res.send(event);
+ } catch (e) {
+ res.code(500).send("Problem fetching event");
+ }
+});
+
app.get("/nostr/:pubkey", async (req, res) => {
try {
let { pubkey } = req.params;
@@ -26,10 +45,14 @@ app.get("/nostr/:pubkey", async (req, res) => {
followers: []
};
- q(`${pubkey}:notes`, {
- kinds: [1],
- authors: [pubkey]
- }).catch(nada);
+ q(
+ `${pubkey}:notes`,
+ {
+ kinds: [1],
+ authors: [pubkey]
+ },
+ { since: 0 }
+ ).catch(nada);
await redis.set(`user:${pubkey}`, JSON.stringify(user));
@@ -39,7 +62,7 @@ app.get("/nostr/:pubkey", async (req, res) => {
? (await redis.mGet(ids.map(k => "ev:" + k))).map(JSON.parse)
: [];
- res.send(events);
+ res.send(events.map(e => ({ ...e, user })));
} catch (e) {
console.log(e);
res.code(500).send("problem fetching user events");
diff --git a/routes/users.js b/routes/users.js
index 329210c8..4e6b8a63 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -619,7 +619,7 @@ app.get("/contacts", auth, async function(req, res) {
with_id: { [Sequelize.Op.ne]: null }
},
include: {
- attributes: ["username", "profile"],
+ attributes: ["username", "profile", "uuid"],
model: db.User,
as: "with"
},
@@ -666,9 +666,7 @@ app.get("/:pubkey/follows", async (req, res) => {
kinds: [3],
authors: [pubkey]
},
- 60000,
- 3600,
- 60000
+ { timeout: 60000, eager: 60000 }
).catch(nada);
let tags = JSON.parse(await redis.get(`${pubkey}:follows`)) || [];
@@ -722,9 +720,7 @@ app.get("/:pubkey/followers", async (req, res) => {
q(
`${pubkey}:followers`,
{ kinds: [3], "#p": [pubkey] },
- 60000,
- 3600,
- 60000
+ { timeout: 60000, eager: 60000 }
).catch(nada);
for (let pubkey of pubkeys) {