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'
|
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-21 07:18:12 -05:00
|
|
|
async function readContext(context, req) {
|
|
|
|
req.body = context.requestBody
|
|
|
|
req.params = context.params.path
|
|
|
|
req.query = context.params.query
|
|
|
|
if (typeof req.params.projectId !== 'undefined') {
|
|
|
|
if (!ObjectId.isValid(req.params.projectId)) {
|
|
|
|
context.res.status(400).setBody('Invalid projectId')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof req.params.threadId !== 'undefined') {
|
|
|
|
if (!ObjectId.isValid(req.params.threadId)) {
|
|
|
|
context.res.status(400).setBody('Invalid threadId')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function callMessageHttpController(context, ControllerMethod) {
|
|
|
|
const req = {}
|
|
|
|
readContext(context, req)
|
|
|
|
if (context.res.statusCode !== 400) {
|
|
|
|
return await ControllerMethod(req, context.res)
|
|
|
|
} else {
|
|
|
|
return context.res.body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getGlobalMessages(context) {
|
|
|
|
return await callMessageHttpController(context, _getGlobalMessages)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendGlobalMessage(context) {
|
|
|
|
return await callMessageHttpController(context, _sendGlobalMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendMessage(context) {
|
|
|
|
return await callMessageHttpController(context, _sendThreadMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getThreads(context) {
|
|
|
|
return await callMessageHttpController(context, _getAllThreads)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function resolveThread(context) {
|
|
|
|
return await callMessageHttpController(context, _resolveThread)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function reopenThread(context) {
|
|
|
|
return await callMessageHttpController(context, _reopenThread)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteThread(context) {
|
|
|
|
return await callMessageHttpController(context, _deleteThread)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function editMessage(context) {
|
|
|
|
return await callMessageHttpController(context, _editMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteMessage(context) {
|
|
|
|
return await callMessageHttpController(context, _deleteMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function destroyProject(context) {
|
|
|
|
return await callMessageHttpController(context, _destroyProject)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getStatus(context) {
|
|
|
|
const message = 'chat is alive'
|
|
|
|
context.res.status(200).setBody(message)
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
const _getGlobalMessages = async (req, res) => {
|
2022-01-07 09:30:29 -05:00
|
|
|
await _getMessages(ThreadManager.GLOBAL_THREAD, req, res)
|
2022-12-21 07:18:12 -05:00
|
|
|
}
|
2017-01-04 08:51:08 -05:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
async function _sendGlobalMessage(req, res) {
|
|
|
|
const { user_id: userId, content } = req.body
|
|
|
|
const { projectId } = req.params
|
|
|
|
return await _sendMessage(
|
|
|
|
userId,
|
|
|
|
projectId,
|
|
|
|
content,
|
|
|
|
ThreadManager.GLOBAL_THREAD,
|
|
|
|
res
|
|
|
|
)
|
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
async function _sendThreadMessage(req, res) {
|
|
|
|
const { user_id: userId, content } = req.body
|
|
|
|
const { projectId, threadId } = req.params
|
|
|
|
return await _sendMessage(userId, projectId, content, threadId, res)
|
|
|
|
}
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
const _getAllThreads = 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-21 07:18:12 -05:00
|
|
|
}
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
const _resolveThread = 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)
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(204)
|
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
const _reopenThread = 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)
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(204)
|
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
const _deleteThread = 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)
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(204)
|
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
const _editMessage = 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) {
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(404)
|
|
|
|
return
|
2022-03-31 05:23:22 -04:00
|
|
|
}
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(204)
|
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
const _deleteMessage = 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)
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(204)
|
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
const _destroyProject = 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)
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(204)
|
|
|
|
}
|
2022-03-16 08:20:52 -04:00
|
|
|
|
2022-12-21 07:18:12 -05:00
|
|
|
async function _sendMessage(userId, projectId, content, clientThreadId, res) {
|
2022-01-07 06:48:39 -05:00
|
|
|
if (!ObjectId.isValid(userId)) {
|
2022-12-21 07:18:12 -05:00
|
|
|
const message = 'Invalid userId'
|
|
|
|
res.status(400).setBody(message)
|
|
|
|
return message
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
if (!content) {
|
2022-12-21 07:18:12 -05:00
|
|
|
const message = 'No content provided'
|
|
|
|
res.status(400).setBody(message)
|
|
|
|
return message
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
if (content.length > MAX_MESSAGE_LENGTH) {
|
2022-12-21 07:18:12 -05:00
|
|
|
const message = `Content too long (> ${MAX_MESSAGE_LENGTH} bytes)`
|
|
|
|
res.status(400).setBody(message)
|
|
|
|
return message
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
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
|
2022-12-21 07:18:12 -05:00
|
|
|
res.status(201).setBody(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-12-21 07:18:12 -05:00
|
|
|
res.status(200).setBody(messages)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|