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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
import { nada, uniq } from "$lib/utils";
import config from "$config";
import { g, s, db } from "$lib/db";
import { pool, q } from "$lib/nostr";
export default {
async event({ params: { id } }, res) {
let event = await g(`ev:${id}`);
let { pubkey } = event;
event.user = (await g(`user:${pubkey}`)) || {
username: pubkey.substr(0, 6),
pubkey,
anon: true,
follows: [],
followers: []
};
res.send(event);
},
async notes({ params: { pubkey } }, res) {
let user = await g(`user:${pubkey}`);
if (!user)
user = {
username: pubkey.substr(0, 6),
pubkey,
anon: true,
follows: [],
followers: []
};
let params = {
kinds: [1],
authors: [pubkey]
},
opts = { since: 0 };
q(`${pubkey}:notes`, params, opts).catch(nada);
await s(`user:${pubkey}`, user);
let ids = await db.sMembers(pubkey);
let events = ids.length
? (await db.mGet(ids.map(k => "ev:" + k))).map(JSON.parse)
: [];
res.send(events.map(e => ({ ...e, user })));
},
async broadcast(req, res) {
let { event } = req.body;
pool.send(["EVENT", event]);
res.send(event);
},
async follows({ params: { pubkey }, query: { tagsonly } }, res) {
let sub = `${pubkey}:follows`,
params = {
limit: 1,
kinds: [3],
authors: [pubkey]
},
opts = { timeout: 60000, eager: 60000 };
q(sub, params, opts).catch(nada);
let tags = (await g(`${pubkey}:follows`)) || [];
if (tagsonly) return res.send(tags);
let follows = [];
for (let f of tags) {
let [_, pubkey] = f;
q(`${pubkey}:profile:f1`, {
limit: 1,
kinds: [0],
authors: [pubkey]
}).catch(nada);
let user = await g(`user:${pubkey}`);
if (!user)
user = {
username: pubkey.substr(0, 6),
pubkey,
anon: true
};
follows.push(user);
}
follows = uniq(follows, e => e.pubkey);
follows.sort((a, b) => a.username.localeCompare(b.username));
res.send(follows);
},
async followers({ params: { pubkey } }, res) {
try {
let pubkeys = [
...new Set([...(await db.sMembers(`${pubkey}:followers`))])
];
let followers = [];
q(
`${pubkey}:followers`,
{ kinds: [3], "#p": [pubkey] },
{ timeout: 60000, eager: 60000 }
).catch(nada);
for (let pubkey of pubkeys) {
let user = await g(`user:${pubkey}`);
if (!user || !user.updated) {
q(`${pubkey}:profile:f2`, {
limit: 1,
kinds: [0],
authors: [pubkey]
}).catch(nada);
user = await g(`user:${pubkey}`);
}
if (!user)
user = {
username: pubkey.substr(0, 6),
pubkey,
anon: true
};
followers.push(user);
}
followers = uniq(followers, e => e.pubkey);
followers.sort((a, b) => a.username.localeCompare(b.username));
res.send(followers);
} catch (e) {
console.log(e);
res.code(500).send(e && e.message);
}
},
async identities(req, res) {
res.send({
names: {
adam:
"566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8",
asoltys:
"566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8"
}
});
}
};
|