This repository was archived by the owner on Nov 24, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcomments.js
More file actions
402 lines (345 loc) · 11.5 KB
/
Copy pathcomments.js
File metadata and controls
402 lines (345 loc) · 11.5 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
async function comments_init(thread)
{
const relay = await Relay("wss://relay.damus.io")
const now = (new Date().getTime()) / 1000
const model = {events: [], profiles: {},treeEvents:[]}
const comments_id = uuidv4()
const profiles_id = uuidv4()
model.pool = relay
model.el = document.querySelector("#comments")
relay.subscribe(comments_id, {kinds: [1], "#e": [thread]})
relay.event = (sub_id, ev) => {
if (sub_id === comments_id) {
if (ev.content !== "")
insert_event_sorted(model.events, ev)
if (model.realtime)
render_home_view(model)
} else if (sub_id === profiles_id) {
try {
model.profiles[ev.pubkey] = JSON.parse(ev.content)
} catch {
console.log("failed to parse", ev.content)
}
}
}
// model.events.forEach((item) => {
// console.log(item)
// const node = {
// eventid:item.id,
// children:[]
// };
// console.log(node,'b')
// const parent = findParentEvent(item).id
// addNode(node, parent);
// });
// model.events = data
relay.eose = async (sub_id) => {
const data = []
const groupedEvents = {
eventid:thread,
children:[],
ev:{}
}
function addNode(node, parentNodeid) {
if (parentNodeid) {
const parent = findNode(parentNodeid, data);
if (!parent.children) {
parent.children = [];
}
parent.children.push(node);
} else {
data.push(node);
}
}
// Function to find a node by evid
function findNode(eventid, nodes) {
for (const node of nodes) {
if (node.eventid === eventid) {
return node;
}
if (node.children) {
const found = findNode(eventid, node.children);
if (found) {
return found;
}
}
}
return null;
}
data.push(groupedEvents)
for (const item of model.events) {
const node = {
eventid:item.id,
children:[],
ev:item
};
const parent = findParentEvent(item)
addNode(node, parent);
}
model.treeEvents = data
if (sub_id === comments_id) {
handle_comments_loaded(profiles_id, model)
} else if (sub_id === profiles_id) {
handle_profiles_loaded(profiles_id, model)
}
}
return relay
}
function handle_profiles_loaded(profiles_id, model) {
// stop asking for profiles
model.pool.unsubscribe(profiles_id)
model.realtime = true
render_home_view(model)
}
// load profiles after comment notes are loaded
function handle_comments_loaded(profiles_id, model)
{
const pubkeys = model.events.reduce((s, ev) => {
s.add(ev.pubkey)
return s
}, new Set())
const authors = Array.from(pubkeys)
// load profiles
model.pool.subscribe(profiles_id, {kinds: [0], authors: authors})
}
function render_home_view(model) {
render_events(model)
const delta = time_delta(new Date().getTime(), model.treeEvents[0].ev.created_at*1000)
model.el.appendChild(createTree(model.treeEvents[0], 0,model.treeEvents[0].ev,delta,model))
// console.log(model.el)
}
function createTree(data, depth = 0,ev,delta,model) {
const profile = model.profiles[data.ev.pubkey] || {
name: "anon",
display_name: "Anonymous",
}
const comment = document.createElement('div');
comment.classList.add('comment');
if ( ev.created_at > 1678191250 &&
ev.id !== "04ca02a37b216047eb1501bdd62f9c1a568f0c84940fc9f361cb299e74ade325"){
const commenthead = document.createElement('div');
commenthead.classList.add('commenthead');
comment.appendChild(commenthead);
const info = document.createElement('div');
info.classList.add('info');
const username = document.createElement('div');
username.classList.add('username');
ev = data.ev
const pfp = document.createElement('img');
pfp.classList.add('pfp');
pfp.setAttribute('src', get_picture(ev.pubkey, profile));
commenthead.appendChild(pfp);
username.textContent = sanitize(get_name(ev.pubkey, profile));
info.appendChild(username);
const deltaSpan = document.createElement('span');
deltaSpan.textContent = delta;
info.appendChild(deltaSpan);
commenthead.appendChild(info);
const content = document.createElement('p');
try {
content.innerHTML = format_content(ev.content);
} catch (e) {content.textContent = ""; }
comment.appendChild(content);
}
comment.style.marginLeft = `${depth * 5}px`; // increase indentation based on depth
if (data.children && data.children != []) {
const subtree = document.createElement('div');
let divname = (depth==0)?'comment':'subtree'
subtree.classList.add(divname);
// subtree.style.marginLeft = `${depth * 10}px`;
data.children.forEach(child => {
if (verifyBitcoinAddress(child.ev)) {
return;
}
let delta = time_delta(new Date().getTime(), child.ev.created_at*1000)
subtree.appendChild(createTree(child, depth + 1,child.ev,delta,model));
// data, depth = 0,ev,delta,model
// increase depth for child nodes
});
if (subtree.hasChildNodes()) {
comment.appendChild(subtree);
}
// console.log(comment)
}
return comment;
}
function render_events(model) {
const render = render_event.bind(null, model)
return model.events.map(render).join("\n")
}
function render_event(model, ev) {
const profile = model.profiles[ev.pubkey] || {
name: "anon",
display_name: "Anonymous",
}
let parsed = verifyBitcoinAddress(ev)
if(parsed) {
fundingEvent(parsed, profile, ev)
} else if (ev.created_at > 1678191250 && ev.pubkey !== "d91191e30e00444b942c0e82cad470b32af171764c2275bee0bd99377efd4075"){
const delta = time_delta(new Date().getTime(), ev.created_at*1000)
console.log(model.treeEvents[0],"im the three")
return createTree(model.treeEvents[0], 0,ev,delta,model)
// data, depth = 0,ev,delta,model
// return `
// <div class="comment">
// <div class="info">
// <div class="username">${sanitize(get_name(ev.pubkey, profile))}</div>
// <span>${delta}</span>
// </div>
// <img class="pfp" src="${get_picture(ev.pubkey, profile)}">
// <p>
// ${format_content(ev.content)}
// </p>
// </div>
// `
}
}
var funders = -1
var addresses = new Map;
var totalFunding = 0
function fundingEvent(parsed, profile, ev) {
try {
if(verifyBitcoinSignedEvent(window.NostrTools.nip19.npubEncode(ev.pubkey), parsed[0], parsed[1])) {
if (!addresses.get(parsed[0])) {
addresses.set(parsed[0], true)
let t = document.getElementById("funders")
let tr = document.createElement("tr")
tr.id = "table_row_"+ev.id
tr.appendChild(makeTd(funders+1))
let name = sanitize(get_name(ev.pubkey, profile))
let link = document.createElement("a")
link.href = "https://snort.social/e/" + window.NostrTools.nip19.noteEncode(ev.id)
link.innerText = name
let name_proof = makeTd()
name_proof.appendChild(link)
tr.appendChild(name_proof)
let amountRow = makeTd("Fetching amount....")
getBalance(parsed[0]).then(result => {
if(result) {
amountRow.innerText = result.toLocaleString()+ " sats";
totalFunding += result
if (document.getElementById("total_funding_row")) {
document.getElementById("total_funding_row").remove()
}
let tr_total = document.createElement("tr")
tr_total.id = "total_funding_row"
tr_total.append(makeTd(), makeTd("TOTAL"), makeTd(totalFunding.toLocaleString()+ " sats"))
t.appendChild(tr_total)
}
if (!result) {
if (result === 0) {
document.getElementById("table_row_"+ev.id).remove()
funders--
}
}
})
tr.appendChild(amountRow)
if (funders < 0) {
t.replaceChildren(tr)
} else {
t.appendChild(tr)
}
funders++
}
}
} catch (e) {}
}
function makeTd(inner) {
theadh = document.createElement("td")
try {theadh.appendChild(inner)} catch (e) {
if (typeof inner != "undefined") {
theadh.innerText = inner
}
}
return theadh
}
function convert_quote_blocks(content)
{
const split = content.split("\n")
let blockin = false
return split.reduce((str, line) => {
if (line !== "" && line[0] === '>') {
if (!blockin) {
str += "<span class='quote'>"
blockin = true
}
str += sanitize(line.slice(1))
} else {
if (blockin) {
blockin = false
str += "</span>"
}
str += sanitize(line)
}
return str + "<br/>"
}, "")
}
function format_content(content)
{
return convert_quote_blocks(content)
}
function sanitize(content)
{
if (!content)
return ""
return content.replaceAll("<","<").replaceAll(">",">")
}
function get_picture(pk, profile)
{
return sanitize(profile.picture) || "https://robohash.org/" + pk
}
function get_name(pk, profile={})
{
const display_name = profile.display_name || profile.user
const username = profile.name || "anon"
return display_name || username
}
function time_delta(current, previous) {
var msPerMinute = 60 * 1000;
var msPerHour = msPerMinute * 60;
var msPerDay = msPerHour * 24;
var msPerMonth = msPerDay * 30;
var msPerYear = msPerDay * 365;
var elapsed = current - previous;
if (elapsed < msPerMinute) {
return Math.round(elapsed/1000) + ' seconds ago';
}
else if (elapsed < msPerHour) {
return Math.round(elapsed/msPerMinute) + ' minutes ago';
}
else if (elapsed < msPerDay ) {
return Math.round(elapsed/msPerHour ) + ' hours ago';
}
else if (elapsed < msPerMonth) {
return Math.round(elapsed/msPerDay) + ' days ago';
}
else if (elapsed < msPerYear) {
return Math.round(elapsed/msPerMonth) + ' months ago';
}
else {
return Math.round(elapsed/msPerYear ) + ' years ago';
}
}
function findParentEvent(ev) {
const dataWithReply = ev.tags.find(
(tag) => tag[0] === "e" && tag[2] === "reply"
);
if (dataWithReply === undefined) {
let lastE = null;
for (let i = ev.tags.length - 1; i >= 0; i--) {
const tag = ev.tags[i];
if (tag.includes("e")) {
lastE = tag;
break;
}
}
return lastE[1];
} else {
return dataWithReply[1];
}
}