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