Files: ea333278df31de123808813a031b904b32dab41f / lib / users.js
4463 bytesRaw
1 | var pull = require('pull-stream') |
2 | var cat = require('pull-cat') |
3 | var paramap = require('pull-paramap') |
4 | var multicb = require('multicb') |
5 | var u = require('./util') |
6 | |
7 | module.exports = function (web) { |
8 | return new UserRoutes(web) |
9 | } |
10 | |
11 | function UserRoutes(web) { |
12 | this.web = web |
13 | } |
14 | |
15 | var U = UserRoutes.prototype |
16 | |
17 | U.serveUserPage = function (req, feedId, dirs) { |
18 | switch (dirs[0]) { |
19 | case undefined: |
20 | case '': |
21 | case 'activity': |
22 | return this.serveUserActivity(req, feedId) |
23 | case 'repos': |
24 | return this.serveUserRepos(req, feedId) |
25 | case 'digs': |
26 | return this.serveUserDigs(req, feedId) |
27 | } |
28 | } |
29 | |
30 | U.renderUserPage = function (req, feedId, page, titleTemplate, body) { |
31 | var self = this |
32 | return u.readNext(function (cb) { |
33 | self.web.about.getName(feedId, function (err, name) { |
34 | if (err) return cb(err) |
35 | var title = titleTemplate ? titleTemplate |
36 | .replace(/\%{name\}/g, u.escape(name)) |
37 | : u.escape(name) |
38 | cb(null, self.web.serveTemplate(req, title)(cat([ |
39 | pull.once('<h2>' + u.link([feedId], name) + |
40 | '<code class="user-id">' + feedId + '</code></h2>' + |
41 | u.nav([ |
42 | [[feedId], req._t('Activity'), 'activity'], |
43 | [[feedId, 'repos'], req._t('Repos'), 'repos'], |
44 | [[feedId, 'digs'], req._t('ReposDug'), 'digs'], |
45 | ], page)), |
46 | body |
47 | ]))) |
48 | }) |
49 | }) |
50 | } |
51 | |
52 | U.serveUserActivity = function (req, feedId) { |
53 | return this.renderUserPage(req, feedId, 'activity', null, |
54 | this.web.renderFeed(req, feedId)) |
55 | } |
56 | |
57 | U.serveUserRepos = function (req, feedId) { |
58 | var self = this |
59 | var title = req._t('UsersRepos', {name: '%{name}'}) |
60 | return self.renderUserPage(req, feedId, 'repos', title, pull( |
61 | cat([ |
62 | self.web.ssb.messagesByType({ |
63 | type: 'git-update', |
64 | reverse: true |
65 | }), |
66 | self.web.ssb.messagesByType({ |
67 | type: 'git-repo', |
68 | reverse: true |
69 | }) |
70 | ]), |
71 | pull.filter(function (msg) { |
72 | return msg.value.author == feedId |
73 | }), |
74 | pull.unique(function (msg) { |
75 | return msg.value.content.repo || msg.key |
76 | }), |
77 | pull.take(20), |
78 | paramap(function (msg, cb) { |
79 | var repoId = msg.value.content.repo || msg.key |
80 | var done = multicb({ pluck: 1, spread: true }) |
81 | self.web.getRepoName(feedId, repoId, done()) |
82 | self.web.getVotes(repoId, done()) |
83 | done(function (err, repoName, votes) { |
84 | if (err) return cb(err) |
85 | cb(null, '<section class="collapse">' + |
86 | '<span class="right-bar">' + |
87 | '<i>✌</i> ' + |
88 | u.link([repoId, 'digs'], votes.upvotes, true, |
89 | ' title="' + req._t('Digs') + '"') + |
90 | '</span>' + |
91 | '<strong>' + u.link([repoId], repoName) + '</strong>' + |
92 | '<div class="date-info">' + |
93 | req._t(msg.value.content.type == 'git-update' ? |
94 | 'UpdatedOnDate' : 'CreatedOnDate', |
95 | { |
96 | date: u.timestamp(msg.value.timestamp, req) |
97 | }) + '</div>' + |
98 | '</section>') |
99 | }) |
100 | }, 8) |
101 | )) |
102 | } |
103 | |
104 | U.serveUserDigs = function (req, feedId) { |
105 | var self = this |
106 | var title = req._t('UsersReposDug', {name: '%{name}'}) |
107 | return self.renderUserPage(req, feedId, 'digs', title, pull( |
108 | self.web.ssb.links({ |
109 | source: feedId, |
110 | rel: 'vote', |
111 | dest: '%', |
112 | reverse: true, |
113 | values: true |
114 | }), |
115 | pull.unique('dest'), |
116 | u.sortMsgs(true), |
117 | paramap(function (link, cb) { |
118 | self.web.getMsg(link.dest, function (err, destMsg) { |
119 | if (err) return cb(err) |
120 | if (destMsg.content.type != 'git-repo') return cb() |
121 | var vote = link.value.content && link.value.content.vote |
122 | if (!(vote && vote.value > 0)) return cb() |
123 | var repoId = link.dest |
124 | var repoAuthor = destMsg.author |
125 | var done = multicb({ pluck: 1, spread: true }) |
126 | self.web.getRepoName(repoAuthor, repoId, done()) |
127 | self.web.about.getName(repoAuthor, done()) |
128 | done(function (err, repoName, authorName) { |
129 | var authorLink = u.link([repoAuthor], authorName) |
130 | var repoLink = u.link([repoId], repoName) |
131 | if (err) return cb(err) |
132 | cb(null, '<section class="collapse">' + |
133 | '<strong class="bgslash">' + |
134 | authorLink + ' / ' + repoLink + '</strong>' + |
135 | '<div class="date-info">' + |
136 | req._t('DugOnDate', { |
137 | date: u.timestamp(link.value.timestamp, req) |
138 | }) + '</div>' + |
139 | '</section>') |
140 | }) |
141 | }) |
142 | }, 12), |
143 | pull.filter() |
144 | )) |
145 | } |
146 |
Built with git-ssb-web