git ssb

30+

cel / git-ssb-web



Commit d906788c7463b62013560e99db80c29c8e7ca974

Paginate feed pages and commits list

Close %p5d5lpE5HtEZWNWDq5OtV8JbXYCJrvIXs0FPbjm6ZSk=.sha256

Remove limit from repo activity feed, since links() can't be paginated
Charles Lehner committed on 4/1/2016, 4:53:13 AM
Parent: 77ade4785c4f38b2cd894a1f7c1daf2707c24308

Files changed

index.jschanged
index.jsView
@@ -232,8 +232,51 @@
232232 })
233233 }
234234 }
235235
236 +function paginate(onFirst, through, onLast, onEmpty) {
237 + var ended, last, first = true, queue = []
238 + return function (read) {
239 + var mappedRead = through(function (end, cb) {
240 + if (ended = end) return read(ended, cb)
241 + if (queue.length)
242 + return cb(null, queue.shift())
243 + read(null, function (end, data) {
244 + if (end) return cb(end)
245 + last = data
246 + cb(null, data)
247 + })
248 + })
249 + return function (end, cb) {
250 + var tmp
251 + if (ended) return cb(ended)
252 + if (ended = end) return read(ended, cb)
253 + if (first)
254 + return read(null, function (end, data) {
255 + if (ended = end) {
256 + if (end === true && onEmpty)
257 + return onEmpty(cb)
258 + return cb(ended)
259 + }
260 + first = false
261 + last = data
262 + queue.push(data)
263 + if (onFirst)
264 + onFirst(data, cb)
265 + else
266 + mappedRead(null, cb)
267 + })
268 + mappedRead(null, function (end, data) {
269 + if (ended = end) {
270 + if (end === true && last)
271 + return onLast(last, cb)
272 + }
273 + cb(end, data)
274 + })
275 + }
276 + }
277 +}
278 +
236279 function readObjectString(obj, cb) {
237280 pull(obj.read, pull.collect(function (err, bufs) {
238281 if (err) return cb(err)
239282 cb(null, Buffer.concat(bufs, obj.length).toString('utf8'))
@@ -267,8 +310,18 @@
267310 function sortMsgs() {
268311 return pullSort(compareMsgs)
269312 }
270313
314 +function pullReverse() {
315 + return function (read) {
316 + return readNext(function (cb) {
317 + pull(read, pull.collect(function (err, items) {
318 + cb(err, items && pull.values(items.reverse()))
319 + }))
320 + })
321 + }
322 +}
323 +
271324 function tryDecodeURIComponent(str) {
272325 if (!str || (str[0] == '%' && ref.isBlobId(str)))
273326 return str
274327 try {
@@ -521,9 +574,9 @@
521574 return serveBlob(req, dir)
522575 else if (ref.isMsgId(dir))
523576 return serveMessage(req, dir, dirs.slice(1))
524577 else if (ref.isFeedId(dir))
525- return serveUserPage(dir, dirs.slice(1))
578 + return serveUserPage(req, dir, dirs.slice(1))
526579 else
527580 return serveFile(req, dirs)
528581 }
529582
@@ -661,22 +714,46 @@
661714 }
662715
663716 /* Feed */
664717
665- function renderFeed(feedId) {
718 + function renderFeed(req, feedId) {
719 + var query = req._u.query
666720 var opts = {
667- reverse: true,
721 + reverse: !query.forwards,
722 + lt: query.lt && +query.lt || Date.now(),
723 + gt: query.gt && +query.gt,
668724 id: feedId
669725 }
670726 return pull(
671727 feedId ? ssb.createUserStream(opts) : ssb.createFeedStream(opts),
672728 pull.filter(function (msg) {
673- return msg.value.content.type in msgTypes &&
674- msg.value.timestamp < Date.now()
729 + return msg.value.content.type in msgTypes
675730 }),
676731 pull.take(20),
677732 addAuthorName(about),
678- paramap(renderFeedItem, 8)
733 + query.forwards && pullReverse(),
734 + paginate(
735 + function (first, cb) {
736 + if (!query.lt && !query.gt) return cb(null, '')
737 + var gt = feedId ? first.value.sequence : first.value.timestamp + 1
738 + var q = qs.stringify({
739 + gt: gt,
740 + forwards: 1
741 + })
742 + cb(null, '<a href="?' + q + '">Newer</a>')
743 + },
744 + paramap(renderFeedItem, 8),
745 + function (last, cb) {
746 + cb(null, '<a href="?' + qs.stringify({
747 + lt: feedId ? last.value.sequence : last.value.timestamp - 1
748 + }) + '">Older</a>')
749 + },
750 + function (cb) {
751 + cb(null, query.forwards ?
752 + '<a href="?lt=' + (opts.gt + 1) + '">Older</a>' :
753 + '<a href="?gt=' + (opts.lt - 1) + '&amp;forwards=1">Newer</a>')
754 + }
755 + )
679756 )
680757 }
681758
682759 function renderFeedItem(msg, cb) {
@@ -713,18 +790,18 @@
713790 }
714791
715792 /* Index */
716793
717- function serveIndex() {
718- return serveTemplate('git ssb')(renderFeed())
794 + function serveIndex(req) {
795 + return serveTemplate('git ssb')(renderFeed(req))
719796 }
720797
721- function serveUserPage(feedId, dirs) {
798 + function serveUserPage(req, feedId, dirs) {
722799 switch (dirs[0]) {
723800 case undefined:
724801 case '':
725802 case 'activity':
726- return serveUserActivity(feedId)
803 + return serveUserActivity(req, feedId)
727804 case 'repos':
728805 return serveUserRepos(feedId)
729806 }
730807 }
@@ -744,10 +821,10 @@
744821 body,
745822 ]))
746823 }
747824
748- function serveUserActivity(feedId) {
749- return renderUserPage(feedId, 'activity', renderFeed(feedId))
825 + function serveUserActivity(req, feedId) {
826 + return renderUserPage(feedId, 'activity', renderFeed(req, feedId))
750827 }
751828
752829 function serveUserRepos(feedId) {
753830 return renderUserPage(feedId, 'repos', pull(
@@ -871,9 +948,9 @@
871948 return serveRepoTree(repo, branch, [])
872949 case 'activity':
873950 return serveRepoActivity(repo, branch)
874951 case 'commits':
875- return serveRepoCommits(repo, branch)
952 + return serveRepoCommits(req, repo, branch)
876953 case 'commit':
877954 return serveRepoCommit(repo, path[1])
878955 case 'tree':
879956 return serveRepoTree(repo, branch, filePath)
@@ -1004,10 +1081,9 @@
10041081 dest: repo.id,
10051082 source: repo.feed,
10061083 rel: 'repo',
10071084 values: true,
1008- reverse: true,
1009- limit: 8
1085 + reverse: true
10101086 }),
10111087 pull.map(renderRepoUpdate.bind(this, repo))
10121088 )
10131089 ]))
@@ -1038,19 +1114,27 @@
10381114 }
10391115
10401116 /* Repo commits */
10411117
1042- function serveRepoCommits(repo, branch) {
1118 + function serveRepoCommits(req, repo, branch) {
1119 + var query = req._u.query
10431120 return renderRepoPage(repo, 'commits', branch, cat([
10441121 pull.once('<h3>Commits</h3>'),
10451122 pull(
1046- repo.readLog(branch),
1047- paramap(function (hash, cb) {
1048- repo.getCommitParsed(hash, function (err, commit) {
1049- if (err) return cb(err)
1050- cb(null, renderCommit(repo, commit))
1051- })
1052- }, 8)
1123 + repo.readLog(query.start || branch),
1124 + pull.take(20),
1125 + paginate(
1126 + !query.start ? '' : function (first, cb) {
1127 + cb(null, '&hellip;')
1128 + },
1129 + pull(
1130 + paramap(repo.getCommitParsed.bind(repo), 8),
1131 + pull.map(renderCommit.bind(this, repo))
1132 + ),
1133 + function (last, cb) {
1134 + cb(null, '<a href="?start=' + last + '">Older</a>')
1135 + }
1136 + )
10531137 )
10541138 ]))
10551139 }
10561140

Built with git-ssb-web