git ssb

30+

cel / git-ssb-web



Tree: 7a409b06ac2da63a7cb40bf010ed89e2954f32f3

Files: 7a409b06ac2da63a7cb40bf010ed89e2954f32f3 / lib / util.js

5426 bytesRaw
1var pull = require('pull-stream')
2var h = require('pull-hyperscript')
3var paramap = require('pull-paramap')
4var Highlight = require('highlight.js')
5var u = exports
6
7u.imgMimes = {
8 png: 'image/png',
9 jpeg: 'image/jpeg',
10 jpg: 'image/jpeg',
11 gif: 'image/gif',
12 tif: 'image/tiff',
13 svg: 'image/svg+xml',
14 bmp: 'image/bmp'
15}
16
17u.getExtension = function(filename) {
18 return (/\.([^.]+)$/.exec(filename) || [,filename])[1]
19}
20
21u.readNext = function (fn) {
22 var next
23 return function (end, cb) {
24 if (next) return next(end, cb)
25 fn(function (err, _next) {
26 if (err) return cb(err)
27 next = _next
28 next(null, cb)
29 })
30 }
31}
32
33u.readOnce = function (fn) {
34 var ended
35 return function (end, cb) {
36 fn(function (err, data) {
37 if (err || ended) return cb(err || ended)
38 ended = true
39 cb(null, data)
40 })
41 }
42}
43
44u.when = function (bool, elFunction) {
45 return bool
46 ? elFunction()
47 : pull.once('')
48}
49
50u.sourceMap = function (source, fn) {
51 return pull(
52 source,
53 pull.filter(Boolean),
54 pull.map(fn)
55 )
56}
57
58u.paraSourceMap = function paraSourceMap (source, fn) {
59 return pull(
60 pull.values(source),
61 paramap(fn, 8)
62 )
63}
64
65u.escape = function (str) {
66 return String(str)
67 .replace(/&/g, '&')
68 .replace(/</g, '&lt;')
69 .replace(/>/g, '&gt;')
70 .replace(/"/g, '&quot;')
71}
72
73u.encodeLink = function (url) {
74 if (!Array.isArray(url)) url = [url]
75 return '/' + url.map(encodeURIComponent).join('/')
76}
77
78u.link = function (parts, text, raw, props) {
79 if (text == null) text = parts[parts.length-1]
80 if (!raw) text = u.escape(text)
81 return '<a href="' + u.encodeLink(parts) + '"' +
82 (props ? ' ' + props : '') +
83 '>' + text + '</a>'
84}
85
86u.timestamp = function (time, req) {
87 time = Number(time)
88 var d = new Date(time)
89 return '<span title="' + time + '">' +
90 d.toLocaleString(req._locale) + '</span>'
91}
92
93u.nav = function (links, page, after) {
94 return h('nav', [
95 u.sourceMap(pull.values(links), link => {
96 return h('a', {
97 href: (typeof link[0] == 'string') ? link[0] : u.encodeLink(link[0]),
98 class: (link[2] == page) ? 'active' : ''
99 },
100 link[1])
101 }),
102 pull.once(after || '')
103 ])
104}
105
106u.hiddenInputs = function (values) {
107 return Object.keys(values).map(function (key) {
108 return '<input type="hidden"' +
109 ' name="' + u.escape(key) + '"' +
110 ' value="' + u.escape(values[key]) + '"/>'
111 }).join('')
112}
113
114u.highlight = function(code, lang) {
115 if (code.length > 100000) return u.escape(code)
116 try {
117 return lang
118 ? Highlight.highlight(lang, code).value
119 : Highlight.highlightAuto(code).value
120 } catch(e) {
121 if (/^Unknown language/.test(e.message))
122 return u.escape(code)
123 throw e
124 }
125}
126
127u.pre = function (text) {
128 return '<pre>' + u.escape(text) + '</pre>'
129}
130
131u.json = function (obj) {
132 return u.linkify(u.pre(JSON.stringify(obj, null, 2)))
133}
134
135u.linkify = function (text) {
136 // regex is from ssb-ref
137 return text.replace(/(@|%|&|&amp;)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) {
138 return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>'
139 })
140}
141
142u.readObjectString = function (obj, cb) {
143 pull(obj.read, pull.collect(function (err, bufs) {
144 if (err) return cb(err)
145 cb(null, Buffer.concat(bufs, obj.length).toString('utf8'))
146 }))
147}
148
149u.pullReverse = function () {
150 return function (read) {
151 return u.readNext(function (cb) {
152 pull(read, pull.collect(function (err, items) {
153 cb(err, items && pull.values(items.reverse()))
154 }))
155 })
156 }
157}
158
159function compareMsgs(a, b) {
160 return (a.value.timestamp - b.value.timestamp) || (a.key - b.key)
161}
162
163u.pullSort = function (comparator, descending) {
164 return function (read) {
165 return u.readNext(function (cb) {
166 pull(read, pull.collect(function (err, items) {
167 if (err) return cb(err)
168 items.sort(comparator)
169 if (descending) items.reverse()
170 cb(null, pull.values(items))
171 }))
172 })
173 }
174}
175
176u.sortMsgs = function (descending) {
177 return u.pullSort(compareMsgs, descending)
178}
179
180u.truncate = function (str, len) {
181 str = String(str)
182 return str.length < len ? str : str.substr(0, len) + '…'
183}
184
185u.messageTitle = function (msg) {
186 var c = msg.value.content
187 return u.truncate(c.title || c.text || msg.key, 40)
188}
189
190u.ifModifiedSince = function (req, lastMod) {
191 var ifModSince = req.headers['if-modified-since']
192 if (!ifModSince) return false
193 var d = new Date(ifModSince)
194 return d && Math.floor(d/1000) >= Math.floor(lastMod/1000)
195}
196
197u.decryptMessages = function (sbot) {
198 return paramap(function (msg, cb) {
199 var c = msg && msg.value && msg.value.content
200 if (c && typeof c === 'string' && c.slice(-4) === '.box') {
201 sbot.private.unbox(msg.value.content, function (err, content) {
202 if (err) return cb(null, msg) // leave message encrypted
203 var msg1 = {}
204 for (var k in msg) msg1[k] = msg[k]
205 msg1.value = {}
206 for (var j in msg.value) msg1.value[j] = msg.value[j]
207 msg1.value.private = true
208 msg1.value.content = content
209 if (!content.recps) {
210 sbot.whoami(function (err, feed) {
211 if (err) return cb(err)
212 content.recps = [msg1.value.author]
213 if (feed.id !== msg1.value.author) content.recps.push(feed.id)
214 cb(null, msg1)
215 })
216 } else cb(null, msg1)
217 })
218 } else cb(null, msg)
219 }, 4)
220}
221

Built with git-ssb-web