index.jsView |
---|
10 | 10 | var memo = require('asyncmemo') |
11 | 11 | var lru = require('lrucache') |
12 | 12 | var serveEmoji = require('emoji-server')() |
13 | 13 | var { |
14 | | - MdRenderer, |
15 | | - renderEmoji, |
16 | | - formatMsgs, |
17 | | - wrapPage, |
18 | | - renderThread, |
19 | | - renderAbout, |
20 | | - renderShowAll, |
21 | | - renderRssItem, |
22 | | - wrapRss, |
| 14 | + MdRenderer, |
| 15 | + renderEmoji, |
| 16 | + formatMsgs, |
| 17 | + wrapPage, |
| 18 | + renderThread, |
| 19 | + renderAbout, |
| 20 | + renderShowAll, |
| 21 | + renderRssItem, |
| 22 | + wrapRss, |
23 | 23 | } = require('./render'); |
24 | 24 | |
25 | 25 | var appHash = hash([fs.readFileSync(__filename)]) |
26 | 26 | |
51 | 51 | emoji_base: conf.emoji_base || (base + 'emoji/'), |
52 | 52 | } |
53 | 53 | |
54 | 54 | defaultOpts.marked = { |
55 | | - gfm: true, |
56 | | - mentions: true, |
57 | | - tables: true, |
58 | | - breaks: true, |
59 | | - pedantic: false, |
60 | | - sanitize: true, |
61 | | - smartLists: true, |
62 | | - smartypants: false, |
63 | | - emoji: renderEmoji, |
64 | | - renderer: new MdRenderer(defaultOpts) |
| 55 | + gfm: true, |
| 56 | + mentions: true, |
| 57 | + tables: true, |
| 58 | + breaks: true, |
| 59 | + pedantic: false, |
| 60 | + sanitize: true, |
| 61 | + smartLists: true, |
| 62 | + smartypants: false, |
| 63 | + emoji: renderEmoji, |
| 64 | + renderer: new MdRenderer(defaultOpts) |
65 | 65 | } |
66 | 66 | |
67 | 67 | var getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot) |
68 | 68 | var getAbout = memo({cache: lru(100)}, require('./lib/about'), sbot) |
95 | 95 | } |
96 | 96 | } |
97 | 97 | |
98 | 98 | function serveFeed(req, res, feedId, ext) { |
99 | | - console.log("serving feed: " + feedId) |
| 99 | + console.log("serving feed: " + feedId) |
100 | 100 | |
101 | | - var showAll = req.url.endsWith("?showAll"); |
| 101 | + var showAll = req.url.endsWith("?showAll"); |
102 | 102 | |
103 | | - getAbout(feedId, function (err, about) { |
104 | | - if (err) return respond(res, 500, err.stack || err) |
| 103 | + getAbout(feedId, function (err, about) { |
| 104 | + if (err) return respond(res, 500, err.stack || err) |
105 | 105 | |
106 | | - function render() { |
107 | | - switch (ext) { |
108 | | - case 'rss': |
109 | | - return pull( |
110 | | - |
111 | | - renderRssItem(defaultOpts), wrapRss(about.name, defaultOpts) |
112 | | - ); |
113 | | - default: |
114 | | - return pull( |
115 | | - renderAbout(defaultOpts, about, |
116 | | - renderShowAll(showAll, req.url)), wrapPage(about.name) |
117 | | - ); |
118 | | - } |
119 | | - } |
| 106 | + function render() { |
| 107 | + switch (ext) { |
| 108 | + case 'rss': |
| 109 | + return pull( |
| 110 | + |
| 111 | + renderRssItem(defaultOpts), wrapRss(about.name, defaultOpts) |
| 112 | + ); |
| 113 | + default: |
| 114 | + return pull( |
| 115 | + renderAbout(defaultOpts, about, |
| 116 | + renderShowAll(showAll, req.url)), wrapPage(about.name) |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
120 | 120 | |
| 121 | + pull( |
| 122 | + sbot.createUserStream({ id: feedId, reverse: true, limit: showAll ? -1 : (ext == 'rss' ? 25 : 10) }), |
| 123 | + pull.collect(function (err, logs) { |
| 124 | + if (err) return respond(res, 500, err.stack || err) |
| 125 | + res.writeHead(200, { |
| 126 | + 'Content-Type': ctype(ext) |
| 127 | + }) |
121 | 128 | pull( |
122 | | - sbot.createUserStream({ id: feedId, reverse: true, limit: showAll ? -1 : (ext == 'rss' ? 25 :10) }), |
123 | | - pull.collect(function (err, logs) { |
124 | | - if (err) return respond(res, 500, err.stack || err) |
125 | | - res.writeHead(200, { |
126 | | - 'Content-Type': ctype(ext) |
127 | | - }) |
128 | | - pull( |
129 | | - pull.values(logs), |
130 | | - paramap(addAuthorAbout, 8), |
131 | | - paramap(addFollowAbout, 8), |
132 | | - paramap(addVoteMessage, 8), |
133 | | - paramap(addGitLinks, 8), |
134 | | - render(), |
135 | | - toPull(res, function (err) { |
136 | | - if (err) console.error('[viewer]', err) |
137 | | - }) |
138 | | - ) |
139 | | - }) |
| 129 | + pull.values(logs), |
| 130 | + paramap(addAuthorAbout, 8), |
| 131 | + paramap(addFollowAbout, 8), |
| 132 | + paramap(addVoteMessage, 8), |
| 133 | + paramap(addGitLinks, 8), |
| 134 | + render(), |
| 135 | + toPull(res, function (err) { |
| 136 | + if (err) console.error('[viewer]', err) |
| 137 | + }) |
140 | 138 | ) |
141 | | - }) |
| 139 | + }) |
| 140 | + ) |
| 141 | + }) |
142 | 142 | } |
143 | 143 | |
144 | 144 | function serveUserFeed(req, res, url) { |
145 | | - var feedId = url.substring(url.lastIndexOf('user-feed/')+10, 100) |
146 | | - console.log("serving user feed: " + feedId) |
| 145 | + var feedId = url.substring(url.lastIndexOf('user-feed/')+10, 100) |
| 146 | + console.log("serving user feed: " + feedId) |
147 | 147 | |
148 | | - var following = [] |
149 | | - var channelSubscriptions = [] |
150 | | - |
151 | | - getAbout(feedId, function (err, about) { |
152 | | - pull( |
153 | | - sbot.createUserStream({ id: feedId }), |
154 | | - pull.filter((msg) => { |
155 | | - return !msg.value || |
156 | | - msg.value.content.type == 'contact' || |
157 | | - (msg.value.content.type == 'channel' && |
158 | | - typeof msg.value.content.subscribed != 'undefined') |
159 | | - }), |
160 | | - pull.collect(function (err, msgs) { |
161 | | - msgs.forEach((msg) => { |
162 | | - if (msg.value.content.type == 'contact') |
163 | | - { |
164 | | - if (msg.value.content.following) |
165 | | - following[msg.value.content.contact] = 1 |
166 | | - else |
167 | | - delete following[msg.value.content.contact] |
168 | | - } |
169 | | - else |
170 | | - { |
171 | | - if (msg.value.content.subscribed) |
172 | | - channelSubscriptions[msg.value.content.channel] = 1 |
173 | | - else |
174 | | - delete channelSubscriptions[msg.value.content.channel] |
175 | | - } |
176 | | - }) |
177 | | - |
178 | | - serveFeeds(req, res, following, channelSubscriptions, feedId, 'user feed ' + (about ? about.name : "")) |
179 | | - }) |
180 | | - ) |
181 | | - }) |
| 148 | + var following = [] |
| 149 | + var channelSubscriptions = [] |
| 150 | + |
| 151 | + getAbout(feedId, function (err, about) { |
| 152 | + pull( |
| 153 | + sbot.createUserStream({ id: feedId }), |
| 154 | + pull.filter((msg) => { |
| 155 | + return !msg.value || |
| 156 | + msg.value.content.type == 'contact' || |
| 157 | + (msg.value.content.type == 'channel' && |
| 158 | + typeof msg.value.content.subscribed != 'undefined') |
| 159 | + }), |
| 160 | + pull.collect(function (err, msgs) { |
| 161 | + msgs.forEach((msg) => { |
| 162 | + if (msg.value.content.type == 'contact') |
| 163 | + { |
| 164 | + if (msg.value.content.following) |
| 165 | + following[msg.value.content.contact] = 1 |
| 166 | + else |
| 167 | + delete following[msg.value.content.contact] |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + if (msg.value.content.subscribed) |
| 172 | + channelSubscriptions[msg.value.content.channel] = 1 |
| 173 | + else |
| 174 | + delete channelSubscriptions[msg.value.content.channel] |
| 175 | + } |
| 176 | + }) |
| 177 | + |
| 178 | + serveFeeds(req, res, following, channelSubscriptions, feedId, 'user feed ' + (about ? about.name : "")) |
| 179 | + }) |
| 180 | + ) |
| 181 | + }) |
182 | 182 | } |
183 | 183 | |
184 | 184 | function serveFeeds(req, res, following, channelSubscriptions, feedId, name) { |
185 | | - pull( |
186 | | - sbot.createLogStream({ reverse: true, limit: 5000 }), |
187 | | - pull.filter((msg) => { |
188 | | - return !msg.value || |
189 | | - (msg.value.author in following || |
190 | | - msg.value.content.channel in channelSubscriptions) |
191 | | - }), |
192 | | - pull.take(150), |
193 | | - pull.collect(function (err, logs) { |
194 | | - if (err) return respond(res, 500, err.stack || err) |
195 | | - res.writeHead(200, { |
196 | | - 'Content-Type': ctype("html") |
197 | | - }) |
198 | | - pull( |
199 | | - pull.values(logs), |
200 | | - paramap(addAuthorAbout, 8), |
201 | | - paramap(addFollowAbout, 8), |
202 | | - paramap(addVoteMessage, 8), |
203 | | - paramap(addGitLinks, 8), |
204 | | - pull(renderThread(defaultOpts), wrapPage(name)), |
205 | | - toPull(res, function (err) { |
206 | | - if (err) console.error('[viewer]', err) |
207 | | - }) |
208 | | - ) |
| 185 | + pull( |
| 186 | + sbot.createLogStream({ reverse: true, limit: 5000 }), |
| 187 | + pull.filter((msg) => { |
| 188 | + return !msg.value || |
| 189 | + (msg.value.author in following || |
| 190 | + msg.value.content.channel in channelSubscriptions) |
| 191 | + }), |
| 192 | + pull.take(150), |
| 193 | + pull.collect(function (err, logs) { |
| 194 | + if (err) return respond(res, 500, err.stack || err) |
| 195 | + res.writeHead(200, { |
| 196 | + 'Content-Type': ctype("html") |
| 197 | + }) |
| 198 | + pull( |
| 199 | + pull.values(logs), |
| 200 | + paramap(addAuthorAbout, 8), |
| 201 | + paramap(addFollowAbout, 8), |
| 202 | + paramap(addVoteMessage, 8), |
| 203 | + paramap(addGitLinks, 8), |
| 204 | + pull(renderThread(defaultOpts), wrapPage(name)), |
| 205 | + toPull(res, function (err) { |
| 206 | + if (err) console.error('[viewer]', err) |
209 | 207 | }) |
210 | | - ) |
| 208 | + ) |
| 209 | + }) |
| 210 | + ) |
211 | 211 | } |
212 | 212 | |
213 | 213 | function serveChannel(req, res, url) { |
214 | | - var channelId = url.substring(url.lastIndexOf('channel/')+8, 100) |
215 | | - console.log("serving channel: " + channelId) |
| 214 | + var channelId = url.substring(url.lastIndexOf('channel/')+8, 100) |
| 215 | + console.log("serving channel: " + channelId) |
216 | 216 | |
217 | | - var showAll = req.url.endsWith("?showAll") |
218 | | - |
219 | | - pull( |
220 | | - sbot.query.read({ limit: showAll ? 300 : 10, reverse: true, query: [{$filter: { value: { content: { channel: channelId }}}}]}), |
221 | | - pull.collect(function (err, logs) { |
222 | | - if (err) return respond(res, 500, err.stack || err) |
223 | | - res.writeHead(200, { |
224 | | - 'Content-Type': ctype("html") |
225 | | - }) |
226 | | - pull( |
227 | | - pull.values(logs), |
228 | | - paramap(addAuthorAbout, 8), |
229 | | - paramap(addVoteMessage, 8), |
230 | | - pull(renderThread(defaultOpts, '', |
231 | | - renderShowAll(showAll, req.url)), |
232 | | - wrapPage('#' + channelId)), |
233 | | - toPull(res, function (err) { |
234 | | - if (err) console.error('[viewer]', err) |
235 | | - }) |
236 | | - ) |
| 217 | + var showAll = req.url.endsWith("?showAll") |
| 218 | + |
| 219 | + pull( |
| 220 | + sbot.query.read({ limit: showAll ? 300 : 10, reverse: true, query: [{$filter: { value: { content: { channel: channelId }}}}]}), |
| 221 | + pull.collect(function (err, logs) { |
| 222 | + if (err) return respond(res, 500, err.stack || err) |
| 223 | + res.writeHead(200, { |
| 224 | + 'Content-Type': ctype("html") |
| 225 | + }) |
| 226 | + pull( |
| 227 | + pull.values(logs), |
| 228 | + paramap(addAuthorAbout, 8), |
| 229 | + paramap(addVoteMessage, 8), |
| 230 | + pull(renderThread(defaultOpts, '', |
| 231 | + renderShowAll(showAll, req.url)), |
| 232 | + wrapPage('#' + channelId)), |
| 233 | + toPull(res, function (err) { |
| 234 | + if (err) console.error('[viewer]', err) |
237 | 235 | }) |
238 | | - ) |
| 236 | + ) |
| 237 | + }) |
| 238 | + ) |
239 | 239 | } |
240 | 240 | |
241 | 241 | function serveId(req, res, id, ext, query) { |
242 | 242 | var q = query ? qs.parse(query) : {} |
296 | 296 | ) |
297 | 297 | } |
298 | 298 | |
299 | 299 | function addFollowAbout(msg, cb) { |
300 | | - if (msg.value.content.contact) |
301 | | - getAbout(msg.value.content.contact, function (err, about) { |
302 | | - if (err) return cb(err) |
303 | | - msg.value.content.contactAbout = about |
304 | | - cb(null, msg) |
305 | | - }) |
306 | | - else |
307 | | - cb(null, msg) |
| 300 | + if (msg.value.content.contact) |
| 301 | + getAbout(msg.value.content.contact, function (err, about) { |
| 302 | + if (err) return cb(err) |
| 303 | + msg.value.content.contactAbout = about |
| 304 | + cb(null, msg) |
| 305 | + }) |
| 306 | + else |
| 307 | + cb(null, msg) |
308 | 308 | } |
309 | 309 | |
310 | 310 | function addVoteMessage(msg, cb) { |
311 | | - if (msg.value.content.type == 'vote' && msg.value.content.vote.link[0] == '%') |
312 | | - getMsg(msg.value.content.vote.link, function (err, linkedMsg) { |
313 | | - if (linkedMsg) |
314 | | - msg.value.content.vote.linkedText = linkedMsg.value.content.text |
315 | | - cb(null, msg) |
316 | | - }) |
317 | | - else |
318 | | - cb(null, msg) |
| 311 | + if (msg.value.content.type == 'vote' && msg.value.content.vote.link[0] == '%') |
| 312 | + getMsg(msg.value.content.vote.link, function (err, linkedMsg) { |
| 313 | + if (linkedMsg) |
| 314 | + msg.value.content.vote.linkedText = linkedMsg.value.content.text |
| 315 | + cb(null, msg) |
| 316 | + }) |
| 317 | + else |
| 318 | + cb(null, msg) |
319 | 319 | } |
320 | 320 | |
321 | 321 | function addAuthorAbout(msg, cb) { |
322 | 322 | getAbout(msg.value.author, function (err, about) { |
326 | 326 | }) |
327 | 327 | } |
328 | 328 | |
329 | 329 | function addGitLinks(msg, cb) { |
330 | | - if (msg.value.content.type == 'git-update') |
331 | | - getMsg(msg.value.content.repo, function (err, gitRepo) { |
332 | | - if (gitRepo) |
333 | | - msg.value.content.repoName = gitRepo.value.content.name |
334 | | - cb(null, msg) |
335 | | - }) |
336 | | - else if (msg.value.content.type == 'issue') |
337 | | - getMsg(msg.value.content.project, function (err, gitRepo) { |
338 | | - if (gitRepo) |
339 | | - msg.value.content.repoName = gitRepo.value.content.name |
340 | | - cb(null, msg) |
341 | | - }) |
342 | | - else |
343 | | - cb(null, msg) |
| 330 | + if (msg.value.content.type == 'git-update') |
| 331 | + getMsg(msg.value.content.repo, function (err, gitRepo) { |
| 332 | + if (gitRepo) |
| 333 | + msg.value.content.repoName = gitRepo.value.content.name |
| 334 | + cb(null, msg) |
| 335 | + }) |
| 336 | + else if (msg.value.content.type == 'issue') |
| 337 | + getMsg(msg.value.content.project, function (err, gitRepo) { |
| 338 | + if (gitRepo) |
| 339 | + msg.value.content.repoName = gitRepo.value.content.name |
| 340 | + cb(null, msg) |
| 341 | + }) |
| 342 | + else |
| 343 | + cb(null, msg) |
344 | 344 | } |
345 | 345 | } |
346 | 346 | |
347 | 347 | function serveBlob(req, res, sbot, id) { |