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')
|
2022-01-07 09:30:29 -05:00
|
|
|
const { expressify } = require('../../util/promises')
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
const DEFAULT_MESSAGE_LIMIT = 50
|
|
|
|
const MAX_MESSAGE_LENGTH = 10 * 1024 // 10kb, about 1,500 words
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function getGlobalMessages(req, res) {
|
|
|
|
await _getMessages(ThreadManager.GLOBAL_THREAD, req, res)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-01-04 08:51:08 -05:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function sendGlobalMessage(req, res) {
|
|
|
|
await _sendMessage(ThreadManager.GLOBAL_THREAD, req, res)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function sendThreadMessage(req, res) {
|
|
|
|
await _sendMessage(req.params.threadId, req, res)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function getAllThreads(req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId } = req.params
|
|
|
|
logger.log({ projectId }, 'getting all threads')
|
2022-01-07 09:30:29 -05:00
|
|
|
const rooms = await ThreadManager.findAllThreadRooms(projectId)
|
|
|
|
const roomIds = rooms.map(r => r._id)
|
|
|
|
const messages = await MessageManager.findAllMessagesInRooms(roomIds)
|
|
|
|
const threads = MessageFormatter.groupMessagesByThreads(rooms, messages)
|
|
|
|
res.json(threads)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function resolveThread(req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
const { user_id: userId } = req.body
|
|
|
|
logger.log({ userId, projectId, threadId }, 'marking thread as resolved')
|
2022-01-07 09:30:29 -05:00
|
|
|
await ThreadManager.resolveThread(projectId, threadId, userId)
|
|
|
|
res.sendStatus(204)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function reopenThread(req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
logger.log({ projectId, threadId }, 'reopening thread')
|
2022-01-07 09:30:29 -05:00
|
|
|
await ThreadManager.reopenThread(projectId, threadId)
|
|
|
|
res.sendStatus(204)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function deleteThread(req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
logger.log({ projectId, threadId }, 'deleting thread')
|
2022-01-07 09:30:29 -05:00
|
|
|
const roomId = await ThreadManager.deleteThread(projectId, threadId)
|
|
|
|
await MessageManager.deleteAllMessagesInRoom(roomId)
|
|
|
|
res.sendStatus(204)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function editMessage(req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { content } = req.body
|
|
|
|
const { projectId, threadId, messageId } = req.params
|
|
|
|
logger.log({ projectId, threadId, messageId, content }, 'editing message')
|
2022-01-07 09:30:29 -05:00
|
|
|
const room = await ThreadManager.findOrCreateThread(projectId, threadId)
|
|
|
|
await MessageManager.updateMessage(room._id, messageId, content, Date.now())
|
|
|
|
res.sendStatus(204)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function deleteMessage(req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId, messageId } = req.params
|
|
|
|
logger.log({ projectId, threadId, messageId }, 'deleting message')
|
2022-01-07 09:30:29 -05:00
|
|
|
const room = await ThreadManager.findOrCreateThread(projectId, threadId)
|
|
|
|
await MessageManager.deleteMessage(room._id, messageId)
|
|
|
|
res.sendStatus(204)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function _sendMessage(clientThreadId, req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { user_id: userId, content } = req.body
|
|
|
|
const { projectId } = req.params
|
|
|
|
if (!ObjectId.isValid(userId)) {
|
|
|
|
return res.status(400).send('Invalid userId')
|
|
|
|
}
|
|
|
|
if (!content) {
|
|
|
|
return res.status(400).send('No content provided')
|
|
|
|
}
|
|
|
|
if (content.length > MAX_MESSAGE_LENGTH) {
|
|
|
|
return res
|
|
|
|
.status(400)
|
|
|
|
.send(`Content too long (> ${MAX_MESSAGE_LENGTH} bytes)`)
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ clientThreadId, projectId, userId, content },
|
|
|
|
'new message received'
|
|
|
|
)
|
2022-01-07 09:30:29 -05:00
|
|
|
const thread = await ThreadManager.findOrCreateThread(
|
2022-01-07 06:48:39 -05:00
|
|
|
projectId,
|
2022-01-07 09:30:29 -05:00
|
|
|
clientThreadId
|
2022-01-07 06:48:39 -05:00
|
|
|
)
|
2022-01-07 09:30:29 -05:00
|
|
|
let message = await MessageManager.createMessage(
|
|
|
|
thread._id,
|
|
|
|
userId,
|
|
|
|
content,
|
|
|
|
Date.now()
|
|
|
|
)
|
|
|
|
message = MessageFormatter.formatMessageForClientSide(message)
|
|
|
|
message.room_id = projectId
|
|
|
|
res.status(201).send(message)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 09:30:29 -05:00
|
|
|
async function _getMessages(clientThreadId, req, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
let before, limit
|
|
|
|
const { projectId } = req.params
|
|
|
|
if (req.query.before) {
|
|
|
|
before = parseInt(req.query.before, 10)
|
|
|
|
} else {
|
|
|
|
before = null
|
|
|
|
}
|
|
|
|
if (req.query.limit) {
|
|
|
|
limit = parseInt(req.query.limit, 10)
|
|
|
|
} else {
|
|
|
|
limit = DEFAULT_MESSAGE_LIMIT
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ limit, before, projectId, clientThreadId },
|
|
|
|
'get message request received'
|
|
|
|
)
|
2022-01-07 09:30:29 -05:00
|
|
|
const thread = await ThreadManager.findOrCreateThread(
|
2022-01-07 06:48:39 -05:00
|
|
|
projectId,
|
2022-01-07 09:30:29 -05:00
|
|
|
clientThreadId
|
|
|
|
)
|
|
|
|
const threadObjectId = thread._id
|
|
|
|
logger.log(
|
|
|
|
{ limit, before, projectId, clientThreadId, threadObjectId },
|
|
|
|
'found or created thread'
|
2022-01-07 06:48:39 -05:00
|
|
|
)
|
2022-01-07 09:30:29 -05:00
|
|
|
let messages = await MessageManager.getMessages(threadObjectId, limit, before)
|
|
|
|
messages = MessageFormatter.formatMessagesForClientSide(messages)
|
|
|
|
logger.log({ projectId, messages }, 'got messages')
|
|
|
|
res.status(200).send(messages)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2022-01-07 09:30:29 -05:00
|
|
|
getGlobalMessages: expressify(getGlobalMessages),
|
|
|
|
sendGlobalMessage: expressify(sendGlobalMessage),
|
|
|
|
sendThreadMessage: expressify(sendThreadMessage),
|
|
|
|
getAllThreads: expressify(getAllThreads),
|
|
|
|
resolveThread: expressify(resolveThread),
|
|
|
|
reopenThread: expressify(reopenThread),
|
|
|
|
deleteThread: expressify(deleteThread),
|
|
|
|
editMessage: expressify(editMessage),
|
|
|
|
deleteMessage: expressify(deleteMessage),
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|