2022-12-13 07:37:49 -05:00
|
|
|
import logger from '@overleaf/logger'
|
|
|
|
import * as MessageManager from './MessageManager.js'
|
|
|
|
import * as MessageFormatter from './MessageFormatter.js'
|
|
|
|
import * as ThreadManager from '../Threads/ThreadManager.js'
|
|
|
|
import { ObjectId } from '../../mongodb.js'
|
|
|
|
import { expressify } from '../../util/promises.js'
|
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-12-13 07:37:49 -05:00
|
|
|
export const getGlobalMessages = expressify(async (req, res) => {
|
2022-01-07 09:30:29 -05:00
|
|
|
await _getMessages(ThreadManager.GLOBAL_THREAD, req, res)
|
2022-12-13 07:37:49 -05:00
|
|
|
})
|
2017-01-04 08:51:08 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const sendGlobalMessage = expressify(async (req, res) => {
|
2022-01-07 09:30:29 -05:00
|
|
|
await _sendMessage(ThreadManager.GLOBAL_THREAD, req, res)
|
2022-12-13 07:37:49 -05:00
|
|
|
})
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const sendThreadMessage = expressify(async (req, res) => {
|
2022-01-07 09:30:29 -05:00
|
|
|
await _sendMessage(req.params.threadId, req, res)
|
2022-12-13 07:37:49 -05:00
|
|
|
})
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const getAllThreads = expressify(async (req, res) => {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ 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-12-13 07:37:49 -05:00
|
|
|
})
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const resolveThread = expressify(async (req, res) => {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
const { user_id: userId } = req.body
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ 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-12-13 07:37:49 -05:00
|
|
|
})
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const reopenThread = expressify(async (req, res) => {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, threadId }, 'reopening thread')
|
2022-01-07 09:30:29 -05:00
|
|
|
await ThreadManager.reopenThread(projectId, threadId)
|
|
|
|
res.sendStatus(204)
|
2022-12-13 07:37:49 -05:00
|
|
|
})
|
2022-01-07 06:48:39 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const deleteThread = expressify(async (req, res) => {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ 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-12-13 07:37:49 -05:00
|
|
|
})
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const editMessage = expressify(async (req, res) => {
|
2022-03-31 03:54:33 -04:00
|
|
|
const { content, userId } = req.body
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId, messageId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, threadId, messageId, content }, 'editing message')
|
2022-01-07 09:30:29 -05:00
|
|
|
const room = await ThreadManager.findOrCreateThread(projectId, threadId)
|
2022-03-31 05:23:22 -04:00
|
|
|
const found = await MessageManager.updateMessage(
|
2022-03-31 03:54:33 -04:00
|
|
|
room._id,
|
|
|
|
messageId,
|
|
|
|
userId,
|
|
|
|
content,
|
|
|
|
Date.now()
|
|
|
|
)
|
2022-03-31 05:23:22 -04:00
|
|
|
if (!found) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
2022-01-07 09:30:29 -05:00
|
|
|
res.sendStatus(204)
|
2022-12-13 07:37:49 -05:00
|
|
|
})
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const deleteMessage = expressify(async (req, res) => {
|
2022-01-07 06:48:39 -05:00
|
|
|
const { projectId, threadId, messageId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ 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-12-13 07:37:49 -05:00
|
|
|
})
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export const destroyProject = expressify(async (req, res) => {
|
2022-03-16 08:20:52 -04:00
|
|
|
const { projectId } = req.params
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'destroying project')
|
2022-03-16 08:20:52 -04:00
|
|
|
const rooms = await ThreadManager.findAllThreadRoomsAndGlobalThread(projectId)
|
|
|
|
const roomIds = rooms.map(r => r._id)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, roomIds }, 'deleting all messages in rooms')
|
2022-03-16 08:20:52 -04:00
|
|
|
await MessageManager.deleteAllMessagesInRooms(roomIds)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId }, 'deleting all threads in project')
|
2022-03-16 08:20:52 -04:00
|
|
|
await ThreadManager.deleteAllThreadsInProject(projectId)
|
|
|
|
res.sendStatus(204)
|
2022-12-13 07:37:49 -05:00
|
|
|
})
|
2022-03-16 08:20:52 -04: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)`)
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2022-01-07 06:48:39 -05:00
|
|
|
{ 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
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2022-01-07 06:48:39 -05:00
|
|
|
{ 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
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2022-01-07 09:30:29 -05:00
|
|
|
{ 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)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ projectId, messages }, 'got messages')
|
2022-01-07 09:30:29 -05:00
|
|
|
res.status(200).send(messages)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|