overleaf/services/contacts/app/js/ContactManager.js

77 lines
1.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* 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 ContactManager
const { db, ObjectId } = require('./mongodb')
const logger = require('@overleaf/logger')
const metrics = require('@overleaf/metrics')
module.exports = ContactManager = {
touchContact(user_id, contact_id, callback) {
if (callback == null) {
callback = function () {}
}
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()
db.contacts.updateOne(
{
2021-07-13 11:04:47 +00:00
user_id,
},
update,
{
2021-07-13 11:04:47 +00:00
upsert: true,
},
callback
)
},
2017-03-17 09:31:12 +00:00
getContacts(user_id, callback) {
if (callback == null) {
callback = function () {}
}
try {
user_id = ObjectId(user_id.toString())
} catch (error1) {
const error = error1
return callback(error)
}
return db.contacts.findOne(
{
2021-07-13 11:04:47 +00:00
user_id,
},
2020-06-04 07:49:46 +00:00
function (error, user) {
if (error != null) {
return callback(error)
}
return callback(null, user != null ? user.contacts : undefined)
}
)
2021-07-13 11:04:47 +00:00
},
}
2021-07-13 11:04:47 +00:00
;['touchContact', 'getContacts'].map(method =>
metrics.timeAsyncMethod(
ContactManager,
method,
'mongo.ContactManager',
logger
)
)