diff --git a/services/chat/app.js b/services/chat/app.js index 8be23ccd72..30300b840d 100644 --- a/services/chat/app.js +++ b/services/chat/app.js @@ -16,12 +16,12 @@ if (!module.parent) { const port = __guard__( settings.internal != null ? settings.internal.chat : undefined, - (x) => x.port + x => x.port ) || 3010 const host = __guard__( settings.internal != null ? settings.internal.chat : undefined, - (x1) => x1.host + x1 => x1.host ) || 'localhost' mongodb .waitForDb() @@ -34,7 +34,7 @@ if (!module.parent) { return logger.info(`Chat starting up, listening on ${host}:${port}`) }) }) - .catch((err) => { + .catch(err => { logger.fatal({ err }, 'Cannot connect to mongo. Exiting.') process.exit(1) }) diff --git a/services/chat/app/js/Features/Messages/MessageFormatter.js b/services/chat/app/js/Features/Messages/MessageFormatter.js index 7a685ea258..c12e9b7f8e 100644 --- a/services/chat/app/js/Features/Messages/MessageFormatter.js +++ b/services/chat/app/js/Features/Messages/MessageFormatter.js @@ -22,7 +22,7 @@ module.exports = MessageFormatter = { id: message.id, content: message.content, timestamp: message.timestamp, - user_id: message.user_id + user_id: message.user_id, } if (message.edited_at != null) { formattedMessage.edited_at = message.edited_at @@ -31,7 +31,7 @@ module.exports = MessageFormatter = { }, formatMessagesForClientSide(messages) { - return Array.from(messages).map((message) => + return Array.from(messages).map(message => this.formatMessageForClientSide(message) ) }, @@ -76,5 +76,5 @@ module.exports = MessageFormatter = { } return threads - } + }, } diff --git a/services/chat/app/js/Features/Messages/MessageHttpController.js b/services/chat/app/js/Features/Messages/MessageHttpController.js index d3b6eca7ac..26dd6f5738 100644 --- a/services/chat/app/js/Features/Messages/MessageHttpController.js +++ b/services/chat/app/js/Features/Messages/MessageHttpController.js @@ -53,25 +53,28 @@ module.exports = MessageHttpController = { getAllThreads(req, res, next) { const { project_id } = req.params logger.log({ project_id }, 'getting all threads') - return ThreadManager.findAllThreadRooms(project_id, function ( - error, - rooms - ) { - if (error != null) { - return next(error) - } - const room_ids = rooms.map((r) => r._id) - return MessageManager.findAllMessagesInRooms(room_ids, function ( - error, - messages - ) { + return ThreadManager.findAllThreadRooms( + project_id, + function (error, rooms) { if (error != null) { return next(error) } - const threads = MessageFormatter.groupMessagesByThreads(rooms, messages) - return res.json(threads) - }) - }) + 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) + } + ) + } + ) }, resolveThread(req, res, next) { @@ -105,20 +108,24 @@ module.exports = MessageHttpController = { deleteThread(req, res, next) { const { project_id, thread_id } = req.params logger.log({ project_id, thread_id }, 'deleting thread') - return ThreadManager.deleteThread(project_id, thread_id, function ( - error, - room_id - ) { - if (error != null) { - return next(error) - } - return MessageManager.deleteAllMessagesInRoom(room_id, function (error) { + return ThreadManager.deleteThread( + project_id, + thread_id, + function (error, room_id) { if (error != null) { return next(error) } - return res.sendStatus(204) - }) - }) + return MessageManager.deleteAllMessagesInRoom( + room_id, + function (error) { + if (error != null) { + return next(error) + } + return res.sendStatus(204) + } + ) + } + ) }, // No content editMessage(req, res, next) { @@ -128,48 +135,51 @@ module.exports = MessageHttpController = { { project_id, thread_id, message_id, content }, 'editing message' ) - return ThreadManager.findOrCreateThread(project_id, thread_id, function ( - 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.sendStatus(204) + return ThreadManager.findOrCreateThread( + project_id, + thread_id, + function (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.sendStatus(204) + } + ) + } + ) }, deleteMessage(req, res, next) { const { project_id, thread_id, message_id } = req.params logger.log({ project_id, thread_id, message_id }, 'deleting message') - return ThreadManager.findOrCreateThread(project_id, thread_id, function ( - error, - room - ) { - if (error != null) { - return next(error) - } - return MessageManager.deleteMessage(room._id, message_id, function ( - error, - message - ) { + return ThreadManager.findOrCreateThread( + project_id, + thread_id, + function (error, room) { if (error != null) { return next(error) } - return res.sendStatus(204) - }) - }) + return MessageManager.deleteMessage( + room._id, + message_id, + function (error, message) { + if (error != null) { + return next(error) + } + return res.sendStatus(204) + } + ) + } + ) }, _sendMessage(client_thread_id, req, res, next) { @@ -259,5 +269,5 @@ module.exports = MessageHttpController = { ) } ) - } + }, } diff --git a/services/chat/app/js/Features/Messages/MessageManager.js b/services/chat/app/js/Features/Messages/MessageManager.js index bc1afda0c2..7b73674af2 100644 --- a/services/chat/app/js/Features/Messages/MessageManager.js +++ b/services/chat/app/js/Features/Messages/MessageManager.js @@ -26,7 +26,7 @@ module.exports = MessageManager = { content, room_id, user_id, - timestamp + timestamp, } newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts) db.messages.insertOne(newMessageOpts, function (error, confirmation) { @@ -60,7 +60,7 @@ module.exports = MessageManager = { } db.messages .find({ - room_id: { $in: room_ids } + room_id: { $in: room_ids }, }) .toArray(callback) }, @@ -71,7 +71,7 @@ module.exports = MessageManager = { } db.messages.deleteMany( { - room_id + room_id, }, callback ) @@ -83,15 +83,15 @@ module.exports = MessageManager = { } const query = this._ensureIdsAreObjectIds({ _id: message_id, - room_id + room_id, }) db.messages.updateOne( query, { $set: { content, - edited_at: timestamp - } + edited_at: timestamp, + }, }, callback ) @@ -103,7 +103,7 @@ module.exports = MessageManager = { } const query = this._ensureIdsAreObjectIds({ _id: message_id, - room_id + room_id, }) db.messages.deleteOne(query, callback) }, @@ -119,15 +119,15 @@ module.exports = MessageManager = { query._id = ObjectId(query._id) } return query - } + }, } ;[ 'createMessage', 'getMessages', 'findAllMessagesInRooms', 'updateMessage', - 'deleteMessage' -].map((method) => + 'deleteMessage', +].map(method => metrics.timeAsyncMethod( MessageManager, method, diff --git a/services/chat/app/js/Features/Threads/ThreadManager.js b/services/chat/app/js/Features/Threads/ThreadManager.js index 6c02767a55..885ecb84c5 100644 --- a/services/chat/app/js/Features/Threads/ThreadManager.js +++ b/services/chat/app/js/Features/Threads/ThreadManager.js @@ -32,30 +32,33 @@ module.exports = ThreadManager = { if (thread_id === ThreadManager.GLOBAL_THREAD) { query = { project_id, - thread_id: { $exists: false } + thread_id: { $exists: false }, } update = { - project_id + project_id, } } else { query = { project_id, - thread_id + thread_id, } update = { project_id, - thread_id + thread_id, } } - db.rooms.updateOne(query, { $set: update }, { upsert: true }, function ( - error - ) { - if (error != null) { - return callback(error) + db.rooms.updateOne( + query, + { $set: update }, + { upsert: true }, + function (error) { + if (error != null) { + return callback(error) + } + db.rooms.findOne(query, callback) } - db.rooms.findOne(query, callback) - }) + ) }, findAllThreadRooms(project_id, callback) { @@ -66,11 +69,11 @@ module.exports = ThreadManager = { .find( { project_id: ObjectId(project_id.toString()), - thread_id: { $exists: true } + thread_id: { $exists: true }, }, { thread_id: 1, - resolved: 1 + resolved: 1, } ) .toArray(callback) @@ -83,15 +86,15 @@ module.exports = ThreadManager = { db.rooms.updateOne( { project_id: ObjectId(project_id.toString()), - thread_id: ObjectId(thread_id.toString()) + thread_id: ObjectId(thread_id.toString()), }, { $set: { resolved: { user_id, - ts: new Date() - } - } + ts: new Date(), + }, + }, }, callback ) @@ -104,12 +107,12 @@ module.exports = ThreadManager = { db.rooms.updateOne( { project_id: ObjectId(project_id.toString()), - thread_id: ObjectId(thread_id.toString()) + thread_id: ObjectId(thread_id.toString()), }, { $unset: { - resolved: true - } + resolved: true, + }, }, callback ) @@ -119,33 +122,34 @@ module.exports = ThreadManager = { if (callback == null) { callback = function (error, room_id) {} } - return this.findOrCreateThread(project_id, thread_id, function ( - error, - room - ) { - if (error != null) { - return callback(error) - } - db.rooms.deleteOne( - { - _id: room._id - }, - function (error) { - if (error != null) { - return callback(error) - } - return callback(null, room._id) + return this.findOrCreateThread( + project_id, + thread_id, + function (error, room) { + if (error != null) { + return callback(error) } - ) - }) - } + db.rooms.deleteOne( + { + _id: room._id, + }, + function (error) { + if (error != null) { + return callback(error) + } + return callback(null, room._id) + } + ) + } + ) + }, } ;[ 'findOrCreateThread', 'findAllThreadRooms', 'resolveThread', 'reopenThread', - 'deleteThread' -].map((method) => + 'deleteThread', +].map(method => metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger) ) diff --git a/services/chat/app/js/mongodb.js b/services/chat/app/js/mongodb.js index e634b66a96..1f68518df0 100644 --- a/services/chat/app/js/mongodb.js +++ b/services/chat/app/js/mongodb.js @@ -25,5 +25,5 @@ async function setupDb() { module.exports = { db, ObjectId, - waitForDb + waitForDb, } diff --git a/services/chat/app/js/router.js b/services/chat/app/js/router.js index ddce25e749..3ce8b3a3a3 100644 --- a/services/chat/app/js/router.js +++ b/services/chat/app/js/router.js @@ -80,5 +80,5 @@ module.exports = Router = { ) return app.get('/status', (req, res, next) => res.send('chat is alive')) - } + }, } diff --git a/services/chat/app/js/server.js b/services/chat/app/js/server.js index e83ee81ca1..131f5809b8 100644 --- a/services/chat/app/js/server.js +++ b/services/chat/app/js/server.js @@ -37,5 +37,5 @@ Router.route(app) module.exports = { server, - app + app, } diff --git a/services/chat/config/settings.defaults.js b/services/chat/config/settings.defaults.js index 8784445adf..553e9ea7fa 100644 --- a/services/chat/config/settings.defaults.js +++ b/services/chat/config/settings.defaults.js @@ -2,8 +2,8 @@ module.exports = { internal: { chat: { host: process.env.LISTEN_ADDRESS || 'localhost', - port: 3010 - } + port: 3010, + }, }, apis: { @@ -12,17 +12,17 @@ module.exports = { process.env.WEB_PORT || 3000 }`, user: process.env.WEB_API_USER || 'sharelatex', - pass: process.env.WEB_API_PASSWORD || 'password' - } + pass: process.env.WEB_API_PASSWORD || 'password', + }, }, mongo: { options: { useUnifiedTopology: - (process.env.MONGO_USE_UNIFIED_TOPOLOGY || 'true') === 'true' + (process.env.MONGO_USE_UNIFIED_TOPOLOGY || 'true') === 'true', }, url: process.env.MONGO_CONNECTION_STRING || - `mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex` - } + `mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex`, + }, } diff --git a/services/chat/test/acceptance/js/GettingMessagesTests.js b/services/chat/test/acceptance/js/GettingMessagesTests.js index cb881c34a5..1640852ca8 100644 --- a/services/chat/test/acceptance/js/GettingMessagesTests.js +++ b/services/chat/test/acceptance/js/GettingMessagesTests.js @@ -32,20 +32,20 @@ describe('Getting messages', function () { this.project_id = ObjectId().toString() return async.series( [ - (cb) => + cb => ChatClient.sendGlobalMessage( this.project_id, this.user_id1, this.content1, cb ), - (cb) => + cb => ChatClient.sendGlobalMessage( this.project_id, this.user_id2, this.content2, cb - ) + ), ], done ) @@ -74,7 +74,7 @@ describe('Getting messages', function () { this.thread_id2 = ObjectId().toString() return async.series( [ - (cb) => + cb => ChatClient.sendMessage( this.project_id, this.thread_id1, @@ -82,7 +82,7 @@ describe('Getting messages', function () { 'one', cb ), - (cb) => + cb => ChatClient.sendMessage( this.project_id, this.thread_id2, @@ -90,7 +90,7 @@ describe('Getting messages', function () { 'two', cb ), - (cb) => + cb => ChatClient.sendMessage( this.project_id, this.thread_id1, @@ -98,14 +98,14 @@ describe('Getting messages', function () { 'three', cb ), - (cb) => + cb => ChatClient.sendMessage( this.project_id, this.thread_id2, this.user_id2, 'four', cb - ) + ), ], done ) diff --git a/services/chat/test/acceptance/js/helpers/ChatApp.js b/services/chat/test/acceptance/js/helpers/ChatApp.js index ada969ac05..9b4f8f8a0e 100644 --- a/services/chat/test/acceptance/js/helpers/ChatApp.js +++ b/services/chat/test/acceptance/js/helpers/ChatApp.js @@ -30,7 +30,7 @@ module.exports = { this.initing = true this.callbacks.push(callback) waitForDb().then(() => { - return app.listen(3010, 'localhost', (error) => { + return app.listen(3010, 'localhost', error => { if (error != null) { throw error } @@ -44,5 +44,5 @@ module.exports = { })() }) }) - } + }, } diff --git a/services/chat/test/acceptance/js/helpers/ChatClient.js b/services/chat/test/acceptance/js/helpers/ChatClient.js index 932e6cc651..8b0ba333d1 100644 --- a/services/chat/test/acceptance/js/helpers/ChatClient.js +++ b/services/chat/test/acceptance/js/helpers/ChatClient.js @@ -10,7 +10,7 @@ * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ const request = require('request').defaults({ - baseUrl: 'http://localhost:3010' + baseUrl: 'http://localhost:3010', }) module.exports = { @@ -20,8 +20,8 @@ module.exports = { url: `/project/${project_id}/messages`, json: { user_id, - content - } + content, + }, }, callback ) @@ -31,7 +31,7 @@ module.exports = { return request.get( { url: `/project/${project_id}/messages`, - json: true + json: true, }, callback ) @@ -43,8 +43,8 @@ module.exports = { url: `/project/${project_id}/thread/${thread_id}/messages`, json: { user_id, - content - } + content, + }, }, callback ) @@ -54,7 +54,7 @@ module.exports = { return request.get( { url: `/project/${project_id}/threads`, - json: true + json: true, }, callback ) @@ -65,8 +65,8 @@ module.exports = { { url: `/project/${project_id}/thread/${thread_id}/resolve`, json: { - user_id - } + user_id, + }, }, callback ) @@ -75,7 +75,7 @@ module.exports = { reopenThread(project_id, thread_id, callback) { return request.post( { - url: `/project/${project_id}/thread/${thread_id}/reopen` + url: `/project/${project_id}/thread/${thread_id}/reopen`, }, callback ) @@ -84,7 +84,7 @@ module.exports = { deleteThread(project_id, thread_id, callback) { return request.del( { - url: `/project/${project_id}/thread/${thread_id}` + url: `/project/${project_id}/thread/${thread_id}`, }, callback ) @@ -95,8 +95,8 @@ module.exports = { { url: `/project/${project_id}/thread/${thread_id}/messages/${message_id}/edit`, json: { - content - } + content, + }, }, callback ) @@ -105,9 +105,9 @@ module.exports = { deleteMessage(project_id, thread_id, message_id, callback) { return request.del( { - url: `/project/${project_id}/thread/${thread_id}/messages/${message_id}` + url: `/project/${project_id}/thread/${thread_id}/messages/${message_id}`, }, callback ) - } + }, }