git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 5cffb63fdf14eb3f8d854d5bbc37d5282796f402

Files: 5cffb63fdf14eb3f8d854d5bbc37d5282796f402 / tools.js

17080 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
16
17module.exports.getBlocks = function (src) {
18 var blocks = h('div.blocks', 'Blocking: ')
19
20 pull(
21 sbot.query({query: [{$filter: { value: { author: src, content: {type: 'contact'}}}}], live: true}),
22 pull.drain(function (msg) {
23 if (msg.value) {
24 if (msg.value.content.blocking == true) {
25 console.log(msg.value)
26 var gotIt = document.getElementById('blocks:' + msg.value.content.contact.substring(0, 44))
27 if (gotIt == null) {
28 blocks.appendChild(h('a#blocks:'+ 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))))
29 }
30 }
31 if (msg.value.content.blocking == false) {
32 var gotIt = document.getElementById('blocks:' + msg.value.content.contact.substring(0, 44))
33 if (gotIt != null) {
34 gotIt.outerHTML = ''
35 }
36 }
37 }
38 })
39 )
40
41 return blocks
42
43}
44
45module.exports.getBlocked = function (src) {
46 var blocked = h('div.blocked', 'Blocked by: ')
47
48 pull(
49 sbot.query({query: [{$filter: { value: { content: {type: 'contact', contact: src}}}}], live: true}),
50 pull.drain(function (msg) {
51 if (msg.value) {
52 if (msg.value.content.blocking == true) {
53 console.log(msg.value)
54 var gotIt = document.getElementById('blocked:' + msg.value.content.contact.substring(0, 44))
55 if (gotIt == null) {
56 blocked.appendChild(h('a#blocked:'+ 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))))
57 }
58 }
59 if (msg.value.content.blocking == false) {
60 var gotIt = document.getElementById('blocked:' + msg.value.author.substring(0, 44))
61 if (gotIt != null) {
62 gotIt.outerHTML = ''
63 }
64 }
65 }
66 })
67 )
68
69 return blocked
70
71}
72
73module.exports.getFollowing = function (src) {
74 var followingCount = 0
75
76 var following = h('div.following', 'Following: ')
77
78 following.appendChild(h('span#followingcount', '0'))
79 following.appendChild(h('br'))
80
81 pull(
82 sbot.query({query: [{$filter: { value: { author: src, content: {type: 'contact'}}}}], live: true}),
83 pull.drain(function (msg) {
84 if (msg.value) {
85 if (msg.value.content.following == true) {
86 followingcount = document.getElementById('followingcount')
87 followingCount++
88 followingcount.textContent = followingCount
89 var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44))
90 if (gotIt == null) {
91 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))))
92 }
93 }
94 if (msg.value.content.following == false) {
95 followingcount = document.getElementById('followingcount')
96 followingCount--
97 followingcount.textContent = followingCount
98 var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44))
99 if (gotIt != null) {
100 gotIt.outerHTML = ''
101 }
102 }
103 }
104 })
105 )
106 return following
107}
108
109module.exports.getFollowers = function (src) {
110 var followerCount = 0
111
112 var followers = h('div.followers', 'Followers: ')
113
114 followers.appendChild(h('span#followercount', '0'))
115 followers.appendChild(h('br'))
116
117 pull(
118 sbot.query({query: [{$filter: { value: { content: {type: 'contact', contact: src}}}}], live: true}),
119 pull.drain(function (msg) {
120 if (msg.value) {
121 if (msg.value.content.following == true) {
122 followcount = document.getElementById('followercount')
123 followerCount++
124 followcount.textContent = followerCount
125 var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44))
126 if (gotIt == null) {
127 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))))
128 }
129 }
130 if (msg.value.content.following == false) {
131 followcount = document.getElementById('followercount')
132 followerCount--
133 followcount.textContent = followerCount
134 var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44))
135 if (gotIt != null) {
136 gotIt.outerHTML = ''
137 }
138 }
139 }
140 })
141 )
142
143 return followers
144}
145
146module.exports.queueButton = function (src) {
147 var queueButton = h('span.queue:' + src.key.substring(0,44))
148
149 var addToQueue = h('button.btn.right', 'Queue', {
150 onclick: function () {
151 var content = {
152 type: 'queue',
153 message: src.key,
154 queue: true
155 }
156 sbot.publish(content, function (err, publish) {
157 if (err) throw err
158 console.log(publish)
159 })
160 }
161 })
162
163 var removeFromQueue = h('button.btn.right#', 'Done', {
164 onclick: function () {
165 var content = {
166 type: 'queue',
167 message: src.key,
168 queue: false
169 }
170 sbot.publish(content, function (err, publish) {
171 if (err) throw err
172 console.log(publish)
173 if (window.location.hash.substring(1) == 'queue') {
174 setTimeout(function () {
175 var gotIt = document.getElementById(src.key.substring(0,44))
176 if (gotIt != null) {
177 gotIt.outerHTML = ''
178 }
179 }, 100)
180
181 }
182 })
183 }
184 })
185
186 pull(
187 sbot.query({query: [{$filter: { value: { author: id, content: {type: 'queue', message: src.key}}}}], live: true}),
188 pull.drain(function (msg) {
189 if (msg.value) {
190 if (msg.value.content.queue == true) {
191 queueButton.removeChild(queueButton.childNodes[0])
192 queueButton.appendChild(removeFromQueue)
193 }
194 if (msg.value.content.queue == false) {
195 queueButton.removeChild(queueButton.childNodes[0])
196 queueButton.appendChild(addToQueue)
197 }
198 }
199 })
200 )
201
202 queueButton.appendChild(addToQueue)
203
204 return queueButton
205}
206module.exports.block = function (src) {
207 var button = h('span.button')
208
209 var followButton = h('button.btn', 'Block (Private)', avatar.name(src), {
210 onclick: function () {
211 var content = {
212 type: 'contact',
213 contact: src,
214 blocking: true,
215 recps: id
216 }
217 sbot.publish(content, function (err, publish) {
218 if (err) throw err
219 console.log(publish)
220 })
221 }
222 })
223
224 var unfollowButton = h('button.btn', 'Unblock (Private)', avatar.name(src), {
225 onclick: function () {
226 var content = {
227 type: 'contact',
228 contact: src,
229 blocking: false,
230 recps: id
231 }
232 sbot.publish(content, function (err, publish) {
233 if (err) throw err
234 console.log(publish)
235 })
236 }
237 })
238
239 pull(
240 sbot.query({query: [{$filter: { value: { author: id, content: {type: 'contact', contact: src}}}}], live: true}),
241 pull.drain(function (msg) {
242 if (msg.value) {
243 if (msg.value.content.blocking == true) {
244 button.removeChild(button.firstChild)
245 button.appendChild(unfollowButton)
246 }
247 if (msg.value.content.blocking == false) {
248 button.removeChild(button.firstChild)
249 button.appendChild(followButton)
250 }
251 }
252 })
253 )
254
255 button.appendChild(followButton)
256
257 return button
258}
259
260module.exports.box = function (content) {
261 return ssbKeys.box(content, content.recps.map(function (e) {
262 return ref.isFeed(e) ? e : e.link
263 }))
264}
265
266module.exports.publish = function (content, cb) {
267 if(content.recps)
268 content = exports.box(content)
269 sbot.publish(content, function (err, msg) {
270 if(err) throw err
271 console.log('Published!', msg)
272 if(cb) cb(err, msg)
273 })
274}
275
276
277
278module.exports.follow = function (src) {
279 var button = h('span.button')
280
281 var followButton = h('button.btn', 'Follow ', avatar.name(src), {
282 onclick: function () {
283 var content = {
284 type: 'contact',
285 contact: src,
286 following: true
287 }
288 sbot.publish(content, function (err, publish) {
289 if (err) throw err
290 console.log(publish)
291 })
292 }
293 })
294
295 var unfollowButton = h('button.btn', 'Unfollow ', avatar.name(src), {
296 onclick: function () {
297 var content = {
298 type: 'contact',
299 contact: src,
300 following: false
301 }
302 sbot.publish(content, function (err, publish) {
303 if (err) throw err
304 console.log(publish)
305 })
306 }
307 })
308
309 pull(
310 sbot.query({query: [{$filter: { value: { author: id, content: {type: 'contact', contact: src}}}}], live: true}),
311 pull.drain(function (msg) {
312 if (msg.value) {
313 if (msg.value.content.following == true) {
314 button.removeChild(button.firstChild)
315 button.appendChild(unfollowButton)
316 }
317 if (msg.value.content.following == false) {
318 button.removeChild(button.firstChild)
319 button.appendChild(followButton)
320 }
321 }
322 })
323 )
324
325 button.appendChild(followButton)
326
327 return button
328}
329
330module.exports.box = function (content) {
331 return ssbKeys.box(content, content.recps.map(function (e) {
332 return ref.isFeed(e) ? e : e.link
333 }))
334}
335
336module.exports.publish = function (content, cb) {
337 if(content.recps)
338 content = exports.box(content)
339 sbot.publish(content, function (err, msg) {
340 if(err) throw err
341 console.log('Published!', msg)
342 if(cb) cb(err, msg)
343 })
344}
345
346
347
348module.exports.mute = function (src) {
349 if (!localStorage[src])
350 var cache = {mute: false}
351 else
352 var cache = JSON.parse(localStorage[src])
353
354 if (cache.mute == true) {
355 var mute = h('button.btn', 'Unmute', {
356 onclick: function () {
357 cache.mute = false
358 localStorage[src] = JSON.stringify(cache)
359 location.hash = '#'
360 location.hash = src
361 }
362 })
363 return mute
364 } else {
365 var mute = h('button.btn', 'Mute', {
366 onclick: function () {
367 cache.mute = true
368 localStorage[src] = JSON.stringify(cache)
369 location.hash = '#'
370 location.hash = src
371 }
372 })
373 return mute
374 }
375}
376
377module.exports.star = function (msg) {
378 var votebutton = h('span.star:' + msg.key.substring(0,44))
379
380 var vote = {
381 type: 'vote',
382 vote: { link: msg.key, expression: 'Star' }
383 }
384
385 if (msg.value.content.recps) {
386 vote.recps = msg.value.content.recps
387 }
388
389 var star = h('button.btn.right', 'Star ',
390 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
391 onclick: function () {
392 vote.vote.value = 1
393 if (vote.recps) {
394 vote = exports.box(vote)
395 }
396 sbot.publish(vote, function (err, voted) {
397 if(err) throw err
398 })
399 }
400 }
401 )
402
403 var unstar = h('button.btn.right ', 'Unstar ',
404 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
405 onclick: function () {
406 vote.vote.value = -1
407 sbot.publish(vote, function (err, voted) {
408 if(err) throw err
409 })
410 }
411 }
412 )
413
414 votebutton.appendChild(star)
415
416 pull(
417 sbot.links({rel: 'vote', dest: msg.key, live: true}),
418 pull.drain(function (link) {
419 if (link.key) {
420 sbot.get(link.key, function (err, data) {
421 if (err) throw err
422 if (data.content.vote) {
423 if (data.author == id) {
424 if (data.content.vote.value == 1)
425 votebutton.replaceChild(unstar, star)
426 if (data.content.vote.value == -1)
427 votebutton.replaceChild(star, unstar)
428 }
429 }
430 })
431 }
432 })
433 )
434
435 return votebutton
436}
437
438function votes (msg) {
439 var votes = h('div.votes')
440 if (msg.key) {
441 pull(
442 sbot.links({rel: 'vote', dest: msg.key/*, live: true*/ }),
443 pull.drain(function (link) {
444 if (link.key) {
445 sbot.get(link.key, function (err, data) {
446 if (err) throw err
447 if (data.content.vote) {
448 if (data.content.vote.value == 1) {
449 if (localStorage[data.author + 'name'])
450 name = localStorage[data.author + 'name']
451 else
452 name = data.author
453 votes.appendChild(h('a#vote:' + data.author.substring(0, 44), {href:'#' + data.author, title: name}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
454 }
455 else if (data.content.vote.value == -1) {
456 var lookFor = 'vote:' + data.author.substring(0, 44)
457 document.getElementById(lookFor, function (err, gotit) {
458 if (err) throw err
459 gotit.parentNode.removeChild(remove)
460 })
461 }
462 }
463 })
464 }
465 })
466 )
467 }
468 return votes
469}
470
471module.exports.timestamp = function (msg, edited) {
472 var timestamp
473 if (edited)
474 timestamp = h('span.timestamp', 'Edited by: ', h('a', {href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author))), h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
475 else
476 timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
477 return timestamp
478}
479
480
481module.exports.mini = function (msg, content) {
482 var mini = h('div.mini')
483
484 mini.appendChild(
485 h('span.avatar',
486 h('a', {href: '#' + msg.value.author},
487 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
488 avatar.cachedName(msg.value.author)
489 )
490 )
491 )
492 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
493
494
495 mini.appendChild(h('span', content))
496 mini.appendChild(exports.timestamp(msg))
497
498 if (msg.value.content.recps) {
499 mini.appendChild(lock)
500 }
501
502 if (typeof msg.value.content === 'string') {
503 mini.appendChild(lock)
504 }
505
506 return mini
507}
508
509module.exports.header = function (msg) {
510 var header = h('div.header')
511
512 header.appendChild(h('span.avatar',
513 h('a', {href: '#' + msg.value.author},
514 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
515 avatar.cachedName(msg.value.author)
516 )
517 )
518 )
519
520 header.appendChild(exports.timestamp(msg))
521 header.appendChild(votes(msg))
522
523 if (msg.value.private) {
524 header.appendChild(h('span.right', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})))
525 }
526 if (msg.value.content.type == 'edit') {
527 header.appendChild(h('span.right', ' Edited: ', h('a', {href: '#' + msg.value.content.original}, exports.messageLink(msg.value.content.original))))
528 }
529 return header
530}
531
532
533
534
535module.exports.messageName = function (id, cb) {
536 // gets the first few characters of a message, for message-link
537 function title (s) {
538 var m = /^\n*([^\n]{0,40})/.exec(s)
539 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
540 }
541
542 sbot.get(id, function (err, value) {
543 if(err && err.name == 'NotFoundError')
544 return cb(null, id.substring(0, 10)+'...(missing)')
545 if(value.content.type === 'post' && 'string' === typeof value.content.text)
546 return cb(null, title(value.content.text))
547 else if('string' === typeof value.content.text)
548 return cb(null, value.content.type + ':'+title(value.content.text))
549 else
550 return cb(null, id.substring(0, 10)+'...')
551 })
552}
553
554var messageName = exports.messageName
555
556module.exports.messageLink = function (id) {
557 if (ref.isMsg(id)) {
558 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
559 messageName(id, function (err, name) {
560 if(err) console.error(err)
561 else link.textContent = name
562 })
563 } else {
564 var link = id
565 }
566 return link
567}
568
569module.exports.rawJSON = function (obj) {
570 return JSON.stringify(obj, null, 2)
571 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
572 .map(function (e) {
573 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
574 return h('a', {href: '#' + e}, e)
575 }
576 return e
577 })
578}
579
580var markdown = require('ssb-markdown')
581var config = require('./config')()
582
583module.exports.markdown = function (msg, md) {
584 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
585 if(url[0] == '%' || url[0] == '@' || url[0] == '#') return '#' + url
586 if(url[0] !== '&') return url
587 //if(url[0] == '&') return config.blobsUrl + url
588 //if(!image) return url
589 return config.blobsUrl + url
590 }})}
591}
592

Built with git-ssb-web