git ssb

2+

ev / mvd



Tree: 1d4c84a26e992a18358cb0e3c675e1608c9f9ed7

Files: 1d4c84a26e992a18358cb0e3c675e1608c9f9ed7 / tools.js

10042 bytesRaw
1var h = require('hyperscript')
2var human = require('human-time')
3var avatar = require('./avatar')
4var ref = require('ssb-ref')
5
6var ssbKeys = require('ssb-keys')
7
8var pull = require('pull-stream')
9
10var sbot = require('./scuttlebot')
11
12var config = require('./config')()
13
14var id = require('./keys').id
15
16module.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
37module.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
57module.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
109module.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
115module.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
125module.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.hash = '#'
137 location.hash = src
138 }
139 })
140 return mute
141 } else {
142 var mute = h('button.btn', 'Mute', {
143 onclick: function () {
144 cache.mute = true
145 localStorage[src] = JSON.stringify(cache)
146 location.hash = '#'
147 location.hash = src
148 }
149 })
150 return mute
151 }
152}
153
154module.exports.star = function (msg) {
155 var votebutton = h('span.star:' + msg.key.substring(0,44))
156
157 var vote = {
158 type: 'vote',
159 vote: { link: msg.key, expression: 'Star' }
160 }
161
162 var star = h('button.btn.right', 'Star ',
163 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
164 onclick: function () {
165 vote.vote.value = 1
166 sbot.publish(vote, function (err, voted) {
167 if(err) throw err
168 })
169 }
170 }
171 )
172
173 var unstar = h('button.btn.right ', 'Unstar ',
174 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
175 onclick: function () {
176 vote.vote.value = -1
177 sbot.publish(vote, function (err, voted) {
178 if(err) throw err
179 })
180 }
181 }
182 )
183
184 votebutton.appendChild(star)
185
186 pull(
187 sbot.links({rel: 'vote', dest: msg.key, live: true}),
188 pull.drain(function (link) {
189 if (link.key) {
190 sbot.get(link.key, function (err, data) {
191 if (err) throw err
192 if (data.content.vote) {
193 if (data.author == id) {
194 if (data.content.vote.value == 1)
195 votebutton.replaceChild(unstar, star)
196 if (data.content.vote.value == -1)
197 votebutton.replaceChild(star, unstar)
198 }
199 }
200 })
201 }
202 })
203 )
204
205 return votebutton
206}
207
208function votes (msg) {
209 var votes = h('div.votes')
210 if (msg.key) {
211 pull(
212 sbot.links({rel: 'vote', dest: msg.key, live: true }),
213 pull.drain(function (link) {
214 if (link.key) {
215 sbot.get(link.key, function (err, data) {
216 if (err) throw err
217 if (data.content.vote) {
218 if (data.content.vote.value == 1) {
219 if (localStorage[data.author + 'name'])
220 name = localStorage[data.author + 'name']
221 else
222 name = data.author
223 votes.appendChild(h('a#vote:' + data.author.substring(0, 44), {href:'#' + data.author, title: name}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
224 }
225 else if (data.content.vote.value == -1) {
226 var lookFor = 'vote:' + data.author.substring(0, 44)
227 document.getElementById(lookFor, function (err, gotit) {
228 if (err) throw err
229 gotit.parentNode.removeChild(remove)
230 })
231 }
232 }
233 })
234 }
235 })
236 )
237 }
238 return votes
239}
240
241module.exports.timestamp = function (msg, edited) {
242 var timestamp
243 if (edited)
244 timestamp = h('span.timestamp', 'Edited: ', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
245 else
246 timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
247 return timestamp
248}
249
250
251module.exports.mini = function (msg, content) {
252 return h('div.mini',
253 h('span.avatar',
254 h('a', {href: '#' + msg.value.author},
255 h('span.avatar--small', avatar.image(msg.value.author)),
256 avatar.name(msg.value.author)
257 )
258 ),
259 exports.timestamp(msg),
260 content
261 )
262}
263
264
265module.exports.header = function (msg) {
266 var header = h('div.header')
267
268 header.appendChild(h('span.avatar',
269 h('a', {href: '#' + msg.value.author},
270 h('span.avatar--small', avatar.image(msg.value.author)),
271 avatar.name(msg.value.author)
272 )
273 )
274 )
275
276 header.appendChild(exports.timestamp(msg))
277 header.appendChild(votes(msg))
278
279 if (msg.value.private)
280 header.appendChild(h('span.right', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})))
281
282 return header
283}
284
285var ref = require('ssb-ref')
286
287module.exports.messageName = function (id, cb) {
288 // gets the first few characters of a message, for message-link
289 function title (s) {
290 var m = /^\n*([^\n]{0,40})/.exec(s)
291 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
292 }
293
294 sbot.get(id, function (err, value) {
295 if(err && err.name == 'NotFoundError')
296 return cb(null, id.substring(0, 10)+'...(missing)')
297 if(value.content.type === 'post' && 'string' === typeof value.content.text)
298 return cb(null, title(value.content.text))
299 else if('string' === typeof value.content.text)
300 return cb(null, value.content.type + ':'+title(value.content.text))
301 else
302 return cb(null, id.substring(0, 10)+'...')
303 })
304}
305
306var messageName = exports.messageName
307var ref = require('ssb-ref')
308
309module.exports.messageLink = function (id) {
310 if (ref.isMsg(id)) {
311 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
312 messageName(id, function (err, name) {
313 if(err) console.error(err)
314 else link.textContent = name
315 })
316 } else {
317 var link = id
318 }
319 return link
320}
321
322
323/*module.exports.messageLink = function (msglink) {
324 var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 44) + '...'))
325
326 if (ref.isMsg(msglink)) {
327 pull(
328 sbot.get(msglink, function (err, data) {
329 console.log(data)
330 if(err && err.name == 'NotFoundError') {
331 var newlink = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 35) + ' (Missing)...'))
332 }
333 if(data.content.type === 'post' && 'string' === typeof data.content.text) {
334 var newlink = h('span', h('a', {href: '#' + msglink}, data.content.text.substring(0, 44) + '...'))
335 }
336 else {
337 var newlink = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 44) + '...'))
338 }
339 if (link) {
340 link.parentNode.replaceChild(newlink, link)
341 }
342 })
343 )
344 }
345
346 return link
347}*/
348
349module.exports.rawJSON = function (obj) {
350 return JSON.stringify(obj, null, 2)
351 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
352 .map(function (e) {
353 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
354 return h('a', {href: '#' + e}, e)
355 }
356 return e
357 })
358}
359
360var markdown = require('ssb-markdown')
361var config = require('./config')()
362
363module.exports.markdown = function (msg, md) {
364 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
365 if(url[0] == '%' || url[0] == '@') return '#' + url
366 if(!image) return url
367 if(url[0] !== '&') return url
368 return config.blobsUrl + url
369 }})}
370}
371

Built with git-ssb-web