Files: 63bcff079120449a73ea2cceec00484d3378041c / lib / users.js
4701 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 | paramap(function (msg, cb) { |
82 | var repoId = msg.value.content.repo || msg.key |
83 | var done = multicb({ pluck: 1, spread: true }) |
84 | self.web.getRepoName(feedId, repoId, done()) |
85 | self.web.getVotes(repoId, done()) |
86 | done(function (err, repoName, votes) { |
87 | if (err) return cb(err) |
88 | cb(null, '<section class="collapse">' + |
89 | '<span class="right-bar">' + |
90 | '<i>✌</i> ' + |
91 | u.link([repoId, 'digs'], votes.upvotes, true, |
92 | ' title="' + req._t('Digs') + '"') + |
93 | '</span>' + |
94 | '<strong>' + u.link([repoId], repoName) + '</strong>' + |
95 | '<div class="date-info">' + |
96 | req._t(msg.value.content.type == 'git-update' ? |
97 | 'UpdatedOnDate' : 'CreatedOnDate', |
98 | { |
99 | date: u.timestamp(msg.value.timestamp, req) |
100 | }) + '</div>' + |
101 | '</section>') |
102 | }) |
103 | }, 8) |
104 | )) |
105 | } |
106 | |
107 | U.serveUserDigs = function (req, feedId) { |
108 | var self = this |
109 | var title = req._t('UsersReposDug', {name: '%{name}'}) |
110 | return self.renderUserPage(req, feedId, 'digs', title, pull( |
111 | self.web.ssb.links({ |
112 | source: feedId, |
113 | rel: 'vote', |
114 | dest: '%', |
115 | reverse: true, |
116 | values: true |
117 | }), |
118 | pull.unique('dest'), |
119 | u.sortMsgs(true), |
120 | paramap(function (link, cb) { |
121 | if (!link.dest) return cb() |
122 | self.web.getMsg(link.dest, function (err, destMsg) { |
123 | if (err) return cb(err) |
124 | if (destMsg.value.content.type != 'git-repo') return cb() |
125 | var vote = link.value.content && link.value.content.vote |
126 | if (!(vote && vote.value > 0)) return cb() |
127 | var repoId = link.dest |
128 | var repoAuthor = destMsg.value.author |
129 | var done = multicb({ pluck: 1, spread: true }) |
130 | self.web.getRepoName(repoAuthor, repoId, done()) |
131 | self.web.about.getName(repoAuthor, done()) |
132 | done(function (err, repoName, authorName) { |
133 | var authorLink = u.link([repoAuthor], authorName) |
134 | var repoLink = u.link([repoId], repoName) |
135 | if (err) return cb(err) |
136 | cb(null, '<section class="collapse">' + |
137 | '<strong class="bgslash">' + |
138 | authorLink + ' / ' + repoLink + '</strong>' + |
139 | '<div class="date-info">' + |
140 | req._t('DugOnDate', { |
141 | date: u.timestamp(link.value.timestamp, req) |
142 | }) + '</div>' + |
143 | '</section>') |
144 | }) |
145 | }) |
146 | }, 12), |
147 | pull.filter() |
148 | )) |
149 | } |
150 |
Built with git-ssb-web