ContactManager.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* eslint-disable
  2. camelcase,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. let ContactManager
  13. const { db, ObjectId } = require('./mongodb')
  14. const logger = require('@overleaf/logger')
  15. const metrics = require('@overleaf/metrics')
  16. module.exports = ContactManager = {
  17. touchContact(user_id, contact_id, callback) {
  18. if (callback == null) {
  19. callback = function () {}
  20. }
  21. try {
  22. user_id = ObjectId(user_id.toString())
  23. } catch (error1) {
  24. const error = error1
  25. return callback(error)
  26. }
  27. const update = { $set: {}, $inc: {} }
  28. update.$inc[`contacts.${contact_id}.n`] = 1
  29. update.$set[`contacts.${contact_id}.ts`] = new Date()
  30. db.contacts.updateOne(
  31. {
  32. user_id,
  33. },
  34. update,
  35. {
  36. upsert: true,
  37. },
  38. callback
  39. )
  40. },
  41. getContacts(user_id, callback) {
  42. if (callback == null) {
  43. callback = function () {}
  44. }
  45. try {
  46. user_id = ObjectId(user_id.toString())
  47. } catch (error1) {
  48. const error = error1
  49. return callback(error)
  50. }
  51. return db.contacts.findOne(
  52. {
  53. user_id,
  54. },
  55. function (error, user) {
  56. if (error != null) {
  57. return callback(error)
  58. }
  59. return callback(null, user != null ? user.contacts : undefined)
  60. }
  61. )
  62. },
  63. }
  64. ;['touchContact', 'getContacts'].map(method =>
  65. metrics.timeAsyncMethod(
  66. ContactManager,
  67. method,
  68. 'mongo.ContactManager',
  69. logger
  70. )
  71. )