Prettier: convert app/js decaffeinated files to Prettier format

This commit is contained in:
Nate Stemen 2018-12-20 14:13:59 -05:00
parent 976d1675e6
commit 03093dc283
7 changed files with 671 additions and 470 deletions

View file

@ -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) {
formattedMessage.edited_at = message.edited_at;
} }
return formattedMessage; if (message.edited_at != null) {
formattedMessage.edited_at = message.edited_at
}
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
return thread
} }
threads[thread_id] = 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
} }
}); }

View file

@ -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) {
return next(error)
}
return res.send(204)
})
}, // No content }, // 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)
}); }
return res.send(204)
})
}, // No content }, // 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,
room_id
) {
if (error != null) {
return next(error)
}
return MessageManager.deleteAllMessagesInRoom(room_id, function(error) { return MessageManager.deleteAllMessagesInRoom(room_id, function(error) {
if (error != null) { return next(error); } if (error != null) {
return res.send(204); return next(error)
}); }
}); return res.send(204)
})
})
}, // No content }, // 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,
`Content too long (> ${this.MAX_MESSAGE_LENGTH} bytes)`
)
} }
logger.log({client_thread_id, project_id, user_id, content}, "new message received"); logger.log(
return ThreadManager.findOrCreateThread(project_id, client_thread_id, function(error, thread) { { client_thread_id, project_id, user_id, content },
if (error != null) { return next(error); } 'new message received'
return MessageManager.createMessage(thread._id, user_id, content, Date.now(), function(error, message) { )
if (error != null) { return next(error); } return ThreadManager.findOrCreateThread(
message = MessageFormatter.formatMessageForClientSide(message); project_id,
message.room_id = project_id; client_thread_id,
return res.send(201, message); 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)
}
)
}
)
}
}

View file

@ -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) {
callback = function(error, message) {}
}
let newMessageOpts = { let newMessageOpts = {
content, content,
room_id, room_id,
user_id, user_id,
timestamp timestamp
}; }
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts); newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts)
return db.messages.save(newMessageOpts, callback); 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) {
query.timestamp = { $lt: before };
} }
query = this._ensureIdsAreObjectIds(query); let query = { room_id }
const cursor = db.messages.find(query).sort({ timestamp: -1 }).limit(limit); if (before != null) {
return cursor.toArray(callback); query.timestamp = { $lt: before }
}
query = this._ensureIdsAreObjectIds(query)
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) {}
}
return db.messages.find(
{
room_id: { $in: room_ids } room_id: { $in: room_ids }
}, callback); },
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) {}
}
return db.messages.remove(
{
room_id room_id
}, callback); },
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) {
callback = function(error, message) {}
}
const query = this._ensureIdsAreObjectIds({ const query = this._ensureIdsAreObjectIds({
_id: message_id, _id: message_id,
room_id room_id
}); })
return db.messages.update(query, { return db.messages.update(
query,
{
$set: { $set: {
content, content,
edited_at: timestamp edited_at: timestamp
} }
}, function(error) { },
if (error != null) { return callback(error); } function(error) {
return callback(); 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) {
callback = function(error) {}
}
const query = this._ensureIdsAreObjectIds({ const query = this._ensureIdsAreObjectIds({
_id: message_id, _id: message_id,
room_id room_id
}); })
return db.messages.remove(query, function(error) { return db.messages.remove(query, function(error) {
if (error != null) { return callback(error); } if (error != null) {
return callback(); 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 => metrics.timeAsyncMethod(MessageManager, method, 'mongo.MessageManager', logger)); ].map(method =>
metrics.timeAsyncMethod(
MessageManager,
method,
'mongo.MessageManager',
logger
)
)

View file

@ -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) {}
}
project_id = ObjectId(project_id.toString())
if (thread_id !== ThreadManager.GLOBAL_THREAD) { if (thread_id !== ThreadManager.GLOBAL_THREAD) {
thread_id = ObjectId(thread_id.toString()); 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 callback(error)
}
return db.rooms.find(query, function(error, rooms) { return db.rooms.find(query, function(error, rooms) {
if (rooms == null) { rooms = []; } if (rooms == null) {
if (error != null) { return callback(error); } rooms = []
return callback(null, rooms[0]); }
}); 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) {}
}
return db.rooms.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
}, callback); },
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) {}
}
return db.rooms.update(
{
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
)
}, },
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) {}
}
return db.rooms.update(
{
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
)
}, },
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({
_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)
}
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 => metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)); ].map(method =>
metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
)

View file

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

View file

@ -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.get('/project/:project_id/threads', MessageHttpController.getAllThreads)
app.post("/project/:project_id/thread/:thread_id/messages/:message_id/edit", MessageHttpController.editMessage); app.post(
app.del("/project/:project_id/thread/:thread_id/messages/:message_id", MessageHttpController.deleteMessage); '/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(
app.post("/project/:project_id/thread/:thread_id/reopen", MessageHttpController.reopenThread); '/project/:project_id/thread/:thread_id/resolve',
app.del("/project/:project_id/thread/:thread_id", MessageHttpController.deleteThread); 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")); return app.get('/status', (req, res, next) => res.send('chat is alive'))
} }
}); }

View file

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