Commit 67f4dc0ca60e582b79d0e500a198e3463fef7cf5
Allow opening/closing issues by mention
Charles Lehner committed on 3/27/2016, 1:38:57 AMParent: f80a890a7794675d007c3965125971a1405b295b
Files changed
README.md | changed |
index.js | changed |
lib/schemas.js | changed |
package.json | changed |
test.js | changed |
README.md | ||
---|---|---|
@@ -162,8 +162,22 @@ | ||
162 | 162 | Reopen an issue. |
163 | 163 | |
164 | 164 | - `id` (MsgRef): id of an issue to mark as open |
165 | 165 | |
166 | +#### `issueSchemas.closes(msg, id)` | |
167 | + | |
168 | +Mutate a message to make it close an issue | |
169 | + | |
170 | +- `msg` (Msg): message object to update | |
171 | +- `id` (MsgRef): id of an issue to mark as closed | |
172 | + | |
173 | +#### `issueSchemas.reopens(msg, id)` | |
174 | + | |
175 | +Mutate a message to make it reopen an issue | |
176 | + | |
177 | +- `msg` (Msg): message object to update | |
178 | +- `id` (MsgRef): id of an issue to mark as open | |
179 | + | |
166 | 180 | ## License |
167 | 181 | |
168 | 182 | Copyright (c) 2016 Charles Lehner |
169 | 183 |
index.js | ||
---|---|---|
@@ -80,27 +80,56 @@ | ||
80 | 80 | function onOldMsg(msg) { |
81 | 81 | if (!isUpdateValid(issue, msg)) |
82 | 82 | return |
83 | 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 | |
84 | + | |
85 | + // handle updates to issue | |
86 | + if (msg.key == id || c.issue == id || c.link == id) { | |
87 | + if (c.open != null && issue.open == null) | |
88 | + issue.open = c.open | |
89 | + if (c.title != null && issue.title == null) | |
90 | + issue.title = c.title | |
91 | + if (msg.value.timestamp > issue.updated_at) | |
92 | + issue.updated_at = msg.value.timestamp | |
93 | + } | |
94 | + | |
95 | + // handle updates via mention | |
96 | + if (c.issues) { | |
97 | + for (var i = 0; i < c.issues.length; i++) | |
98 | + onOldMsg({value: { | |
99 | + timestamp: msg.value.timestamp, | |
100 | + author: msg.value.author, | |
101 | + content: c.issues[i] | |
102 | + }}) | |
103 | + } | |
104 | + | |
90 | 105 | checkReady() |
91 | 106 | } |
92 | 107 | |
93 | 108 | function onNewMsg(msg) { |
94 | 109 | if (!isUpdateValid(issue, msg)) |
95 | 110 | return |
96 | 111 | 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 | |
112 | + | |
113 | + // handle updates to issue | |
114 | + if (msg.key == id || c.issue == id || c.link == id) { | |
115 | + if (c.open != null) | |
116 | + issue.open = c.open | |
117 | + if (c.title != null) | |
118 | + issue.title = c.title | |
119 | + if (msg.value.timestamp > issue.updated_at) | |
120 | + issue.updated_at = msg.value.timestamp | |
121 | + } | |
122 | + | |
123 | + // handle updates via mention | |
124 | + if (c.issues) { | |
125 | + for (var i = 0; i < c.issues.length; i++) | |
126 | + onNewMsg({value: { | |
127 | + timestamp: msg.value.timestamp, | |
128 | + author: msg.value.author, | |
129 | + content: c.issues[i] | |
130 | + }}) | |
131 | + } | |
103 | 132 | } |
104 | 133 | |
105 | 134 | function checkReady() { |
106 | 135 | // call back once all the issue properties are set |
lib/schemas.js | ||
---|---|---|
@@ -40,4 +40,22 @@ | ||
40 | 40 | |
41 | 41 | exports.reopen = function (id) { |
42 | 42 | return exports.edit(id, {open: true}) |
43 | 43 | } |
44 | + | |
45 | +function editMsg(msg, id, open) { | |
46 | + if (!ssbRef.isMsg(id)) | |
47 | + throw new Error('invalid issue id') | |
48 | + ;(msg.issues || (msg.issues = [])).push({ | |
49 | + link: id, | |
50 | + open: open | |
51 | + }) | |
52 | + return msg | |
53 | +} | |
54 | + | |
55 | +exports.reopens = function (msg, id) { | |
56 | + return editMsg(msg, id, true) | |
57 | +} | |
58 | + | |
59 | +exports.closes = function (msg, id) { | |
60 | + return editMsg(msg, id, false) | |
61 | +} |
package.json | ||
---|---|---|
@@ -21,7 +21,8 @@ | ||
21 | 21 | }, |
22 | 22 | "devDependencies": { |
23 | 23 | "scuttlebot": "^7.6.6", |
24 | 24 | "ssb-keys": "^5.0.0", |
25 | + "ssb-msg-schemas": "^6.2.1", | |
25 | 26 | "tape": "^4.5.1" |
26 | 27 | } |
27 | 28 | } |
test.js | ||
---|---|---|
@@ -1,8 +1,9 @@ | ||
1 | 1 | var test = require('tape') |
2 | 2 | var Issues = require('.') |
3 | 3 | var ssbKeys = require('ssb-keys') |
4 | 4 | var pull = require('pull-stream') |
5 | +var schemas = require('ssb-msg-schemas') | |
5 | 6 | |
6 | 7 | function awaitMsg(sbot, msg, cb) { |
7 | 8 | sbot.createUserStream({ |
8 | 9 | id: msg.value.author, |
@@ -38,8 +39,9 @@ | ||
38 | 39 | }) |
39 | 40 | }) |
40 | 41 | |
41 | 42 | var issue1 |
43 | +var updated_at | |
42 | 44 | |
43 | 45 | test('create an issue', function (t) { |
44 | 46 | var title = 'Test Title' |
45 | 47 | issues.new({ |
@@ -92,8 +94,23 @@ | ||
92 | 94 | t.ok(msg, 'msg') |
93 | 95 | awaitMsg(sbot, msg, function (err) { |
94 | 96 | t.error(err, 'await') |
95 | 97 | t.equals(issue1.open, true, 'open') |
98 | + updated_at = issue1.updated_at | |
96 | 99 | t.end() |
97 | 100 | }) |
98 | 101 | }) |
99 | 102 | }) |
103 | + | |
104 | +test('close the issue via a post', function (t) { | |
105 | + var msg = schemas.post('I like closing issues') | |
106 | + Issues.schemas.closes(msg, issue1.id) | |
107 | + sbot.publish(msg, function (err, msg) { | |
108 | + t.error(err, 'publish') | |
109 | + awaitMsg(sbot, msg, function (err) { | |
110 | + t.error(err, 'await') | |
111 | + t.equals(issue1.open, false, 'closed') | |
112 | + t.ok(issue1.updated_at > updated_at, 'updated_at is updated') | |
113 | + t.end() | |
114 | + }) | |
115 | + }) | |
116 | +}) |
Built with git-ssb-web