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