git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 45e59d3471f3beeee49304361c2c16c9fc3c819f

Files: 45e59d3471f3beeee49304361c2c16c9fc3c819f / tools.js

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

Built with git-ssb-web