git ssb

0+

Kira / %V53yIAO6ZNGv1Lx9tCP…



forked from cel / ssb-issues

Tree: f80a890a7794675d007c3965125971a1405b295b

Files: f80a890a7794675d007c3965125971a1405b295b / index.js

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

Built with git-ssb-web