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