prettier: convert app/js decaffeinated files to Prettier format

This commit is contained in:
Simon Detheridge 2020-02-17 07:38:00 +00:00
parent 31f06e13d6
commit 8f6dfedc6c
4 changed files with 157 additions and 136 deletions

View file

@ -10,51 +10,69 @@
* 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');
let ContactManager
const { db, ObjectId } = require('./mongojs')
const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex')
module.exports = (ContactManager = {
touchContact(user_id, contact_id, callback) {
if (callback == null) { callback = function(error) {}; }
try {
user_id = ObjectId(user_id.toString());
} catch (error1) {
const error = error1;
return callback(error);
}
module.exports = ContactManager = {
touchContact(user_id, contact_id, callback) {
if (callback == null) {
callback = function(error) {}
}
try {
user_id = ObjectId(user_id.toString())
} catch (error1) {
const error = error1
return callback(error)
}
const update = { $set: {}, $inc: {} };
update.$inc[`contacts.${contact_id}.n`] = 1;
update.$set[`contacts.${contact_id}.ts`] = new Date();
const update = { $set: {}, $inc: {} }
update.$inc[`contacts.${contact_id}.n`] = 1
update.$set[`contacts.${contact_id}.ts`] = new Date()
return db.contacts.update({
user_id
}, update, {
upsert: true
}, callback);
},
getContacts(user_id, callback) {
if (callback == null) { callback = function(error) {}; }
try {
user_id = ObjectId(user_id.toString());
} catch (error1) {
const error = error1;
return callback(error);
}
return db.contacts.findOne({
user_id
}, function(error, user) {
if (error != null) { return callback(error); }
return callback(null, user != null ? user.contacts : undefined);
});
}
});
return db.contacts.update(
{
user_id
},
update,
{
upsert: true
},
callback
)
},
[
'touchContact',
'getContacts',
].map(method => metrics.timeAsyncMethod(ContactManager, method, 'mongo.ContactManager', logger));
getContacts(user_id, callback) {
if (callback == null) {
callback = function(error) {}
}
try {
user_id = ObjectId(user_id.toString())
} catch (error1) {
const error = error1
return callback(error)
}
return db.contacts.findOne(
{
user_id
},
function(error, user) {
if (error != null) {
return callback(error)
}
return callback(null, user != null ? user.contacts : undefined)
}
)
}
}
;['touchContact', 'getContacts'].map(method =>
metrics.timeAsyncMethod(
ContactManager,
method,
'mongo.ContactManager',
logger
)
)

View file

@ -4,15 +4,13 @@
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
let Errors;
let Errors
var NotFoundError = function(message) {
const error = new Error(message);
error.name = "NotFoundError";
error.__proto__ = NotFoundError.prototype;
return error;
};
NotFoundError.prototype.__proto__ = Error.prototype;
module.exports = (Errors =
{NotFoundError});
const error = new Error(message)
error.name = 'NotFoundError'
error.__proto__ = NotFoundError.prototype
return error
}
NotFoundError.prototype.__proto__ = Error.prototype
module.exports = Errors = { NotFoundError }

View file

@ -9,86 +9,92 @@
* 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");
let HttpController
const ContactManager = require('./ContactManager')
const logger = require('logger-sharelatex')
module.exports = (HttpController = {
addContact(req, res, next) {
const {user_id} = req.params;
const {contact_id} = req.body;
if ((contact_id == null) || (contact_id === "")) {
res.status(400).send("contact_id should be a non-blank string");
return;
}
logger.log({user_id, contact_id}, "adding contact");
module.exports = HttpController = {
addContact(req, res, next) {
const { user_id } = req.params
const { contact_id } = req.body
return ContactManager.touchContact(user_id, contact_id, function(error) {
if (error != null) { return next(error); }
return ContactManager.touchContact(contact_id, user_id, function(error) {
if (error != null) { return next(error); }
return res.status(204).end();
});
});
},
if (contact_id == null || contact_id === '') {
res.status(400).send('contact_id should be a non-blank string')
return
}
CONTACT_LIMIT: 50,
getContacts(req, res, next) {
let limit;
let {user_id} = req.params;
logger.log({ user_id, contact_id }, 'adding contact')
if ((req.query != null ? req.query.limit : undefined) != null) {
limit = parseInt(req.query.limit, 10);
} else {
limit = HttpController.CONTACT_LIMIT;
}
limit = Math.min(limit, HttpController.CONTACT_LIMIT);
return ContactManager.touchContact(user_id, contact_id, function(error) {
if (error != null) {
return next(error)
}
return ContactManager.touchContact(contact_id, user_id, function(error) {
if (error != null) {
return next(error)
}
return res.status(204).end()
})
})
},
logger.log({user_id}, "getting contacts");
CONTACT_LIMIT: 50,
getContacts(req, res, next) {
let limit
let { user_id } = req.params
return ContactManager.getContacts(user_id, function(error, contact_dict) {
if (error != null) { return next(error); }
let contacts = [];
const object = contact_dict || {};
for (user_id in object) {
const data = object[user_id];
contacts.push({
user_id,
n: data.n,
ts: data.ts
});
}
if ((req.query != null ? req.query.limit : undefined) != null) {
limit = parseInt(req.query.limit, 10)
} else {
limit = HttpController.CONTACT_LIMIT
}
limit = Math.min(limit, HttpController.CONTACT_LIMIT)
HttpController._sortContacts(contacts);
contacts = contacts.slice(0, limit);
const contact_ids = contacts.map(contact => contact.user_id);
logger.log({ user_id }, 'getting contacts')
return res.status(200).send({
contact_ids
});
});
},
_sortContacts(contacts) {
return contacts.sort(function(a, b) {
// Sort by decreasing count, descreasing timestamp.
// I.e. biggest count, and most recent at front.
if (a.n > b.n) {
return -1;
} else if (a.n < b.n) {
return 1;
} else {
if (a.ts > b.ts) {
return -1;
} else if (a.ts < b.ts) {
return 1;
} else {
return 0;
}
}
});
}
});
return ContactManager.getContacts(user_id, function(error, contact_dict) {
if (error != null) {
return next(error)
}
let contacts = []
const object = contact_dict || {}
for (user_id in object) {
const data = object[user_id]
contacts.push({
user_id,
n: data.n,
ts: data.ts
})
}
HttpController._sortContacts(contacts)
contacts = contacts.slice(0, limit)
const contact_ids = contacts.map(contact => contact.user_id)
return res.status(200).send({
contact_ids
})
})
},
_sortContacts(contacts) {
return contacts.sort(function(a, b) {
// Sort by decreasing count, descreasing timestamp.
// I.e. biggest count, and most recent at front.
if (a.n > b.n) {
return -1
} else if (a.n < b.n) {
return 1
} else {
if (a.ts > b.ts) {
return -1
} else if (a.ts < b.ts) {
return 1
} else {
return 0
}
}
})
}
}

View file

@ -1,10 +1,9 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
const Settings = require("settings-sharelatex");
const mongojs = require("mongojs");
const db = mongojs(Settings.mongo.url, ["contacts"]);
const Settings = require('settings-sharelatex')
const mongojs = require('mongojs')
const db = mongojs(Settings.mongo.url, ['contacts'])
module.exports = {
db,
ObjectId: mongojs.ObjectId
};
db,
ObjectId: mongojs.ObjectId
}