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