git ssb

30+

cel / git-ssb-web



Tree: 6086011c37856d731385a00682e6f7fc4c01ed4b

Files: 6086011c37856d731385a00682e6f7fc4c01ed4b / lib / util.js

5319 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 (!lang || code.length > 100000) return u.escape(code)
116 // auto highlighting removed because it was causing highlight.js to hang
117 return Highlight.highlight(lang, code).value
118}
119
120u.pre = function (text) {
121 return '<pre>' + u.escape(text) + '</pre>'
122}
123
124u.json = function (obj) {
125 return u.linkify(u.pre(JSON.stringify(obj, null, 2)))
126}
127
128u.linkify = function (text) {
129 // regex is from ssb-ref
130 return text.replace(/(@|%|&|&amp;)[A-Za-z0-9\/+]{43}=\.[\w\d]+/g, function (str) {
131 return '<a href="/' + str + '">' + str + '</a>'
132 })
133}
134
135u.readObjectString = function (obj, cb) {
136 pull(obj.read, pull.collect(function (err, bufs) {
137 if (err) return cb(err)
138 cb(null, Buffer.concat(bufs, obj.length).toString('utf8'))
139 }))
140}
141
142u.pullReverse = function () {
143 return function (read) {
144 return u.readNext(function (cb) {
145 pull(read, pull.collect(function (err, items) {
146 cb(err, items && pull.values(items.reverse()))
147 }))
148 })
149 }
150}
151
152function compareMsgs(a, b) {
153 return (a.value.timestamp - b.value.timestamp) || (a.key - b.key)
154}
155
156u.pullSort = function (comparator, descending) {
157 return function (read) {
158 return u.readNext(function (cb) {
159 pull(read, pull.collect(function (err, items) {
160 if (err) return cb(err)
161 items.sort(comparator)
162 if (descending) items.reverse()
163 cb(null, pull.values(items))
164 }))
165 })
166 }
167}
168
169u.sortMsgs = function (descending) {
170 return u.pullSort(compareMsgs, descending)
171}
172
173u.truncate = function (str, len) {
174 str = String(str)
175 return str.length < len ? str : str.substr(0, len) + '…'
176}
177
178u.messageTitle = function (msg) {
179 var c = msg.value.content
180 return u.truncate(c.title || c.text || msg.key, 40)
181}
182
183u.ifModifiedSince = function (req, lastMod) {
184 var ifModSince = req.headers['if-modified-since']
185 if (!ifModSince) return false
186 var d = new Date(ifModSince)
187 return d && Math.floor(d/1000) >= Math.floor(lastMod/1000)
188}
189
190u.decryptMessages = function (sbot) {
191 return paramap(function (msg, cb) {
192 var c = msg && msg.value && msg.value.content
193 if (c && typeof c === 'string' && c.slice(-4) === '.box') {
194 sbot.private.unbox(msg.value.content, function (err, content) {
195 if (err) return cb(null, msg) // leave message encrypted
196 var msg1 = {}
197 for (var k in msg) msg1[k] = msg[k]
198 msg1.value = {}
199 for (var j in msg.value) msg1.value[j] = msg.value[j]
200 msg1.value.private = true
201 msg1.value.content = content
202 if (!content.recps) {
203 sbot.whoami(function (err, feed) {
204 if (err) return cb(err)
205 content.recps = [msg1.value.author]
206 if (feed.id !== msg1.value.author) content.recps.push(feed.id)
207 cb(null, msg1)
208 })
209 } else cb(null, msg1)
210 })
211 } else cb(null, msg)
212 }, 4)
213}
214

Built with git-ssb-web