git ssb

30+

cel / git-ssb-web



Tree: 0b95005d33b39339c34360a02ee6a8f696c7ec92

Files: 0b95005d33b39339c34360a02ee6a8f696c7ec92 / lib / util.js

5292 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.escape = function (str) {
58 return String(str)
59 .replace(/&/g, '&')
60 .replace(/</g, '&lt;')
61 .replace(/>/g, '&gt;')
62 .replace(/"/g, '&quot;')
63}
64
65u.encodeLink = function (url) {
66 if (!Array.isArray(url)) url = [url]
67 return '/' + url.map(encodeURIComponent).join('/')
68}
69
70u.link = function (parts, text, raw, props) {
71 if (text == null) text = parts[parts.length-1]
72 if (!raw) text = u.escape(text)
73 return '<a href="' + u.encodeLink(parts) + '"' +
74 (props ? ' ' + props : '') +
75 '>' + text + '</a>'
76}
77
78u.timestamp = function (time, req) {
79 time = Number(time)
80 var d = new Date(time)
81 return '<span title="' + time + '">' +
82 d.toLocaleString(req._locale) + '</span>'
83}
84
85u.nav = function (links, page, after) {
86 return pull.once(['<nav>'].concat(
87 links.map(function (link) {
88 var href = typeof link[0] == 'string' ? link[0] : u.encodeLink(link[0])
89 var props = link[2] == page ? ' class="active"' : ''
90 return '<a href="' + href + '"' + props + '>' + link[1] + '</a>'
91 }), after || '', '</nav>').join('')
92 )
93}
94
95u.hiddenInputs = function (values) {
96 return Object.keys(values).map(function (key) {
97 return '<input type="hidden"' +
98 ' name="' + u.escape(key) + '"' +
99 ' value="' + u.escape(values[key]) + '"/>'
100 }).join('')
101}
102
103u.highlight = function(code, lang) {
104 if (code.length > 100000) return u.escape(code)
105 try {
106 return lang
107 ? Highlight.highlight(lang, code).value
108 : Highlight.highlightAuto(code).value
109 } catch(e) {
110 if (/^Unknown language/.test(e.message))
111 return u.escape(code)
112 throw e
113 }
114}
115
116u.pre = function (text) {
117 return '<pre>' + u.escape(text) + '</pre>'
118}
119
120u.json = function (obj) {
121 return u.linkify(u.pre(JSON.stringify(obj, null, 2)))
122}
123
124u.linkify = function (text) {
125 // regex is from ssb-ref
126 return text.replace(/(@|%|&|&amp;)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) {
127 return '<a href="/' + encodeURIComponent(str) + '">' + str + '</a>'
128 })
129}
130
131u.readObjectString = function (obj, cb) {
132 pull(obj.read, pull.collect(function (err, bufs) {
133 if (err) return cb(err)
134 cb(null, Buffer.concat(bufs, obj.length).toString('utf8'))
135 }))
136}
137
138u.pullReverse = function () {
139 return function (read) {
140 return u.readNext(function (cb) {
141 pull(read, pull.collect(function (err, items) {
142 cb(err, items && pull.values(items.reverse()))
143 }))
144 })
145 }
146}
147
148function compareMsgs(a, b) {
149 return (a.value.timestamp - b.value.timestamp) || (a.key - b.key)
150}
151
152u.pullSort = function (comparator, descending) {
153 return function (read) {
154 return u.readNext(function (cb) {
155 pull(read, pull.collect(function (err, items) {
156 if (err) return cb(err)
157 items.sort(comparator)
158 if (descending) items.reverse()
159 cb(null, pull.values(items))
160 }))
161 })
162 }
163}
164
165u.sortMsgs = function (descending) {
166 return u.pullSort(compareMsgs, descending)
167}
168
169u.truncate = function (str, len) {
170 str = String(str)
171 return str.length < len ? str : str.substr(0, len) + '…'
172}
173
174u.messageTitle = function (msg) {
175 var c = msg.value.content
176 return u.truncate(c.title || c.text || msg.key, 40)
177}
178
179u.ifModifiedSince = function (req, lastMod) {
180 var ifModSince = req.headers['if-modified-since']
181 if (!ifModSince) return false
182 var d = new Date(ifModSince)
183 return d && Math.floor(d/1000) >= Math.floor(lastMod/1000)
184}
185
186u.decryptMessages = function (sbot) {
187 return paramap(function (msg, cb) {
188 var c = msg && msg.value && msg.value.content
189 if (c && typeof c === 'string' && c.slice(-4) === '.box') {
190 sbot.private.unbox(msg.value.content, function (err, content) {
191 if (err) return cb(null, msg) // leave message encrypted
192 var msg1 = {}
193 for (var k in msg) msg1[k] = msg[k]
194 msg1.value = {}
195 for (var j in msg.value) msg1.value[j] = msg.value[j]
196 msg1.value.private = true
197 msg1.value.content = content
198 if (!content.recps) {
199 sbot.whoami(function (err, feed) {
200 if (err) return cb(err)
201 content.recps = [msg1.value.author]
202 if (feed.id !== msg1.value.author) content.recps.push(feed.id)
203 cb(null, msg1)
204 })
205 } else cb(null, msg1)
206 })
207 } else cb(null, msg)
208 }, 4)
209}
210

Built with git-ssb-web