prettier: convert app/js decaffeinated files to Prettier format

This commit is contained in:
Tim Alby 2020-01-13 20:00:49 +01:00
parent 9835481cbf
commit dc218cd503
3 changed files with 283 additions and 203 deletions

View file

@ -11,79 +11,112 @@
* 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
*/ */
const { ObjectId } = require("mongojs"); const { ObjectId } = require('mongojs')
const request = require("request"); const request = require('request')
const async = require("async"); const async = require('async')
const _ = require("underscore"); const _ = require('underscore')
const settings = require("settings-sharelatex"); const settings = require('settings-sharelatex')
const { port } = settings.internal.notifications; const { port } = settings.internal.notifications
const logger = require("logger-sharelatex"); const logger = require('logger-sharelatex')
const mongojs = require('mongojs'); const mongojs = require('mongojs')
const Settings = require('settings-sharelatex'); const Settings = require('settings-sharelatex')
const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, ['notifications']); const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, [
'notifications'
])
module.exports = { module.exports = {
check(callback){ check(callback) {
const user_id = ObjectId(); const user_id = ObjectId()
const cleanupNotifications = callback=> db.notifications.remove({user_id}, callback); const cleanupNotifications = callback =>
db.notifications.remove({ user_id }, callback)
let notification_key = `smoke-test-notification-${ObjectId()}`; let notification_key = `smoke-test-notification-${ObjectId()}`
const getOpts = endPath=> ({url:`http://localhost:${port}/user/${user_id}${endPath}`, timeout:5000}); const getOpts = endPath => ({
logger.log({user_id, opts:getOpts(), key:notification_key, user_id}, "Health Check: running"); url: `http://localhost:${port}/user/${user_id}${endPath}`,
const jobs = [ timeout: 5000
function(cb){ })
const opts = getOpts("/"); logger.log(
opts.json = {key: notification_key, messageOpts:'', templateKey:'f4g5', user_id}; { user_id, opts: getOpts(), key: notification_key, user_id },
return request.post(opts, cb); 'Health Check: running'
}, )
function(cb){ const jobs = [
const opts = getOpts("/"); function(cb) {
opts.json = true; const opts = getOpts('/')
return request.get(opts, function(err, res, body){ opts.json = {
if (err != null) { key: notification_key,
logger.err({err}, "Health Check: error getting notification"); messageOpts: '',
return callback(err); templateKey: 'f4g5',
} else if (res.statusCode !== 200) { user_id
const e = `status code not 200 ${res.statusCode}`; }
logger.err({err}, e); return request.post(opts, cb)
return cb(e); },
} function(cb) {
const hasNotification = _.some(body, notification=> (notification.key === notification_key) && (notification.user_id === user_id.toString())); const opts = getOpts('/')
if (hasNotification) { opts.json = true
return cb(null, body); return request.get(opts, function(err, res, body) {
} else { if (err != null) {
logger.err({body, notification_key}, "Health Check: notification not in response"); logger.err({ err }, 'Health Check: error getting notification')
return cb("notification not found in response"); return callback(err)
} } else if (res.statusCode !== 200) {
}); const e = `status code not 200 ${res.statusCode}`
} logger.err({ err }, e)
]; return cb(e)
return async.series(jobs, function(err, body){ }
if (err != null) { const hasNotification = _.some(
logger.err({err}, "Health Check: error running health check"); body,
return cleanupNotifications(() => callback(err)); notification =>
} else { notification.key === notification_key &&
const notification_id = body[1][0]._id; notification.user_id === user_id.toString()
notification_key = body[1][0].key; )
let opts = getOpts(`/notification/${notification_id}`); if (hasNotification) {
logger.log({notification_id, notification_key}, "Health Check: doing cleanup"); return cb(null, body)
return request.del(opts, function(err, res, body){ } else {
if (err != null) { logger.err(
logger.err(err, opts, "Health Check: error cleaning up notification"); { body, notification_key },
return callback(err); 'Health Check: notification not in response'
} )
opts = getOpts(""); return cb('notification not found in response')
opts.json = {key: notification_key}; }
return request.del(opts, function(err, res, body){ })
if (err != null) { }
logger.err(err, opts, "Health Check: error cleaning up notification"); ]
return callback(err); return async.series(jobs, function(err, body) {
} if (err != null) {
return cleanupNotifications(callback); logger.err({ err }, 'Health Check: error running health check')
}); return cleanupNotifications(() => callback(err))
}); } else {
} const notification_id = body[1][0]._id
}); notification_key = body[1][0].key
} let opts = getOpts(`/notification/${notification_id}`)
}; logger.log(
{ notification_id, notification_key },
'Health Check: doing cleanup'
)
return request.del(opts, function(err, res, body) {
if (err != null) {
logger.err(
err,
opts,
'Health Check: error cleaning up notification'
)
return callback(err)
}
opts = getOpts('')
opts.json = { key: notification_key }
return request.del(opts, function(err, res, body) {
if (err != null) {
logger.err(
err,
opts,
'Health Check: error cleaning up notification'
)
return callback(err)
}
return cleanupNotifications(callback)
})
})
}
})
}
}

