[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 =
__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)
})

View file

@ -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
}
},
}

View file

@ -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 = {
)
}
)
}
},
}

View file

@ -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,

View file

@ -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)
)

View file

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

View file

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

View file

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

View file

@ -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`,
},
}

View file

@ -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
)

View file

@ -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 = {
})()
})
})
}
},
}

View file

@ -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
)
}
},
}