2020-02-17 02:37:54 -05:00
|
|
|
/*
|
|
|
|
* 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("./mongojs");
|
|
|
|
const logger = require('logger-sharelatex');
|
|
|
|
const metrics = require('metrics-sharelatex');
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:37:54 -05:00
|
|
|
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);
|
|
|
|
}
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:37:54 -05:00
|
|
|
const update = { $set: {}, $inc: {} };
|
|
|
|
update.$inc[`contacts.${contact_id}.n`] = 1;
|
|
|
|
update.$set[`contacts.${contact_id}.ts`] = new Date();
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:37:54 -05:00
|
|
|
return db.contacts.update({
|
|
|
|
user_id
|
2015-10-06 11:41:35 -04:00
|
|
|
}, update, {
|
|
|
|
upsert: true
|
2020-02-17 02:37:54 -05:00
|
|
|
}, callback);
|
|
|
|
},
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:37:54 -05:00
|
|
|
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);
|
|
|
|
}
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:37:54 -05:00
|
|
|
return db.contacts.findOne({
|
|
|
|
user_id
|
|
|
|
}, function(error, user) {
|
|
|
|
if (error != null) { return callback(error); }
|
|
|
|
return callback(null, user != null ? user.contacts : undefined);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-03-17 05:31:12 -04:00
|
|
|
|
2017-03-17 11:30:38 -04:00
|
|
|
[
|
|
|
|
'touchContact',
|
|
|
|
'getContacts',
|
2020-02-17 02:37:54 -05:00
|
|
|
].map(method => metrics.timeAsyncMethod(ContactManager, method, 'mongo.ContactManager', logger));
|