git ssb

0+

cel / ssb-issues



Commit 67f4dc0ca60e582b79d0e500a198e3463fef7cf5

Allow opening/closing issues by mention

Charles Lehner committed on 3/27/2016, 1:38:57 AM
Parent: f80a890a7794675d007c3965125971a1405b295b

Files changed

README.mdchanged
index.jschanged
lib/schemas.jschanged
package.jsonchanged
test.jschanged
README.mdView
@@ -162,8 +162,22 @@
162162 Reopen an issue.
163163
164164 - `id` (MsgRef): id of an issue to mark as open
165165
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+
166180 ## License
167181
168182 Copyright (c) 2016 Charles Lehner
169183
index.jsView
@@ -80,27 +80,56 @@
8080 function onOldMsg(msg) {
8181 if (!isUpdateValid(issue, msg))
8282 return
8383 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+
90105 checkReady()
91106 }
92107
93108 function onNewMsg(msg) {
94109 if (!isUpdateValid(issue, msg))
95110 return
96111 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+ }
103132 }
104133
105134 function checkReady() {
106135 // call back once all the issue properties are set
lib/schemas.jsView
@@ -40,4 +40,22 @@
4040
4141 exports.reopen = function (id) {
4242 return exports.edit(id, {open: true})
4343 }
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.jsonView
@@ -21,7 +21,8 @@
2121 },
2222 "devDependencies": {
2323 "scuttlebot": "^7.6.6",
2424 "ssb-keys": "^5.0.0",
25+ "ssb-msg-schemas": "^6.2.1",
2526 "tape": "^4.5.1"
2627 }
2728 }
test.jsView
@@ -1,8 +1,9 @@
11 var test = require('tape')
22 var Issues = require('.')
33 var ssbKeys = require('ssb-keys')
44 var pull = require('pull-stream')
5+var schemas = require('ssb-msg-schemas')
56
67 function awaitMsg(sbot, msg, cb) {
78 sbot.createUserStream({
89 id: msg.value.author,
@@ -38,8 +39,9 @@
3839 })
3940 })
4041
4142 var issue1
43+var updated_at
4244
4345 test('create an issue', function (t) {
4446 var title = 'Test Title'
4547 issues.new({
@@ -92,8 +94,23 @@
9294 t.ok(msg, 'msg')
9395 awaitMsg(sbot, msg, function (err) {
9496 t.error(err, 'await')
9597 t.equals(issue1.open, true, 'open')
98+ updated_at = issue1.updated_at
9699 t.end()
97100 })
98101 })
99102 })
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