mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-29 05:33:44 -05:00
Prettier: convert app/js decaffeinated files to Prettier format
This commit is contained in:
parent
976d1675e6
commit
03093dc283
7 changed files with 671 additions and 470 deletions
|
@ -11,66 +11,70 @@
|
||||||
* DS207: Consider shorter variations of null checks
|
* DS207: Consider shorter variations of null checks
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||||
*/
|
*/
|
||||||
let MessageFormatter;
|
let MessageFormatter
|
||||||
module.exports = (MessageFormatter = {
|
module.exports = MessageFormatter = {
|
||||||
formatMessageForClientSide(message) {
|
formatMessageForClientSide(message) {
|
||||||
if (message._id != null) {
|
if (message._id != null) {
|
||||||
message.id = message._id.toString();
|
message.id = message._id.toString()
|
||||||
delete message._id;
|
delete message._id
|
||||||
}
|
}
|
||||||
const formattedMessage = {
|
const formattedMessage = {
|
||||||
id: message.id,
|
id: message.id,
|
||||||
content: message.content,
|
content: message.content,
|
||||||
timestamp: message.timestamp,
|
timestamp: message.timestamp,
|
||||||
user_id: message.user_id
|
user_id: message.user_id
|
||||||
};
|
}
|
||||||
if (message.edited_at != null) {
|
if (message.edited_at != null) {
|
||||||
formattedMessage.edited_at = message.edited_at;
|
formattedMessage.edited_at = message.edited_at
|
||||||
}
|
}
|
||||||
return formattedMessage;
|
return formattedMessage
|
||||||
},
|
},
|
||||||
|
|
||||||
formatMessagesForClientSide(messages) {
|
formatMessagesForClientSide(messages) {
|
||||||
return (Array.from(messages).map((message) => this.formatMessageForClientSide(message)));
|
return Array.from(messages).map(message =>
|
||||||
},
|
this.formatMessageForClientSide(message)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
groupMessagesByThreads(rooms, messages) {
|
groupMessagesByThreads(rooms, messages) {
|
||||||
let room, thread;
|
let room, thread
|
||||||
const rooms_by_id = {};
|
const rooms_by_id = {}
|
||||||
for (room of Array.from(rooms)) {
|
for (room of Array.from(rooms)) {
|
||||||
rooms_by_id[room._id.toString()] = room;
|
rooms_by_id[room._id.toString()] = room
|
||||||
}
|
}
|
||||||
|
|
||||||
const threads = {};
|
const threads = {}
|
||||||
const getThread = function(room) {
|
const getThread = function(room) {
|
||||||
const thread_id = room.thread_id.toString();
|
const thread_id = room.thread_id.toString()
|
||||||
if (threads[thread_id] != null) {
|
if (threads[thread_id] != null) {
|
||||||
return threads[thread_id];
|
return threads[thread_id]
|
||||||
} else {
|
} else {
|
||||||
const thread = { messages: [] };
|
const thread = { messages: [] }
|
||||||
if (room.resolved != null) {
|
if (room.resolved != null) {
|
||||||
thread.resolved = true;
|
thread.resolved = true
|
||||||
thread.resolved_at = room.resolved.ts;
|
thread.resolved_at = room.resolved.ts
|
||||||
thread.resolved_by_user_id = room.resolved.user_id;
|
thread.resolved_by_user_id = room.resolved.user_id
|
||||||
}
|
}
|
||||||
threads[thread_id] = thread;
|
threads[thread_id] = thread
|
||||||
return thread;
|
return thread
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
for (let message of Array.from(messages)) {
|
for (let message of Array.from(messages)) {
|
||||||
room = rooms_by_id[message.room_id.toString()];
|
room = rooms_by_id[message.room_id.toString()]
|
||||||
if (room != null) {
|
if (room != null) {
|
||||||
thread = getThread(room);
|
thread = getThread(room)
|
||||||
thread.messages.push(MessageFormatter.formatMessageForClientSide(message));
|
thread.messages.push(
|
||||||
}
|
MessageFormatter.formatMessageForClientSide(message)
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (let thread_id in threads) {
|
for (let thread_id in threads) {
|
||||||
thread = threads[thread_id];
|
thread = threads[thread_id]
|
||||||
thread.messages.sort((a,b) => a.timestamp - b.timestamp);
|
thread.messages.sort((a, b) => a.timestamp - b.timestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
return threads;
|
return threads
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
|
@ -11,148 +11,248 @@
|
||||||
* DS207: Consider shorter variations of null checks
|
* DS207: Consider shorter variations of null checks
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||||
*/
|
*/
|
||||||
let MessageHttpController;
|
let MessageHttpController
|
||||||
const logger = require("logger-sharelatex");
|
const logger = require('logger-sharelatex')
|
||||||
const metrics = require("metrics-sharelatex");
|
const metrics = require('metrics-sharelatex')
|
||||||
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("../../mongojs");
|
const { ObjectId } = require('../../mongojs')
|
||||||
|
|
||||||
module.exports = (MessageHttpController = {
|
module.exports = MessageHttpController = {
|
||||||
DEFAULT_MESSAGE_LIMIT: 50,
|
DEFAULT_MESSAGE_LIMIT: 50,
|
||||||
MAX_MESSAGE_LENGTH: 10 * 1024, // 10kb, about 1,500 words
|
MAX_MESSAGE_LENGTH: 10 * 1024, // 10kb, about 1,500 words
|
||||||
|
|
||||||
getGlobalMessages(req, res, next) {
|
getGlobalMessages(req, res, next) {
|
||||||
return MessageHttpController._getMessages(ThreadManager.GLOBAL_THREAD, req, res, next);
|
return MessageHttpController._getMessages(
|
||||||
},
|
ThreadManager.GLOBAL_THREAD,
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
next
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
sendGlobalMessage(req, res, next) {
|
sendGlobalMessage(req, res, next) {
|
||||||
return MessageHttpController._sendMessage(ThreadManager.GLOBAL_THREAD, req, res, next);
|
return MessageHttpController._sendMessage(
|
||||||
},
|
ThreadManager.GLOBAL_THREAD,
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
next
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
sendThreadMessage(req, res, next) {
|
sendThreadMessage(req, res, next) {
|
||||||
return MessageHttpController._sendMessage(req.params.thread_id, req, res, next);
|
return MessageHttpController._sendMessage(
|
||||||
},
|
req.params.thread_id,
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
next
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
getAllThreads(req, res, next) {
|
getAllThreads(req, res, next) {
|
||||||
const {project_id} = req.params;
|
const { project_id } = req.params
|
||||||
logger.log({project_id}, "getting all threads");
|
logger.log({ project_id }, 'getting all threads')
|
||||||
return ThreadManager.findAllThreadRooms(project_id, function(error, rooms) {
|
return ThreadManager.findAllThreadRooms(project_id, function(error, rooms) {
|
||||||
if (error != null) { return next(error); }
|
if (error != null) {
|
||||||
const room_ids = rooms.map(r => r._id);
|
return next(error)
|
||||||
return MessageManager.findAllMessagesInRooms(room_ids, function(error, messages) {
|
}
|
||||||
if (error != null) { return next(error); }
|
const room_ids = rooms.map(r => r._id)
|
||||||
const threads = MessageFormatter.groupMessagesByThreads(rooms, messages);
|
return MessageManager.findAllMessagesInRooms(room_ids, function(
|
||||||
return res.json(threads);
|
error,
|
||||||
});
|
messages
|
||||||
});
|
) {
|
||||||
},
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
const threads = MessageFormatter.groupMessagesByThreads(rooms, messages)
|
||||||
|
return res.json(threads)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
resolveThread(req, res, next) {
|
resolveThread(req, res, next) {
|
||||||
const {project_id, thread_id} = req.params;
|
const { project_id, thread_id } = req.params
|
||||||
const {user_id} = req.body;
|
const { user_id } = req.body
|
||||||
logger.log({user_id, project_id, thread_id}, "marking thread as resolved");
|
logger.log({ user_id, project_id, thread_id }, 'marking thread as resolved')
|
||||||
return ThreadManager.resolveThread(project_id, thread_id, user_id, function(error) {
|
return ThreadManager.resolveThread(project_id, thread_id, user_id, function(
|
||||||
if (error != null) { return next(error); }
|
error
|
||||||
return res.send(204);
|
) {
|
||||||
});
|
if (error != null) {
|
||||||
}, // No content
|
return next(error)
|
||||||
|
}
|
||||||
|
return res.send(204)
|
||||||
|
})
|
||||||
|
}, // No content
|
||||||
|
|
||||||
reopenThread(req, res, next) {
|
reopenThread(req, res, next) {
|
||||||
const {project_id, thread_id} = req.params;
|
const { project_id, thread_id } = req.params
|
||||||
logger.log({project_id, thread_id}, "reopening thread");
|
logger.log({ project_id, thread_id }, 'reopening thread')
|
||||||
return ThreadManager.reopenThread(project_id, thread_id, function(error) {
|
return ThreadManager.reopenThread(project_id, thread_id, function(error) {
|
||||||
if (error != null) { return next(error); }
|
if (error != null) {
|
||||||
return res.send(204);
|
return next(error)
|
||||||
});
|
}
|
||||||
}, // No content
|
return res.send(204)
|
||||||
|
})
|
||||||
|
}, // No content
|
||||||
|
|
||||||
deleteThread(req, res, next) {
|
deleteThread(req, res, next) {
|
||||||
const {project_id, thread_id} = req.params;
|
const { project_id, thread_id } = req.params
|
||||||
logger.log({project_id, thread_id}, "deleting thread");
|
logger.log({ project_id, thread_id }, 'deleting thread')
|
||||||
return ThreadManager.deleteThread(project_id, thread_id, function(error, room_id) {
|
return ThreadManager.deleteThread(project_id, thread_id, function(
|
||||||
if (error != null) { return next(error); }
|
error,
|
||||||
return MessageManager.deleteAllMessagesInRoom(room_id, function(error) {
|
room_id
|
||||||
if (error != null) { return next(error); }
|
) {
|
||||||
return res.send(204);
|
if (error != null) {
|
||||||
});
|
return next(error)
|
||||||
});
|
}
|
||||||
}, // No content
|
return MessageManager.deleteAllMessagesInRoom(room_id, function(error) {
|
||||||
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
return res.send(204)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}, // No content
|
||||||
|
|
||||||
editMessage(req, res, next) {
|
editMessage(req, res, next) {
|
||||||
const {content} = req != null ? req.body : undefined;
|
const { content } = req != null ? req.body : undefined
|
||||||
const {project_id, thread_id, message_id} = req.params;
|
const { project_id, thread_id, message_id } = req.params
|
||||||
logger.log({project_id, thread_id, message_id, content}, "editing message");
|
logger.log(
|
||||||
return ThreadManager.findOrCreateThread(project_id, thread_id, function(error, room) {
|
{ project_id, thread_id, message_id, content },
|
||||||
if (error != null) { return next(error); }
|
'editing message'
|
||||||
return MessageManager.updateMessage(room._id, message_id, content, Date.now(), function(error) {
|
)
|
||||||
if (error != null) { return next(error); }
|
return ThreadManager.findOrCreateThread(project_id, thread_id, function(
|
||||||
return res.send(204);
|
error,
|
||||||
});
|
room
|
||||||
});
|
) {
|
||||||
},
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
return MessageManager.updateMessage(
|
||||||
|
room._id,
|
||||||
|
message_id,
|
||||||
|
content,
|
||||||
|
Date.now(),
|
||||||
|
function(error) {
|
||||||
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
return res.send(204)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
deleteMessage(req, res, next) {
|
deleteMessage(req, res, next) {
|
||||||
const {project_id, thread_id, message_id} = req.params;
|
const { project_id, thread_id, message_id } = req.params
|
||||||
logger.log({project_id, thread_id, message_id}, "deleting message");
|
logger.log({ project_id, thread_id, message_id }, 'deleting message')
|
||||||
return ThreadManager.findOrCreateThread(project_id, thread_id, function(error, room) {
|
return ThreadManager.findOrCreateThread(project_id, thread_id, function(
|
||||||
if (error != null) { return next(error); }
|
error,
|
||||||
return MessageManager.deleteMessage(room._id, message_id, function(error, message) {
|
room
|
||||||
if (error != null) { return next(error); }
|
) {
|
||||||
return res.send(204);
|
if (error != null) {
|
||||||
});
|
return next(error)
|
||||||
});
|
}
|
||||||
},
|
return MessageManager.deleteMessage(room._id, message_id, function(
|
||||||
|
error,
|
||||||
|
message
|
||||||
|
) {
|
||||||
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
return res.send(204)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
_sendMessage(client_thread_id, req, res, next) {
|
_sendMessage(client_thread_id, req, res, next) {
|
||||||
const {user_id, content} = req != null ? req.body : undefined;
|
const { user_id, content } = req != null ? req.body : undefined
|
||||||
const {project_id} = req.params;
|
const { project_id } = req.params
|
||||||
if (!ObjectId.isValid(user_id)) {
|
if (!ObjectId.isValid(user_id)) {
|
||||||
return res.send(400, "Invalid user_id");
|
return res.send(400, 'Invalid user_id')
|
||||||
}
|
}
|
||||||
if ((content == null)) {
|
if (content == null) {
|
||||||
return res.send(400, "No content provided");
|
return res.send(400, 'No content provided')
|
||||||
}
|
}
|
||||||
if (content.length > this.MAX_MESSAGE_LENGTH) {
|
if (content.length > this.MAX_MESSAGE_LENGTH) {
|
||||||
return res.send(400, `Content too long (> ${this.MAX_MESSAGE_LENGTH} bytes)`);
|
return res.send(
|
||||||
}
|
400,
|
||||||
logger.log({client_thread_id, project_id, user_id, content}, "new message received");
|
`Content too long (> ${this.MAX_MESSAGE_LENGTH} bytes)`
|
||||||
return ThreadManager.findOrCreateThread(project_id, client_thread_id, function(error, thread) {
|
)
|
||||||
if (error != null) { return next(error); }
|
}
|
||||||
return MessageManager.createMessage(thread._id, user_id, content, Date.now(), function(error, message) {
|
logger.log(
|
||||||
if (error != null) { return next(error); }
|
{ client_thread_id, project_id, user_id, content },
|
||||||
message = MessageFormatter.formatMessageForClientSide(message);
|
'new message received'
|
||||||
message.room_id = project_id;
|
)
|
||||||
return res.send(201, message);
|
return ThreadManager.findOrCreateThread(
|
||||||
});
|
project_id,
|
||||||
});
|
client_thread_id,
|
||||||
},
|
function(error, thread) {
|
||||||
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
return MessageManager.createMessage(
|
||||||
|
thread._id,
|
||||||
|
user_id,
|
||||||
|
content,
|
||||||
|
Date.now(),
|
||||||
|
function(error, message) {
|
||||||
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
message = MessageFormatter.formatMessageForClientSide(message)
|
||||||
|
message.room_id = project_id
|
||||||
|
return res.send(201, message)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
_getMessages(client_thread_id, req, res, next) {
|
_getMessages(client_thread_id, req, res, next) {
|
||||||
let before, limit;
|
let before, limit
|
||||||
const {project_id} = req.params;
|
const { project_id } = req.params
|
||||||
if ((req.query != null ? req.query.before : undefined) != null) {
|
if ((req.query != null ? req.query.before : undefined) != null) {
|
||||||
before = parseInt(req.query.before, 10);
|
before = parseInt(req.query.before, 10)
|
||||||
} else {
|
} else {
|
||||||
before = null;
|
before = null
|
||||||
}
|
}
|
||||||
if ((req.query != null ? req.query.limit : undefined) != null) {
|
if ((req.query != null ? req.query.limit : undefined) != null) {
|
||||||
limit = parseInt(req.query.limit, 10);
|
limit = parseInt(req.query.limit, 10)
|
||||||
} else {
|
} else {
|
||||||
limit = MessageHttpController.DEFAULT_MESSAGE_LIMIT;
|
limit = MessageHttpController.DEFAULT_MESSAGE_LIMIT
|
||||||
}
|
}
|
||||||
logger.log({limit, before, project_id, client_thread_id}, "get message request received");
|
logger.log(
|
||||||
return ThreadManager.findOrCreateThread(project_id, client_thread_id, function(error, thread) {
|
{ limit, before, project_id, client_thread_id },
|
||||||
if (error != null) { return next(error); }
|
'get message request received'
|
||||||
const thread_object_id = thread._id;
|
)
|
||||||
logger.log({limit, before, project_id, client_thread_id, thread_object_id}, "found or created thread");
|
return ThreadManager.findOrCreateThread(
|
||||||
return MessageManager.getMessages(thread_object_id, limit, before, function(error, messages) {
|
project_id,
|
||||||
if (error != null) { return next(error); }
|
client_thread_id,
|
||||||
messages = MessageFormatter.formatMessagesForClientSide(messages);
|
function(error, thread) {
|
||||||
logger.log({project_id, messages}, "got messages");
|
if (error != null) {
|
||||||
return res.send(200, messages);
|
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,
|
||||||
|
function(error, messages) {
|
||||||
|
if (error != null) {
|
||||||
|
return next(error)
|
||||||
|
}
|
||||||
|
messages = MessageFormatter.formatMessagesForClientSide(messages)
|
||||||
|
logger.log({ project_id, messages }, 'got messages')
|
||||||
|
return res.send(200, messages)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -12,101 +12,135 @@
|
||||||
* DS207: Consider shorter variations of null checks
|
* DS207: Consider shorter variations of null checks
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||||
*/
|
*/
|
||||||
let MessageManager;
|
let MessageManager
|
||||||
const mongojs = require("../../mongojs");
|
const mongojs = require('../../mongojs')
|
||||||
const { db } = mongojs;
|
const { db } = mongojs
|
||||||
const { ObjectId } = mongojs;
|
const { ObjectId } = mongojs
|
||||||
const async = require("async");
|
const async = require('async')
|
||||||
const metrics = require('metrics-sharelatex');
|
const metrics = require('metrics-sharelatex')
|
||||||
const logger = require('logger-sharelatex');
|
const logger = require('logger-sharelatex')
|
||||||
|
|
||||||
module.exports = (MessageManager = {
|
module.exports = MessageManager = {
|
||||||
createMessage(room_id, user_id, content, timestamp, callback) {
|
createMessage(room_id, user_id, content, timestamp, callback) {
|
||||||
if (callback == null) { callback = function(error, message) {}; }
|
if (callback == null) {
|
||||||
let newMessageOpts = {
|
callback = function(error, message) {}
|
||||||
content,
|
}
|
||||||
room_id,
|
let newMessageOpts = {
|
||||||
user_id,
|
content,
|
||||||
timestamp
|
room_id,
|
||||||
};
|
user_id,
|
||||||
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts);
|
timestamp
|
||||||
return db.messages.save(newMessageOpts, callback);
|
}
|
||||||
},
|
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts)
|
||||||
|
return db.messages.save(newMessageOpts, callback)
|
||||||
|
},
|
||||||
|
|
||||||
getMessages(room_id, limit, before, callback) {
|
getMessages(room_id, limit, before, callback) {
|
||||||
if (callback == null) { callback = function(error, messages) {}; }
|
if (callback == null) {
|
||||||
let query =
|
callback = function(error, messages) {}
|
||||||
{room_id};
|
}
|
||||||
if (before != null) {
|
let query = { room_id }
|
||||||
query.timestamp = { $lt: before };
|
if (before != null) {
|
||||||
}
|
query.timestamp = { $lt: before }
|
||||||
query = this._ensureIdsAreObjectIds(query);
|
}
|
||||||
const cursor = db.messages.find(query).sort({ timestamp: -1 }).limit(limit);
|
query = this._ensureIdsAreObjectIds(query)
|
||||||
return cursor.toArray(callback);
|
const cursor = db.messages
|
||||||
},
|
.find(query)
|
||||||
|
.sort({ timestamp: -1 })
|
||||||
|
.limit(limit)
|
||||||
|
return cursor.toArray(callback)
|
||||||
|
},
|
||||||
|
|
||||||
findAllMessagesInRooms(room_ids, callback) {
|
findAllMessagesInRooms(room_ids, callback) {
|
||||||
if (callback == null) { callback = function(error, messages) {}; }
|
if (callback == null) {
|
||||||
return db.messages.find({
|
callback = function(error, messages) {}
|
||||||
room_id: { $in: room_ids }
|
}
|
||||||
}, callback);
|
return db.messages.find(
|
||||||
},
|
{
|
||||||
|
room_id: { $in: room_ids }
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
deleteAllMessagesInRoom(room_id, callback) {
|
deleteAllMessagesInRoom(room_id, callback) {
|
||||||
if (callback == null) { callback = function(error) {}; }
|
if (callback == null) {
|
||||||
return db.messages.remove({
|
callback = function(error) {}
|
||||||
room_id
|
}
|
||||||
}, callback);
|
return db.messages.remove(
|
||||||
},
|
{
|
||||||
|
room_id
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
updateMessage(room_id, message_id, content, timestamp, callback) {
|
updateMessage(room_id, message_id, content, timestamp, callback) {
|
||||||
if (callback == null) { callback = function(error, message) {}; }
|
if (callback == null) {
|
||||||
const query = this._ensureIdsAreObjectIds({
|
callback = function(error, message) {}
|
||||||
_id: message_id,
|
}
|
||||||
room_id
|
const query = this._ensureIdsAreObjectIds({
|
||||||
});
|
_id: message_id,
|
||||||
return db.messages.update(query, {
|
room_id
|
||||||
$set: {
|
})
|
||||||
content,
|
return db.messages.update(
|
||||||
edited_at: timestamp
|
query,
|
||||||
}
|
{
|
||||||
}, function(error) {
|
$set: {
|
||||||
if (error != null) { return callback(error); }
|
content,
|
||||||
return callback();
|
edited_at: timestamp
|
||||||
});
|
}
|
||||||
},
|
},
|
||||||
|
function(error) {
|
||||||
|
if (error != null) {
|
||||||
|
return callback(error)
|
||||||
|
}
|
||||||
|
return callback()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
deleteMessage(room_id, message_id, callback) {
|
deleteMessage(room_id, message_id, callback) {
|
||||||
if (callback == null) { callback = function(error) {}; }
|
if (callback == null) {
|
||||||
const query = this._ensureIdsAreObjectIds({
|
callback = function(error) {}
|
||||||
_id: message_id,
|
}
|
||||||
room_id
|
const query = this._ensureIdsAreObjectIds({
|
||||||
});
|
_id: message_id,
|
||||||
return db.messages.remove(query, function(error) {
|
room_id
|
||||||
if (error != null) { return callback(error); }
|
})
|
||||||
return callback();
|
return db.messages.remove(query, function(error) {
|
||||||
});
|
if (error != null) {
|
||||||
},
|
return callback(error)
|
||||||
|
}
|
||||||
|
return callback()
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
_ensureIdsAreObjectIds(query) {
|
_ensureIdsAreObjectIds(query) {
|
||||||
if ((query.user_id != null) && !(query.user_id instanceof ObjectId)) {
|
if (query.user_id != null && !(query.user_id instanceof ObjectId)) {
|
||||||
query.user_id = ObjectId(query.user_id);
|
query.user_id = ObjectId(query.user_id)
|
||||||
}
|
}
|
||||||
if ((query.room_id != null) && !(query.room_id instanceof ObjectId)) {
|
if (query.room_id != null && !(query.room_id instanceof ObjectId)) {
|
||||||
query.room_id = ObjectId(query.room_id);
|
query.room_id = ObjectId(query.room_id)
|
||||||
}
|
}
|
||||||
if ((query._id != null) && !(query._id instanceof ObjectId)) {
|
if (query._id != null && !(query._id instanceof ObjectId)) {
|
||||||
query._id = ObjectId(query._id);
|
query._id = ObjectId(query._id)
|
||||||
}
|
}
|
||||||
return query;
|
return query
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
;[
|
||||||
[
|
'createMessage',
|
||||||
'createMessage',
|
'getMessages',
|
||||||
'getMessages',
|
'findAllMessagesInRooms',
|
||||||
'findAllMessagesInRooms',
|
'updateMessage',
|
||||||
'updateMessage',
|
'deleteMessage'
|
||||||
'deleteMessage'
|
].map(method =>
|
||||||
].map(method => metrics.timeAsyncMethod(MessageManager, method, 'mongo.MessageManager', logger));
|
metrics.timeAsyncMethod(
|
||||||
|
MessageManager,
|
||||||
|
method,
|
||||||
|
'mongo.MessageManager',
|
||||||
|
logger
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
|
@ -11,110 +11,149 @@
|
||||||
* DS207: Consider shorter variations of null checks
|
* DS207: Consider shorter variations of null checks
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||||
*/
|
*/
|
||||||
let ThreadManager;
|
let ThreadManager
|
||||||
const mongojs = require("../../mongojs");
|
const mongojs = require('../../mongojs')
|
||||||
const { db } = mongojs;
|
const { db } = mongojs
|
||||||
const { ObjectId } = mongojs;
|
const { ObjectId } = mongojs
|
||||||
const logger = require('logger-sharelatex');
|
const logger = require('logger-sharelatex')
|
||||||
const metrics = require('metrics-sharelatex');
|
const metrics = require('metrics-sharelatex')
|
||||||
|
|
||||||
module.exports = (ThreadManager = {
|
module.exports = ThreadManager = {
|
||||||
GLOBAL_THREAD: "GLOBAL",
|
GLOBAL_THREAD: 'GLOBAL',
|
||||||
|
|
||||||
findOrCreateThread(project_id, thread_id, callback) {
|
findOrCreateThread(project_id, thread_id, callback) {
|
||||||
let query, update;
|
let query, update
|
||||||
if (callback == null) { callback = function(error, thread) {}; }
|
if (callback == null) {
|
||||||
project_id = ObjectId(project_id.toString());
|
callback = function(error, thread) {}
|
||||||
if (thread_id !== ThreadManager.GLOBAL_THREAD) {
|
}
|
||||||
thread_id = ObjectId(thread_id.toString());
|
project_id = ObjectId(project_id.toString())
|
||||||
}
|
if (thread_id !== ThreadManager.GLOBAL_THREAD) {
|
||||||
|
thread_id = ObjectId(thread_id.toString())
|
||||||
|
}
|
||||||
|
|
||||||
if (thread_id === ThreadManager.GLOBAL_THREAD) {
|
if (thread_id === ThreadManager.GLOBAL_THREAD) {
|
||||||
query = {
|
query = {
|
||||||
project_id,
|
project_id,
|
||||||
thread_id: { $exists: false }
|
thread_id: { $exists: false }
|
||||||
};
|
}
|
||||||
update = {
|
update = {
|
||||||
project_id
|
project_id
|
||||||
};
|
}
|
||||||
} else {
|
} else {
|
||||||
query = {
|
query = {
|
||||||
project_id,
|
project_id,
|
||||||
thread_id
|
thread_id
|
||||||
};
|
}
|
||||||
update = {
|
update = {
|
||||||
project_id,
|
project_id,
|
||||||
thread_id
|
thread_id
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.rooms.update(query, update, { upsert: true }, function(error) {
|
return db.rooms.update(query, update, { upsert: true }, function(error) {
|
||||||
if (error != null) { return callback(error); }
|
if (error != null) {
|
||||||
return db.rooms.find(query, function(error, rooms) {
|
return callback(error)
|
||||||
if (rooms == null) { rooms = []; }
|
}
|
||||||
if (error != null) { return callback(error); }
|
return db.rooms.find(query, function(error, rooms) {
|
||||||
return callback(null, rooms[0]);
|
if (rooms == null) {
|
||||||
});
|
rooms = []
|
||||||
});
|
}
|
||||||
},
|
if (error != null) {
|
||||||
|
return callback(error)
|
||||||
|
}
|
||||||
|
return callback(null, rooms[0])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
findAllThreadRooms(project_id, callback) {
|
findAllThreadRooms(project_id, callback) {
|
||||||
if (callback == null) { callback = function(error, rooms) {}; }
|
if (callback == null) {
|
||||||
return db.rooms.find({
|
callback = function(error, rooms) {}
|
||||||
project_id: ObjectId(project_id.toString()),
|
}
|
||||||
thread_id: { $exists: true }
|
return db.rooms.find(
|
||||||
}, {
|
{
|
||||||
thread_id: 1,
|
project_id: ObjectId(project_id.toString()),
|
||||||
resolved: 1
|
thread_id: { $exists: true }
|
||||||
}, callback);
|
},
|
||||||
},
|
{
|
||||||
|
thread_id: 1,
|
||||||
|
resolved: 1
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
resolveThread(project_id, thread_id, user_id, callback) {
|
resolveThread(project_id, thread_id, user_id, callback) {
|
||||||
if (callback == null) { callback = function(error) {}; }
|
if (callback == null) {
|
||||||
return db.rooms.update({
|
callback = function(error) {}
|
||||||
project_id: ObjectId(project_id.toString()),
|
}
|
||||||
thread_id: ObjectId(thread_id.toString())
|
return db.rooms.update(
|
||||||
}, {
|
{
|
||||||
$set: {
|
project_id: ObjectId(project_id.toString()),
|
||||||
resolved: {
|
thread_id: ObjectId(thread_id.toString())
|
||||||
user_id,
|
},
|
||||||
ts: new Date()
|
{
|
||||||
}
|
$set: {
|
||||||
}
|
resolved: {
|
||||||
}, callback);
|
user_id,
|
||||||
},
|
ts: new Date()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
reopenThread(project_id, thread_id, callback) {
|
reopenThread(project_id, thread_id, callback) {
|
||||||
if (callback == null) { callback = function(error) {}; }
|
if (callback == null) {
|
||||||
return db.rooms.update({
|
callback = function(error) {}
|
||||||
project_id: ObjectId(project_id.toString()),
|
}
|
||||||
thread_id: ObjectId(thread_id.toString())
|
return db.rooms.update(
|
||||||
}, {
|
{
|
||||||
$unset: {
|
project_id: ObjectId(project_id.toString()),
|
||||||
resolved: true
|
thread_id: ObjectId(thread_id.toString())
|
||||||
}
|
},
|
||||||
}, callback);
|
{
|
||||||
},
|
$unset: {
|
||||||
|
resolved: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|
||||||
deleteThread(project_id, thread_id, callback) {
|
deleteThread(project_id, thread_id, callback) {
|
||||||
if (callback == null) { callback = function(error, room_id) {}; }
|
if (callback == null) {
|
||||||
return this.findOrCreateThread(project_id, thread_id, function(error, room) {
|
callback = function(error, room_id) {}
|
||||||
if (error != null) { return callback(error); }
|
}
|
||||||
return db.rooms.remove({
|
return this.findOrCreateThread(project_id, thread_id, function(
|
||||||
_id: room._id
|
error,
|
||||||
}, function(error) {
|
room
|
||||||
if (error != null) { return callback(error); }
|
) {
|
||||||
return callback(null, room._id);
|
if (error != null) {
|
||||||
});
|
return callback(error)
|
||||||
});
|
}
|
||||||
}
|
return db.rooms.remove(
|
||||||
});
|
{
|
||||||
|
_id: room._id
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
if (error != null) {
|
||||||
|
return callback(error)
|
||||||
|
}
|
||||||
|
return callback(null, room._id)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
;[
|
||||||
[
|
'findOrCreateThread',
|
||||||
'findOrCreateThread',
|
'findAllThreadRooms',
|
||||||
'findAllThreadRooms',
|
'resolveThread',
|
||||||
'resolveThread',
|
'reopenThread',
|
||||||
'reopenThread',
|
'deleteThread'
|
||||||
'deleteThread',
|
].map(method =>
|
||||||
].map(method => metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger));
|
metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
|
||||||
|
)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
// TODO: This file was created by bulk-decaffeinate.
|
// TODO: This file was created by bulk-decaffeinate.
|
||||||
// Sanity-check the conversion and remove this comment.
|
// Sanity-check the conversion and remove this comment.
|
||||||
const Settings = require("settings-sharelatex");
|
const Settings = require('settings-sharelatex')
|
||||||
const mongojs = require("mongojs");
|
const mongojs = require('mongojs')
|
||||||
const db = mongojs(Settings.mongo.url, ["rooms", "messages"]);
|
const db = mongojs(Settings.mongo.url, ['rooms', 'messages'])
|
||||||
module.exports = {
|
module.exports = {
|
||||||
db,
|
db,
|
||||||
ObjectId: mongojs.ObjectId
|
ObjectId: mongojs.ObjectId
|
||||||
};
|
}
|
||||||
|
|
|
@ -10,48 +10,75 @@
|
||||||
* DS102: Remove unnecessary code created because of implicit returns
|
* DS102: Remove unnecessary code created because of implicit returns
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||||
*/
|
*/
|
||||||
let Router;
|
let Router
|
||||||
const MessageHttpController = require('./Features/Messages/MessageHttpController');
|
const MessageHttpController = require('./Features/Messages/MessageHttpController')
|
||||||
const {ObjectId} = require("./mongojs");
|
const { ObjectId } = require('./mongojs')
|
||||||
|
|
||||||
module.exports = (Router = {
|
module.exports = Router = {
|
||||||
route(app) {
|
route(app) {
|
||||||
app.param('project_id', function(req, res, next, project_id) {
|
app.param('project_id', function(req, res, next, project_id) {
|
||||||
if (ObjectId.isValid(project_id)) {
|
if (ObjectId.isValid(project_id)) {
|
||||||
return next();
|
return next()
|
||||||
} else {
|
} else {
|
||||||
return res.send(400, "Invalid project_id");
|
return res.send(400, 'Invalid project_id')
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
app.param('thread_id', function(req, res, next, thread_id) {
|
app.param('thread_id', function(req, res, next, thread_id) {
|
||||||
if (ObjectId.isValid(thread_id)) {
|
if (ObjectId.isValid(thread_id)) {
|
||||||
return next();
|
return next()
|
||||||
} else {
|
} else {
|
||||||
return res.send(400, "Invalid thread_id");
|
return res.send(400, 'Invalid thread_id')
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
// These are for backwards compatibility
|
// These are for backwards compatibility
|
||||||
app.get("/room/:project_id/messages", MessageHttpController.getGlobalMessages);
|
app.get(
|
||||||
app.post("/room/:project_id/messages", MessageHttpController.sendGlobalMessage);
|
'/room/:project_id/messages',
|
||||||
|
MessageHttpController.getGlobalMessages
|
||||||
|
)
|
||||||
|
app.post(
|
||||||
|
'/room/:project_id/messages',
|
||||||
|
MessageHttpController.sendGlobalMessage
|
||||||
|
)
|
||||||
|
|
||||||
app.get("/project/:project_id/messages", MessageHttpController.getGlobalMessages);
|
app.get(
|
||||||
app.post("/project/:project_id/messages", MessageHttpController.sendGlobalMessage);
|
'/project/:project_id/messages',
|
||||||
|
MessageHttpController.getGlobalMessages
|
||||||
|
)
|
||||||
|
app.post(
|
||||||
|
'/project/:project_id/messages',
|
||||||
|
MessageHttpController.sendGlobalMessage
|
||||||
|
)
|
||||||
|
|
||||||
app.post("/project/:project_id/thread/:thread_id/messages", MessageHttpController.sendThreadMessage);
|
app.post(
|
||||||
app.get("/project/:project_id/threads", MessageHttpController.getAllThreads);
|
'/project/:project_id/thread/:thread_id/messages',
|
||||||
|
MessageHttpController.sendThreadMessage
|
||||||
app.post("/project/:project_id/thread/:thread_id/messages/:message_id/edit", MessageHttpController.editMessage);
|
)
|
||||||
app.del("/project/:project_id/thread/:thread_id/messages/:message_id", MessageHttpController.deleteMessage);
|
app.get('/project/:project_id/threads', MessageHttpController.getAllThreads)
|
||||||
|
|
||||||
app.post("/project/:project_id/thread/:thread_id/resolve", MessageHttpController.resolveThread);
|
|
||||||
app.post("/project/:project_id/thread/:thread_id/reopen", MessageHttpController.reopenThread);
|
|
||||||
app.del("/project/:project_id/thread/:thread_id", MessageHttpController.deleteThread);
|
|
||||||
|
|
||||||
return app.get("/status", (req, res, next) => res.send("chat is alive"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
app.post(
|
||||||
|
'/project/:project_id/thread/:thread_id/messages/:message_id/edit',
|
||||||
|
MessageHttpController.editMessage
|
||||||
|
)
|
||||||
|
app.del(
|
||||||
|
'/project/:project_id/thread/:thread_id/messages/:message_id',
|
||||||
|
MessageHttpController.deleteMessage
|
||||||
|
)
|
||||||
|
|
||||||
|
app.post(
|
||||||
|
'/project/:project_id/thread/:thread_id/resolve',
|
||||||
|
MessageHttpController.resolveThread
|
||||||
|
)
|
||||||
|
app.post(
|
||||||
|
'/project/:project_id/thread/:thread_id/reopen',
|
||||||
|
MessageHttpController.reopenThread
|
||||||
|
)
|
||||||
|
app.del(
|
||||||
|
'/project/:project_id/thread/:thread_id',
|
||||||
|
MessageHttpController.deleteThread
|
||||||
|
)
|
||||||
|
|
||||||
|
return app.get('/status', (req, res, next) => res.send('chat is alive'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,46 +8,43 @@
|
||||||
* DS102: Remove unnecessary code created because of implicit returns
|
* DS102: Remove unnecessary code created because of implicit returns
|
||||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||||
*/
|
*/
|
||||||
const logger = require('logger-sharelatex');
|
const logger = require('logger-sharelatex')
|
||||||
logger.initialize("chat-sharelatex");
|
logger.initialize('chat-sharelatex')
|
||||||
const metrics = require("metrics-sharelatex");
|
const metrics = require('metrics-sharelatex')
|
||||||
metrics.initialize("chat");
|
metrics.initialize('chat')
|
||||||
const Path = require("path");
|
const Path = require('path')
|
||||||
const express = require("express");
|
const express = require('express')
|
||||||
const app = express();
|
const app = express()
|
||||||
const server = require("http").createServer(app);
|
const server = require('http').createServer(app)
|
||||||
const Router = require("./router");
|
const Router = require('./router')
|
||||||
|
|
||||||
app.use(express.bodyParser());
|
app.use(express.bodyParser())
|
||||||
app.use(metrics.http.monitor(logger));
|
app.use(metrics.http.monitor(logger))
|
||||||
|
|
||||||
if ((app.get('env')) === 'development') {
|
if (app.get('env') === 'development') {
|
||||||
console.log("Development Enviroment");
|
console.log('Development Enviroment')
|
||||||
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
|
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((app.get('env')) === 'production') {
|
if (app.get('env') === 'production') {
|
||||||
console.log("Production Enviroment");
|
console.log('Production Enviroment')
|
||||||
app.use(express.logger());
|
app.use(express.logger())
|
||||||
app.use(express.errorHandler());
|
app.use(express.errorHandler())
|
||||||
}
|
}
|
||||||
|
|
||||||
const profiler = require("v8-profiler");
|
const profiler = require('v8-profiler')
|
||||||
app.get("/profile", function(req, res) {
|
app.get('/profile', function(req, res) {
|
||||||
const time = parseInt(req.query.time || "1000");
|
const time = parseInt(req.query.time || '1000')
|
||||||
profiler.startProfiling("test");
|
profiler.startProfiling('test')
|
||||||
return setTimeout(function() {
|
return setTimeout(function() {
|
||||||
const profile = profiler.stopProfiling("test");
|
const profile = profiler.stopProfiling('test')
|
||||||
return res.json(profile);
|
return res.json(profile)
|
||||||
}
|
}, time)
|
||||||
, time);
|
})
|
||||||
});
|
|
||||||
|
|
||||||
Router.route(app);
|
Router.route(app)
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
server,
|
server,
|
||||||
app
|
app
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue