git ssb

30+

cel / git-ssb-web



Tree: 1d9e51d877533a7ee7791bb1d85c793c746ac193

Files: 1d9e51d877533a7ee7791bb1d85c793c746ac193 / lib / repos / issues.js

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

Built with git-ssb-web