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