ContactController.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let ContactsController
  16. const SessionManager = require('../Authentication/SessionManager')
  17. const ContactManager = require('./ContactManager')
  18. const UserGetter = require('../User/UserGetter')
  19. const logger = require('@overleaf/logger')
  20. const Modules = require('../../infrastructure/Modules')
  21. module.exports = ContactsController = {
  22. getContacts(req, res, next) {
  23. const user_id = SessionManager.getLoggedInUserId(req.session)
  24. return ContactManager.getContactIds(
  25. user_id,
  26. { limit: 50 },
  27. function (error, contact_ids) {
  28. if (error != null) {
  29. return next(error)
  30. }
  31. return UserGetter.getUsers(
  32. contact_ids,
  33. {
  34. email: 1,
  35. first_name: 1,
  36. last_name: 1,
  37. holdingAccount: 1,
  38. },
  39. function (error, contacts) {
  40. if (error != null) {
  41. return next(error)
  42. }
  43. // UserGetter.getUsers may not preserve order so put them back in order
  44. const positions = {}
  45. for (let i = 0; i < contact_ids.length; i++) {
  46. const contact_id = contact_ids[i]
  47. positions[contact_id] = i
  48. }
  49. contacts.sort(
  50. (a, b) =>
  51. positions[a._id != null ? a._id.toString() : undefined] -
  52. positions[b._id != null ? b._id.toString() : undefined]
  53. )
  54. // Don't count holding accounts to discourage users from repeating mistakes (mistyped or wrong emails, etc)
  55. contacts = contacts.filter(c => !c.holdingAccount)
  56. contacts = contacts.map(ContactsController._formatContact)
  57. return Modules.hooks.fire(
  58. 'getContacts',
  59. user_id,
  60. contacts,
  61. function (error, additional_contacts) {
  62. if (error != null) {
  63. return next(error)
  64. }
  65. contacts = contacts.concat(
  66. ...Array.from(additional_contacts || [])
  67. )
  68. return res.send({
  69. contacts,
  70. })
  71. }
  72. )
  73. }
  74. )
  75. }
  76. )
  77. },
  78. _formatContact(contact) {
  79. return {
  80. id: contact._id != null ? contact._id.toString() : undefined,
  81. email: contact.email || '',
  82. first_name: contact.first_name || '',
  83. last_name: contact.last_name || '',
  84. type: 'user',
  85. }
  86. },
  87. }