2015-10-06 11:41:35 -04:00
|
|
|
{db, ObjectId} = require "./mongojs"
|
2017-03-17 05:31:12 -04:00
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
metrics = require('metrics-sharelatex')
|
2015-10-06 11:41:35 -04:00
|
|
|
|
|
|
|
module.exports = ContactManager =
|
|
|
|
touchContact: (user_id, contact_id, callback = (error) ->) ->
|
|
|
|
try
|
|
|
|
user_id = ObjectId(user_id.toString())
|
|
|
|
catch error
|
|
|
|
return callback error
|
|
|
|
|
|
|
|
update = { $set: {}, $inc: {} }
|
|
|
|
update.$inc["contacts.#{contact_id}.n"] = 1
|
|
|
|
update.$set["contacts.#{contact_id}.ts"] = new Date()
|
|
|
|
|
|
|
|
db.contacts.update({
|
|
|
|
user_id: user_id
|
|
|
|
}, update, {
|
|
|
|
upsert: true
|
2015-10-06 12:22:11 -04:00
|
|
|
}, callback)
|
|
|
|
|
|
|
|
getContacts: (user_id, callback = (error) ->) ->
|
|
|
|
try
|
|
|
|
user_id = ObjectId(user_id.toString())
|
|
|
|
catch error
|
|
|
|
return callback error
|
|
|
|
|
|
|
|
db.contacts.findOne {
|
|
|
|
user_id: user_id
|
|
|
|
}, (error, user) ->
|
|
|
|
return callback(error) if error?
|
2017-03-17 05:31:12 -04:00
|
|
|
callback null, user?.contacts
|
|
|
|
|
2017-03-17 11:30:38 -04:00
|
|
|
[
|
|
|
|
'touchContact',
|
|
|
|
'getContacts',
|
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod(ContactManager, method, 'mongo.ContactManager', logger)
|