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