Files: fc00f1e2dd977f0c5a6d4f86c8e89ce1a35ba02c / lib / users.js
5072 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 | |
65 | // use git index, if present |
66 | var source |
67 | if (self.web.ssb.gitindex) { |
68 | source = pull( |
69 | self.web.ssb.gitindex.author({ |
70 | lt: '9999999999999', |
71 | values: true, |
72 | reverse: true, |
73 | id: feedId |
74 | }), |
75 | pull.map(function (msg) { return msg.value }) |
76 | ) |
77 | } else { |
78 | source = pull( |
79 | cat([ |
80 | self.web.ssb.messagesByType({ |
81 | type: 'git-update', |
82 | reverse: true |
83 | }), |
84 | self.web.ssb.messagesByType({ |
85 | type: 'git-repo', |
86 | reverse: true |
87 | }) |
88 | ]), |
89 | pull.filter(function (msg) { |
90 | return msg.value.author == feedId |
91 | }) |
92 | ) |
93 | } |
94 | |
95 | return self.renderUserPage(req, feedId, 'repos', title, pull( |
96 | source, |
97 | pull.unique(function (msg) { |
98 | return msg.value.content.repo || msg.key |
99 | }), |
100 | paramap(function (msg, cb) { |
101 | var repoId = msg.value.content.repo || msg.key |
102 | var done = multicb({ pluck: 1, spread: true }) |
103 | self.web.getRepoName(feedId, repoId, done()) |
104 | self.web.getVotes(repoId, done()) |
105 | done(function (err, repoName, votes) { |
106 | if (err) return cb(err) |
107 | cb(null, '<section class="collapse">' + |
108 | '<span class="right-bar">' + |
109 | '<i>✌</i> ' + |
110 | u.link([repoId, 'digs'], votes.upvotes, true, |
111 | ' title="' + req._t('Digs') + '"') + |
112 | '</span>' + |
113 | '<strong>' + u.link([repoId], repoName) + '</strong>' + |
114 | '<div class="date-info">' + |
115 | req._t(msg.value.content.type == 'git-update' ? |
116 | 'UpdatedOnDate' : 'CreatedOnDate', |
117 | { |
118 | date: u.timestamp(msg.value.timestamp, req) |
119 | }) + '</div>' + |
120 | '</section>') |
121 | }) |
122 | }, 8) |
123 | )) |
124 | } |
125 | |
126 | U.serveUserDigs = function (req, feedId) { |
127 | var self = this |
128 | var title = req._t('UsersReposDug', {name: '%{name}'}) |
129 | return self.renderUserPage(req, feedId, 'digs', title, pull( |
130 | self.web.ssb.links({ |
131 | source: feedId, |
132 | rel: 'vote', |
133 | dest: '%', |
134 | reverse: true, |
135 | values: true |
136 | }), |
137 | pull.unique('dest'), |
138 | u.sortMsgs(true), |
139 | paramap(function (link, cb) { |
140 | if (!link.dest) return cb() |
141 | self.web.getMsg(link.dest, function (err, destMsg) { |
142 | if (err) return cb(err) |
143 | if (destMsg.value.content.type != 'git-repo') return cb() |
144 | var vote = link.value.content && link.value.content.vote |
145 | if (!(vote && vote.value > 0)) return cb() |
146 | var repoId = link.dest |
147 | var repoAuthor = destMsg.value.author |
148 | var done = multicb({ pluck: 1, spread: true }) |
149 | self.web.getRepoName(repoAuthor, repoId, done()) |
150 | self.web.about.getName(repoAuthor, done()) |
151 | done(function (err, repoName, authorName) { |
152 | var authorLink = u.link([repoAuthor], authorName) |
153 | var repoLink = u.link([repoId], repoName) |
154 | if (err) return cb(err) |
155 | cb(null, '<section class="collapse">' + |
156 | '<strong class="bgslash">' + |
157 | authorLink + ' / ' + repoLink + '</strong>' + |
158 | '<div class="date-info">' + |
159 | req._t('DugOnDate', { |
160 | date: u.timestamp(link.value.timestamp, req) |
161 | }) + '</div>' + |
162 | '</section>') |
163 | }) |
164 | }) |
165 | }, 12), |
166 | pull.filter() |
167 | )) |
168 | } |
169 |
Built with git-ssb-web