git ssb

0+

ev / gitmx



Tree: d1e853f691416e29ae636b734bbda1bfa0bd4b05

Files: d1e853f691416e29ae636b734bbda1bfa0bd4b05 / tools.js

10826 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.queueButton = function (src) {
13 var queueButton = h('span.queue:' + src.key.substring(0,44))
14
15 var addToQueue = h('button.btn.right', 'Queue', {
16 onclick: function () {
17 var content = {
18 type: 'queue',
19 message: src.key,
20 queue: true
21 }
22 sbot.publish(content, function (err, publish) {
23 if (err) throw err
24 console.log(publish)
25 })
26 }
27 })
28
29 var removeFromQueue = h('button.btn.right#', 'Done', {
30 onclick: function () {
31 var content = {
32 type: 'queue',
33 message: src.key,
34 queue: false
35 }
36 sbot.publish(content, function (err, publish) {
37 if (err) throw err
38 console.log(publish)
39 if (window.location.hash.substring(1) == 'queue') {
40 setTimeout(function () {
41 var gotIt = document.getElementById(src.key.substring(0,44))
42 if (gotIt != null) {
43 gotIt.outerHTML = ''
44 }
45 }, 100)
46
47 }
48 })
49 }
50 })
51
52 pull(
53 sbot.query({query: [{$filter: { value: { author: id, content: {type: 'queue', message: src.key}}}}], live: true}),
54 pull.drain(function (msg) {
55 if (msg.value) {
56 if (msg.value.content.queue == true) {
57 queueButton.removeChild(queueButton.childNodes[0])
58 queueButton.appendChild(removeFromQueue)
59 }
60 if (msg.value.content.queue == false) {
61 queueButton.removeChild(queueButton.childNodes[0])
62 queueButton.appendChild(addToQueue)
63 }
64 }
65 })
66 )
67
68 queueButton.appendChild(addToQueue)
69
70 return queueButton
71}
72
73module.exports.box = function (content) {
74 return ssbKeys.box(content, content.recps.map(function (e) {
75 return ref.isFeed(e) ? e : e.link
76 }))
77}
78
79module.exports.publish = function (content, cb) {
80 if(content.recps)
81 content = exports.box(content)
82 sbot.publish(content, function (err, msg) {
83 if(err) throw err
84 console.log('Published!', msg)
85 if(cb) cb(err, msg)
86 })
87}
88
89module.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
141module.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
147module.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
158
159module.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.hash = '#'
171 location.hash = src
172 }
173 })
174 return mute
175 } else {
176 var mute = h('button.btn', 'Mute', {
177 onclick: function () {
178 cache.mute = true
179 localStorage[src] = JSON.stringify(cache)
180 location.hash = '#'
181 location.hash = src
182 }
183 })
184 return mute
185 }
186}
187
188module.exports.star = function (msg) {
189 var votebutton = h('span.star:' + msg.key.substring(0,44))
190
191 var vote = {
192 type: 'vote',
193 vote: { link: msg.key, expression: 'Star' }
194 }
195
196 if (msg.value.content.recps) {
197 vote.recps = msg.value.content.recps
198 }
199
200 var star = h('button.btn.right', 'Star ',
201 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
202 onclick: function () {
203 vote.vote.value = 1
204 if (vote.recps) {
205 vote = exports.box(vote)
206 }
207 sbot.publish(vote, function (err, voted) {
208 if(err) throw err
209 })
210 }
211 }
212 )
213
214 var unstar = h('button.btn.right ', 'Unstar ',
215 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
216 onclick: function () {
217 vote.vote.value = -1
218 sbot.publish(vote, function (err, voted) {
219 if(err) throw err
220 })
221 }
222 }
223 )
224
225 votebutton.appendChild(star)
226
227 pull(
228 sbot.links({rel: 'vote', dest: msg.key, live: true}),
229 pull.drain(function (link) {
230 if (link.key) {
231 sbot.get(link.key, function (err, data) {
232 if (err) throw err
233 if (data.content.vote) {
234 if (data.author == id) {
235 if (data.content.vote.value == 1)
236 votebutton.replaceChild(unstar, star)
237 if (data.content.vote.value == -1)
238 votebutton.replaceChild(star, unstar)
239 }
240 }
241 })
242 }
243 })
244 )
245
246 return votebutton
247}
248
249function votes (msg) {
250 var votes = h('div.votes')
251 if (msg.key) {
252 pull(
253 sbot.links({rel: 'vote', dest: msg.key, live: true }),
254 pull.drain(function (link) {
255 if (link.key) {
256 sbot.get(link.key, function (err, data) {
257 if (err) throw err
258 if (data.content.vote) {
259 if (data.content.vote.value == 1) {
260 if (localStorage[data.author + 'name'])
261 name = localStorage[data.author + 'name']
262 else
263 name = data.author
264 votes.appendChild(h('a#vote:' + data.author.substring(0, 44), {href:'#' + data.author, title: name}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
265 }
266 else if (data.content.vote.value == -1) {
267 var lookFor = 'vote:' + data.author.substring(0, 44)
268 document.getElementById(lookFor, function (err, gotit) {
269 if (err) throw err
270 gotit.parentNode.removeChild(remove)
271 })
272 }
273 }
274 })
275 }
276 })
277 )
278 }
279 return votes
280}
281
282module.exports.timestamp = function (msg, edited) {
283 var timestamp
284 if (edited)
285 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))))
286 else
287 timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
288 return timestamp
289}
290
291
292module.exports.mini = function (msg, content) {
293 var mini = h('div.mini')
294
295 mini.appendChild(
296 h('span.avatar',
297 h('a', {href: '#' + msg.value.author},
298 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
299 avatar.cachedName(msg.value.author)
300 )
301 )
302 )
303 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
304
305
306 mini.appendChild(h('span', content))
307 mini.appendChild(exports.timestamp(msg))
308
309 if (msg.value.content.recps) {
310 mini.appendChild(lock)
311 }
312
313 if (typeof msg.value.content === 'string') {
314 mini.appendChild(lock)
315 }
316
317 return mini
318}
319
320module.exports.header = function (msg) {
321 var header = h('div.header')
322
323 header.appendChild(h('span.avatar',
324 h('a', {href: '#' + msg.value.author},
325 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
326 avatar.cachedName(msg.value.author)
327 )
328 )
329 )
330
331 header.appendChild(exports.timestamp(msg))
332 header.appendChild(votes(msg))
333
334 if (msg.value.private) {
335 header.appendChild(h('span.right', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})))
336 }
337 if (msg.value.content.type == 'edit') {
338 header.appendChild(h('span.right', ' Edited: ', h('a', {href: '#' + msg.value.content.original}, exports.messageLink(msg.value.content.original))))
339 }
340 return header
341}
342
343
344
345
346module.exports.messageName = function (id, cb) {
347 // gets the first few characters of a message, for message-link
348 function title (s) {
349 var m = /^\n*([^\n]{0,40})/.exec(s)
350 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
351 }
352
353 sbot.get(id, function (err, value) {
354 if(err && err.name == 'NotFoundError')
355 return cb(null, id.substring(0, 10)+'...(missing)')
356 if(value.content.type === 'post' && 'string' === typeof value.content.text)
357 return cb(null, title(value.content.text))
358 else if('string' === typeof value.content.text)
359 return cb(null, value.content.type + ':'+title(value.content.text))
360 else
361 return cb(null, id.substring(0, 10)+'...')
362 })
363}
364
365var messageName = exports.messageName
366
367module.exports.messageLink = function (id) {
368 if (ref.isMsg(id)) {
369 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
370 messageName(id, function (err, name) {
371 if(err) console.error(err)
372 else link.textContent = name
373 })
374 } else {
375 var link = id
376 }
377 return link
378}
379
380module.exports.rawJSON = function (obj) {
381 return JSON.stringify(obj, null, 2)
382 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
383 .map(function (e) {
384 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
385 return h('a', {href: '#' + e}, e)
386 }
387 return e
388 })
389}
390
391var markdown = require('ssb-markdown')
392var config = require('./config')()
393
394module.exports.markdown = function (msg, md) {
395 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
396 if(url[0] == '%' || url[0] == '@' || url[0] == '#') return '#' + url
397 if(url[0] !== '&') return url
398 //if(url[0] == '&') return config.blobsUrl + url
399 //if(!image) return url
400 return config.blobsUrl + url
401 }})}
402}
403

Built with git-ssb-web