mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
move module functions to top level
GitOrigin-RevId: 73524dcec49faa05d1fd4dd4ffa6950803175f33
This commit is contained in:
parent
b5d2122f05
commit
f3a5e8a0e8
4 changed files with 450 additions and 455 deletions
|
@ -1,6 +1,4 @@
|
||||||
let MessageFormatter
|
function formatMessageForClientSide(message) {
|
||||||
module.exports = MessageFormatter = {
|
|
||||||
formatMessageForClientSide(message) {
|
|
||||||
if (message._id) {
|
if (message._id) {
|
||||||
message.id = message._id.toString()
|
message.id = message._id.toString()
|
||||||
delete message._id
|
delete message._id
|
||||||
|
@ -15,13 +13,13 @@ module.exports = MessageFormatter = {
|
||||||
formattedMessage.edited_at = message.edited_at
|
formattedMessage.edited_at = message.edited_at
|
||||||
}
|
}
|
||||||
return formattedMessage
|
return formattedMessage
|
||||||
},
|
}
|
||||||
|
|
||||||
formatMessagesForClientSide(messages) {
|
function formatMessagesForClientSide(messages) {
|
||||||
return messages.map(message => this.formatMessageForClientSide(message))
|
return messages.map(message => formatMessageForClientSide(message))
|
||||||
},
|
}
|
||||||
|
|
||||||
groupMessagesByThreads(rooms, messages) {
|
function groupMessagesByThreads(rooms, messages) {
|
||||||
let room, thread
|
let room, thread
|
||||||
const roomsById = {}
|
const roomsById = {}
|
||||||
for (room of rooms) {
|
for (room of rooms) {
|
||||||
|
@ -49,9 +47,7 @@ module.exports = MessageFormatter = {
|
||||||
room = roomsById[message.room_id.toString()]
|
room = roomsById[message.room_id.toString()]
|
||||||
if (room) {
|
if (room) {
|
||||||
thread = getThread(room)
|
thread = getThread(room)
|
||||||
thread.messages.push(
|
thread.messages.push(formatMessageForClientSide(message))
|
||||||
MessageFormatter.formatMessageForClientSide(message)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,5 +57,10 @@ module.exports = MessageFormatter = {
|
||||||
}
|
}
|
||||||
|
|
||||||
return threads
|
return threads
|
||||||
},
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
formatMessagesForClientSide,
|
||||||
|
formatMessageForClientSide,
|
||||||
|
groupMessagesByThreads,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,37 +1,25 @@
|
||||||
let MessageHttpController
|
|
||||||
const logger = require('@overleaf/logger')
|
const logger = require('@overleaf/logger')
|
||||||
const MessageManager = require('./MessageManager')
|
const MessageManager = require('./MessageManager')
|
||||||
const MessageFormatter = require('./MessageFormatter')
|
const MessageFormatter = require('./MessageFormatter')
|
||||||
const ThreadManager = require('../Threads/ThreadManager')
|
const ThreadManager = require('../Threads/ThreadManager')
|
||||||
const { ObjectId } = require('../../mongodb')
|
const { ObjectId } = require('../../mongodb')
|
||||||
|
|
||||||
module.exports = MessageHttpController = {
|
const DEFAULT_MESSAGE_LIMIT = 50
|
||||||
DEFAULT_MESSAGE_LIMIT: 50,
|
const MAX_MESSAGE_LENGTH = 10 * 1024 // 10kb, about 1,500 words
|
||||||
MAX_MESSAGE_LENGTH: 10 * 1024, // 10kb, about 1,500 words
|
|
||||||
|
|
||||||
getGlobalMessages(req, res, next) {
|
function getGlobalMessages(req, res, next) {
|
||||||
MessageHttpController._getMessages(
|
_getMessages(ThreadManager.GLOBAL_THREAD, req, res, next)
|
||||||
ThreadManager.GLOBAL_THREAD,
|
}
|
||||||
req,
|
|
||||||
res,
|
|
||||||
next
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
sendGlobalMessage(req, res, next) {
|
function sendGlobalMessage(req, res, next) {
|
||||||
MessageHttpController._sendMessage(
|
_sendMessage(ThreadManager.GLOBAL_THREAD, req, res, next)
|
||||||
ThreadManager.GLOBAL_THREAD,
|
}
|
||||||
req,
|
|
||||||
res,
|
|
||||||
next
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|
|
||||||
sendThreadMessage(req, res, next) {
|
function sendThreadMessage(req, res, next) {
|
||||||
MessageHttpController._sendMessage(req.params.threadId, req, res, next)
|
_sendMessage(req.params.threadId, req, res, next)
|
||||||
},
|
}
|
||||||
|
|
||||||
getAllThreads(req, res, next) {
|
function getAllThreads(req, res, next) {
|
||||||
const { projectId } = req.params
|
const { projectId } = req.params
|
||||||
logger.log({ projectId }, 'getting all threads')
|
logger.log({ projectId }, 'getting all threads')
|
||||||
ThreadManager.findAllThreadRooms(projectId, function (error, rooms) {
|
ThreadManager.findAllThreadRooms(projectId, function (error, rooms) {
|
||||||
|
@ -39,23 +27,17 @@ module.exports = MessageHttpController = {
|
||||||
return next(error)
|
return next(error)
|
||||||
}
|
}
|
||||||
const roomIds = rooms.map(r => r._id)
|
const roomIds = rooms.map(r => r._id)
|
||||||
MessageManager.findAllMessagesInRooms(
|
MessageManager.findAllMessagesInRooms(roomIds, function (error, messages) {
|
||||||
roomIds,
|
|
||||||
function (error, messages) {
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return next(error)
|
return next(error)
|
||||||
}
|
}
|
||||||
const threads = MessageFormatter.groupMessagesByThreads(
|
const threads = MessageFormatter.groupMessagesByThreads(rooms, messages)
|
||||||
rooms,
|
|
||||||
messages
|
|
||||||
)
|
|
||||||
res.json(threads)
|
res.json(threads)
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
},
|
})
|
||||||
|
}
|
||||||
|
|
||||||
resolveThread(req, res, next) {
|
function resolveThread(req, res, next) {
|
||||||
const { projectId, threadId } = req.params
|
const { projectId, threadId } = req.params
|
||||||
const { user_id: userId } = req.body
|
const { user_id: userId } = req.body
|
||||||
logger.log({ userId, projectId, threadId }, 'marking thread as resolved')
|
logger.log({ userId, projectId, threadId }, 'marking thread as resolved')
|
||||||
|
@ -65,9 +47,9 @@ module.exports = MessageHttpController = {
|
||||||
}
|
}
|
||||||
res.sendStatus(204)
|
res.sendStatus(204)
|
||||||
})
|
})
|
||||||
}, // No content
|
}
|
||||||
|
|
||||||
reopenThread(req, res, next) {
|
function reopenThread(req, res, next) {
|
||||||
const { projectId, threadId } = req.params
|
const { projectId, threadId } = req.params
|
||||||
logger.log({ projectId, threadId }, 'reopening thread')
|
logger.log({ projectId, threadId }, 'reopening thread')
|
||||||
ThreadManager.reopenThread(projectId, threadId, function (error) {
|
ThreadManager.reopenThread(projectId, threadId, function (error) {
|
||||||
|
@ -76,9 +58,9 @@ module.exports = MessageHttpController = {
|
||||||
}
|
}
|
||||||
res.sendStatus(204)
|
res.sendStatus(204)
|
||||||
})
|
})
|
||||||
}, // No content
|
}
|
||||||
|
|
||||||
deleteThread(req, res, next) {
|
function deleteThread(req, res, next) {
|
||||||
const { projectId, threadId } = req.params
|
const { projectId, threadId } = req.params
|
||||||
logger.log({ projectId, threadId }, 'deleting thread')
|
logger.log({ projectId, threadId }, 'deleting thread')
|
||||||
ThreadManager.deleteThread(projectId, threadId, function (error, roomId) {
|
ThreadManager.deleteThread(projectId, threadId, function (error, roomId) {
|
||||||
|
@ -92,16 +74,13 @@ module.exports = MessageHttpController = {
|
||||||
res.sendStatus(204)
|
res.sendStatus(204)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}, // No content
|
}
|
||||||
|
|
||||||
editMessage(req, res, next) {
|
function editMessage(req, res, next) {
|
||||||
const { content } = req.body
|
const { content } = req.body
|
||||||
const { projectId, threadId, messageId } = req.params
|
const { projectId, threadId, messageId } = req.params
|
||||||
logger.log({ projectId, threadId, messageId, content }, 'editing message')
|
logger.log({ projectId, threadId, messageId, content }, 'editing message')
|
||||||
ThreadManager.findOrCreateThread(
|
ThreadManager.findOrCreateThread(projectId, threadId, function (error, room) {
|
||||||
projectId,
|
|
||||||
threadId,
|
|
||||||
function (error, room) {
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return next(error)
|
return next(error)
|
||||||
}
|
}
|
||||||
|
@ -117,17 +96,13 @@ module.exports = MessageHttpController = {
|
||||||
res.sendStatus(204)
|
res.sendStatus(204)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
)
|
}
|
||||||
},
|
|
||||||
|
|
||||||
deleteMessage(req, res, next) {
|
function deleteMessage(req, res, next) {
|
||||||
const { projectId, threadId, messageId } = req.params
|
const { projectId, threadId, messageId } = req.params
|
||||||
logger.log({ projectId, threadId, messageId }, 'deleting message')
|
logger.log({ projectId, threadId, messageId }, 'deleting message')
|
||||||
ThreadManager.findOrCreateThread(
|
ThreadManager.findOrCreateThread(projectId, threadId, function (error, room) {
|
||||||
projectId,
|
|
||||||
threadId,
|
|
||||||
function (error, room) {
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return next(error)
|
return next(error)
|
||||||
}
|
}
|
||||||
|
@ -141,11 +116,10 @@ module.exports = MessageHttpController = {
|
||||||
res.sendStatus(204)
|
res.sendStatus(204)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
)
|
}
|
||||||
},
|
|
||||||
|
|
||||||
_sendMessage(clientThreadId, req, res, next) {
|
function _sendMessage(clientThreadId, req, res, next) {
|
||||||
const { user_id: userId, content } = req.body
|
const { user_id: userId, content } = req.body
|
||||||
const { projectId } = req.params
|
const { projectId } = req.params
|
||||||
if (!ObjectId.isValid(userId)) {
|
if (!ObjectId.isValid(userId)) {
|
||||||
|
@ -154,10 +128,10 @@ module.exports = MessageHttpController = {
|
||||||
if (!content) {
|
if (!content) {
|
||||||
return res.status(400).send('No content provided')
|
return res.status(400).send('No content provided')
|
||||||
}
|
}
|
||||||
if (content.length > this.MAX_MESSAGE_LENGTH) {
|
if (content.length > MAX_MESSAGE_LENGTH) {
|
||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
.send(`Content too long (> ${this.MAX_MESSAGE_LENGTH} bytes)`)
|
.send(`Content too long (> ${MAX_MESSAGE_LENGTH} bytes)`)
|
||||||
}
|
}
|
||||||
logger.log(
|
logger.log(
|
||||||
{ clientThreadId, projectId, userId, content },
|
{ clientThreadId, projectId, userId, content },
|
||||||
|
@ -186,9 +160,9 @@ module.exports = MessageHttpController = {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
|
|
||||||
_getMessages(clientThreadId, req, res, next) {
|
function _getMessages(clientThreadId, req, res, next) {
|
||||||
let before, limit
|
let before, limit
|
||||||
const { projectId } = req.params
|
const { projectId } = req.params
|
||||||
if (req.query.before) {
|
if (req.query.before) {
|
||||||
|
@ -199,7 +173,7 @@ module.exports = MessageHttpController = {
|
||||||
if (req.query.limit) {
|
if (req.query.limit) {
|
||||||
limit = parseInt(req.query.limit, 10)
|
limit = parseInt(req.query.limit, 10)
|
||||||
} else {
|
} else {
|
||||||
limit = MessageHttpController.DEFAULT_MESSAGE_LIMIT
|
limit = DEFAULT_MESSAGE_LIMIT
|
||||||
}
|
}
|
||||||
logger.log(
|
logger.log(
|
||||||
{ limit, before, projectId, clientThreadId },
|
{ limit, before, projectId, clientThreadId },
|
||||||
|
@ -232,5 +206,16 @@ module.exports = MessageHttpController = {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getGlobalMessages,
|
||||||
|
sendGlobalMessage,
|
||||||
|
sendThreadMessage,
|
||||||
|
getAllThreads,
|
||||||
|
resolveThread,
|
||||||
|
reopenThread,
|
||||||
|
deleteThread,
|
||||||
|
editMessage,
|
||||||
|
deleteMessage,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,15 +3,14 @@ const { db, ObjectId } = require('../../mongodb')
|
||||||
const metrics = require('@overleaf/metrics')
|
const metrics = require('@overleaf/metrics')
|
||||||
const logger = require('@overleaf/logger')
|
const logger = require('@overleaf/logger')
|
||||||
|
|
||||||
module.exports = MessageManager = {
|
function createMessage(roomId, userId, content, timestamp, callback) {
|
||||||
createMessage(roomId, userId, content, timestamp, callback) {
|
|
||||||
let newMessageOpts = {
|
let newMessageOpts = {
|
||||||
content,
|
content,
|
||||||
room_id: roomId,
|
room_id: roomId,
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
timestamp,
|
timestamp,
|
||||||
}
|
}
|
||||||
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts)
|
newMessageOpts = _ensureIdsAreObjectIds(newMessageOpts)
|
||||||
db.messages.insertOne(newMessageOpts, function (error, confirmation) {
|
db.messages.insertOne(newMessageOpts, function (error, confirmation) {
|
||||||
if (error) {
|
if (error) {
|
||||||
return callback(error)
|
return callback(error)
|
||||||
|
@ -19,40 +18,36 @@ module.exports = MessageManager = {
|
||||||
newMessageOpts._id = confirmation.insertedId
|
newMessageOpts._id = confirmation.insertedId
|
||||||
callback(null, newMessageOpts)
|
callback(null, newMessageOpts)
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
|
|
||||||
getMessages(roomId, limit, before, callback) {
|
function getMessages(roomId, limit, before, callback) {
|
||||||
let query = { room_id: roomId }
|
let query = { room_id: roomId }
|
||||||
if (before) {
|
if (before) {
|
||||||
query.timestamp = { $lt: before }
|
query.timestamp = { $lt: before }
|
||||||
}
|
}
|
||||||
query = this._ensureIdsAreObjectIds(query)
|
query = _ensureIdsAreObjectIds(query)
|
||||||
db.messages
|
db.messages.find(query).sort({ timestamp: -1 }).limit(limit).toArray(callback)
|
||||||
.find(query)
|
}
|
||||||
.sort({ timestamp: -1 })
|
|
||||||
.limit(limit)
|
|
||||||
.toArray(callback)
|
|
||||||
},
|
|
||||||
|
|
||||||
findAllMessagesInRooms(roomIds, callback) {
|
function findAllMessagesInRooms(roomIds, callback) {
|
||||||
db.messages
|
db.messages
|
||||||
.find({
|
.find({
|
||||||
room_id: { $in: roomIds },
|
room_id: { $in: roomIds },
|
||||||
})
|
})
|
||||||
.toArray(callback)
|
.toArray(callback)
|
||||||
},
|
}
|
||||||
|
|
||||||
deleteAllMessagesInRoom(roomId, callback) {
|
function deleteAllMessagesInRoom(roomId, callback) {
|
||||||
db.messages.deleteMany(
|
db.messages.deleteMany(
|
||||||
{
|
{
|
||||||
room_id: roomId,
|
room_id: roomId,
|
||||||
},
|
},
|
||||||
callback
|
callback
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
|
|
||||||
updateMessage(roomId, messageId, content, timestamp, callback) {
|
function updateMessage(roomId, messageId, content, timestamp, callback) {
|
||||||
const query = this._ensureIdsAreObjectIds({
|
const query = _ensureIdsAreObjectIds({
|
||||||
_id: messageId,
|
_id: messageId,
|
||||||
room_id: roomId,
|
room_id: roomId,
|
||||||
})
|
})
|
||||||
|
@ -66,17 +61,17 @@ module.exports = MessageManager = {
|
||||||
},
|
},
|
||||||
callback
|
callback
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
|
|
||||||
deleteMessage(roomId, messageId, callback) {
|
function deleteMessage(roomId, messageId, callback) {
|
||||||
const query = this._ensureIdsAreObjectIds({
|
const query = _ensureIdsAreObjectIds({
|
||||||
_id: messageId,
|
_id: messageId,
|
||||||
room_id: roomId,
|
room_id: roomId,
|
||||||
})
|
})
|
||||||
db.messages.deleteOne(query, callback)
|
db.messages.deleteOne(query, callback)
|
||||||
},
|
}
|
||||||
|
|
||||||
_ensureIdsAreObjectIds(query) {
|
function _ensureIdsAreObjectIds(query) {
|
||||||
if (query.user_id && !(query.user_id instanceof ObjectId)) {
|
if (query.user_id && !(query.user_id instanceof ObjectId)) {
|
||||||
query.user_id = ObjectId(query.user_id)
|
query.user_id = ObjectId(query.user_id)
|
||||||
}
|
}
|
||||||
|
@ -87,7 +82,15 @@ module.exports = MessageManager = {
|
||||||
query._id = ObjectId(query._id)
|
query._id = ObjectId(query._id)
|
||||||
}
|
}
|
||||||
return query
|
return query
|
||||||
},
|
}
|
||||||
|
|
||||||
|
module.exports = MessageManager = {
|
||||||
|
createMessage,
|
||||||
|
getMessages,
|
||||||
|
findAllMessagesInRooms,
|
||||||
|
deleteAllMessagesInRoom,
|
||||||
|
updateMessage,
|
||||||
|
deleteMessage,
|
||||||
}
|
}
|
||||||
;[
|
;[
|
||||||
'createMessage',
|
'createMessage',
|
||||||
|
|
|
@ -3,17 +3,16 @@ const { db, ObjectId } = require('../../mongodb')
|
||||||
const logger = require('@overleaf/logger')
|
const logger = require('@overleaf/logger')
|
||||||
const metrics = require('@overleaf/metrics')
|
const metrics = require('@overleaf/metrics')
|
||||||
|
|
||||||
module.exports = ThreadManager = {
|
const GLOBAL_THREAD = 'GLOBAL'
|
||||||
GLOBAL_THREAD: 'GLOBAL',
|
|
||||||
|
|
||||||
findOrCreateThread(projectId, threadId, callback) {
|
function findOrCreateThread(projectId, threadId, callback) {
|
||||||
let query, update
|
let query, update
|
||||||
projectId = ObjectId(projectId.toString())
|
projectId = ObjectId(projectId.toString())
|
||||||
if (threadId !== ThreadManager.GLOBAL_THREAD) {
|
if (threadId !== GLOBAL_THREAD) {
|
||||||
threadId = ObjectId(threadId.toString())
|
threadId = ObjectId(threadId.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
if (threadId === ThreadManager.GLOBAL_THREAD) {
|
if (threadId === GLOBAL_THREAD) {
|
||||||
query = {
|
query = {
|
||||||
project_id: projectId,
|
project_id: projectId,
|
||||||
thread_id: { $exists: false },
|
thread_id: { $exists: false },
|
||||||
|
@ -43,9 +42,9 @@ module.exports = ThreadManager = {
|
||||||
callback(null, result.value)
|
callback(null, result.value)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
|
|
||||||
findAllThreadRooms(projectId, callback) {
|
function findAllThreadRooms(projectId, callback) {
|
||||||
db.rooms
|
db.rooms
|
||||||
.find(
|
.find(
|
||||||
{
|
{
|
||||||
|
@ -58,9 +57,9 @@ module.exports = ThreadManager = {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.toArray(callback)
|
.toArray(callback)
|
||||||
},
|
}
|
||||||
|
|
||||||
resolveThread(projectId, threadId, userId, callback) {
|
function resolveThread(projectId, threadId, userId, callback) {
|
||||||
db.rooms.updateOne(
|
db.rooms.updateOne(
|
||||||
{
|
{
|
||||||
project_id: ObjectId(projectId.toString()),
|
project_id: ObjectId(projectId.toString()),
|
||||||
|
@ -76,9 +75,9 @@ module.exports = ThreadManager = {
|
||||||
},
|
},
|
||||||
callback
|
callback
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
|
|
||||||
reopenThread(projectId, threadId, callback) {
|
function reopenThread(projectId, threadId, callback) {
|
||||||
db.rooms.updateOne(
|
db.rooms.updateOne(
|
||||||
{
|
{
|
||||||
project_id: ObjectId(projectId.toString()),
|
project_id: ObjectId(projectId.toString()),
|
||||||
|
@ -88,13 +87,12 @@ module.exports = ThreadManager = {
|
||||||
$unset: {
|
$unset: {
|
||||||
resolved: true,
|
resolved: true,
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
callback
|
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
|
|
||||||
deleteThread(projectId, threadId, callback) {
|
function deleteThread(projectId, threadId, callback) {
|
||||||
this.findOrCreateThread(projectId, threadId, function (error, room) {
|
findOrCreateThread(projectId, threadId, function (error, room) {
|
||||||
if (error) {
|
if (error) {
|
||||||
return callback(error)
|
return callback(error)
|
||||||
}
|
}
|
||||||
|
@ -110,7 +108,15 @@ module.exports = ThreadManager = {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
|
|
||||||
|
module.exports = ThreadManager = {
|
||||||
|
GLOBAL_THREAD,
|
||||||
|
findOrCreateThread,
|
||||||
|
findAllThreadRooms,
|
||||||
|
resolveThread,
|
||||||
|
reopenThread,
|
||||||
|
deleteThread,
|
||||||
}
|
}
|
||||||
;[
|
;[
|
||||||
'findOrCreateThread',
|
'findOrCreateThread',
|
||||||
|
|
Loading…
Reference in a new issue