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