2018-12-20 14:13:56 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// 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
|
|
|
|
const logger = require('logger-sharelatex')
|
2020-09-09 05:37:56 -04:00
|
|
|
const metrics = require('@overleaf/metrics')
|
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(
|
|
|
|
req.params.thread_id,
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
next
|
|
|
|
)
|
|
|
|
},
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
getAllThreads(req, res, next) {
|
|
|
|
const { project_id } = req.params
|
|
|
|
logger.log({ project_id }, 'getting all threads')
|
2021-07-13 07:04:48 -04:00
|
|
|
return ThreadManager.findAllThreadRooms(
|
|
|
|
project_id,
|
|
|
|
function (error, rooms) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
const room_ids = rooms.map(r => r._id)
|
|
|
|
return MessageManager.findAllMessagesInRooms(
|
|
|
|
room_ids,
|
|
|
|
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) {
|
|
|
|
const { project_id, thread_id } = req.params
|
|
|
|
const { user_id } = req.body
|
|
|
|
logger.log({ user_id, project_id, thread_id }, 'marking thread as resolved')
|
2020-08-10 12:01:11 -04:00
|
|
|
return ThreadManager.resolveThread(
|
|
|
|
project_id,
|
|
|
|
thread_id,
|
|
|
|
user_id,
|
|
|
|
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) {
|
|
|
|
const { project_id, thread_id } = req.params
|
|
|
|
logger.log({ project_id, thread_id }, 'reopening thread')
|
2020-08-10 12:01:11 -04:00
|
|
|
return ThreadManager.reopenThread(project_id, thread_id, 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) {
|
|
|
|
const { project_id, thread_id } = req.params
|
|
|
|
logger.log({ project_id, thread_id }, 'deleting thread')
|
2021-07-13 07:04:48 -04:00
|
|
|
return ThreadManager.deleteThread(
|
|
|
|
project_id,
|
|
|
|
thread_id,
|
|
|
|
function (error, room_id) {
|
2018-12-20 14:13:59 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return MessageManager.deleteAllMessagesInRoom(
|
|
|
|
room_id,
|
|
|
|
function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
}, // No content
|
|
|
|
|
|
|
|
editMessage(req, res, next) {
|
|
|
|
const { content } = req != null ? req.body : undefined
|
|
|
|
const { project_id, thread_id, message_id } = req.params
|
|
|
|
logger.log(
|
|
|
|
{ project_id, thread_id, message_id, content },
|
|
|
|
'editing message'
|
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
return ThreadManager.findOrCreateThread(
|
|
|
|
project_id,
|
|
|
|
thread_id,
|
|
|
|
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,
|
|
|
|
message_id,
|
|
|
|
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) {
|
|
|
|
const { project_id, thread_id, message_id } = req.params
|
|
|
|
logger.log({ project_id, thread_id, message_id }, 'deleting message')
|
2021-07-13 07:04:48 -04:00
|
|
|
return ThreadManager.findOrCreateThread(
|
|
|
|
project_id,
|
|
|
|
thread_id,
|
|
|
|
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,
|
|
|
|
message_id,
|
|
|
|
function (error, message) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_sendMessage(client_thread_id, req, res, next) {
|
|
|
|
const { user_id, content } = req != null ? req.body : undefined
|
|
|
|
const { project_id } = req.params
|
|
|
|
if (!ObjectId.isValid(user_id)) {
|
2020-03-06 13:33:17 -05:00
|
|
|
return res.status(400).send('Invalid user_id')
|
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(
|
|
|
|
{ client_thread_id, project_id, user_id, content },
|
|
|
|
'new message received'
|
|
|
|
)
|
|
|
|
return ThreadManager.findOrCreateThread(
|
|
|
|
project_id,
|
|
|
|
client_thread_id,
|
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,
|
|
|
|
user_id,
|
|
|
|
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)
|
|
|
|
message.room_id = project_id
|
2020-03-06 13:33:17 -05:00
|
|
|
return res.status(201).send(message)
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
_getMessages(client_thread_id, req, res, next) {
|
|
|
|
let before, limit
|
|
|
|
const { project_id } = req.params
|
|
|
|
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(
|
|
|
|
{ limit, before, project_id, client_thread_id },
|
|
|
|
'get message request received'
|
|
|
|
)
|
|
|
|
return ThreadManager.findOrCreateThread(
|
|
|
|
project_id,
|
|
|
|
client_thread_id,
|
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)
|
|
|
|
}
|
|
|
|
const thread_object_id = thread._id
|
|
|
|
logger.log(
|
|
|
|
{ limit, before, project_id, client_thread_id, thread_object_id },
|
|
|
|
'found or created thread'
|
|
|
|
)
|
|
|
|
return MessageManager.getMessages(
|
|
|
|
thread_object_id,
|
|
|
|
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)
|
|
|
|
logger.log({ project_id, 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
|
|
|
}
|