-
Notifications
You must be signed in to change notification settings - Fork 0
/
nostr.js
130 lines (123 loc) · 5.3 KB
/
nostr.js
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
//const socket = new WebSocket('wss://nos.lol');
//var eose = false;
const defaultServers = ["wss://nos.lol", "wss://brb.io", "wss://relay.nostr.info", "wss://relay.damus.io"]
//todo: properly handle lightning instead of just removing them lol
//i know that removing t.me links is wrong, theres just too much chinese spam right now to tolerate
const spam = /(.*\u2588.*)|(^[0-9 ]+$)|(^lnbc\S*$)|(damusvip)|(t.me)/;
var identities = new Object;
var EventArray = new Array;
var TimeStampPostArray = new Array;
const objectComparisonCallback = (arrayItemA, arrayItemB) => {
if (arrayItemA.timestamp < arrayItemB.timestamp) {
return -1
}
if (arrayItemA.timestamp > arrayItemB.timestamp) {
return 1
}
return 0
}
var servers = new Object;
function connect(url) {
let socket = new WebSocket(url);
let eose = false;
socket.addEventListener('open', (event) => {
console.log(`${url} open`)
socket.send(`["REQ", "identities", {"kinds": [0]}]`);
//socket.send(`["REQ", "posts", {"kinds": [1]}]`);
socket.send(`["REQ", "relays", {"kinds": [2], "limit": 10}]`);
});
let server = {socket: socket, eose: eose, url: url}
socket.addEventListener('message', (event) => {handleMessage(event, server)});
servers[btoa(url)] = server;
}
function handleMessage(event, server) {
let data = JSON.parse(event.data)
switch(data[0]) {
case "EVENT": {
let key = data[2].pubkey
let id = data[2].id
console.log(`new event from ${server.url}`)
if (!EventArray.includes(id)) {
EventArray.push(id);
switch(data[2].kind) {
case 0: {
let iden = JSON.parse(data[2].content)
if (iden != undefined) {
identities[key] = iden
}
break;
}
case 1: {
if (spam.test(data[2].content)) {break;};
TimeStampPostArray.push({id: id, timestamp: data[2].created_at*1000});
TimeStampPostArray.sort(objectComparisonCallback);
let display = (function () {if (key in identities) {return identities[key].display_name || identities[key].name} else {return key.substring(0,8)};})();
let posts = document.getElementById("posts")
let element = document.createElement('li');
let avatar = (function(){
if ((key in identities) && identities[key].picture != undefined) {
return `<img loading="lazy" src="${DOMPurify.sanitize(identities[key].picture)}" class="avatar">`;
} else {return '';}
})();
element.setAttribute('id', id);
element.innerHTML = `${avatar} <p class="name" title=${key}>${DOMPurify.sanitize(display)}</p><a class="relay small">${server.url}</a> <a class="date small">${new Date(data[2].created_at*1000).toLocaleString()}</a> <a class="content">${DOMPurify.sanitize(marked.parse(data[2].content))}</a>`
/*switch(server.eose) {
case false: {
posts.appendChild(element);
break;
}
case true: {
posts.insertBefore(element, posts.firstChild)
}
}*/
let index = TimeStampPostArray.findIndex(x => x.id === id) - 1
if (TimeStampPostArray[index] != undefined) {
posts.insertBefore(element, document.getElementById(TimeStampPostArray[index].id))
} else {
posts.appendChild(element);
}
break;
}
case 2: {
console.log(`new server found: ${data[2].content}`);
//chujkurwa
/*if(servers[btoa(data[2].content)] == undefined) {
connect(data[2].content)
};
break;*/
}
}
}
break;
}
case "EOSE": {
switch(data[1]) {
case "posts": {
server.eose = true
break;
}
case "identities": {
server.socket.send(`["REQ", "posts", {"kinds": [1], "limit": 100}]`);
break;
}
}
//console.log(data)
break;
};
default: {console.log(`unhandled ${data}`)}
}
};
function ensureEvent(id) {
if (!EventArray.includes(id)) {
console.log(`ensuring ${id}`);
for (const server of Object.entries(servers)) {
server[1].socket.send(`["REQ", "${id.substring(0,8)}", {"ids": ["${id}"]}]`)
server[1].socket.send(`["CLOSE", "${id.substring(0,8)}"]`)
}
}
}
function post(server, message, key) {
}
for (const relay of defaultServers) {
connect(relay);
}