View file

@ -11,104 +11,120 @@
* 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 Notifications; let Notifications
const Settings = require('settings-sharelatex'); const Settings = require('settings-sharelatex')
const logger = require('logger-sharelatex'); const logger = require('logger-sharelatex')
const mongojs = require('mongojs'); const mongojs = require('mongojs')
const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, ['notifications']); const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, [
const { ObjectId } = require("mongojs"); 'notifications'
const metrics = require('metrics-sharelatex'); ])
const { ObjectId } = require('mongojs')
const metrics = require('metrics-sharelatex')
module.exports = (Notifications = { module.exports = Notifications = {
getUserNotifications(user_id, callback) {
if (callback == null) {
callback = function(err, notifications) {}
}
const query = {
user_id: ObjectId(user_id),
templateKey: { $exists: true }
}
return db.notifications.find(query, (err, notifications) =>
callback(err, notifications)
)
},
getUserNotifications(user_id, callback){ _countExistingNotifications(user_id, notification, callback) {
if (callback == null) { callback = function(err, notifications){}; } if (callback == null) {
const query = { callback = function(err, count) {}
user_id: ObjectId(user_id), }
templateKey: {"$exists":true} const query = {
}; user_id: ObjectId(user_id),
return db.notifications.find(query, (err, notifications)=> callback(err, notifications)); key: notification.key
}, }
return db.notifications.count(query, function(err, count) {
if (err != null) {
return callback(err)
}
return callback(null, count)
})
},
addNotification(user_id, notification, callback) {
return this._countExistingNotifications(user_id, notification, function(
err,
count
) {
if (err != null) {
return callback(err)
}
if (count !== 0 && !notification.forceCreate) {
return callback()
}
const doc = {
user_id: ObjectId(user_id),
key: notification.key,
messageOpts: notification.messageOpts,
templateKey: notification.templateKey
}
// TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
// a datetime in the future when the document should be automatically removed.
// in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
// see `README.md` for instruction on creating TTL index
if (notification.expires != null) {
try {
doc.expires = new Date(notification.expires)
const _testValue = doc.expires.toISOString()
} catch (error) {
err = error
logger.error(
{ user_id, expires: notification.expires },
'error converting `expires` field to Date'
)
return callback(err)
}
}
return db.notifications.update(
{ user_id: doc.user_id, key: notification.key },
doc,
{ upsert: true },
callback
)
})
},
_countExistingNotifications(user_id, notification, callback){ removeNotificationId(user_id, notification_id, callback) {
if (callback == null) { callback = function(err, count){}; } const searchOps = {
const query = { user_id: ObjectId(user_id),
user_id: ObjectId(user_id), _id: ObjectId(notification_id)
key: notification.key }
}; const updateOperation = { $unset: { templateKey: true, messageOpts: true } }
return db.notifications.count(query, function(err, count){ return db.notifications.update(searchOps, updateOperation, callback)
if (err != null) { return callback(err); } },
return callback(null, count);
});
},
addNotification(user_id, notification, callback){ removeNotificationKey(user_id, notification_key, callback) {
return this._countExistingNotifications(user_id, notification, function(err, count){ const searchOps = {
if (err != null) { return callback(err); } user_id: ObjectId(user_id),
if ((count !== 0) && !notification.forceCreate) { return callback(); } key: notification_key
const doc = { }
user_id: ObjectId(user_id), const updateOperation = { $unset: { templateKey: true } }
key: notification.key, return db.notifications.update(searchOps, updateOperation, callback)
messageOpts: notification.messageOpts, },
templateKey: notification.templateKey
};
// TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
// a datetime in the future when the document should be automatically removed.
// in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
// see `README.md` for instruction on creating TTL index
if (notification.expires != null) {
try {
doc.expires = new Date(notification.expires);
const _testValue = doc.expires.toISOString();
} catch (error) {
err = error;
logger.error({user_id, expires: notification.expires}, "error converting `expires` field to Date");
return callback(err);
}
}
return db.notifications.update({user_id: doc.user_id, key: notification.key}, doc, {upsert: true}, callback);
});
},
removeNotificationId(user_id, notification_id, callback){ removeNotificationByKeyOnly(notification_key, callback) {
const searchOps = { const searchOps = { key: notification_key }
user_id:ObjectId(user_id), const updateOperation = { $unset: { templateKey: true } }
_id:ObjectId(notification_id) return db.notifications.update(searchOps, updateOperation, callback)
}; },
const updateOperation =
{"$unset": {templateKey:true, messageOpts: true}};
return db.notifications.update(searchOps, updateOperation, callback);
},
removeNotificationKey(user_id, notification_key, callback){ // hard delete of doc, rather than removing the templateKey
const searchOps = { deleteNotificationByKeyOnly(notification_key, callback) {
user_id:ObjectId(user_id), const searchOps = { key: notification_key }
key: notification_key return db.notifications.remove(searchOps, { justOne: true }, callback)
}; }
const updateOperation = }
{"$unset": {templateKey:true}};
return db.notifications.update(searchOps, updateOperation, callback);
},
removeNotificationByKeyOnly(notification_key, callback){ ;['getUserNotifications', 'addNotification'].map(method =>
const searchOps = metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger)
{key: notification_key}; )
const updateOperation =
{"$unset": {templateKey:true}};
return db.notifications.update(searchOps, updateOperation, callback);
},
// hard delete of doc, rather than removing the templateKey
deleteNotificationByKeyOnly(notification_key, callback){
const searchOps =
{key: notification_key};
return db.notifications.remove(searchOps, {justOne: true}, callback);
}
});
[
'getUserNotifications',
'addNotification'
].map(method => metrics.timeAsyncMethod(Notifications, method, 'mongo.Notifications', logger));

