mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #18375 from overleaf/em-promisify-chat-api-handler
Promisify ChatApiHandler GitOrigin-RevId: 83cedb14b5e2b187fb2cb02fcbf888ada5a599b1
This commit is contained in:
parent
28c18e2486
commit
dfd1652c35
2 changed files with 162 additions and 251 deletions
|
@ -1,190 +1,120 @@
|
||||||
/* eslint-disable
|
// @ts-check
|
||||||
n/handle-callback-err,
|
|
||||||
max-len,
|
const { fetchJson, fetchNothing } = require('@overleaf/fetch-utils')
|
||||||
*/
|
|
||||||
// TODO: This file was created by bulk-decaffeinate.
|
|
||||||
// Fix any style issues and re-enable lint.
|
|
||||||
/*
|
|
||||||
* decaffeinate suggestions:
|
|
||||||
* DS102: Remove unnecessary code created because of implicit returns
|
|
||||||
* DS207: Consider shorter variations of null checks
|
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
||||||
*/
|
|
||||||
let ChatApiHandler
|
|
||||||
const OError = require('@overleaf/o-error')
|
|
||||||
const request = require('request')
|
|
||||||
const settings = require('@overleaf/settings')
|
const settings = require('@overleaf/settings')
|
||||||
const { promisify } = require('util')
|
const { callbackify } = require('util')
|
||||||
|
|
||||||
function getThreads(projectId, callback) {
|
async function getThreads(projectId) {
|
||||||
if (callback == null) {
|
return await fetchJson(chatApiUrl(`/project/${projectId}/threads`))
|
||||||
callback = function () {}
|
}
|
||||||
}
|
|
||||||
return ChatApiHandler._apiRequest(
|
async function destroyProject(projectId) {
|
||||||
|
await fetchNothing(chatApiUrl(`/project/${projectId}`), { method: 'DELETE' })
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendGlobalMessage(projectId, userId, content) {
|
||||||
|
const message = await fetchJson(
|
||||||
|
chatApiUrl(`/project/${projectId}/messages`),
|
||||||
{
|
{
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/threads`,
|
method: 'POST',
|
||||||
method: 'GET',
|
json: { user_id: userId, content },
|
||||||
json: true,
|
}
|
||||||
},
|
)
|
||||||
callback
|
return message
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getGlobalMessages(projectId, limit, before) {
|
||||||
|
const url = chatApiUrl(`/project/${projectId}/messages`)
|
||||||
|
if (limit != null) {
|
||||||
|
url.searchParams.set('limit', limit)
|
||||||
|
}
|
||||||
|
if (before != null) {
|
||||||
|
url.searchParams.set('before', before)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await fetchJson(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendComment(projectId, threadId, userId, content) {
|
||||||
|
const comment = await fetchJson(
|
||||||
|
chatApiUrl(`/project/${projectId}/thread/${threadId}/messages`),
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
json: { user_id: userId, content },
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return comment
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resolveThread(projectId, threadId, userId) {
|
||||||
|
await fetchNothing(
|
||||||
|
chatApiUrl(`/project/${projectId}/thread/${threadId}/resolve`),
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
json: { user_id: userId },
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function destroyProject(projectId, callback) {
|
async function reopenThread(projectId, threadId) {
|
||||||
if (callback == null) {
|
await fetchNothing(
|
||||||
callback = function () {}
|
chatApiUrl(`/project/${projectId}/thread/${threadId}/reopen`),
|
||||||
}
|
{ method: 'POST' }
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}`,
|
|
||||||
method: 'DELETE',
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ChatApiHandler = {
|
async function deleteThread(projectId, threadId) {
|
||||||
_apiRequest(opts, callback) {
|
await fetchNothing(chatApiUrl(`/project/${projectId}/thread/${threadId}`), {
|
||||||
if (callback == null) {
|
method: 'DELETE',
|
||||||
callback = function () {}
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function editMessage(projectId, threadId, messageId, userId, content) {
|
||||||
|
await fetchNothing(
|
||||||
|
chatApiUrl(
|
||||||
|
`/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`
|
||||||
|
),
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
json: { content, userId },
|
||||||
}
|
}
|
||||||
return request(opts, function (error, response, data) {
|
)
|
||||||
if (error != null) {
|
}
|
||||||
return callback(error)
|
|
||||||
}
|
|
||||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
||||||
return callback(null, data)
|
|
||||||
} else {
|
|
||||||
error = new OError(
|
|
||||||
`chat api returned non-success code: ${response.statusCode}`,
|
|
||||||
opts
|
|
||||||
)
|
|
||||||
error.statusCode = response.statusCode
|
|
||||||
return callback(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
sendGlobalMessage(projectId, userId, content, callback) {
|
async function deleteMessage(projectId, threadId, messageId) {
|
||||||
return ChatApiHandler._apiRequest(
|
await fetchNothing(
|
||||||
{
|
chatApiUrl(
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/messages`,
|
`/project/${projectId}/thread/${threadId}/messages/${messageId}`
|
||||||
method: 'POST',
|
),
|
||||||
json: { user_id: userId, content },
|
{ method: 'DELETE' }
|
||||||
},
|
)
|
||||||
callback
|
}
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
getGlobalMessages(projectId, limit, before, callback) {
|
function chatApiUrl(path) {
|
||||||
const qs = {}
|
return new URL(path, settings.apis.chat.internal_url)
|
||||||
if (limit != null) {
|
}
|
||||||
qs.limit = limit
|
|
||||||
}
|
|
||||||
if (before != null) {
|
|
||||||
qs.before = before
|
|
||||||
}
|
|
||||||
|
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/messages`,
|
|
||||||
method: 'GET',
|
|
||||||
qs,
|
|
||||||
json: true,
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
sendComment(projectId, threadId, userId, content, callback) {
|
|
||||||
if (callback == null) {
|
|
||||||
callback = function () {}
|
|
||||||
}
|
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/messages`,
|
|
||||||
method: 'POST',
|
|
||||||
json: { user_id: userId, content },
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
resolveThread(projectId, threadId, userId, callback) {
|
|
||||||
if (callback == null) {
|
|
||||||
callback = function () {}
|
|
||||||
}
|
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/resolve`,
|
|
||||||
method: 'POST',
|
|
||||||
json: { user_id: userId },
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
reopenThread(projectId, threadId, callback) {
|
|
||||||
if (callback == null) {
|
|
||||||
callback = function () {}
|
|
||||||
}
|
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/reopen`,
|
|
||||||
method: 'POST',
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
deleteThread(projectId, threadId, callback) {
|
|
||||||
if (callback == null) {
|
|
||||||
callback = function () {}
|
|
||||||
}
|
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}`,
|
|
||||||
method: 'DELETE',
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
editMessage(projectId, threadId, messageId, userId, content, callback) {
|
|
||||||
if (callback == null) {
|
|
||||||
callback = function () {}
|
|
||||||
}
|
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
|
|
||||||
method: 'POST',
|
|
||||||
json: {
|
|
||||||
content,
|
|
||||||
userId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
deleteMessage(projectId, threadId, messageId, callback) {
|
|
||||||
if (callback == null) {
|
|
||||||
callback = function () {}
|
|
||||||
}
|
|
||||||
return ChatApiHandler._apiRequest(
|
|
||||||
{
|
|
||||||
url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/messages/${messageId}`,
|
|
||||||
method: 'DELETE',
|
|
||||||
},
|
|
||||||
callback
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
getThreads,
|
|
||||||
destroyProject,
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getThreads: callbackify(getThreads),
|
||||||
|
destroyProject: callbackify(destroyProject),
|
||||||
|
sendGlobalMessage: callbackify(sendGlobalMessage),
|
||||||
|
getGlobalMessages: callbackify(getGlobalMessages),
|
||||||
|
sendComment: callbackify(sendComment),
|
||||||
|
resolveThread: callbackify(resolveThread),
|
||||||
|
reopenThread: callbackify(reopenThread),
|
||||||
|
deleteThread: callbackify(deleteThread),
|
||||||
|
editMessage: callbackify(editMessage),
|
||||||
|
deleteMessage: callbackify(deleteMessage),
|
||||||
promises: {
|
promises: {
|
||||||
getThreads: promisify(getThreads),
|
getThreads,
|
||||||
destroyProject: promisify(destroyProject),
|
destroyProject,
|
||||||
|
sendGlobalMessage,
|
||||||
|
getGlobalMessages,
|
||||||
|
sendComment,
|
||||||
|
resolveThread,
|
||||||
|
reopenThread,
|
||||||
|
deleteThread,
|
||||||
|
editMessage,
|
||||||
|
deleteMessage,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,93 +1,83 @@
|
||||||
/* eslint-disable
|
|
||||||
max-len,
|
|
||||||
no-return-assign,
|
|
||||||
no-unused-vars,
|
|
||||||
*/
|
|
||||||
// TODO: This file was created by bulk-decaffeinate.
|
|
||||||
// Fix any style issues and re-enable lint.
|
|
||||||
/*
|
|
||||||
* decaffeinate suggestions:
|
|
||||||
* DS102: Remove unnecessary code created because of implicit returns
|
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
||||||
*/
|
|
||||||
const SandboxedModule = require('sandboxed-module')
|
const SandboxedModule = require('sandboxed-module')
|
||||||
const assert = require('assert')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
const modulePath = path.join(
|
const { expect } = require('chai')
|
||||||
|
const { RequestFailedError } = require('@overleaf/fetch-utils')
|
||||||
|
|
||||||
|
const MODULE_PATH = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
'../../../../app/src/Features/Chat/ChatApiHandler'
|
'../../../../app/src/Features/Chat/ChatApiHandler'
|
||||||
)
|
)
|
||||||
const { expect } = require('chai')
|
|
||||||
|
|
||||||
describe('ChatApiHandler', function () {
|
describe('ChatApiHandler', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
this.settings = {
|
this.settings = {
|
||||||
apis: {
|
apis: {
|
||||||
chat: {
|
chat: {
|
||||||
internal_url: 'chat.overleaf.env',
|
internal_url: 'http://chat.overleaf.env',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
this.request = sinon.stub()
|
this.FetchUtils = {
|
||||||
this.ChatApiHandler = SandboxedModule.require(modulePath, {
|
fetchJson: sinon.stub(),
|
||||||
|
fetchNothing: sinon.stub().resolves(),
|
||||||
|
}
|
||||||
|
this.ChatApiHandler = SandboxedModule.require(MODULE_PATH, {
|
||||||
requires: {
|
requires: {
|
||||||
'@overleaf/settings': this.settings,
|
'@overleaf/settings': this.settings,
|
||||||
request: this.request,
|
'@overleaf/fetch-utils': this.FetchUtils,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
this.project_id = '3213213kl12j'
|
this.project_id = '3213213kl12j'
|
||||||
this.user_id = '2k3jlkjs9'
|
this.user_id = '2k3jlkjs9'
|
||||||
this.content = 'my message here'
|
this.content = 'my message here'
|
||||||
return (this.callback = sinon.stub())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('sendGlobalMessage', function () {
|
describe('sendGlobalMessage', function () {
|
||||||
describe('successfully', function () {
|
describe('successfully', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.message = { mock: 'message' }
|
this.message = { mock: 'message' }
|
||||||
this.request.callsArgWith(1, null, { statusCode: 200 }, this.message)
|
this.FetchUtils.fetchJson.resolves(this.message)
|
||||||
return this.ChatApiHandler.sendGlobalMessage(
|
this.result = await this.ChatApiHandler.promises.sendGlobalMessage(
|
||||||
this.project_id,
|
this.project_id,
|
||||||
this.user_id,
|
this.user_id,
|
||||||
this.content,
|
this.content
|
||||||
this.callback
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should post the data to the chat api', function () {
|
it('should post the data to the chat api', function () {
|
||||||
return this.request
|
this.FetchUtils.fetchJson.should.have.been.calledWith(
|
||||||
.calledWith({
|
sinon.match(
|
||||||
url: `${this.settings.apis.chat.internal_url}/project/${this.project_id}/messages`,
|
url =>
|
||||||
|
url.toString() ===
|
||||||
|
`${this.settings.apis.chat.internal_url}/project/${this.project_id}/messages`
|
||||||
|
),
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
json: {
|
json: {
|
||||||
content: this.content,
|
content: this.content,
|
||||||
user_id: this.user_id,
|
user_id: this.user_id,
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
.should.equal(true)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return the message from the post', function () {
|
it('should return the message from the post', function () {
|
||||||
return this.callback.calledWith(null, this.message).should.equal(true)
|
expect(this.result).to.deep.equal(this.message)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('with a non-success status code', function () {
|
describe('with a non-success status code', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.request.callsArgWith(1, null, { statusCode: 500 })
|
this.error = new RequestFailedError('some-url', {}, { status: 500 })
|
||||||
return this.ChatApiHandler.sendGlobalMessage(
|
this.FetchUtils.fetchJson.rejects(this.error)
|
||||||
this.project_id,
|
await expect(
|
||||||
this.user_id,
|
this.ChatApiHandler.promises.sendGlobalMessage(
|
||||||
this.content,
|
this.project_id,
|
||||||
this.callback
|
this.user_id,
|
||||||
)
|
this.content
|
||||||
})
|
)
|
||||||
|
).to.be.rejectedWith(this.error)
|
||||||
it('should return an error', function () {
|
|
||||||
expect(this.callback).to.have.been.calledWith(
|
|
||||||
sinon.match.instanceOf(Error).and(sinon.match.has('statusCode', 500))
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -96,54 +86,45 @@ describe('ChatApiHandler', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
this.messages = [{ mock: 'message' }]
|
this.messages = [{ mock: 'message' }]
|
||||||
this.limit = 30
|
this.limit = 30
|
||||||
return (this.before = '1234')
|
this.before = '1234'
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('successfully', function () {
|
describe('successfully', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.request.callsArgWith(1, null, { statusCode: 200 }, this.messages)
|
this.FetchUtils.fetchJson.resolves(this.messages)
|
||||||
return this.ChatApiHandler.getGlobalMessages(
|
this.result = await this.ChatApiHandler.promises.getGlobalMessages(
|
||||||
this.project_id,
|
this.project_id,
|
||||||
this.limit,
|
this.limit,
|
||||||
this.before,
|
this.before
|
||||||
this.callback
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should make get request for room to chat api', function () {
|
it('should make get request for room to chat api', function () {
|
||||||
return this.request
|
this.FetchUtils.fetchJson.should.have.been.calledWith(
|
||||||
.calledWith({
|
sinon.match(
|
||||||
method: 'GET',
|
url =>
|
||||||
url: `${this.settings.apis.chat.internal_url}/project/${this.project_id}/messages`,
|
url.toString() ===
|
||||||
qs: {
|
`${this.settings.apis.chat.internal_url}/project/${this.project_id}/messages?limit=${this.limit}&before=${this.before}`
|
||||||
limit: this.limit,
|
)
|
||||||
before: this.before,
|
)
|
||||||
},
|
|
||||||
json: true,
|
|
||||||
})
|
|
||||||
.should.equal(true)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return the messages from the request', function () {
|
it('should return the messages from the request', function () {
|
||||||
return this.callback.calledWith(null, this.messages).should.equal(true)
|
expect(this.result).to.deep.equal(this.messages)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('with failure error code', function () {
|
describe('with failure error code', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.request.callsArgWith(1, null, { statusCode: 500 }, null)
|
this.error = new RequestFailedError('some-url', {}, { status: 500 })
|
||||||
return this.ChatApiHandler.getGlobalMessages(
|
this.FetchUtils.fetchJson.rejects(this.error)
|
||||||
this.project_id,
|
await expect(
|
||||||
this.limit,
|
this.ChatApiHandler.getGlobalMessages(
|
||||||
this.before,
|
this.project_id,
|
||||||
this.callback
|
this.limit,
|
||||||
)
|
this.before
|
||||||
})
|
)
|
||||||
|
).to.be.rejectedWith(this.error)
|
||||||
it('should return an error', function () {
|
|
||||||
expect(this.callback).to.have.been.calledWith(
|
|
||||||
sinon.match.instanceOf(Error).and(sinon.match.has('statusCode', 500))
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue