git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: f0fdaeb91fa797d297d56d29a35a9c40cb5be312

Files: f0fdaeb91fa797d297d56d29a35a9c40cb5be312 / tools.js

13356 bytesRaw
1var h = require('hyperscript')
2var human = require('human-time')
3var avatar = require('./avatar')
4var ref = require('ssb-ref')
5
6var ssbKeys = require('ssb-keys')
7
8var pull = require('pull-stream')
9
10var sbot = require('./scuttlebot')
11
12var config = require('./config')()
13
14var id = require('./keys').id
15
16module.exports.getFollowing = function (src) {
17 var followingCount = 0
18
19 var following = h('div.following', 'Following: ')
20
21 following.appendChild(h('span#followingcount', '0'))
22 following.appendChild(h('br'))
23
24 pull(
25 sbot.query({query: [{$filter: { value: { author: src, content: {type: 'contact'}}}}], live: true}),
26 pull.drain(function (msg) {
27 if (msg.value) {
28 if (msg.value.content.following == true) {
29 followingcount = document.getElementById('followingcount')
30 followingCount++
31 followingcount.textContent = followingCount
32 var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44))
33 if (gotIt == null) {
34 following.appendChild(h('a#following:'+ msg.value.content.contact.substring(0, 44), {title: avatar.cachedName(msg.value.content.contact).textContent, href: '#' + msg.value.content.contact}, h('span.avatar--small', avatar.cachedImage(msg.value.content.contact))))
35 }
36 }
37 if (msg.value.content.following == false) {
38 followingcount = document.getElementById('followingcount')
39 followingCount--
40 followingcount.textContent = followingCount
41 var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44))
42 if (gotIt != null) {
43 gotIt.outerHTML = ''
44 }
45 }
46 }
47 })
48 )
49 return following
50/* var count = 0
51
52 var following = h('div.following')
53
54 following.appendChild(h('span', 'Following: ' + count))
55 following.appendChild(h('br'))
56
57 sbot.friends.get({source: src}, function (err, follows) {
58 for (var i in follows) {
59 if (follows.hasOwnProperty(i)) {
60 if (follows[i] == true)
61 following.appendChild(h('a', {title: avatar.name(i).textContent, href: '#' + i}, h('span.avatar--small', avatar.image(i))))
62 count++
63 following.firstChild.textContent = 'Following: ' + count
64 }
65 }
66 })
67
68 return following
69*/
70}
71
72module.exports.getFollowers = function (src) {
73 var followerCount = 0
74
75 var followers = h('div.followers', 'Followers: ')
76
77 followers.appendChild(h('span#followercount', '0'))
78 followers.appendChild(h('br'))
79
80 pull(
81 sbot.query({query: [{$filter: { value: { content: {type: 'contact', contact: src}}}}], live: true}),
82 pull.drain(function (msg) {
83 if (msg.value) {
84 if (msg.value.content.following == true) {
85 followcount = document.getElementById('followercount')
86 followerCount++
87 followcount.textContent = followerCount
88 var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44))
89 if (gotIt == null) {
90 followers.appendChild(h('a#followers:'+ msg.value.author.substring(0, 44), {title: avatar.cachedName(msg.value.author).textContent, href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author))))
91 }
92 }
93 if (msg.value.content.following == false) {
94 followcount = document.getElementById('followercount')
95 followerCount--
96 followcount.textContent = followerCount
97 var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44))
98 if (gotIt != null) {
99 gotIt.outerHTML = ''
100 }
101 }
102 }
103 })
104 )
105
106 return followers
107
108
109 /*var count = 0
110
111 var followers = h('div.followers')
112
113 followers.appendChild(h('span', 'Followers: ' + count))
114 followers.appendChild(h('br'))
115
116 sbot.friends.get({dest: src}, function (err, follows) {
117 for (var i in follows) {
118 if (follows.hasOwnProperty(i)) {
119 if (follows[i] == true) {
120 followers.appendChild(h('a', {title: avatar.name(i).textContent, href: '#' + i}, h('span.avatar--small', avatar.image(i))))
121 count++
122 followers.firstChild.textContent = 'Followers: ' + count
123 }
124 }
125 }
126 })
127
128
129 return followers*/
130}
131
132module.exports.follow = function (src) {
133 var button = h('span.button')
134
135 var followButton = h('button.btn', 'Follow ', avatar.name(src), {
136 onclick: function () {
137 var content = {
138 type: 'contact',
139 contact: src,
140 following: true
141 }
142 sbot.publish(content, function (err, publish) {
143 if (err) throw err
144 console.log(publish)
145 })
146 }
147 })
148
149 var unfollowButton = h('button.btn', 'Unfollow ', avatar.name(src), {
150 onclick: function () {
151 var content = {
152 type: 'contact',
153 contact: src,
154 following: false
155 }
156 sbot.publish(content, function (err, publish) {
157 if (err) throw err
158 console.log(publish)
159 })
160 }
161 })
162
163 pull(
164 sbot.query({query: [{$filter: { value: { author: id, content: {type: 'contact', contact: src}}}}], live: true}),
165 pull.drain(function (msg) {
166 if (msg.value) {
167 if (msg.value.content.following == true) {
168 button.removeChild(button.firstChild)
169 button.appendChild(unfollowButton)
170 }
171 if (msg.value.content.following == false) {
172 button.removeChild(button.firstChild)
173 button.appendChild(followButton)
174 }
175 }
176 })
177 )
178
179 button.appendChild(followButton)
180
181 return button
182}
183
184module.exports.box = function (content) {
185 return ssbKeys.box(content, content.recps.map(function (e) {
186 return ref.isFeed(e) ? e : e.link
187 }))
188}
189
190module.exports.publish = function (content, cb) {
191 if(content.recps)
192 content = exports.box(content)
193 sbot.publish(content, function (err, msg) {
194 if(err) throw err
195 console.log('Published!', msg)
196 if(cb) cb(err, msg)
197 })
198}
199
200module.exports.mute = function (src) {
201 if (!localStorage[src])
202 var cache = {mute: false}
203 else
204 var cache = JSON.parse(localStorage[src])
205
206 if (cache.mute == true) {
207 var mute = h('button.btn', 'Unmute', {
208 onclick: function () {
209 cache.mute = false
210 localStorage[src] = JSON.stringify(cache)
211 location.hash = '#'
212 location.hash = src
213 }
214 })
215 return mute
216 } else {
217 var mute = h('button.btn', 'Mute', {
218 onclick: function () {
219 cache.mute = true
220 localStorage[src] = JSON.stringify(cache)
221 location.hash = '#'
222 location.hash = src
223 }
224 })
225 return mute
226 }
227}
228
229module.exports.star = function (msg) {
230 var votebutton = h('span.star:' + msg.key.substring(0,44))
231
232 var vote = {
233 type: 'vote',
234 vote: { link: msg.key, expression: 'Star' }
235 }
236 console.log(msg)
237 if (msg.value.content.recps) {
238 vote.recps = msg.value.content.recps
239 }
240
241 var star = h('button.btn.right', 'Star ',
242 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
243 onclick: function () {
244 vote.vote.value = 1
245 if (vote.recps) {
246 vote = exports.box(vote)
247 }
248 sbot.publish(vote, function (err, voted) {
249 if(err) throw err
250 })
251 }
252 }
253 )
254
255 var unstar = h('button.btn.right ', 'Unstar ',
256 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
257 onclick: function () {
258 vote.vote.value = -1
259 sbot.publish(vote, function (err, voted) {
260 if(err) throw err
261 })
262 }
263 }
264 )
265
266 votebutton.appendChild(star)
267
268 pull(
269 sbot.links({rel: 'vote', dest: msg.key, live: true}),
270 pull.drain(function (link) {
271 if (link.key) {
272 sbot.get(link.key, function (err, data) {
273 if (err) throw err
274 if (data.content.vote) {
275 if (data.author == id) {
276 if (data.content.vote.value == 1)
277 votebutton.replaceChild(unstar, star)
278 if (data.content.vote.value == -1)
279 votebutton.replaceChild(star, unstar)
280 }
281 }
282 })
283 }
284 })
285 )
286
287 return votebutton
288}
289
290function votes (msg) {
291 var votes = h('div.votes')
292 if (msg.key) {
293 pull(
294 sbot.links({rel: 'vote', dest: msg.key, live: true }),
295 pull.drain(function (link) {
296 if (link.key) {
297 sbot.get(link.key, function (err, data) {
298 if (err) throw err
299 if (data.content.vote) {
300 if (data.content.vote.value == 1) {
301 if (localStorage[data.author + 'name'])
302 name = localStorage[data.author + 'name']
303 else
304 name = data.author
305 votes.appendChild(h('a#vote:' + data.author.substring(0, 44), {href:'#' + data.author, title: name}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
306 }
307 else if (data.content.vote.value == -1) {
308 var lookFor = 'vote:' + data.author.substring(0, 44)
309 document.getElementById(lookFor, function (err, gotit) {
310 if (err) throw err
311 gotit.parentNode.removeChild(remove)
312 })
313 }
314 }
315 })
316 }
317 })
318 )
319 }
320 return votes
321}
322
323module.exports.timestamp = function (msg, edited) {
324 var timestamp
325 if (edited)
326 timestamp = h('span.timestamp', 'Edited: ', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
327 else
328 timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
329 return timestamp
330}
331
332
333module.exports.mini = function (msg, content) {
334 var mini = h('div.mini')
335
336 mini.appendChild(
337 h('span.avatar',
338 h('a', {href: '#' + msg.value.author},
339 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
340 avatar.cachedName(msg.value.author)
341 )
342 )
343 )
344 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
345
346
347 mini.appendChild(h('span', content))
348 mini.appendChild(exports.timestamp(msg))
349
350 if (msg.value.content.recps) {
351 mini.appendChild(lock)
352 }
353
354 if (typeof msg.value.content === 'string') {
355 mini.appendChild(lock)
356 }
357
358 return mini
359}
360
361
362module.exports.header = function (msg) {
363 var header = h('div.header')
364
365 header.appendChild(h('span.avatar',
366 h('a', {href: '#' + msg.value.author},
367 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
368 avatar.cachedName(msg.value.author)
369 )
370 )
371 )
372
373 header.appendChild(exports.timestamp(msg))
374 header.appendChild(votes(msg))
375
376 if (msg.value.private)
377 header.appendChild(h('span.right', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})))
378
379 return header
380}
381
382var ref = require('ssb-ref')
383
384module.exports.messageName = function (id, cb) {
385 // gets the first few characters of a message, for message-link
386 function title (s) {
387 var m = /^\n*([^\n]{0,40})/.exec(s)
388 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
389 }
390
391 sbot.get(id, function (err, value) {
392 if(err && err.name == 'NotFoundError')
393 return cb(null, id.substring(0, 10)+'...(missing)')
394 if(value.content.type === 'post' && 'string' === typeof value.content.text)
395 return cb(null, title(value.content.text))
396 else if('string' === typeof value.content.text)
397 return cb(null, value.content.type + ':'+title(value.content.text))
398 else
399 return cb(null, id.substring(0, 10)+'...')
400 })
401}
402
403var messageName = exports.messageName
404var ref = require('ssb-ref')
405
406module.exports.messageLink = function (id) {
407 if (ref.isMsg(id)) {
408 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
409 messageName(id, function (err, name) {
410 if(err) console.error(err)
411 else link.textContent = name
412 })
413 } else {
414 var link = id
415 }
416 return link
417}
418
419
420/*module.exports.messageLink = function (msglink) {
421 var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 44) + '...'))
422
423 if (ref.isMsg(msglink)) {
424 pull(
425 sbot.get(msglink, function (err, data) {
426 console.log(data)
427 if(err && err.name == 'NotFoundError') {
428 var newlink = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 35) + ' (Missing)...'))
429 }
430 if(data.content.type === 'post' && 'string' === typeof data.content.text) {
431 var newlink = h('span', h('a', {href: '#' + msglink}, data.content.text.substring(0, 44) + '...'))
432 }
433 else {
434 var newlink = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 44) + '...'))
435 }
436 if (link) {
437 link.parentNode.replaceChild(newlink, link)
438 }
439 })
440 )
441 }
442
443 return link
444}*/
445
446module.exports.rawJSON = function (obj) {
447 return JSON.stringify(obj, null, 2)
448 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
449 .map(function (e) {
450 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
451 return h('a', {href: '#' + e}, e)
452 }
453 return e
454 })
455}
456
457var markdown = require('ssb-markdown')
458var config = require('./config')()
459
460module.exports.markdown = function (msg, md) {
461 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
462 if(url[0] == '%' || url[0] == '@') return '#' + url
463 if(!image) return url
464 if(url[0] !== '&') return url
465 return config.blobsUrl + url
466 }})}
467}
468

Built with git-ssb-web