git ssb

0+

ev / gitmx



Tree: ae35d4b398efe8352b6b31fd5853d40aa9009cfc

Files: ae35d4b398efe8352b6b31fd5853d40aa9009cfc / tools.js

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

Built with git-ssb-web