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) {
|
2022-01-07 05:41:23 -05:00
|
|
|
MessageHttpController._getMessages(
|
2018-12-20 14:13:59 -05:00
|
|
|
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) {
|
2022-01-07 05:41:23 -05:00
|
|
|
MessageHttpController._sendMessage(
|
2018-12-20 14:13:59 -05:00
|
|
|
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) {
|
2022-01-07 05:41:23 -05:00
|
|
|
MessageHttpController._sendMessage(req.params.threadId, req, res, next)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
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')
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.findAllThreadRooms(projectId, function (error, rooms) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2022-01-06 11:32:46 -05:00
|
|
|
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)
|
2022-01-07 05:41:23 -05:00
|
|
|
MessageManager.findAllMessagesInRooms(
|
2022-01-06 11:32:46 -05:00
|
|
|
roomIds,
|
|
|
|
function (error, messages) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2022-01-06 11:32:46 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
const threads = MessageFormatter.groupMessagesByThreads(
|
|
|
|
rooms,
|
|
|
|
messages
|
|
|
|
)
|
2022-01-07 05:41:23 -05:00
|
|
|
res.json(threads)
|
2022-01-06 11:32:46 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
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')
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.resolveThread(projectId, threadId, userId, function (error) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2022-01-07 05:41:23 -05:00
|
|
|
return next(error)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
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')
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.reopenThread(projectId, threadId, function (error) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
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')
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.deleteThread(projectId, threadId, function (error, roomId) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2022-01-07 05:41:23 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
MessageManager.deleteAllMessagesInRoom(roomId, function (error) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:13:59 -05:00
|
|
|
}, // No content
|
|
|
|
|
|
|
|
editMessage(req, res, next) {
|
2022-01-07 05:29:50 -05:00
|
|
|
const { content } = req.body
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId, threadId, messageId } = req.params
|
|
|
|
logger.log({ projectId, threadId, messageId, content }, 'editing message')
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
threadId,
|
2021-07-13 07:04:48 -04:00
|
|
|
function (error, room) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return next(error)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
MessageManager.updateMessage(
|
2021-07-13 07:04:48 -04:00
|
|
|
room._id,
|
2022-01-06 11:32:46 -05:00
|
|
|
messageId,
|
2021-07-13 07:04:48 -04:00
|
|
|
content,
|
|
|
|
Date.now(),
|
|
|
|
function (error) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
res.sendStatus(204)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
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')
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
threadId,
|
2021-07-13 07:04:48 -04:00
|
|
|
function (error, room) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
MessageManager.deleteMessage(
|
2021-07-13 07:04:48 -04:00
|
|
|
room._id,
|
2022-01-06 11:32:46 -05:00
|
|
|
messageId,
|
2021-07-13 07:04:48 -04:00
|
|
|
function (error, message) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
res.sendStatus(204)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
_sendMessage(clientThreadId, req, res, next) {
|
2022-01-07 05:29:50 -05:00
|
|
|
const { user_id: userId, content } = req.body
|
2022-01-06 11:32:46 -05:00
|
|
|
const { projectId } = req.params
|
|
|
|
if (!ObjectId.isValid(userId)) {
|
|
|
|
return res.status(400).send('Invalid userId')
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 05:29:50 -05:00
|
|
|
if (!content) {
|
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'
|
|
|
|
)
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
clientThreadId,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, thread) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
MessageManager.createMessage(
|
2018-12-20 14:13:59 -05:00
|
|
|
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) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
message = MessageFormatter.formatMessageForClientSide(message)
|
2022-01-06 11:32:46 -05:00
|
|
|
message.room_id = projectId
|
2022-01-07 05:41:23 -05:00
|
|
|
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
|
2022-01-07 05:29:50 -05:00
|
|
|
if (req.query.before) {
|
2018-12-20 14:13:59 -05:00
|
|
|
before = parseInt(req.query.before, 10)
|
|
|
|
} else {
|
|
|
|
before = null
|
|
|
|
}
|
2022-01-07 05:29:50 -05:00
|
|
|
if (req.query.limit) {
|
2018-12-20 14:13:59 -05:00
|
|
|
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'
|
|
|
|
)
|
2022-01-07 05:41:23 -05:00
|
|
|
ThreadManager.findOrCreateThread(
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId,
|
|
|
|
clientThreadId,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (error, thread) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
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'
|
|
|
|
)
|
2022-01-07 05:41:23 -05:00
|
|
|
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) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2018-12-20 14:13:59 -05:00
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
messages = MessageFormatter.formatMessagesForClientSide(messages)
|
2022-01-06 11:32:46 -05:00
|
|
|
logger.log({ projectId, messages }, 'got messages')
|
2022-01-07 05:41:23 -05:00
|
|
|
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
|
|
|
}
|