git ssb

0+

ev / 0qc



Tree: e875995b3285440296b82b811ebcba99145cfd56

Files: e875995b3285440296b82b811ebcba99145cfd56 / tools.js

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

Built with git-ssb-web