2018-12-20 14:13:56 -05:00
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-12-20 14:13:53 -05:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2018-12-20 14:13:59 -05:00
|
|
|
let MessageHttpController
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2018-12-20 14:13:59 -05:00
|
|
|
const MessageManager = require('./MessageManager')
|
|
|
|
const MessageFormatter = require('./MessageFormatter')
|
|
|
|
const ThreadManager = require('../Threads/ThreadManager')
|
2020-08-19 08:00:01 -04:00
|
|
|
const { ObjectId } = require('../../mongodb')
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
module.exports = MessageHttpController = {
|
|
|
|
DEFAULT_MESSAGE_LIMIT: 50,
|
|
|
|
MAX_MESSAGE_LENGTH: 10 * 1024, // 10kb, about 1,500 words
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
getGlobalMessages(req, res, next) {
|
|
|
|
return MessageHttpController._getMessages(
|
|
|
|
ThreadManager.GLOBAL_THREAD,
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
next
|
|
|
|
)
|
|
|
|
},
|
2017-01-04 08:51:08 -05:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
sendGlobalMessage(req, res, next) {
|
|
|
|
return MessageHttpController._sendMessage(
|
|
|
|
ThreadManager.GLOBAL_THREAD,
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
next
|
|
|
|
)
|
|
|
|
},
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
sendThreadMessage(req, res, next) {
|
|
|
|
return MessageHttpController._sendMessage(
|
2022-01-06 11:32:46 -05:00
|
|
|
req.params.threadId,
|
2018-12-20 14:13:59 -05:00
|
|
|
req,
|
|
|
|
res,
|
|
|
|
next
|
|
|
|
)
|
|
|
|
},
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
getAllThreads(req, res, next) {
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId } = req.params
|
|
|
|
logger.log({ projectId }, 'getting all threads')
|
|
|
|
return ThreadManager.findAllThreadRooms(projectId, function (error, rooms) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
2022-01-06 11:32:46 -05:00
|
|
|
const roomIds = rooms.map(r => r._id)
|
|
|
|
return MessageManager.findAllMessagesInRooms(
|
|
|
|
roomIds,
|
|
|
|
function (error, messages) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
const threads = MessageFormatter.groupMessagesByThreads(
|
|
|
|
rooms,
|
|
|
|
messages
|
|
|
|
)
|
|
|
|
return res.json(threads)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
resolveThread(req, res, next) {
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
const { user_id: userId } = req.body
|
|
|
|
logger.log({ userId, projectId, threadId }, 'marking thread as resolved')
|
2020-08-10 12:01:11 -04:00
|
|
|
return ThreadManager.resolveThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
threadId,
|
|
|
|
userId,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
}, // No content
|
|
|
|
|
|
|
|
reopenThread(req, res, next) {
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
logger.log({ projectId, threadId }, 'reopening thread')
|
|
|
|
return ThreadManager.reopenThread(projectId, threadId, function (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-03-06 13:33:17 -05:00
|
|
|
return res.sendStatus(204)
|
2018-12-20 14:13:59 -05:00
|
|
|
})
|
|
|
|
}, // No content
|
|
|
|
|
|
|
|
deleteThread(req, res, next) {
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
logger.log({ projectId, threadId }, 'deleting thread')
|
2021-07-13 07:04:48 -04:00
|
|
|
return ThreadManager.deleteThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
threadId,
|
|
|
|
function (error, roomId) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-06 11:32:46 -05:00
|
|
|
return MessageManager.deleteAllMessagesInRoom(roomId, function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
2022-01-06 11:32:46 -05:00
|
|
|
return res.sendStatus(204)
|
|
|
|
})
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
}, // No content
|
|
|
|
|
|
|
|
editMessage(req, res, next) {
|
|
|
|
const { content } = req != null ? req.body : undefined
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId, threadId, messageId } = req.params
|
|
|
|
logger.log({ projectId, threadId, messageId, content }, 'editing message')
|
2021-07-13 07:04:48 -04:00
|
|
|
return ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
threadId,
|
2021-07-13 07:04:48 -04:00
|
|
|
function (error, room) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return MessageManager.updateMessage(
|
|
|
|
room._id,
|
2022-01-06 11:32:46 -05:00
|
|
|
messageId,
|
2021-07-13 07:04:48 -04:00
|
|
|
content,
|
|
|
|
Date.now(),
|
|
|
|
function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteMessage(req, res, next) {
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId, threadId, messageId } = req.params
|
|
|
|
logger.log({ projectId, threadId, messageId }, 'deleting message')
|
2021-07-13 07:04:48 -04:00
|
|
|
return ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
threadId,
|
2021-07-13 07:04:48 -04:00
|
|
|
function (error, room) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return MessageManager.deleteMessage(
|
|
|
|
room._id,
|
2022-01-06 11:32:46 -05:00
|
|
|
messageId,
|
2021-07-13 07:04:48 -04:00
|
|
|
function (error, message) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
_sendMessage(clientThreadId, req, res, next) {
|
|
|
|
const { user_id: userId, content } = req != null ? req.body : undefined
|
|
|
|
const { projectId } = req.params
|
|
|
|
if (!ObjectId.isValid(userId)) {
|
|
|
|
return res.status(400).send('Invalid userId')
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
if (content == null) {
|
2020-03-06 13:33:17 -05:00
|
|
|
return res.status(400).send('No content provided')
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
if (content.length > this.MAX_MESSAGE_LENGTH) {
|
2020-03-06 13:33:17 -05:00
|
|
|
return res
|
|
|
|
.status(400)
|
|
|
|
.send(`Content too long (> ${this.MAX_MESSAGE_LENGTH} bytes)`)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
logger.log(
|
2022-01-06 11:32:46 -05:00
|
|
|
{ clientThreadId, projectId, userId, content },
|
2018-12-20 14:13:59 -05:00
|
|
|
'new message received'
|
|
|
|
)
|
|
|
|
return ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
clientThreadId,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, thread) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return MessageManager.createMessage(
|
|
|
|
thread._id,
|
2022-01-06 11:32:46 -05:00
|
|
|
userId,
|
2018-12-20 14:13:59 -05:00
|
|
|
content,
|
|
|
|
Date.now(),
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, message) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
message = MessageFormatter.formatMessageForClientSide(message)
|
2022-01-06 11:32:46 -05:00
|
|
|
message.room_id = projectId
|
2020-03-06 13:33:17 -05:00
|
|
|
return res.status(201).send(message)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
_getMessages(clientThreadId, req, res, next) {
|
2018-12-20 14:13:59 -05:00
|
|
|
let before, limit
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId } = req.params
|
2018-12-20 14:13:59 -05:00
|
|
|
if ((req.query != null ? req.query.before : undefined) != null) {
|
|
|
|
before = parseInt(req.query.before, 10)
|
|
|
|
} else {
|
|
|
|
before = null
|
|
|
|
}
|
|
|
|
if ((req.query != null ? req.query.limit : undefined) != null) {
|
|
|
|
limit = parseInt(req.query.limit, 10)
|
|
|
|
} else {
|
|
|
|
limit = MessageHttpController.DEFAULT_MESSAGE_LIMIT
|
|
|
|
}
|
|
|
|
logger.log(
|
2022-01-06 11:32:46 -05:00
|
|
|
{ limit, before, projectId, clientThreadId },
|
2018-12-20 14:13:59 -05:00
|
|
|
'get message request received'
|
|
|
|
)
|
|
|
|
return ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
clientThreadId,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, thread) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-06 11:32:46 -05:00
|
|
|
const threadObjectId = thread._id
|
2018-12-20 14:13:59 -05:00
|
|
|
logger.log(
|
2022-01-06 11:32:46 -05:00
|
|
|
{ limit, before, projectId, clientThreadId, threadObjectId },
|
2018-12-20 14:13:59 -05:00
|
|
|
'found or created thread'
|
|
|
|
)
|
|
|
|
return MessageManager.getMessages(
|
2022-01-06 11:32:46 -05:00
|
|
|
threadObjectId,
|
2018-12-20 14:13:59 -05:00
|
|
|
limit,
|
|
|
|
before,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, messages) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
messages = MessageFormatter.formatMessagesForClientSide(messages)
|
2022-01-06 11:32:46 -05:00
|
|
|
logger.log({ projectId, messages }, 'got messages')
|
2020-03-06 13:33:17 -05:00
|
|
|
return res.status(200).send(messages)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|