git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: ccf197ae2af45b1d9ae17a111e3c3d3c335948e5

Files: ccf197ae2af45b1d9ae17a111e3c3d3c335948e5 / tools.js

7943 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.box = function (content) {
17 return ssbKeys.box(content, content.recps.map(function (e) {
18 return ref.isFeed(e) ? e : e.link
19 }))
20}
21
22module.exports.publish = function (content, cb) {
23 if(content.recps)
24 content = exports.box(content)
25 sbot.publish(content, function (err, msg) {
26 if(err) throw err
27 console.log('Published!', msg)
28 if(cb) cb(err, msg)
29 })
30}
31
32module.exports.done = function (src) {
33 var content = {
34 type: 'done',
35 vote: {'link': src}
36 }
37
38 var done = h('button.btn.right', 'Done ', h('img.emoji', {src: config.emojiUrl + 'v.png'}), {
39 onclick: function () {
40 content.done = true
41 content.mentions = [id]
42 content.recps = [id]
43 exports.publish(content, function (err, published) {
44 if (err) throw err
45 })
46 }
47 })
48
49 return done
50
51}
52
53module.exports.mute = function (src) {
54 if (!localStorage[src])
55 var cache = {mute: false}
56 else
57 var cache = JSON.parse(localStorage[src])
58
59 if (cache.mute == true) {
60 var mute = h('button.btn', 'Unmute', {
61 onclick: function () {
62 cache.mute = false
63 localStorage[src] = JSON.stringify(cache)
64 location.reload()
65 }
66 })
67 return mute
68 } else {
69 var mute = h('button.btn', 'Mute', {
70 onclick: function () {
71 cache.mute = true
72 localStorage[src] = JSON.stringify(cache)
73 location.reload()
74 }
75 })
76 return mute
77 }
78}
79
80module.exports.star = function (msg) {
81 var votebutton = h('span.star:' + msg.key.substring(0,44))
82
83 var vote = {
84 type: 'vote',
85 vote: { link: msg.key, expression: 'Star' }
86 }
87
88 var star = h('button.btn.right', 'Star ',
89 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
90 onclick: function () {
91 vote.vote.value = 1
92 sbot.publish(vote, function (err, voted) {
93 if(err) throw err
94 })
95 }
96 }
97 )
98
99 var unstar = h('button.btn.right ', 'Unstar ',
100 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
101 onclick: function () {
102 vote.vote.value = -1
103 sbot.publish(vote, function (err, voted) {
104 if(err) throw err
105 })
106 }
107 }
108 )
109
110 votebutton.appendChild(star)
111
112 pull(
113 sbot.links({rel: 'vote', dest: msg.key, live: true}),
114 pull.drain(function (link) {
115 if (link.key) {
116 sbot.get(link.key, function (err, data) {
117 if (err) throw err
118 if (data.content.vote) {
119 if (data.author == id) {
120 if (data.content.vote.value == 1)
121 votebutton.replaceChild(unstar, star)
122 if (data.content.vote.value == -1)
123 votebutton.replaceChild(star, unstar)
124 }
125 }
126 })
127 }
128 })
129 )
130
131 return votebutton
132}
133
134function votes (msg) {
135 var votes = h('div.votes')
136 if (msg.key) {
137 pull(
138 sbot.links({rel: 'vote', dest: msg.key, live: true }),
139 pull.drain(function (link) {
140 if (link.key) {
141 sbot.get(link.key, function (err, data) {
142 if (err) throw err
143 if (data.content.vote) {
144 if (data.content.vote.value == 1) {
145 if (localStorage[data.author + 'name'])
146 name = localStorage[data.author + 'name']
147 else
148 name = data.author
149 votes.appendChild(h('a#vote:' + data.author.substring(0, 44), {href:'#' + data.author, title: name}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
150 }
151 else if (data.content.vote.value == -1) {
152 var lookFor = 'vote:' + data.author.substring(0, 44)
153 document.getElementById(lookFor, function (err, gotit) {
154 if (err) throw err
155 gotit.parentNode.removeChild(remove)
156 })
157 }
158 }
159 })
160 }
161 })
162 )
163 }
164 return votes
165}
166
167module.exports.timestamp = function (msg, edited) {
168 var timestamp
169 if (edited)
170 timestamp = h('span.timestamp', 'Edited: ', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
171 else
172 timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
173 return timestamp
174}
175
176
177module.exports.mini = function (msg, content) {
178 return h('div.mini',
179 h('span.avatar',
180 h('a', {href: '#' + msg.value.author},
181 h('span.avatar--small', avatar.image(msg.value.author)),
182 avatar.name(msg.value.author)
183 )
184 ),
185 exports.timestamp(msg),
186 content
187 )
188}
189
190
191module.exports.header = function (msg) {
192 var header = h('div.header')
193
194 header.appendChild(h('span.avatar',
195 h('a', {href: '#' + msg.value.author},
196 h('span.avatar--small', avatar.image(msg.value.author)),
197 avatar.name(msg.value.author)
198 )
199 )
200 )
201
202 header.appendChild(exports.timestamp(msg))
203 header.appendChild(votes(msg))
204
205 if (msg.value.private)
206 header.appendChild(h('span.right', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})))
207
208 return header
209}
210
211var ref = require('ssb-ref')
212
213module.exports.messageName = function (id, cb) {
214 // gets the first few characters of a message, for message-link
215 function title (s) {
216 var m = /^\n*([^\n]{0,40})/.exec(s)
217 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
218 }
219
220 sbot.get(id, function (err, value) {
221 if(err && err.name == 'NotFoundError')
222 return cb(null, id.substring(0, 10)+'...(missing)')
223 if(value.content.type === 'post' && 'string' === typeof value.content.text)
224 return cb(null, title(value.content.text))
225 else if('string' === typeof value.content.text)
226 return cb(null, value.content.type + ':'+title(value.content.text))
227 else
228 return cb(null, id.substring(0, 10)+'...')
229 })
230}
231
232var messageName = exports.messageName
233var ref = require('ssb-ref')
234
235module.exports.messageLink = function (id) {
236 if (ref.isMsg(id)) {
237 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
238 messageName(id, function (err, name) {
239 if(err) console.error(err)
240 else link.textContent = name
241 })
242 } else {
243 var link = id
244 }
245 return link
246}
247
248
249/*module.exports.messageLink = function (msglink) {
250 var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 44) + '...'))
251
252 if (ref.isMsg(msglink)) {
253 pull(
254 sbot.get(msglink, function (err, data) {
255 console.log(data)
256 if(err && err.name == 'NotFoundError') {
257 var newlink = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 35) + ' (Missing)...'))
258 }
259 if(data.content.type === 'post' && 'string' === typeof data.content.text) {
260 var newlink = h('span', h('a', {href: '#' + msglink}, data.content.text.substring(0, 44) + '...'))
261 }
262 else {
263 var newlink = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 44) + '...'))
264 }
265 if (link) {
266 link.parentNode.replaceChild(newlink, link)
267 }
268 })
269 )
270 }
271
272 return link
273}*/
274
275module.exports.rawJSON = function (obj) {
276 return JSON.stringify(obj, null, 2)
277 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
278 .map(function (e) {
279 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
280 return h('a', {href: '#' + e}, e)
281 }
282 return e
283 })
284}
285
286var markdown = require('ssb-markdown')
287var config = require('./config')()
288
289module.exports.markdown = function (msg, md) {
290 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
291 if(url[0] == '%' || url[0] == '@') return '#' + url
292 if(!image) return url
293 if(url[0] !== '&') return url
294 return config.blobsUrl + url
295 }})}
296}
297

Built with git-ssb-web