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) { 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) { },
let room, thread;
const rooms_by_id = {};
for (room of Array.from(rooms)) {
rooms_by_id[room._id.toString()] = room;
}
const threads = {}; groupMessagesByThreads(rooms, messages) {
const getThread = function(room) { let room, thread
const thread_id = room.thread_id.toString(); const rooms_by_id = {}
if (threads[thread_id] != null) { for (room of Array.from(rooms)) {
return threads[thread_id]; rooms_by_id[room._id.toString()] = room
} else { }
const thread = { messages: [] };
if (room.resolved != null) { const threads = {}
thread.resolved = true; const getThread = function(room) {
thread.resolved_at = room.resolved.ts; const thread_id = room.thread_id.toString()
thread.resolved_by_user_id = room.resolved.user_id; if (threads[thread_id] != null) {
} return threads[thread_id]
threads[thread_id] = thread; } else {
return thread; const thread = { messages: [] }
} if (room.resolved != null) {
}; thread.resolved = true
thread.resolved_at = room.resolved.ts
for (let message of Array.from(messages)) { thread.resolved_by_user_id = room.resolved.user_id
room = rooms_by_id[message.room_id.toString()]; }
if (room != null) { threads[thread_id] = thread
thread = getThread(room); return thread
thread.messages.push(MessageFormatter.formatMessageForClientSide(message)); }
} }
}
for (let message of Array.from(messages)) {
for (let thread_id in threads) { room = rooms_by_id[message.room_id.toString()]
thread = threads[thread_id]; if (room != null) {
thread.messages.sort((a,b) => a.timestamp - b.timestamp); thread = getThread(room)
} thread.messages.push(
MessageFormatter.formatMessageForClientSide(message)
return threads; )
} }
}); }
for (let thread_id in threads) {
thread = threads[thread_id]
thread.messages.sort((a, b) => a.timestamp - b.timestamp)
}
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) {
return MessageHttpController._getMessages(ThreadManager.GLOBAL_THREAD, req, res, next);
},
sendGlobalMessage(req, res, next) { getGlobalMessages(req, res, next) {
return MessageHttpController._sendMessage(ThreadManager.GLOBAL_THREAD, req, res, next); return MessageHttpController._getMessages(
}, ThreadManager.GLOBAL_THREAD,
req,
sendThreadMessage(req, res, next) { res,
return MessageHttpController._sendMessage(req.params.thread_id, req, res, next); next
}, )
},
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) {
if (error != null) { return next(error); }
const threads = MessageFormatter.groupMessagesByThreads(rooms, messages);
return res.json(threads);
});
});
},
resolveThread(req, res, next) {
const {project_id, thread_id} = req.params;
const {user_id} = req.body;
logger.log({user_id, project_id, thread_id}, "marking thread as resolved");
return ThreadManager.resolveThread(project_id, thread_id, user_id, function(error) {
if (error != null) { return next(error); }
return res.send(204);
});
}, // No content
reopenThread(req, res, next) { sendGlobalMessage(req, res, next) {
const {project_id, thread_id} = req.params; return MessageHttpController._sendMessage(
logger.log({project_id, thread_id}, "reopening thread"); ThreadManager.GLOBAL_THREAD,
return ThreadManager.reopenThread(project_id, thread_id, function(error) { req,
if (error != null) { return next(error); } res,
return res.send(204); next
}); )
}, // No content },
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) {
if (error != null) { return next(error); }
return res.send(204);
});
});
}, // No content
editMessage(req, res, next) {
const {content} = req != null ? req.body : undefined;
const {project_id, thread_id, message_id} = req.params;
logger.log({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.send(204);
});
});
},
deleteMessage(req, res, next) { sendThreadMessage(req, res, next) {
const {project_id, thread_id, message_id} = req.params; return MessageHttpController._sendMessage(
logger.log({project_id, thread_id, message_id}, "deleting message"); req.params.thread_id,
return ThreadManager.findOrCreateThread(project_id, thread_id, function(error, room) { req,
if (error != null) { return next(error); } res,
return MessageManager.deleteMessage(room._id, message_id, function(error, message) { next
if (error != null) { return next(error); } )
return res.send(204); },
});
});
},
_sendMessage(client_thread_id, req, res, next) { getAllThreads(req, res, next) {
const {user_id, content} = req != null ? req.body : undefined; const { project_id } = req.params
const {project_id} = req.params; logger.log({ project_id }, 'getting all threads')
if (!ObjectId.isValid(user_id)) { return ThreadManager.findAllThreadRooms(project_id, function(error, rooms) {
return res.send(400, "Invalid user_id"); if (error != null) {
} return next(error)
if ((content == null)) { }
return res.send(400, "No content provided"); const room_ids = rooms.map(r => r._id)
} return MessageManager.findAllMessagesInRooms(room_ids, function(
if (content.length > this.MAX_MESSAGE_LENGTH) { error,
return res.send(400, `Content too long (> ${this.MAX_MESSAGE_LENGTH} bytes)`); messages
} ) {
logger.log({client_thread_id, project_id, user_id, content}, "new message received"); if (error != null) {
return ThreadManager.findOrCreateThread(project_id, client_thread_id, function(error, thread) { return next(error)
if (error != null) { return next(error); } }
return MessageManager.createMessage(thread._id, user_id, content, Date.now(), function(error, message) { const threads = MessageFormatter.groupMessagesByThreads(rooms, messages)
if (error != null) { return next(error); } return res.json(threads)
message = MessageFormatter.formatMessageForClientSide(message); })
message.room_id = project_id; })
return res.send(201, message); },
});
});
},
_getMessages(client_thread_id, req, res, next) { resolveThread(req, res, next) {
let before, limit; const { project_id, thread_id } = req.params
const {project_id} = req.params; const { user_id } = req.body
if ((req.query != null ? req.query.before : undefined) != null) { logger.log({ user_id, project_id, thread_id }, 'marking thread as resolved')
before = parseInt(req.query.before, 10); return ThreadManager.resolveThread(project_id, thread_id, user_id, function(
} else { error
before = null; ) {
} if (error != null) {
if ((req.query != null ? req.query.limit : undefined) != null) { return next(error)
limit = parseInt(req.query.limit, 10); }
} else { return res.send(204)
limit = MessageHttpController.DEFAULT_MESSAGE_LIMIT; })
} }, // No content
logger.log({limit, before, project_id, client_thread_id}, "get message request received");
return ThreadManager.findOrCreateThread(project_id, client_thread_id, function(error, thread) { reopenThread(req, res, next) {
if (error != null) { return next(error); } const { project_id, thread_id } = req.params
const thread_object_id = thread._id; logger.log({ project_id, thread_id }, 'reopening thread')
logger.log({limit, before, project_id, client_thread_id, thread_object_id}, "found or created thread"); return ThreadManager.reopenThread(project_id, thread_id, function(error) {
return MessageManager.getMessages(thread_object_id, limit, before, function(error, messages) { if (error != null) {
if (error != null) { return next(error); } return next(error)
messages = MessageFormatter.formatMessagesForClientSide(messages); }
logger.log({project_id, messages}, "got messages"); return res.send(204)
return res.send(200, messages); })
}); }, // No content
});
} 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) {
if (error != null) {
return next(error)
}
return res.send(204)
})
})
}, // No content
editMessage(req, res, next) {
const { content } = req != null ? req.body : undefined
const { project_id, thread_id, message_id } = req.params
logger.log(
{ 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.send(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
) {
if (error != null) {
return next(error)
}
return res.send(204)
})
})
},
_sendMessage(client_thread_id, req, res, next) {
const { user_id, content } = req != null ? req.body : undefined
const { project_id } = req.params
if (!ObjectId.isValid(user_id)) {
return res.send(400, 'Invalid user_id')
}
if (content == null) {
return res.send(400, 'No content provided')
}
if (content.length > this.MAX_MESSAGE_LENGTH) {
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'
)
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) {
let before, limit
const { project_id } = req.params
if ((req.query != null ? req.query.before : undefined) != null) {
before = parseInt(req.query.before, 10)
} else {
before = null
}
if ((req.query != null ? req.query.limit : undefined) != null) {
limit = parseInt(req.query.limit, 10)
} else {
limit = MessageHttpController.DEFAULT_MESSAGE_LIMIT
}
logger.log(
{ limit, before, project_id, client_thread_id },
'get message request received'
)
return ThreadManager.findOrCreateThread(
project_id,
client_thread_id,
function(error, thread) {
if (error != null) {
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) {
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 })
findAllMessagesInRooms(room_ids, callback) { .limit(limit)
if (callback == null) { callback = function(error, messages) {}; } return cursor.toArray(callback)
return db.messages.find({ },
room_id: { $in: room_ids }
}, callback);
},
deleteAllMessagesInRoom(room_id, callback) { findAllMessagesInRooms(room_ids, callback) {
if (callback == null) { callback = function(error) {}; } if (callback == null) {
return db.messages.remove({ callback = function(error, messages) {}
room_id }
}, callback); return db.messages.find(
}, {
room_id: { $in: room_ids }
updateMessage(room_id, message_id, content, timestamp, callback) { },
if (callback == null) { callback = function(error, message) {}; } callback
const query = this._ensureIdsAreObjectIds({ )
_id: message_id, },
room_id
});
return db.messages.update(query, {
$set: {
content,
edited_at: timestamp
}
}, function(error) {
if (error != null) { return callback(error); }
return callback();
});
},
deleteMessage(room_id, message_id, callback) { deleteAllMessagesInRoom(room_id, callback) {
if (callback == null) { callback = function(error) {}; } if (callback == null) {
const query = this._ensureIdsAreObjectIds({ callback = function(error) {}
_id: message_id, }
room_id return db.messages.remove(
}); {
return db.messages.remove(query, function(error) { room_id
if (error != null) { return callback(error); } },
return callback(); callback
}); )
}, },
_ensureIdsAreObjectIds(query) { updateMessage(room_id, message_id, content, timestamp, callback) {
if ((query.user_id != null) && !(query.user_id instanceof ObjectId)) { if (callback == null) {
query.user_id = ObjectId(query.user_id); callback = function(error, message) {}
} }
if ((query.room_id != null) && !(query.room_id instanceof ObjectId)) { const query = this._ensureIdsAreObjectIds({
query.room_id = ObjectId(query.room_id); _id: message_id,
} room_id
if ((query._id != null) && !(query._id instanceof ObjectId)) { })
query._id = ObjectId(query._id); return db.messages.update(
} query,
return query; {
} $set: {
}); content,
edited_at: timestamp
}
},
function(error) {
if (error != null) {
return callback(error)
}
return callback()
}
)
},
deleteMessage(room_id, message_id, callback) {
if (callback == null) {
callback = function(error) {}
}
const query = this._ensureIdsAreObjectIds({
_id: message_id,
room_id
})
return db.messages.remove(query, function(error) {
if (error != null) {
return callback(error)
}
return callback()
})
},
[ _ensureIdsAreObjectIds(query) {
'createMessage', if (query.user_id != null && !(query.user_id instanceof ObjectId)) {
'getMessages', query.user_id = ObjectId(query.user_id)
'findAllMessagesInRooms', }
'updateMessage', if (query.room_id != null && !(query.room_id instanceof ObjectId)) {
'deleteMessage' query.room_id = ObjectId(query.room_id)
].map(method => metrics.timeAsyncMethod(MessageManager, method, 'mongo.MessageManager', logger)); }
if (query._id != null && !(query._id instanceof ObjectId)) {
query._id = ObjectId(query._id)
}
return query
}
}
;[
'createMessage',
'getMessages',
'findAllMessagesInRooms',
'updateMessage',
'deleteMessage'
].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) {}
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) {
if (error != null) { return callback(error); }
return db.rooms.find(query, function(error, rooms) {
if (rooms == null) { rooms = []; }
if (error != null) { return callback(error); }
return callback(null, rooms[0]);
});
});
},
findAllThreadRooms(project_id, callback) {
if (callback == null) { callback = function(error, rooms) {}; }
return db.rooms.find({
project_id: ObjectId(project_id.toString()),
thread_id: { $exists: true }
}, {
thread_id: 1,
resolved: 1
}, callback);
},
resolveThread(project_id, thread_id, user_id, callback) {
if (callback == null) { callback = function(error) {}; }
return db.rooms.update({
project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString())
}, {
$set: {
resolved: {
user_id,
ts: new Date()
}
}
}, callback);
},
reopenThread(project_id, thread_id, callback) {
if (callback == null) { callback = function(error) {}; }
return db.rooms.update({
project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString())
}, {
$unset: {
resolved: true
}
}, callback);
},
deleteThread(project_id, thread_id, callback) { return db.rooms.update(query, update, { upsert: true }, function(error) {
if (callback == null) { callback = function(error, room_id) {}; } if (error != null) {
return this.findOrCreateThread(project_id, thread_id, function(error, room) { return callback(error)
if (error != null) { return callback(error); } }
return db.rooms.remove({ return db.rooms.find(query, function(error, rooms) {
_id: room._id if (rooms == null) {
}, function(error) { rooms = []
if (error != null) { return callback(error); } }
return callback(null, room._id); if (error != null) {
}); return callback(error)
}); }
} return callback(null, rooms[0])
}); })
})
},
findAllThreadRooms(project_id, callback) {
if (callback == null) {
callback = function(error, rooms) {}
}
return db.rooms.find(
{
project_id: ObjectId(project_id.toString()),
thread_id: { $exists: true }
},
{
thread_id: 1,
resolved: 1
},
callback
)
},
[ resolveThread(project_id, thread_id, user_id, callback) {
'findOrCreateThread', if (callback == null) {
'findAllThreadRooms', callback = function(error) {}
'resolveThread', }
'reopenThread', return db.rooms.update(
'deleteThread', {
].map(method => metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)); project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString())
},
{
$set: {
resolved: {
user_id,
ts: new Date()
}
}
},
callback
)
},
reopenThread(project_id, thread_id, callback) {
if (callback == null) {
callback = function(error) {}
}
return db.rooms.update(
{
project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString())
},
{
$unset: {
resolved: true
}
},
callback
)
},
deleteThread(project_id, thread_id, callback) {
if (callback == null) {
callback = function(error, 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',
'findAllThreadRooms',
'resolveThread',
'reopenThread',
'deleteThread'
].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");
app.get("/profile", function(req, res) {
const time = parseInt(req.query.time || "1000");
profiler.startProfiling("test");
return setTimeout(function() {
const profile = profiler.stopProfiling("test");
return res.json(profile);
}
, time);
});
Router.route(app); const profiler = require('v8-profiler')
app.get('/profile', function(req, res) {
const time = parseInt(req.query.time || '1000')
profiler.startProfiling('test')
return setTimeout(function() {
const profile = profiler.stopProfiling('test')
return res.json(profile)
}, time)
})
Router.route(app)
module.exports = { module.exports = {
server, server,
app app
}; }