decaffeinate: Convert ContactManager.coffee and 3 other files to JS

This commit is contained in:
decaffeinate 2020-02-17 07:37:54 +00:00 committed by Simon Detheridge
parent 683b9a7cb3
commit af36ed629b
4 changed files with 135 additions and 93 deletions

View file

@ -1,38 +1,54 @@
{db, ObjectId} = require "./mongojs" /*
logger = require('logger-sharelatex') * decaffeinate suggestions:
metrics = require('metrics-sharelatex') * DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let ContactManager;
const {db, ObjectId} = require("./mongojs");
const logger = require('logger-sharelatex');
const metrics = require('metrics-sharelatex');
module.exports = ContactManager = module.exports = (ContactManager = {
touchContact: (user_id, contact_id, callback = (error) ->) -> touchContact(user_id, contact_id, callback) {
try if (callback == null) { callback = function(error) {}; }
user_id = ObjectId(user_id.toString()) try {
catch error user_id = ObjectId(user_id.toString());
return callback error } catch (error1) {
const error = error1;
return callback(error);
}
update = { $set: {}, $inc: {} } const update = { $set: {}, $inc: {} };
update.$inc["contacts.#{contact_id}.n"] = 1 update.$inc[`contacts.${contact_id}.n`] = 1;
update.$set["contacts.#{contact_id}.ts"] = new Date() update.$set[`contacts.${contact_id}.ts`] = new Date();
db.contacts.update({ return db.contacts.update({
user_id: user_id user_id
}, update, { }, update, {
upsert: true upsert: true
}, callback) }, callback);
},
getContacts: (user_id, callback = (error) ->) -> getContacts(user_id, callback) {
try if (callback == null) { callback = function(error) {}; }
user_id = ObjectId(user_id.toString()) try {
catch error user_id = ObjectId(user_id.toString());
return callback error } catch (error1) {
const error = error1;
return callback(error);
}
db.contacts.findOne { return db.contacts.findOne({
user_id: user_id user_id
}, (error, user) -> }, function(error, user) {
return callback(error) if error? if (error != null) { return callback(error); }
callback null, user?.contacts return callback(null, user != null ? user.contacts : undefined);
});
}
});
[ [
'touchContact', 'touchContact',
'getContacts', 'getContacts',
].map (method) -> ].map(method => metrics.timeAsyncMethod(ContactManager, method, 'mongo.ContactManager', logger));
metrics.timeAsyncMethod(ContactManager, method, 'mongo.ContactManager', logger)

View file

@ -1,10 +1,12 @@
NotFoundError = (message) -> let Errors;
error = new Error(message) var NotFoundError = function(message) {
error.name = "NotFoundError" const error = new Error(message);
error.__proto__ = NotFoundError.prototype error.name = "NotFoundError";
return error error.__proto__ = NotFoundError.prototype;
NotFoundError.prototype.__proto__ = Error.prototype return error;
};
NotFoundError.prototype.__proto__ = Error.prototype;
module.exports = Errors = module.exports = (Errors =
NotFoundError: NotFoundError {NotFoundError});

View file

@ -1,66 +1,89 @@
ContactManager = require "./ContactManager" /*
logger = require "logger-sharelatex" * decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let HttpController;
const ContactManager = require("./ContactManager");
const logger = require("logger-sharelatex");
module.exports = HttpController = module.exports = (HttpController = {
addContact: (req, res, next) -> addContact(req, res, next) {
{user_id} = req.params const {user_id} = req.params;
{contact_id} = req.body const {contact_id} = req.body;
if !contact_id? or contact_id == "" if ((contact_id == null) || (contact_id === "")) {
res.status(400).send("contact_id should be a non-blank string") res.status(400).send("contact_id should be a non-blank string");
return return;
}
logger.log {user_id, contact_id}, "adding contact" logger.log({user_id, contact_id}, "adding contact");
ContactManager.touchContact user_id, contact_id, (error) -> return ContactManager.touchContact(user_id, contact_id, function(error) {
return next(error) if error? if (error != null) { return next(error); }
ContactManager.touchContact contact_id, user_id, (error) -> return ContactManager.touchContact(contact_id, user_id, function(error) {
return next(error) if error? if (error != null) { return next(error); }
res.status(204).end() return res.status(204).end();
});
});
},
CONTACT_LIMIT: 50 CONTACT_LIMIT: 50,
getContacts: (req, res, next) -> getContacts(req, res, next) {
{user_id} = req.params let limit;
let {user_id} = req.params;
if req.query?.limit? if ((req.query != null ? req.query.limit : undefined) != null) {
limit = parseInt(req.query.limit, 10) limit = parseInt(req.query.limit, 10);
else } else {
limit = HttpController.CONTACT_LIMIT limit = HttpController.CONTACT_LIMIT;
limit = Math.min(limit, HttpController.CONTACT_LIMIT) }
limit = Math.min(limit, HttpController.CONTACT_LIMIT);
logger.log {user_id}, "getting contacts" logger.log({user_id}, "getting contacts");
ContactManager.getContacts user_id, (error, contact_dict) -> return ContactManager.getContacts(user_id, function(error, contact_dict) {
return next(error) if error? if (error != null) { return next(error); }
contacts = [] let contacts = [];
for user_id, data of (contact_dict or {}) const object = contact_dict || {};
contacts.push { for (user_id in object) {
user_id: user_id const data = object[user_id];
n: data.n contacts.push({
user_id,
n: data.n,
ts: data.ts ts: data.ts
} });
}
HttpController._sortContacts contacts HttpController._sortContacts(contacts);
contacts = contacts.slice(0, limit) contacts = contacts.slice(0, limit);
contact_ids = contacts.map (contact) -> contact.user_id const contact_ids = contacts.map(contact => contact.user_id);
res.status(200).send({ return res.status(200).send({
contact_ids: contact_ids contact_ids
}) });
});
},
_sortContacts: (contacts) -> _sortContacts(contacts) {
contacts.sort (a, b) -> return contacts.sort(function(a, b) {
# Sort by decreasing count, descreasing timestamp. // Sort by decreasing count, descreasing timestamp.
# I.e. biggest count, and most recent at front. // I.e. biggest count, and most recent at front.
if a.n > b.n if (a.n > b.n) {
return -1 return -1;
else if a.n < b.n } else if (a.n < b.n) {
return 1 return 1;
else } else {
if a.ts > b.ts if (a.ts > b.ts) {
return -1 return -1;
else if a.ts < b.ts } else if (a.ts < b.ts) {
return 1 return 1;
else } else {
return 0 return 0;
}
}
});
}
});

View file

@ -1,7 +1,8 @@
Settings = require "settings-sharelatex" const Settings = require("settings-sharelatex");
mongojs = require "mongojs" const mongojs = require("mongojs");
db = mongojs(Settings.mongo.url, ["contacts"]) const db = mongojs(Settings.mongo.url, ["contacts"]);
module.exports = module.exports = {
db: db db,
ObjectId: mongojs.ObjectId ObjectId: mongojs.ObjectId
};