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