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