[misc] run format_fix and lint:fix

This commit is contained in:
Jakob Ackermann 2021-07-13 12:04:48 +01:00
parent d6aee00057
commit 1b7e5e6206
12 changed files with 167 additions and 153 deletions

View file

@ -16,12 +16,12 @@ if (!module.parent) {
const port = const port =
__guard__( __guard__(
settings.internal != null ? settings.internal.chat : undefined, settings.internal != null ? settings.internal.chat : undefined,
(x) => x.port x => x.port
) || 3010 ) || 3010
const host = const host =
__guard__( __guard__(
settings.internal != null ? settings.internal.chat : undefined, settings.internal != null ? settings.internal.chat : undefined,
(x1) => x1.host x1 => x1.host
) || 'localhost' ) || 'localhost'
mongodb mongodb
.waitForDb() .waitForDb()
@ -34,7 +34,7 @@ if (!module.parent) {
return logger.info(`Chat starting up, listening on ${host}:${port}`) return logger.info(`Chat starting up, listening on ${host}:${port}`)
}) })
}) })
.catch((err) => { .catch(err => {
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.') logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
process.exit(1) process.exit(1)
}) })

View file

@ -22,7 +22,7 @@ module.exports = MessageFormatter = {
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
@ -31,7 +31,7 @@ module.exports = MessageFormatter = {
}, },
formatMessagesForClientSide(messages) { formatMessagesForClientSide(messages) {
return Array.from(messages).map((message) => return Array.from(messages).map(message =>
this.formatMessageForClientSide(message) this.formatMessageForClientSide(message)
) )
}, },
@ -76,5 +76,5 @@ module.exports = MessageFormatter = {
} }
return threads return threads
} },
} }

View file

