-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsamizdat.js
222 lines (208 loc) · 7.47 KB
/
samizdat.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
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
//todo ignition
const rootSamizdatId = "9e333343184fe3e98b028782f7098cf596f1f46adf546541e7317d9a5f1d5d57"
let samizdatReady = false
function waitForSamizdatReady(callback) {
var interval = setInterval(function() {
if (samizdatReady) {
clearInterval(interval);
callback();
}
}, 200);
}
function displaySamizdat() {
document.getElementById("maincontent").replaceChildren(prepWindow())
document.getElementById("heading").innerText = "Samizdatree"// spontaneously organising tree of self-published notes and other stuff
document.getElementById("heading").appendChild(spacer())
document.getElementById("content").replaceChildren(loadingSign())
waitForSamizdatReady(function () {
getMuhPubkey().then(pubkey => {
document.getElementById("heading").appendChild(helpButton("58b02c07e1971ad9178293e0c0d39509a715db35ba3a989ed97d25c6f8e9ad07", pubkey))
renderSamizdat(pubkey)
})
})
rewriteURL("samizdat")
}
function spacer() {
s = document.createElement("span")
s.innerText = " "
return s
}
function helpButton(doki, pubkey) {
a = document.createElement("a")
a.onclick = function () {
saz = dokiObjects.get(doki)
if (saz !== undefined) {
document.getElementById("infomodal").classList.add('is-active')
document.getElementById("modal-heading").innerText = document.getElementById("heading").innerText + " Help"
md = new showdown.Converter({
extensions: [...bindings]
})
ht = md.makeHtml(saz.CurrentTip)
mdht = document.createElement("div")
mdht.innerHTML = ht
mdht.className = "content"
document.getElementById("modal-body").replaceChildren(mdht)//
edit = document.createElement("button")
edit.className = "button is-link"
edit.innerText = "Edit this"
edit.onclick = function () {
if (!accountIsInIdentityTree(pubkey)) {
alert("You must be in the Identity Tree to submit edits")
}
setURLID("doki_id", doki)
}
document.getElementById("modal-body").appendChild(edit)
}
}
btn = document.createElement("i")
btn.className = "fas fa-question-circle"
a.appendChild(btn)
return a
}
function renderSamizdat(pubkey) {
box = document.createElement("div")
box.className = "content"
document.getElementById("content").replaceChildren(box)
// if (samizdatObjects.size === 0) {
// document.getElementById("details").replaceChildren(newSamizdatForm())
// }
samizdatObjects.forEach(function (event) {
if (event.id === rootSamizdatId) {
box.appendChild(oneSamizdat(event, pubkey))
}
event.tags.forEach(function (tags) {
if (tags[tags.length-1].length === 64) {
p = document.getElementById(tags[tags.length-1])
if (p !== null) {
p.appendChild(oneSamizdat(event, pubkey))
}
}
})
})
}
function oneSamizdat(event, pubkey) {
var ident
identityObjects.forEach(function (object) {
if (object.Account === event.pubkey) {
ident = object
}
})
//todo if we don't have the identity, ask relays for kind0 event from this pubkey
//console.log(event)
article = document.createElement("article")
article.className = "media"
figure = document.createElement("figure")
figure.className = "media-left"
pic = document.createElement("p")
pic.className = "image is-64x64"
pic.innerHTML = `<img src="https://bulma.io/images/placeholders/128x128.png">`
figure.appendChild(pic)
article.appendChild(figure)
mcontent = document.createElement("div")
mcontent.className = "media-content"
content = document.createElement("div")
content.className = "content"
contentNameAndText = document.createElement("p")
contentName = document.createElement("a")
contentName.innerText = (((typeof ident === "object") && (ident.Name.length > 0)) ? ident.Name : event.pubkey) + " "
if (typeof ident === "object") {
if (ident.UshBy.length > 0) {
contentName.appendChild(bluecheck())
}
}
contentName.onclick = function () {
setURLID("profile_id", event.pubkey)
}
contentNameAndText.appendChild(contentName)
//contentText = document.createElement("p")
md = new showdown.Converter({
extensions: [...bindings]
})
ht = md.makeHtml(event.content)
mdht = document.createElement("div")
mdht.innerHTML = ht
//contentText.innerText = event.content
contentNameAndText.appendChild(mdht)
content.appendChild(contentNameAndText)
mcontent.appendChild(content)
nav = document.createElement("nav")
nav.className = "level is-mobile"
left = document.createElement("div")
left.className = "level-left"
reply = document.createElement("a")
reply.className = "level-item"
reply.onclick = function () {
b = document.getElementById( 'replyform' )
if (b !== null) {b.remove()}
if (!hasPermanym(pubkey)) {
alert("You should really create a username before commenting here")
}
item = document.getElementById(event.id + "reply")//append(newSamizdatForm(event.id, event.content))//appendChild(newSamizdatForm(event.id, event.content))
item.appendChild(newSamizdatForm(event.id, pubkey))
}
span = document.createElement("span")
span.className = "icon is-small"
icon = document.createElement("i")
icon.className = "fas fa-reply"
span.appendChild(icon)
reply.appendChild(span)
left.appendChild(reply)
nav.appendChild(left)
mcontent.appendChild(nav)
mcontent.id = event.id
replydiv = document.createElement("div")
replydiv.id = event.id + "reply"
mcontent.appendChild(replydiv)
article.appendChild(mcontent)
return article
}
function bluecheck(type) {
span = document.createElement("span")
span.className = "icon is-small"
icon = document.createElement("i")
icon.className = "fa-solid fa-user-check"
span.appendChild(icon)
return span
}
function newSamizdatForm(id, pubkey) {
form = document.createElement("div")
form.className = "field"
form.id = "replyform"
ta = document.createElement("textarea")
ta.className = "textarea"
ta.placeholder = "Type your reply here"
ta.id = "reply input"
ta.maxLength = 560
btn = document.createElement("button")
btn.className = "button is-link"
btn.innerText = "Reply"
btn.onclick = function () {
broadcast( document.getElementById( 'reply input' ).value, id, pubkey)
}
btnc = document.createElement("button")
btnc.className = "button is-link is-light"
btnc.innerText = "Cancel"
btnc.onclick = function () {
b = document.getElementById( 'replyform' )
b.remove()
}
form.appendChild(ta)
form.appendChild(btn)
form.appendChild(btnc)
return form
}
function broadcast(content, id, pubkey) {
etags = []
if (id !== undefined) {
if (id.length === 64) {
etags.push(rootSamizdatId)
if (id !== rootSamizdatId) {
etags.push(id)
}
}
}
sendEventToMindmachine(content, etags, 1, pubkey).then(sent => {
console.log(sent)
location.reload()
})
}