git ssb

2+

ev / mvd



Tree: 163a7a8b50f016962bb539bd896cd62e01ae747a

Files: 163a7a8b50f016962bb539bd896cd62e01ae747a / tools.js

15323 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}
206
207module.exports.follow = function (src) {
208 var button = h('span.button')
209
210 var followButton = h('button.btn', 'Follow ', avatar.name(src), {
211 onclick: function () {
212 var content = {
213 type: 'contact',
214 contact: src,
215 following: true
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', 'Unfollow ', avatar.name(src), {
225 onclick: function () {
226 var content = {
227 type: 'contact',
228 contact: src,
229 following: false
230 }
231 sbot.publish(content, function (err, publish) {
232 if (err) throw err
233 console.log(publish)
234 })
235 }
236 })
237
238 pull(
239 sbot.query({query: [{$filter: { value: { author: id, content: {type: 'contact', contact: src}}}}], live: true}),
240 pull.drain(function (msg) {
241 if (msg.value) {
242 if (msg.value.content.following == true) {
243 button.removeChild(button.firstChild)
244 button.appendChild(unfollowButton)
245 }
246 if (msg.value.content.following == false) {
247 button.removeChild(button.firstChild)
248 button.appendChild(followButton)
249 }
250 }
251 })
252 )
253
254 button.appendChild(followButton)
255
256 return button
257}
258
259module.exports.box = function (content) {
260 return ssbKeys.box(content, content.recps.map(function (e) {
261 return ref.isFeed(e) ? e : e.link
262 }))
263}
264
265module.exports.publish = function (content, cb) {
266 if(content.recps)
267 content = exports.box(content)
268 sbot.publish(content, function (err, msg) {
269 if(err) throw err
270 console.log('Published!', msg)
271 if(cb) cb(err, msg)
272 })
273}
274
275module.exports.mute = function (src) {
276 if (!localStorage[src])
277 var cache = {mute: false}
278 else
279 var cache = JSON.parse(localStorage[src])
280
281 if (cache.mute == true) {
282 var mute = h('button.btn', 'Unmute', {
283 onclick: function () {
284 cache.mute = false
285 localStorage[src] = JSON.stringify(cache)
286 location.hash = '#'
287 location.hash = src
288 }
289 })
290 return mute
291 } else {
292 var mute = h('button.btn', 'Mute', {
293 onclick: function () {
294 cache.mute = true
295 localStorage[src] = JSON.stringify(cache)
296 location.hash = '#'
297 location.hash = src
298 }
299 })
300 return mute
301 }
302}
303
304module.exports.star = function (msg) {
305 var votebutton = h('span.star:' + msg.key.substring(0,44))
306
307 var vote = {
308 type: 'vote',
309 vote: { link: msg.key, expression: 'Star' }
310 }
311
312 if (msg.value.content.recps) {
313 vote.recps = msg.value.content.recps
314 }
315
316 var star = h('button.btn.right', 'Star ',
317 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
318 onclick: function () {
319 vote.vote.value = 1
320 if (vote.recps) {
321 vote = exports.box(vote)
322 }
323 sbot.publish(vote, function (err, voted) {
324 if(err) throw err
325 })
326 }
327 }
328 )
329
330 var unstar = h('button.btn.right ', 'Unstar ',
331 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
332 onclick: function () {
333 vote.vote.value = -1
334 sbot.publish(vote, function (err, voted) {
335 if(err) throw err
336 })
337 }
338 }
339 )
340
341 votebutton.appendChild(star)
342
343 pull(
344 sbot.links({rel: 'vote', dest: msg.key, live: true}),
345 pull.drain(function (link) {
346 if (link.key) {
347 sbot.get(link.key, function (err, data) {
348 if (err) throw err
349 if (data.content.vote) {
350 if (data.author == id) {
351 if (data.content.vote.value == 1)
352 votebutton.replaceChild(unstar, star)
353 if (data.content.vote.value == -1)
354 votebutton.replaceChild(star, unstar)
355 }
356 }
357 })
358 }
359 })
360 )
361
362 return votebutton
363}
364
365function votes (msg) {
366 var votes = h('div.votes')
367 if (msg.key) {
368 pull(
369 sbot.links({rel: 'vote', dest: msg.key/*, live: true*/ }),
370 pull.drain(function (link) {
371 if (link.key) {
372 sbot.get(link.key, function (err, data) {
373 if (err) throw err
374 if (data.content.vote) {
375 if (data.content.vote.value == 1) {
376 if (localStorage[data.author + 'name'])
377 name = localStorage[data.author + 'name']
378 else
379 name = data.author
380 votes.appendChild(h('a#vote:' + data.author.substring(0, 44), {href:'#' + data.author, title: name}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
381 }
382 else if (data.content.vote.value == -1) {
383 var lookFor = 'vote:' + data.author.substring(0, 44)
384 document.getElementById(lookFor, function (err, gotit) {
385 if (err) throw err
386 gotit.parentNode.removeChild(remove)
387 })
388 }
389 }
390 })
391 }
392 })
393 )
394 }
395 return votes
396}
397
398module.exports.timestamp = function (msg, edited) {
399 var timestamp
400 if (edited)
401 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))))
402 else
403 timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
404 return timestamp
405}
406
407
408module.exports.mini = function (msg, content) {
409 var mini = h('div.mini')
410
411 mini.appendChild(
412 h('span.avatar',
413 h('a', {href: '#' + msg.value.author},
414 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
415 avatar.cachedName(msg.value.author)
416 )
417 )
418 )
419 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
420
421
422 mini.appendChild(h('span', content))
423 mini.appendChild(exports.timestamp(msg))
424
425 if (msg.value.content.recps) {
426 mini.appendChild(lock)
427 }
428
429 if (typeof msg.value.content === 'string') {
430 mini.appendChild(lock)
431 }
432
433 return mini
434}
435
436module.exports.header = function (msg) {
437 var header = h('div.header')
438
439 header.appendChild(h('span.avatar',
440 h('a', {href: '#' + msg.value.author},
441 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
442 avatar.cachedName(msg.value.author)
443 )
444 )
445 )
446
447 header.appendChild(exports.timestamp(msg))
448 header.appendChild(votes(msg))
449
450 if (msg.value.private) {
451 header.appendChild(h('span.right', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})))
452 }
453 if (msg.value.content.type == 'edit') {
454 header.appendChild(h('span.right', ' Edited: ', h('a', {href: '#' + msg.value.content.original}, exports.messageLink(msg.value.content.original))))
455 }
456 return header
457}
458
459
460
461
462module.exports.messageName = function (id, cb) {
463 // gets the first few characters of a message, for message-link
464 function title (s) {
465 var m = /^\n*([^\n]{0,40})/.exec(s)
466 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
467 }
468
469 sbot.get(id, function (err, value) {
470 if(err && err.name == 'NotFoundError')
471 return cb(null, id.substring(0, 10)+'...(missing)')
472 if(value.content.type === 'post' && 'string' === typeof value.content.text)
473 return cb(null, title(value.content.text))
474 else if('string' === typeof value.content.text)
475 return cb(null, value.content.type + ':'+title(value.content.text))
476 else
477 return cb(null, id.substring(0, 10)+'...')
478 })
479}
480
481var messageName = exports.messageName
482
483module.exports.messageLink = function (id) {
484 if (ref.isMsg(id)) {
485 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
486 messageName(id, function (err, name) {
487 if(err) console.error(err)
488 else link.textContent = name
489 })
490 } else {
491 var link = id
492 }
493 return link
494}
495
496module.exports.rawJSON = function (obj) {
497 return JSON.stringify(obj, null, 2)
498 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
499 .map(function (e) {
500 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
501 return h('a', {href: '#' + e}, e)
502 }
503 return e
504 })
505}
506
507var markdown = require('ssb-markdown')
508var config = require('./config')()
509
510module.exports.markdown = function (msg, md) {
511 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
512 if(url[0] == '%' || url[0] == '@' || url[0] == '#') return '#' + url
513 if(url[0] !== '&') return url
514 //if(url[0] == '&') return config.blobsUrl + url
515 //if(!image) return url
516 return config.blobsUrl + url
517 }})}
518}
519

Built with git-ssb-web