View file

@ -10,46 +10,77 @@
* 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
*/ */
const Notifications = require("./Notifications"); const Notifications = require('./Notifications')
const logger = require("logger-sharelatex"); const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex'); const metrics = require('metrics-sharelatex')
module.exports = { module.exports = {
getUserNotifications(req, res) {
logger.log(
{ user_id: req.params.user_id },
'getting user unread notifications'
)
metrics.inc('getUserNotifications')
return Notifications.getUserNotifications(
req.params.user_id,
(err, notifications) => res.json(notifications)
)
},
getUserNotifications(req, res){ addNotification(req, res) {
logger.log({user_id: req.params.user_id}, "getting user unread notifications"); logger.log(
metrics.inc("getUserNotifications"); { user_id: req.params.user_id, notification: req.body },
return Notifications.getUserNotifications(req.params.user_id, (err, notifications)=> res.json(notifications)); 'adding notification'
}, )
metrics.inc('addNotification')
return Notifications.addNotification(req.params.user_id, req.body, function(
err,
notifications
) {
if (err != null) {
return res.send(500)
} else {
return res.send()
}
})
},
addNotification(req, res){ removeNotificationId(req, res) {
logger.log({user_id: req.params.user_id, notification:req.body}, "adding notification"); logger.log(
metrics.inc("addNotification"); {
return Notifications.addNotification(req.params.user_id, req.body, function(err, notifications){ user_id: req.params.user_id,
if (err != null) { notification_id: req.params.notification_id
return res.send(500); },
} else { 'mark id notification as read'
return res.send(); )
} metrics.inc('removeNotificationId')
}); return Notifications.removeNotificationId(
}, req.params.user_id,
req.params.notification_id,
(err, notifications) => res.send()
)
},
removeNotificationId(req, res){ removeNotificationKey(req, res) {
logger.log({user_id: req.params.user_id, notification_id: req.params.notification_id}, "mark id notification as read"); logger.log(
metrics.inc("removeNotificationId"); { user_id: req.params.user_id, notification_key: req.body.key },
return Notifications.removeNotificationId(req.params.user_id, req.params.notification_id, (err, notifications)=> res.send()); 'mark key notification as read'
}, )
metrics.inc('removeNotificationKey')
return Notifications.removeNotificationKey(
req.params.user_id,
req.body.key,
(err, notifications) => res.send()
)
},
removeNotificationKey(req, res){ removeNotificationByKeyOnly(req, res) {
logger.log({user_id: req.params.user_id, notification_key: req.body.key}, "mark key notification as read"); const notification_key = req.params.key
metrics.inc("removeNotificationKey"); logger.log({ notification_key }, 'mark notification as read by key only')
return Notifications.removeNotificationKey(req.params.user_id, req.body.key, (err, notifications)=> res.send()); metrics.inc('removeNotificationKey')
}, return Notifications.removeNotificationByKeyOnly(
notification_key,
removeNotificationByKeyOnly(req, res){ (err, notifications) => res.send()
const notification_key = req.params.key; )
logger.log({notification_key}, "mark notification as read by key only"); }
metrics.inc("removeNotificationKey"); }
return Notifications.removeNotificationByKeyOnly(notification_key, (err, notifications)=> res.send());
}
};