asyncify tests

GitOrigin-RevId: 62f875bb121a599fab830a3244959596796cd6e1
This commit is contained in:
Tim Alby 2022-01-10 10:29:32 +01:00 committed by Copybot
parent 67ce7c3c75
commit 795ed56034
8 changed files with 385 additions and 502 deletions

View file

@ -4,57 +4,47 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp')
describe('Deleting a message', function () {
before(function (done) {
describe('Deleting a message', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString()
ChatApp.ensureRunning(done)
await ChatApp.ensureRunning()
})
describe('in a thread', function () {
before(function (done) {
ChatClient.sendMessage(
describe('in a thread', async function () {
before(async function () {
const { response, body: message } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
'first message',
(error, response, message) => {
this.message = message
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
'deleted message',
(error, response, message1) => {
this.message = message1
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.deleteMessage(
this.project_id,
this.thread_id,
this.message.id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
)
}
'first message'
)
this.message = message
expect(response.statusCode).to.equal(201)
const { response: response2, body: message2 } =
await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
'deleted message'
)
this.message = message2
expect(response2.statusCode).to.equal(201)
const { response: response3 } = await ChatClient.deleteMessage(
this.project_id,
this.thread_id,
this.message.id
)
expect(response3.statusCode).to.equal(204)
})
it('should then remove the message from the threads', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].messages.length).to.equal(1)
done()
})
it('should then remove the message from the threads', async function () {
const { response, body: threads } = await ChatClient.getThreads(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].messages.length).to.equal(1)
})
})
})

View file

@ -4,45 +4,37 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp')
describe('Deleting a thread', function () {
before(function (done) {
describe('Deleting a thread', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString()
ChatApp.ensureRunning(done)
await ChatApp.ensureRunning()
})
describe('with a thread that is deleted', function () {
before(function (done) {
describe('with a thread that is deleted', async function () {
before(async function () {
this.thread_id = ObjectId().toString()
this.content = 'deleted thread message'
ChatClient.sendMessage(
const { response } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
this.content,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.deleteThread(
this.project_id,
this.thread_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
this.content
)
expect(response.statusCode).to.equal(201)
const { response: response2 } = await ChatClient.deleteThread(
this.project_id,
this.thread_id
)
expect(response2.statusCode).to.equal(204)
})
it('should then not list the thread for the project', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(Object.keys(threads).length).to.equal(0)
done()
})
it('should then not list the thread for the project', async function () {
const { response, body: threads } = await ChatClient.getThreads(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(Object.keys(threads).length).to.equal(0)
})
})
})

View file

@ -4,55 +4,48 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp')
describe('Editing a message', function () {
before(function (done) {
describe('Editing a message', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString()
ChatApp.ensureRunning(done)
await ChatApp.ensureRunning()
})
describe('in a thread', function () {
before(function (done) {
describe('in a thread', async function () {
before(async function () {
this.content = 'thread message'
this.new_content = 'updated thread message'
ChatClient.sendMessage(
const { response, body: message } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
this.content,
(error, response, message) => {
this.message = message
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
expect(this.message.id).to.exist
expect(this.message.content).to.equal(this.content)
ChatClient.editMessage(
this.project_id,
this.thread_id,
this.message.id,
this.new_content,
(error, response, newMessage) => {
this.new_message = newMessage
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
this.content
)
})
it('should then list the updated message in the threads', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].messages.length).to.equal(1)
expect(threads[this.thread_id].messages[0].content).to.equal(
this.message = message
expect(response.statusCode).to.equal(201)
expect(this.message.id).to.exist
expect(this.message.content).to.equal(this.content)
const { response: response2, body: newMessage } =
await ChatClient.editMessage(
this.project_id,
this.thread_id,
this.message.id,
this.new_content
)
done()
})
this.new_message = newMessage
expect(response2.statusCode).to.equal(204)
})
it('should then list the updated message in the threads', async function () {
const { response, body: threads } = await ChatClient.getThreads(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].messages.length).to.equal(1)
expect(threads[this.thread_id].messages[0].content).to.equal(
this.new_content
)
})
})
})

View file

@ -1,124 +1,94 @@
const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai')
const async = require('async')
const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp')
describe('Getting messages', function () {
before(function (done) {
describe('Getting messages', async function () {
before(async function () {
this.user_id1 = ObjectId().toString()
this.user_id2 = ObjectId().toString()
this.content1 = 'foo bar'
this.content2 = 'hello world'
ChatApp.ensureRunning(done)
await ChatApp.ensureRunning()
})
describe('globally', function () {
before(function (done) {
describe('globally', async function () {
before(async function () {
this.project_id = ObjectId().toString()
async.series(
[
cb =>
ChatClient.sendGlobalMessage(
this.project_id,
this.user_id1,
this.content1,
cb
),
cb =>
ChatClient.sendGlobalMessage(
this.project_id,
this.user_id2,
this.content2,
cb
),
],
done
await ChatClient.sendGlobalMessage(
this.project_id,
this.user_id1,
this.content1
)
await ChatClient.sendGlobalMessage(
this.project_id,
this.user_id2,
this.content2
)
})
it('should contain the messages and populated users when getting the messages', function (done) {
ChatClient.getGlobalMessages(
this.project_id,
(error, response, messages) => {
if (error) return done(error)
expect(messages.length).to.equal(2)
messages.reverse()
expect(messages[0].content).to.equal(this.content1)
expect(messages[0].user_id).to.equal(this.user_id1)
expect(messages[1].content).to.equal(this.content2)
expect(messages[1].user_id).to.equal(this.user_id2)
done()
}
it('should contain the messages and populated users when getting the messages', async function () {
const { body: messages } = await ChatClient.getGlobalMessages(
this.project_id
)
expect(messages.length).to.equal(2)
messages.reverse()
expect(messages[0].content).to.equal(this.content1)
expect(messages[0].user_id).to.equal(this.user_id1)
expect(messages[1].content).to.equal(this.content2)
expect(messages[1].user_id).to.equal(this.user_id2)
})
})
describe('from all the threads', function () {
before(function (done) {
describe('from all the threads', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.thread_id1 = ObjectId().toString()
this.thread_id2 = ObjectId().toString()
async.series(
[
cb =>
ChatClient.sendMessage(
this.project_id,
this.thread_id1,
this.user_id1,
'one',
cb
),
cb =>
ChatClient.sendMessage(
this.project_id,
this.thread_id2,
this.user_id2,
'two',
cb
),
cb =>
ChatClient.sendMessage(
this.project_id,
this.thread_id1,
this.user_id1,
'three',
cb
),
cb =>
ChatClient.sendMessage(
this.project_id,
this.thread_id2,
this.user_id2,
'four',
cb
),
],
done
await ChatClient.sendMessage(
this.project_id,
this.thread_id1,
this.user_id1,
'one'
)
await ChatClient.sendMessage(
this.project_id,
this.thread_id2,
this.user_id2,
'two'
)
await ChatClient.sendMessage(
this.project_id,
this.thread_id1,
this.user_id1,
'three'
)
await ChatClient.sendMessage(
this.project_id,
this.thread_id2,
this.user_id2,
'four'
)
})
it('should contain a dictionary of threads with messages with populated users', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
if (error) return done(error)
expect(Object.keys(threads).length).to.equal(2)
const thread1 = threads[this.thread_id1]
expect(thread1.messages.length).to.equal(2)
const thread2 = threads[this.thread_id2]
expect(thread2.messages.length).to.equal(2)
it('should contain a dictionary of threads with messages with populated users', async function () {
const { body: threads } = await ChatClient.getThreads(this.project_id)
expect(Object.keys(threads).length).to.equal(2)
const thread1 = threads[this.thread_id1]
expect(thread1.messages.length).to.equal(2)
const thread2 = threads[this.thread_id2]
expect(thread2.messages.length).to.equal(2)
expect(thread1.messages[0].content).to.equal('one')
expect(thread1.messages[0].user_id).to.equal(this.user_id1)
expect(thread1.messages[1].content).to.equal('three')
expect(thread1.messages[1].user_id).to.equal(this.user_id1)
expect(thread1.messages[0].content).to.equal('one')
expect(thread1.messages[0].user_id).to.equal(this.user_id1)
expect(thread1.messages[1].content).to.equal('three')
expect(thread1.messages[1].user_id).to.equal(this.user_id1)
expect(thread2.messages[0].content).to.equal('two')
expect(thread2.messages[0].user_id).to.equal(this.user_id2)
expect(thread2.messages[1].content).to.equal('four')
expect(thread2.messages[1].user_id).to.equal(this.user_id2)
done()
})
expect(thread2.messages[0].content).to.equal('two')
expect(thread2.messages[0].user_id).to.equal(this.user_id2)
expect(thread2.messages[1].content).to.equal('four')
expect(thread2.messages[1].user_id).to.equal(this.user_id2)
})
})
})

View file

@ -4,122 +4,96 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp')
describe('Resolving a thread', function () {
before(function (done) {
describe('Resolving a thread', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString()
ChatApp.ensureRunning(done)
await ChatApp.ensureRunning()
})
describe('with a resolved thread', function () {
before(function (done) {
describe('with a resolved thread', async function () {
before(async function () {
this.thread_id = ObjectId().toString()
this.content = 'resolved message'
ChatClient.sendMessage(
const { response } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
this.content,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
this.content
)
expect(response.statusCode).to.equal(201)
const { response: response2 } = await ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id
)
expect(response2.statusCode).to.equal(204)
})
it('should then list the thread as resolved', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].resolved).to.equal(true)
expect(threads[this.thread_id].resolved_by_user_id).to.equal(
this.user_id
)
const resolvedAt = new Date(threads[this.thread_id].resolved_at)
expect(new Date() - resolvedAt).to.be.below(1000)
done()
})
it('should then list the thread as resolved', async function () {
const { response, body: threads } = await ChatClient.getThreads(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].resolved).to.equal(true)
expect(threads[this.thread_id].resolved_by_user_id).to.equal(this.user_id)
const resolvedAt = new Date(threads[this.thread_id].resolved_at)
expect(new Date() - resolvedAt).to.be.below(1000)
})
})
describe('when a thread is not resolved', function () {
before(function (done) {
describe('when a thread is not resolved', async function () {
before(async function () {
this.thread_id = ObjectId().toString()
this.content = 'open message'
ChatClient.sendMessage(
const { response } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
this.content,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
done()
}
this.content
)
expect(response.statusCode).to.equal(201)
})
it('should not list the thread as resolved', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].resolved).to.be.undefined
done()
})
it('should not list the thread as resolved', async function () {
const { response, body: threads } = await ChatClient.getThreads(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].resolved).to.be.undefined
})
})
describe('when a thread is resolved then reopened', function () {
before(function (done) {
describe('when a thread is resolved then reopened', async function () {
before(async function () {
this.thread_id = ObjectId().toString()
this.content = 'resolved message'
ChatClient.sendMessage(
const { response } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
this.content,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
ChatClient.reopenThread(
this.project_id,
this.thread_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
)
}
this.content
)
expect(response.statusCode).to.equal(201)
const { response: response2 } = await ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id
)
expect(response2.statusCode).to.equal(204)
const { response: response3 } = await ChatClient.reopenThread(
this.project_id,
this.thread_id
)
expect(response3.statusCode).to.equal(204)
})
it('should not list the thread as resolved', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].resolved).to.be.undefined
done()
})
it('should not list the thread as resolved', async function () {
const { response, body: threads } = await ChatClient.getThreads(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].resolved).to.be.undefined
})
})
})

View file

@ -4,182 +4,143 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp')
describe('Sending a message', function () {
before(function (done) {
ChatApp.ensureRunning(done)
describe('Sending a message', async function () {
before(async function () {
await ChatApp.ensureRunning()
})
describe('globally', function () {
before(function (done) {
describe('globally', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString()
this.content = 'global message'
ChatClient.sendGlobalMessage(
const { response, body } = await ChatClient.sendGlobalMessage(
this.project_id,
this.user_id,
this.content,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
expect(body.content).to.equal(this.content)
expect(body.user_id).to.equal(this.user_id)
expect(body.room_id).to.equal(this.project_id)
done()
}
this.content
)
expect(response.statusCode).to.equal(201)
expect(body.content).to.equal(this.content)
expect(body.user_id).to.equal(this.user_id)
expect(body.room_id).to.equal(this.project_id)
})
it('should then list the message in the project messages', function (done) {
ChatClient.getGlobalMessages(
this.project_id,
(error, response, messages) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(messages.length).to.equal(1)
expect(messages[0].content).to.equal(this.content)
done()
}
it('should then list the message in the project messages', async function () {
const { response, body: messages } = await ChatClient.getGlobalMessages(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(messages.length).to.equal(1)
expect(messages[0].content).to.equal(this.content)
})
})
describe('to a thread', function () {
before(function (done) {
describe('to a thread', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString()
this.content = 'thread message'
ChatClient.sendMessage(
const { response, body } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
this.content,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
expect(body.content).to.equal(this.content)
expect(body.user_id).to.equal(this.user_id)
expect(body.room_id).to.equal(this.project_id)
done()
}
this.content
)
expect(response.statusCode).to.equal(201)
expect(body.content).to.equal(this.content)
expect(body.user_id).to.equal(this.user_id)
expect(body.room_id).to.equal(this.project_id)
})
it('should then list the message in the threads', function (done) {
ChatClient.getThreads(this.project_id, (error, response, threads) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].messages.length).to.equal(1)
expect(threads[this.thread_id].messages[0].content).to.equal(
this.content
)
done()
})
it('should then list the message in the threads', async function () {
const { response, body: threads } = await ChatClient.getThreads(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].messages.length).to.equal(1)
expect(threads[this.thread_id].messages[0].content).to.equal(this.content)
})
it('should not appear in the global messages', function (done) {
ChatClient.getGlobalMessages(
this.project_id,
(error, response, messages) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(messages.length).to.equal(0)
done()
}
it('should not appear in the global messages', async function () {
const { response, body: messages } = await ChatClient.getGlobalMessages(
this.project_id
)
expect(response.statusCode).to.equal(200)
expect(messages.length).to.equal(0)
})
})
describe('failure cases', function () {
before(function () {
describe('failure cases', async function () {
before(async function () {
this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString()
})
describe('with a malformed user_id', function () {
it('should return a graceful error', function (done) {
ChatClient.sendMessage(
describe('with a malformed user_id', async function () {
it('should return a graceful error', async function () {
const { response, body } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
'malformed-user',
'content',
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid userId')
done()
}
'content'
)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid userId')
})
})
describe('with a malformed project_id', function () {
it('should return a graceful error', function (done) {
ChatClient.sendMessage(
describe('with a malformed project_id', async function () {
it('should return a graceful error', async function () {
const { response, body } = await ChatClient.sendMessage(
'malformed-project',
this.thread_id,
this.user_id,
'content',
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid projectId')
done()
}
'content'
)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid projectId')
})
})
describe('with a malformed thread_id', function () {
it('should return a graceful error', function (done) {
ChatClient.sendMessage(
describe('with a malformed thread_id', async function () {
it('should return a graceful error', async function () {
const { response, body } = await ChatClient.sendMessage(
this.project_id,
'malformed-thread-id',
this.user_id,
'content',
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid threadId')
done()
}
'content'
)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid threadId')
})
})
describe('with no content', function () {
it('should return a graceful error', function (done) {
ChatClient.sendMessage(
describe('with no content', async function () {
it('should return a graceful error', async function () {
const { response, body } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
null,
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('No content provided')
done()
}
null
)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('No content provided')
})
})
describe('with very long content', function () {
it('should return a graceful error', function (done) {
describe('with very long content', async function () {
it('should return a graceful error', async function () {
const content = '-'.repeat(10 * 1024 + 1)
ChatClient.sendMessage(
const { response, body } = await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
content,
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Content too long (> 10240 bytes)')
done()
}
content
)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Content too long (> 10240 bytes)')
})
})
})

View file

@ -1,28 +1,27 @@
const { waitForDb } = require('../../../../app/js/mongodb')
const app = require('../../../../app')
module.exports = {
running: false,
initing: false,
callbacks: [],
ensureRunning(callback) {
if (this.running) {
return callback()
} else if (this.initing) {
return this.callbacks.push(callback)
}
this.initing = true
this.callbacks.push(callback)
waitForDb().then(() => {
let serverPromise = null
function startServer(resolve, reject) {
waitForDb()
.then(() => {
app.listen(3010, 'localhost', error => {
if (error) {
throw error
}
this.running = true
for (callback of this.callbacks) {
callback()
return reject(error)
}
resolve()
})
})
},
.catch(reject)
}
async function ensureRunning() {
if (!serverPromise) {
serverPromise = new Promise(startServer)
}
return serverPromise
}
module.exports = {
ensureRunning,
}

View file

@ -2,101 +2,105 @@ const request = require('request').defaults({
baseUrl: 'http://localhost:3010',
})
module.exports = {
sendGlobalMessage(projectId, userId, content, callback) {
request.post(
{
url: `/project/${projectId}/messages`,
json: {
user_id: userId,
content,
},
},
callback
)
},
getGlobalMessages(projectId, callback) {
request.get(
{
url: `/project/${projectId}/messages`,
json: true,
},
callback
)
},
sendMessage(projectId, threadId, userId, content, callback) {
request.post(
{
url: `/project/${projectId}/thread/${threadId}/messages`,
json: {
user_id: userId,
content,
},
},
callback
)
},
getThreads(projectId, callback) {
request.get(
{
url: `/project/${projectId}/threads`,
json: true,
},
callback
)
},
resolveThread(projectId, threadId, userId, callback) {
request.post(
{
url: `/project/${projectId}/thread/${threadId}/resolve`,
json: {
user_id: userId,
},
},
callback
)
},
reopenThread(projectId, threadId, callback) {
request.post(
{
url: `/project/${projectId}/thread/${threadId}/reopen`,
},
callback
)
},
deleteThread(projectId, threadId, callback) {
request.del(
{
url: `/project/${projectId}/thread/${threadId}`,
},
callback
)
},
editMessage(projectId, threadId, messageId, content, callback) {
request.post(
{
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
json: {
content,
},
},
callback
)
},
deleteMessage(projectId, threadId, messageId, callback) {
request.del(
{
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
},
callback
)
},
async function asyncRequest(options) {
return new Promise((resolve, reject) => {
request(options, (err, response, body) => {
if (err) {
reject(err)
} else {
resolve({ response, body })
}
})
})
}
async function sendGlobalMessage(projectId, userId, content) {
return asyncRequest({
method: 'post',
url: `/project/${projectId}/messages`,
json: {
user_id: userId,
content,
},
})
}
async function getGlobalMessages(projectId) {
return asyncRequest({
method: 'get',
url: `/project/${projectId}/messages`,
json: true,
})
}
async function sendMessage(projectId, threadId, userId, content) {
return asyncRequest({
method: 'post',
url: `/project/${projectId}/thread/${threadId}/messages`,
json: {
user_id: userId,
content,
},
})
}
async function getThreads(projectId) {
return asyncRequest({
method: 'get',
url: `/project/${projectId}/threads`,
json: true,
})
}
async function resolveThread(projectId, threadId, userId) {
return asyncRequest({
method: 'post',
url: `/project/${projectId}/thread/${threadId}/resolve`,
json: {
user_id: userId,
},
})
}
async function reopenThread(projectId, threadId) {
return asyncRequest({
method: 'post',
url: `/project/${projectId}/thread/${threadId}/reopen`,
})
}
async function deleteThread(projectId, threadId) {
return asyncRequest({
method: 'delete',
url: `/project/${projectId}/thread/${threadId}`,
})
}
async function editMessage(projectId, threadId, messageId, content) {
return asyncRequest({
method: 'post',
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
json: {
content,
},
})
}
async function deleteMessage(projectId, threadId, messageId) {
return asyncRequest({
method: 'delete',
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
})
}
module.exports = {
sendGlobalMessage,
getGlobalMessages,
sendMessage,
getThreads,
resolveThread,
reopenThread,
deleteThread,
editMessage,
deleteMessage,
}