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