From 8f6dfedc6cee66e94fa819acb695a58959d8b5c8 Mon Sep 17 00:00:00 2001 From: Simon Detheridge Date: Mon, 17 Feb 2020 07:38:00 +0000 Subject: [PATCH] prettier: convert app/js decaffeinated files to Prettier format --- services/contacts/app/js/ContactManager.js | 106 ++++++++------ services/contacts/app/js/Errors.js | 18 ++- services/contacts/app/js/HttpController.js | 156 +++++++++++---------- services/contacts/app/js/mongojs.js | 13 +- 4 files changed, 157 insertions(+), 136 deletions(-) diff --git a/services/contacts/app/js/ContactManager.js b/services/contacts/app/js/ContactManager.js index 3041344587..eb5e48bb51 100644 --- a/services/contacts/app/js/ContactManager.js +++ b/services/contacts/app/js/ContactManager.js @@ -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 + ) +) diff --git a/services/contacts/app/js/Errors.js b/services/contacts/app/js/Errors.js index a000b8f1f4..f3bc8e37eb 100644 --- a/services/contacts/app/js/Errors.js +++ b/services/contacts/app/js/Errors.js @@ -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 } diff --git a/services/contacts/app/js/HttpController.js b/services/contacts/app/js/HttpController.js index fdfa2061ff..fae2ff0b4e 100644 --- a/services/contacts/app/js/HttpController.js +++ b/services/contacts/app/js/HttpController.js @@ -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 + } + } + }) + } +} diff --git a/services/contacts/app/js/mongojs.js b/services/contacts/app/js/mongojs.js index d87e0ebe18..197a8d88a2 100644 --- a/services/contacts/app/js/mongojs.js +++ b/services/contacts/app/js/mongojs.js @@ -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 +}