Files: d5694339a3115c17b822de430711589b7e5c52e4 / index.js
5678 bytesRaw
1 | var pull = require('pull-stream') |
2 | var paramap = require('pull-paramap') |
3 | var asyncMemo = require('asyncmemo') |
4 | var issueSchemas = require('./lib/schemas') |
5 | |
6 | function Cache(fn, ssb) { |
7 | return asyncMemo(fn) |
8 | return function (key, cb) { ac.get(key, cb) } |
9 | } |
10 | |
11 | function isUpdateValid(issue, msg) { |
12 | return msg.value.author == issue.author |
13 | && msg.value.author == issue.projectAuthor |
14 | } |
15 | |
16 | exports.name = 'issues' |
17 | |
18 | exports.manifest = { |
19 | get: 'async', |
20 | createFeedStream: 'source', |
21 | new: 'async', |
22 | edit: 'async', |
23 | close: 'async', |
24 | reopen: 'async', |
25 | isStatusChanged: 'sync' |
26 | } |
27 | |
28 | exports.schemas = issueSchemas |
29 | |
30 | function isStatusChanged(msg, issue) { |
31 | var c = msg.value.content |
32 | if (msg.key == issue.id || c.issue == issue.id || c.link == issue.id) |
33 | if (c.open != null) |
34 | return c.open |
35 | if (c.issues) { |
36 | var changed |
37 | for (var i = 0; i < c.issues.length; i++) { |
38 | changed = isStatusChanged({value: { |
39 | timestamp: msg.value.timestamp, |
40 | author: msg.value.author, |
41 | content: c.issues[i] |
42 | }}, issue) |
43 | if (changed != null) return changed |
44 | } |
45 | } |
46 | } |
47 | |
48 | exports.init = function (ssb) { |
49 | |
50 | var ssbGet = asyncMemo(ssb.get) |
51 | |
52 | var getIssue = asyncMemo(function (id, cb) { |
53 | var issue = {} |
54 | var issueMsg |
55 | |
56 | if (id && id.value && id.key) { |
57 | var msg = id |
58 | id = id.key |
59 | gotIssueMsg(null, msg) |
60 | } else { |
61 | ssbGet(id, gotIssueMsg) |
62 | } |
63 | |
64 | function gotIssueMsg(err, msg) { |
65 | if (err) return cb(err) |
66 | issueMsg = msg |
67 | issue.id = msg.key |
68 | issue.author = msg.value.author |
69 | var c = msg.value.content |
70 | issue.project = c.project |
71 | issue.text = c.text |
72 | issue.created_at = issue.updated_at = msg.value.timestamp |
73 | if (c.project) |
74 | ssbGet(c.project, gotProjectMsg) |
75 | else |
76 | getLinks() |
77 | } |
78 | |
79 | function gotProjectMsg(err, msg) { |
80 | if (err) return cb(err) |
81 | issue.projectAuthor = msg.author |
82 | getLinks() |
83 | } |
84 | |
85 | function getLinks() { |
86 | var now = Date.now() |
87 | // compute the result from the past data |
88 | pull( |
89 | ssb.links({dest: id, lt: now, reverse: true, values: true}), |
90 | pull.drain(onOldMsg, onOldEnd) |
91 | ) |
92 | // keep the results up-to-date in the future |
93 | pull( |
94 | ssb.links({dest: id, gte: now, values: true, live: true}), |
95 | pull.drain(onNewMsg, onNewEnd) |
96 | ) |
97 | } |
98 | |
99 | function onOldMsg(msg) { |
100 | if (!isUpdateValid(issue, msg)) |
101 | return |
102 | var c = msg.value.content |
103 | |
104 | // handle updates to issue |
105 | if (msg.key == id || c.issue == id || c.link == id) { |
106 | if (c.open != null && issue.open == null) |
107 | issue.open = c.open |
108 | if (c.title != null && issue.title == null) |
109 | issue.title = c.title |
110 | if (msg.value.timestamp > issue.updated_at) |
111 | issue.updated_at = msg.value.timestamp |
112 | } |
113 | |
114 | // handle updates via mention |
115 | if (c.issues) { |
116 | for (var i = 0; i < c.issues.length; i++) |
117 | onOldMsg({value: { |
118 | timestamp: msg.value.timestamp, |
119 | author: msg.value.author, |
120 | content: c.issues[i] |
121 | }}) |
122 | } |
123 | |
124 | checkReady() |
125 | } |
126 | |
127 | function onNewMsg(msg) { |
128 | if (!isUpdateValid(issue, msg)) |
129 | return |
130 | var c = msg.value.content |
131 | |
132 | // handle updates to issue |
133 | if (msg.key == id || c.issue == id || c.link == id) { |
134 | if (c.open != null) |
135 | issue.open = c.open |
136 | if (c.title != null) |
137 | issue.title = c.title |
138 | if (msg.value.timestamp > issue.updated_at) |
139 | issue.updated_at = msg.value.timestamp |
140 | } |
141 | |
142 | // handle updates via mention |
143 | if (c.issues) { |
144 | for (var i = 0; i < c.issues.length; i++) |
145 | onNewMsg({value: { |
146 | timestamp: msg.value.timestamp, |
147 | author: msg.value.author, |
148 | content: c.issues[i] |
149 | }}) |
150 | } |
151 | } |
152 | |
153 | function checkReady() { |
154 | // call back once all the issue properties are set |
155 | if (issue.open != null && issue.title != null) { |
156 | var _cb = cb |
157 | delete cb |
158 | _cb(null, issue) |
159 | } |
160 | } |
161 | |
162 | function onOldEnd(err) { |
163 | if (err) { |
164 | if (cb) cb(err) |
165 | else console.error(err) |
166 | return |
167 | } |
168 | // process the root message last |
169 | onOldMsg(issueMsg) |
170 | // if callback hasn't been called yet, the issue is missing a field |
171 | if (cb) { |
172 | if (issue.open == null) |
173 | issue.open = true |
174 | if (issue.title == null) |
175 | issue.title = issue.id |
176 | checkReady() |
177 | } |
178 | } |
179 | |
180 | function onNewEnd(err) { |
181 | if (err) { |
182 | if (cb) cb(err) |
183 | else console.error(err) |
184 | } |
185 | } |
186 | }) |
187 | |
188 | function createFeedStream(opts) { |
189 | opts.type = 'issue' |
190 | delete opts.limit |
191 | return pull( |
192 | // TODO: use links2 for this |
193 | ssb.messagesByType(opts), |
194 | pull.filter(function (msg) { |
195 | return (!opts.project || opts.project == msg.value.content.project) |
196 | && (!opts.author || opts.author == msg.value.author) |
197 | }), |
198 | paramap(getIssue, 8) |
199 | ) |
200 | } |
201 | |
202 | function editIssue(id, opts, cb) { |
203 | ssb.publish(issueSchemas.edit(id, opts), cb) |
204 | } |
205 | |
206 | function closeIssue(id, cb) { |
207 | ssb.publish(issueSchemas.close(id), cb) |
208 | } |
209 | |
210 | function reopenIssue(id, cb) { |
211 | ssb.publish(issueSchemas.reopen(id), cb) |
212 | } |
213 | |
214 | function newIssue(opts, cb) { |
215 | var msg = issueSchemas.new(opts.project, opts.title, opts.text) |
216 | ssb.publish(msg, function (err, msg) { |
217 | if (err) return cb(err) |
218 | getIssue(msg, cb) |
219 | }) |
220 | } |
221 | |
222 | return { |
223 | get: getIssue, |
224 | createFeedStream: createFeedStream, |
225 | new: newIssue, |
226 | edit: editIssue, |
227 | close: closeIssue, |
228 | reopen: reopenIssue, |
229 | isStatusChanged: isStatusChanged |
230 | } |
231 | } |
232 |
Built with git-ssb-web