Files: dcaef795f59bc5e84215b51bc26bbaf77972e711 / lib / repos / issues.js
8298 bytesRaw
1 | var pull = require('pull-stream') |
2 | var cat = require('pull-cat') |
3 | var u = require('../util') |
4 | var markdown = require('../markdown') |
5 | var forms = require('../forms') |
6 | |
7 | module.exports = function (repoRoutes, web) { |
8 | return new RepoIssueRoutes(repoRoutes, web) |
9 | } |
10 | |
11 | function RepoIssueRoutes(repoRoutes, web) { |
12 | this.repo = repoRoutes |
13 | this.web = web |
14 | } |
15 | |
16 | var I = RepoIssueRoutes.prototype |
17 | |
18 | function getMention(msg, id) { |
19 | if (msg.key == id) return msg |
20 | var mentions = msg.value.content.mentions |
21 | if (mentions) for (var i = 0; i < mentions.length; i++) { |
22 | var mention = mentions[i] |
23 | if (mention.link == id) |
24 | return mention |
25 | } |
26 | return null |
27 | } |
28 | |
29 | /* Issues */ |
30 | |
31 | I.serveRepoIssues = function (req, repo, isPRs) { |
32 | var self = this |
33 | var count = 0 |
34 | var state = req._u.query.state || 'open' |
35 | var newPath = isPRs ? [repo.id, 'compare'] : [repo.id, 'issues', 'new'] |
36 | var title = req._t('Issues') + ' · %{author}/%{repo}' |
37 | var page = isPRs ? 'pulls' : 'issues' |
38 | return self.repo.renderRepoPage(req, repo, page, null, title, cat([ |
39 | pull.once( |
40 | (self.web.isPublic ? '' : |
41 | '<form class="right-bar" method="get"' + |
42 | ' action="' + u.encodeLink(newPath) + '">' + |
43 | '<button class="btn btn-primary">+ ' + |
44 | req._t(isPRs ? 'pullRequest.New' : 'issue.New') + |
45 | '</button>' + |
46 | '</form>') + |
47 | '<h3>' + req._t(isPRs ? 'PullRequests' : 'Issues') + '</h3>' + |
48 | u.nav([ |
49 | ['?', req._t('issues.Open'), 'open'], |
50 | ['?state=closed', req._t('issues.Closed'), 'closed'], |
51 | ['?state=all', req._t('issues.All'), 'all'] |
52 | ], state)), |
53 | pull( |
54 | (isPRs ? self.web.pullReqs : self.web.issues).list({ |
55 | repo: repo.id, |
56 | project: repo.id, |
57 | reverse: true, |
58 | open: {open: true, closed: false}[state] |
59 | }), |
60 | pull.map(function (issue) { |
61 | count++ |
62 | var state = (issue.open ? 'open' : 'closed') |
63 | var stateStr = req._t(issue.open ? |
64 | 'issue.state.Open' : 'issue.state.Closed') |
65 | return '<section class="collapse">' + |
66 | '<i class="issue-state issue-state-' + state + '"' + |
67 | ' title="' + stateStr + '">◼</i> ' + |
68 | '<a href="' + u.encodeLink(issue.id) + '">' + |
69 | u.escape(issue.title) + |
70 | '<span class="right-bar">' + |
71 | new Date(issue.created_at).toLocaleString(req._locale) + |
72 | '</span>' + |
73 | '</a>' + |
74 | '</section>' |
75 | }) |
76 | ), |
77 | u.readOnce(function (cb) { |
78 | cb(null, count > 0 ? '' : |
79 | '<p>' + req._t(isPRs ? 'NoPullRequests' : 'NoIssues') + '</p>') |
80 | }) |
81 | ])) |
82 | } |
83 | |
84 | /* New Issue */ |
85 | |
86 | I.serveRepoNewIssue = function (req, repo, issueId, path) { |
87 | var title = req._t('issue.New') + ' · %{author}/%{repo}' |
88 | return this.repo.renderRepoPage(req, repo, 'issues', null, title, pull.once( |
89 | '<h3>' + req._t('issue.New') + '</h3>' + |
90 | '<section><form action="" method="post">' + |
91 | '<input type="hidden" name="action" value="new-issue">' + |
92 | forms.post(req, repo, null, 8) + |
93 | '<button type="submit" class="btn">' + req._t('Create') + '</button>' + |
94 | '</form></section>')) |
95 | } |
96 | |
97 | /* Issue */ |
98 | |
99 | I.serveRepoIssue = function (req, repo, issue, path, postId) { |
100 | var self = this |
101 | var newestMsg = {key: issue.id, value: {timestamp: issue.created_at}} |
102 | var title = u.escape(issue.title) + ' · %{author}/%{repo}' |
103 | return self.repo.renderRepoPage(req, repo, 'issues', null, title, cat([ |
104 | pull.once( |
105 | '<h3>' + u.link([issue.id], issue.title) + '</h3>' + |
106 | '<code>' + issue.id + '</code>' + |
107 | '<section class="collapse">' + |
108 | (issue.open |
109 | ? '<strong class="issue-status open">' + |
110 | req._t('issue.state.Open') + '</strong>' |
111 | : '<strong class="issue-status closed">' + |
112 | req._t('issue.state.Closed') + '</strong>')), |
113 | u.readOnce(function (cb) { |
114 | self.web.about.getName(issue.author, function (err, authorName) { |
115 | if (err) return cb(err) |
116 | var authorLink = u.link([issue.author], authorName) |
117 | cb(null, req._t('issue.Opened', |
118 | {name: authorLink, datetime: u.timestamp(issue.created_at, req)})) |
119 | }) |
120 | }), |
121 | pull.once('<hr/>' + markdown(issue.text, repo) + '</section>'), |
122 | // render posts and edits |
123 | pull( |
124 | self.web.ssb.links({ |
125 | dest: issue.id, |
126 | values: true |
127 | }), |
128 | pull.unique('key'), |
129 | u.decryptMessages(self.web.ssb), |
130 | self.web.addAuthorName(), |
131 | u.sortMsgs(), |
132 | pull.through(function (msg) { |
133 | // the newest message in the issue thread |
134 | // becomes the branch of the new post |
135 | if (msg.value |
136 | && msg.value.timestamp > newestMsg.value.timestamp |
137 | && msg.value.content.root == issue.id) |
138 | newestMsg = msg |
139 | }), |
140 | pull.map(self.renderIssueActivityMsg.bind(self, req, repo, issue, |
141 | req._t('issue.'), postId)) |
142 | ), |
143 | self.web.isPublic ? pull.empty() : u.readOnce(function (cb) { |
144 | cb(null, forms.issueComment(req, issue, repo, |
145 | newestMsg.key, req._t('issue.'))) |
146 | }) |
147 | ])) |
148 | } |
149 | |
150 | I.renderIssueActivityMsg = function (req, repo, issue, type, postId, msg) { |
151 | var authorLink = u.link([msg.value.author], msg.authorName) |
152 | var msgHref = u.encodeLink(msg.key) + '#' + encodeURIComponent(msg.key) |
153 | var msgTimeLink = '<a href="' + msgHref + '"' + |
154 | ' name="' + u.escape(msg.key) + '">' + |
155 | new Date(msg.value.timestamp).toLocaleString(req._locale) + '</a>' |
156 | var c = msg.value.content |
157 | switch (c.type) { |
158 | case 'vote': |
159 | return '' |
160 | case 'post': |
161 | if (c.root == issue.id) { |
162 | var changed = this.web.issues.isStatusChanged(msg, issue) |
163 | return '<section class="collapse">' + |
164 | (msg.key == postId ? '<div class="highlight">' : '') + |
165 | '<tt class="right-bar item-id">' + msg.key + '</tt> ' + |
166 | (changed == null ? authorLink : req._t( |
167 | changed ? 'issue.Reopened' : 'issue.Closed', |
168 | {name: authorLink, type: type})) + |
169 | ' · ' + msgTimeLink + |
170 | (msg.key == postId ? '</div>' : '') + |
171 | markdown(c.text, repo) + |
172 | '</section>' |
173 | } else { |
174 | var text = c.text || (c.type + ' ' + msg.key) |
175 | return '<section class="collapse mention-preview">' + |
176 | req._t('issue.MentionedIn', { |
177 | name: authorLink, |
178 | type: type, |
179 | post: '<a href="/' + msg.key + '#' + msg.key + '">' + |
180 | String(text).substr(0, 140) + '</a>' |
181 | }) + '</section>' |
182 | } |
183 | case 'issue': |
184 | case 'pull-request': |
185 | return '<section class="collapse mention-preview">' + |
186 | req._t('issue.MentionedIn', { |
187 | name: authorLink, |
188 | type: type, |
189 | post: u.link([msg.key], u.messageTitle(msg)) |
190 | }) + '</section>' |
191 | case 'issue-edit': |
192 | return '<section class="collapse">' + |
193 | (msg.key == postId ? '<div class="highlight">' : '') + |
194 | // handle deprecated rename |
195 | (c.title == null ? '' : req._t('issue.Renamed', { |
196 | author: authorLink, |
197 | type: type, |
198 | name: '<q>' + u.escape(c.title) + '</q>' |
199 | })) + ' · ' + msgTimeLink + |
200 | (msg.key == postId ? '</div>' : '') + |
201 | '</section>' |
202 | case 'git-update': |
203 | var mention = this.web.issues.getMention(msg, issue) |
204 | if (mention) { |
205 | var commitLink = u.link([repo.id, 'commit', mention.object], |
206 | mention.label || mention.object) |
207 | return '<section class="collapse">' + |
208 | req._t(mention.open ? 'issue.Reopened' : 'issue.Closed', { |
209 | name: authorLink, |
210 | type: type |
211 | }) + ' · ' + msgTimeLink + '<br/>' + |
212 | commitLink + |
213 | '</section>' |
214 | } else if ((mention = getMention(msg, issue.id))) { |
215 | var commitLink = u.link(mention.object ? |
216 | [repo.id, 'commit', mention.object] : [msg.key], |
217 | mention.label || mention.object || msg.key) |
218 | return '<section class="collapse">' + |
219 | req._t('issue.Mentioned', { |
220 | name: authorLink, |
221 | type: type |
222 | }) + ' · ' + msgTimeLink + '<br/>' + |
223 | commitLink + |
224 | '</section>' |
225 | } else { |
226 | // fallthrough |
227 | } |
228 | |
229 | default: |
230 | return '<section class="collapse">' + |
231 | authorLink + |
232 | ' · ' + msgTimeLink + |
233 | u.json(c) + |
234 | '</section>' |
235 | } |
236 | } |
237 |
Built with git-ssb-web