git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: d2b5693b01842e3376dae3c3cf5c579e03e490c4

Files: d2b5693b01842e3376dae3c3cf5c579e03e490c4 / mvd / tools.js

17338 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 while (votebutton.firstChild) {
425 votebutton.removeChild(votebutton.firstChild)
426 }
427 if (data.content.vote.value == 1)
428 //votebutton.removeChild(votebutton.childNodes[0])
429 votebutton.appendChild(unstar)
430 if (data.content.vote.value == -1)
431 //votebutton.removeChild(votebutton.childNodes[0])
432 votebutton.appendChild(star)
433 }
434 }
435 })
436 }
437 })
438 )
439
440 return votebutton
441}
442
443function votes (msg) {
444 var votes = h('div.votes.right')
445 if (msg.key) {
446 pull(
447 sbot.links({rel: 'vote', dest: msg.key, live: true }),
448 pull.drain(function (link) {
449 if (link.key) {
450 sbot.get(link.key, function (err, data) {
451 if (err) throw err
452 if (data.content.vote) {
453 if (data.content.vote.value == 1) {
454 if (localStorage[data.author + 'name'])
455 name = localStorage[data.author + 'name']
456 else
457 name = data.author
458 votes.appendChild(h('a#vote:' + data.author.substring(0, 44), {href:'#' + data.author, title: name}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
459 }
460 else if (data.content.vote.value == -1) {
461 var lookFor = 'vote:' + data.author.substring(0, 44)
462 document.getElementById(lookFor, function (err, gotit) {
463 if (err) throw err
464 gotit.parentNode.removeChild(remove)
465 })
466 }
467 }
468 })
469 }
470 })
471 )
472 }
473 return votes
474}
475
476module.exports.timestamp = function (msg, edited) {
477 var timestamp
478 if (edited)
479 timestamp = h('span.timestmap.right', '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))))
480 else
481 timestamp = h('span.timestamp.right', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
482 return timestamp
483}
484
485
486module.exports.mini = function (msg, content) {
487 var mini = h('div.mini')
488
489 mini.appendChild(
490 h('span.avatar',
491 h('a', {href: '#' + msg.value.author},
492 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
493 avatar.cachedName(msg.value.author)
494 )
495 )
496 )
497 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
498
499
500 mini.appendChild(h('span', content))
501 mini.appendChild(exports.timestamp(msg))
502
503 if (msg.value.content.recps) {
504 mini.appendChild(lock)
505 }
506
507 if (typeof msg.value.content === 'string') {
508 mini.appendChild(lock)
509 }
510
511 return mini
512}
513
514module.exports.header = function (msg) {
515 var header = h('div.header')
516
517 header.appendChild(h('span.avatar',
518 h('a', {href: '#' + msg.value.author},
519 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
520 avatar.cachedName(msg.value.author)
521 )
522 )
523 )
524
525 header.appendChild(exports.timestamp(msg))
526 header.appendChild(votes(msg))
527
528 if (msg.value.private) {
529 header.appendChild(h('span.right', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})))
530 }
531 if (msg.value.content.type == 'edit') {
532 header.appendChild(h('span.right', ' Edited: ', h('a', {href: '#' + msg.value.content.original}, exports.messageLink(msg.value.content.original))))
533 }
534 return header
535}
536
537
538
539
540module.exports.messageName = function (id, cb) {
541 // gets the first few characters of a message, for message-link
542 function title (s) {
543 var m = /^\n*([^\n]{0,40})/.exec(s)
544 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
545 }
546
547 sbot.get(id, function (err, value) {
548 if(err && err.name == 'NotFoundError')
549 return cb(null, id.substring(0, 10)+'...(missing)')
550 if(value.content.type === 'post' && 'string' === typeof value.content.text)
551 return cb(null, title(value.content.text))
552 else if('string' === typeof value.content.text)
553 return cb(null, value.content.type + ':'+title(value.content.text))
554 else
555 return cb(null, id.substring(0, 10)+'...')
556 })
557}
558
559var messageName = exports.messageName
560
561module.exports.messageLink = function (id) {
562 if (ref.isMsg(id)) {
563 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
564 messageName(id, function (err, name) {
565 if(err) console.error(err)
566 else link.textContent = name
567 })
568 } else {
569 var link = id
570 }
571 return link
572}
573
574module.exports.rawJSON = function (obj) {
575 return JSON.stringify(obj, null, 2)
576 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
577 .map(function (e) {
578 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
579 return h('a', {href: '#' + e}, e)
580 }
581 return e
582 })
583}
584
585var markdown = require('ssb-markdown')
586var config = require('./config')()
587
588module.exports.markdown = function (msg, md) {
589 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
590 if(url[0] == '%' || url[0] == '@' || url[0] == '#') return '#' + url
591 if(url[0] !== '&') return url
592 //if(url[0] == '&') return config.blobsUrl + url
593 //if(!image) return url
594 return config.blobsUrl + url
595 }})}
596}
597

Built with git-ssb-web