@ -53,25 +53,28 @@ module.exports = MessageHttpController = {
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 ( return ThreadManager.findAllThreadRooms(
error, project_id,
rooms 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
) {
if (error != null) { if (error != null) {
return next(error) return next(error)
} }
const threads = MessageFormatter.groupMessagesByThreads(rooms, messages) const room_ids = rooms.map(r => r._id)
return res.json(threads) 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) { resolveThread(req, res, next) {
@ -105,20 +108,24 @@ module.exports = MessageHttpController = {
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 ( return ThreadManager.deleteThread(
error, project_id,
room_id thread_id,
) { function (error, room_id) {
if (error != null) {
return next(error)
}
return MessageManager.deleteAllMessagesInRoom(room_id, function (error) {
if (error != null) { if (error != null) {
return next(error) 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 }, // No content
editMessage(req, res, next) { editMessage(req, res, next) {
@ -128,48 +135,51 @@ module.exports = MessageHttpController = {
{ project_id, thread_id, message_id, content }, { project_id, thread_id, message_id, content },
'editing message' 'editing message'
) )
return ThreadManager.findOrCreateThread(project_id, thread_id, function ( return ThreadManager.findOrCreateThread(
error, project_id,
room thread_id,
) { function (error, room) {
if (error != null) { if (error != null) {
return next(error) 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 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) { 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 ( return ThreadManager.findOrCreateThread(
error, project_id,
room thread_id,
) { function (error, room) {
if (error != null) {
return next(error)
}
return MessageManager.deleteMessage(room._id, message_id, function (
error,
message
) {
if (error != null) { if (error != null) {
return next(error) 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) { _sendMessage(client_thread_id, req, res, next) {
@ -259,5 +269,5 @@ module.exports = MessageHttpController = {
) )
} }
) )
} },
} }

View file

@ -26,7 +26,7 @@ module.exports = MessageManager = {
content, content,
room_id, room_id,
user_id, user_id,
timestamp timestamp,
} }
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts) newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts)
db.messages.insertOne(newMessageOpts, function (error, confirmation) { db.messages.insertOne(newMessageOpts, function (error, confirmation) {
@ -60,7 +60,7 @@ module.exports = MessageManager = {
} }
db.messages db.messages
.find({ .find({
room_id: { $in: room_ids } room_id: { $in: room_ids },
}) })
.toArray(callback) .toArray(callback)
}, },
@ -71,7 +71,7 @@ module.exports = MessageManager = {
} }
db.messages.deleteMany( db.messages.deleteMany(
{ {
room_id room_id,
}, },
callback callback
) )
@ -83,15 +83,15 @@ module.exports = MessageManager = {
} }
const query = this._ensureIdsAreObjectIds({ const query = this._ensureIdsAreObjectIds({
_id: message_id, _id: message_id,
room_id room_id,
}) })
db.messages.updateOne( db.messages.updateOne(
query, query,
{ {
$set: { $set: {
content, content,
edited_at: timestamp edited_at: timestamp,
} },
}, },
callback callback
) )
@ -103,7 +103,7 @@ module.exports = MessageManager = {
} }
const query = this._ensureIdsAreObjectIds({ const query = this._ensureIdsAreObjectIds({
_id: message_id, _id: message_id,
room_id room_id,
}) })
db.messages.deleteOne(query, callback) db.messages.deleteOne(query, callback)
}, },
@ -119,15 +119,15 @@ module.exports = MessageManager = {
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( metrics.timeAsyncMethod(
MessageManager, MessageManager,
method, method,

View file

@ -32,30 +32,33 @@ module.exports = ThreadManager = {
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,
} }
} }
db.rooms.updateOne(query, { $set: update }, { upsert: true }, function ( db.rooms.updateOne(
error query,
) { { $set: update },
if (error != null) { { upsert: true },
return callback(error) function (error) {
if (error != null) {
return callback(error)
}
db.rooms.findOne(query, callback)
} }
db.rooms.findOne(query, callback) )
})
}, },
findAllThreadRooms(project_id, callback) { findAllThreadRooms(project_id, callback) {
@ -66,11 +69,11 @@ module.exports = ThreadManager = {
.find( .find(
{ {
project_id: ObjectId(project_id.toString()), project_id: ObjectId(project_id.toString()),
thread_id: { $exists: true } thread_id: { $exists: true },
}, },
{ {
thread_id: 1, thread_id: 1,
resolved: 1 resolved: 1,
} }
) )
.toArray(callback) .toArray(callback)
@ -83,15 +86,15 @@ module.exports = ThreadManager = {
db.rooms.updateOne( db.rooms.updateOne(
{ {
project_id: ObjectId(project_id.toString()), project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString()) thread_id: ObjectId(thread_id.toString()),
}, },
{ {
$set: { $set: {
resolved: { resolved: {
user_id, user_id,
ts: new Date() ts: new Date(),
} },
} },
}, },
callback callback
) )
@ -104,12 +107,12 @@ module.exports = ThreadManager = {
db.rooms.updateOne( db.rooms.updateOne(
{ {
project_id: ObjectId(project_id.toString()), project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString()) thread_id: ObjectId(thread_id.toString()),
}, },
{ {
$unset: { $unset: {
resolved: true resolved: true,
} },
}, },
callback callback
) )
@ -119,33 +122,34 @@ module.exports = ThreadManager = {
if (callback == null) { if (callback == null) {
callback = function (error, room_id) {} callback = function (error, room_id) {}
} }
return this.findOrCreateThread(project_id, thread_id, function ( return this.findOrCreateThread(
error, project_id,
room thread_id,
) { function (error, room) {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
}
db.rooms.deleteOne(
{
_id: room._id
},
function (error) {
if (error != null) {
return callback(error)
}
return callback(null, room._id)
} }
) db.rooms.deleteOne(
}) {
} _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)
) )

View file

@ -25,5 +25,5 @@ async function setupDb() {
module.exports = { module.exports = {
db, db,
ObjectId, ObjectId,
waitForDb waitForDb,
} }

View file

@ -80,5 +80,5 @@ module.exports = Router = {
) )
return app.get('/status', (req, res, next) => res.send('chat is alive')) return app.get('/status', (req, res, next) => res.send('chat is alive'))
} },
} }

View file

@ -37,5 +37,5 @@ Router.route(app)
module.exports = { module.exports = {
server, server,
app app,
} }

View file

@ -2,8 +2,8 @@ module.exports = {
internal: { internal: {
chat: { chat: {
host: process.env.LISTEN_ADDRESS || 'localhost', host: process.env.LISTEN_ADDRESS || 'localhost',
port: 3010 port: 3010,
} },
}, },
apis: { apis: {
@ -12,17 +12,17 @@ module.exports = {
process.env.WEB_PORT || 3000 process.env.WEB_PORT || 3000
}`, }`,
user: process.env.WEB_API_USER || 'sharelatex', user: process.env.WEB_API_USER || 'sharelatex',
pass: process.env.WEB_API_PASSWORD || 'password' pass: process.env.WEB_API_PASSWORD || 'password',
} },
}, },
mongo: { mongo: {
options: { options: {
useUnifiedTopology: useUnifiedTopology:
(process.env.MONGO_USE_UNIFIED_TOPOLOGY || 'true') === 'true' (process.env.MONGO_USE_UNIFIED_TOPOLOGY || 'true') === 'true',
}, },
url: url:
process.env.MONGO_CONNECTION_STRING || process.env.MONGO_CONNECTION_STRING ||
`mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex` `mongodb://${process.env.MONGO_HOST || 'localhost'}/sharelatex`,
} },
} }

View file

@ -32,20 +32,20 @@ describe('Getting messages', function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
return async.series( return async.series(
[ [
(cb) => cb =>
ChatClient.sendGlobalMessage( ChatClient.sendGlobalMessage(
this.project_id, this.project_id,
this.user_id1, this.user_id1,
this.content1, this.content1,
cb cb
), ),
(cb) => cb =>
ChatClient.sendGlobalMessage( ChatClient.sendGlobalMessage(
this.project_id, this.project_id,
this.user_id2, this.user_id2,
this.content2, this.content2,
cb cb
) ),
], ],
done done
) )
@ -74,7 +74,7 @@ describe('Getting messages', function () {
this.thread_id2 = ObjectId().toString() this.thread_id2 = ObjectId().toString()
return async.series( return async.series(
[ [
(cb) => cb =>
ChatClient.sendMessage( ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id1, this.thread_id1,
@ -82,7 +82,7 @@ describe('Getting messages', function () {
'one', 'one',
cb cb
), ),
(cb) => cb =>
ChatClient.sendMessage( ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id2, this.thread_id2,
@ -90,7 +90,7 @@ describe('Getting messages', function () {
'two', 'two',
cb cb
), ),
(cb) => cb =>
ChatClient.sendMessage( ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id1, this.thread_id1,
@ -98,14 +98,14 @@ describe('Getting messages', function () {
'three', 'three',
cb cb
), ),
(cb) => cb =>
ChatClient.sendMessage( ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id2, this.thread_id2,
this.user_id2, this.user_id2,
'four', 'four',
cb cb
) ),
], ],
done done
) )

View file

@ -30,7 +30,7 @@ module.exports = {
this.initing = true this.initing = true
this.callbacks.push(callback) this.callbacks.push(callback)
waitForDb().then(() => { waitForDb().then(() => {
return app.listen(3010, 'localhost', (error) => { return app.listen(3010, 'localhost', error => {
if (error != null) { if (error != null) {
throw error throw error
} }
@ -44,5 +44,5 @@ module.exports = {
})() })()
}) })
}) })
} },
} }

View file

@ -10,7 +10,7 @@
* 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 request = require('request').defaults({ const request = require('request').defaults({
baseUrl: 'http://localhost:3010' baseUrl: 'http://localhost:3010',
}) })
module.exports = { module.exports = {
@ -20,8 +20,8 @@ module.exports = {
url: `/project/${project_id}/messages`, url: `/project/${project_id}/messages`,
json: { json: {
user_id, user_id,
content content,
} },
}, },
callback callback
) )
@ -31,7 +31,7 @@ module.exports = {
return request.get( return request.get(
{ {
url: `/project/${project_id}/messages`, url: `/project/${project_id}/messages`,
json: true json: true,
}, },
callback callback
) )
@ -43,8 +43,8 @@ module.exports = {
url: `/project/${project_id}/thread/${thread_id}/messages`, url: `/project/${project_id}/thread/${thread_id}/messages`,
json: { json: {
user_id, user_id,
content content,
} },
}, },
callback callback
) )
@ -54,7 +54,7 @@ module.exports = {
return request.get( return request.get(
{ {
url: `/project/${project_id}/threads`, url: `/project/${project_id}/threads`,
json: true json: true,
}, },
callback callback
) )
@ -65,8 +65,8 @@ module.exports = {
{ {
url: `/project/${project_id}/thread/${thread_id}/resolve`, url: `/project/${project_id}/thread/${thread_id}/resolve`,
json: { json: {
user_id user_id,
} },
}, },
callback callback
) )
@ -75,7 +75,7 @@ module.exports = {
reopenThread(project_id, thread_id, callback) { reopenThread(project_id, thread_id, callback) {
return request.post( return request.post(
{ {
url: `/project/${project_id}/thread/${thread_id}/reopen` url: `/project/${project_id}/thread/${thread_id}/reopen`,
}, },
callback callback
) )
@ -84,7 +84,7 @@ module.exports = {
deleteThread(project_id, thread_id, callback) { deleteThread(project_id, thread_id, callback) {
return request.del( return request.del(
{ {
url: `/project/${project_id}/thread/${thread_id}` url: `/project/${project_id}/thread/${thread_id}`,
}, },
callback callback
) )
@ -95,8 +95,8 @@ module.exports = {
{ {
url: `/project/${project_id}/thread/${thread_id}/messages/${message_id}/edit`, url: `/project/${project_id}/thread/${thread_id}/messages/${message_id}/edit`,
json: { json: {
content content,
} },
}, },
callback callback
) )
@ -105,9 +105,9 @@ module.exports = {
deleteMessage(project_id, thread_id, message_id, callback) { deleteMessage(project_id, thread_id, message_id, callback) {
return request.del( return request.del(
{ {
url: `/project/${project_id}/thread/${thread_id}/messages/${message_id}` url: `/project/${project_id}/thread/${thread_id}/messages/${message_id}`,
}, },
callback callback
) )
} },
} }