git ssb

30+

cel / git-ssb-web



Tree: ac38f3bf0c59103522861df8b3a1f1f545247977

Files: ac38f3bf0c59103522861df8b3a1f1f545247977 / lib / util.js

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

Built with git-ssb-web