git ssb

0+

ev / gitmx



Tree: afc9955231b5cf72a19ab39b8e1fba3713fdf6ac

Files: afc9955231b5cf72a19ab39b8e1fba3713fdf6ac / tools.js

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

Built with git-ssb-web