git ssb

30+

cel / git-ssb-web



Commit fd1acb17f9a97b7f8b7d3fac39f92f5ca08402ee

hack: simple notifications page

noffle committed on 5/22/2018, 2:13:30 AM
Parent: a7471b627ea57d0a21ed90814501993e9d28e8dc

Files changed

index.jschanged
package.jsonchanged
index.jsView
@@ -24,8 +24,9 @@
2424 var ident = require('pull-identify-filetype')
2525 var mime = require('mime-types')
2626 var moment = require('moment')
2727 var LRUCache = require('lrucache')
28 +var asyncFilter = require('pull-async-filter')
2829
2930 var hlCssPath = path.resolve(require.resolve('highlight.js'), '../../styles')
3031 var emojiPath = path.resolve(require.resolve('emoji-named-characters'), '../pngs')
3132
@@ -239,8 +240,10 @@
239240 return this.handlePOST(req, dir)
240241
241242 if (dir == '')
242243 return this.serveIndex(req)
244 + else if (dir == 'notifications')
245 + return this.serveNotifications(req)
243246 else if (dir == 'search')
244247 return this.serveSearch(req)
245248 else if (dir[0] === '#')
246249 return this.serveChannel(req, dir, dirs.slice(1))
@@ -537,8 +540,9 @@
537540 '<form action="/search" method="get">' +
538541 '<h1><a href="/">' + app +
539542 (appName == 'ssb' ? '' : ' <sub>' + appName + '</sub>') +
540543 '</a></h1> ' +
544 + '<a href="/notifications">NOTIFICATIONS</a>' +
541545 '<input class="search-bar" name="q" size="60"' +
542546 ' placeholder=" Search" value="' + q + '" />' +
543547 '</form>' +
544548 '</header>' +
@@ -650,8 +654,98 @@
650654 )
651655 )
652656 }
653657
658 +G.renderNotifications = function (req, feedId, filter) {
659 + // TODO: bail immediately if git-ssb-web is set to PUBLIC MODE
660 +
661 + var self = this
662 +
663 + var query = req._u.query
664 + var opts = {
665 + reverse: !query.forwards,
666 + lt: query.lt && +query.lt || Date.now(),
667 + gt: query.gt ? +query.gt : -Infinity,
668 + values: true,
669 + id: feedId
670 + }
671 +
672 + // use git index, if present
673 + var source
674 + if (this.ssb.gitindex) {
675 + source = pull(
676 + feedId ? this.ssb.gitindex.author(opts) : this.ssb.gitindex.read(opts),
677 + pull.map(function (msg) { return msg.value })
678 + )
679 + } else {
680 + source = feedId ? this.ssb.createUserStream(opts) : this.ssb.createFeedStream(opts)
681 + }
682 +
683 + var id = query.feed || self.myId
684 +
685 + return pull(
686 + source,
687 + u.decryptMessages(this.ssb),
688 + u.readableMessages(),
689 + pull.filter(function (msg) {
690 + var c = msg.value.content
691 + return c.type in msgTypes
692 + || (c.type == 'post' && c.repo && c.issue)
693 + }),
694 + typeof filter == 'function' ? filter(opts) : filter,
695 + // {issue, pr} on repo you created
696 + asyncFilter(function (msg, cb) {
697 + if (!msg.value.content.repo) return cb(null, true)
698 + if (msg.value.author === id) return cb(null, true)
699 +
700 + // get repo id the msg is in
701 + var repoId = msg.value.content.repo
702 +
703 + // get author of repo
704 + self.getRepo(repoId, function (err, repo) {
705 + if (err) return cb(null, true)
706 + var mine = (repo.feed === id)
707 + cb(null, !mine)
708 + })
709 + }),
710 + pull.take(25),
711 + this.addAuthorName(),
712 + query.forwards && u.pullReverse(),
713 + paginate(
714 + function (first, cb) {
715 + if (!query.lt && !query.gt) return cb(null, '')
716 + var gt = feedId ? first.value.sequence : first.value.timestamp + 1
717 + query.gt = gt
718 + query.forwards = 1
719 + delete query.lt
720 + cb(null, '<a href="?' + qs.stringify(query) + '">' +
721 + req._t('Next') + '</a>')
722 + },
723 + paramap(this.renderFeedItem.bind(this, req), 8),
724 + function (last, cb) {
725 + query.lt = feedId ? last.value.sequence : last.value.timestamp - 1
726 + delete query.gt
727 + delete query.forwards
728 + cb(null, '<a href="?' + qs.stringify(query) + '">' +
729 + req._t('Previous') + '</a>')
730 + },
731 + function (cb) {
732 + if (query.forwards) {
733 + delete query.gt
734 + delete query.forwards
735 + query.lt = opts.gt + 1
736 + } else {
737 + delete query.lt
738 + query.gt = opts.lt - 1
739 + query.forwards = 1
740 + }
741 + cb(null, '<a href="?' + qs.stringify(query) + '">' +
742 + req._t(query.forwards ? 'Older' : 'Newer') + '</a>')
743 + }
744 + )
745 + )
746 +}
747 +
654748 G.renderFeedItem = function (req, msg, cb) {
655749 var self = this
656750 var c = msg.value.content
657751 var msgDate = moment(new Date(msg.value.timestamp)).fromNow()
@@ -758,8 +852,12 @@
758852 G.serveIndex = function (req) {
759853 return this.serveTemplate(req)(this.renderFeed(req))
760854 }
761855
856 +G.serveNotifications = function (req) {
857 + return this.serveTemplate(req)(this.renderNotifications(req))
858 +}
859 +
762860 G.serveChannel = function (req, id, path) {
763861 var self = this
764862 return u.readNext(function (cb) {
765863 self.getRepo(id, function (err, repo) {
package.jsonView
@@ -12,8 +12,9 @@
1212 "mime-types": "^2.1.12",
1313 "moment": "^2.14.1",
1414 "multicb": "^1.2.1",
1515 "node-polyglot": "^1.0.0",
16 + "pull-async-filter": "^1.0.0",
1617 "pull-cat": "^1.1.11",
1718 "pull-git-pack": "^1.0.1",
1819 "pull-git-repo": "^1.3.0",
1920 "pull-hyperscript": "^0.2.2",

Built with git-ssb-web