2015-10-07 16:32:35 +00:00
|
|
|
AuthenticationController = require "../Authentication/AuthenticationController"
|
|
|
|
ContactManager = require "./ContactManager"
|
|
|
|
UserGetter = require "../User/UserGetter"
|
|
|
|
logger = require "logger-sharelatex"
|
2015-10-08 17:17:53 +00:00
|
|
|
Modules = require "../../infrastructure/Modules"
|
2015-10-07 16:32:35 +00:00
|
|
|
|
|
|
|
module.exports = ContactsController =
|
|
|
|
getContacts: (req, res, next) ->
|
|
|
|
AuthenticationController.getLoggedInUserId req, (error, user_id) ->
|
|
|
|
return next(error) if error?
|
|
|
|
ContactManager.getContactIds user_id, {limit: 50}, (error, contact_ids) ->
|
|
|
|
return next(error) if error?
|
|
|
|
UserGetter.getUsers contact_ids, {
|
|
|
|
email: 1, first_name: 1, last_name: 1, holdingAccount: 1
|
|
|
|
}, (error, contacts) ->
|
|
|
|
return next(error) if error?
|
|
|
|
|
|
|
|
# UserGetter.getUsers may not preserve order so put them back in order
|
|
|
|
positions = {}
|
|
|
|
for contact_id, i in contact_ids
|
|
|
|
positions[contact_id] = i
|
|
|
|
contacts.sort (a,b) -> positions[a._id?.toString()] - positions[b._id?.toString()]
|
|
|
|
|
|
|
|
# Don't count holding accounts to discourage users from repeating mistakes (mistyped or wrong emails, etc)
|
|
|
|
contacts = contacts.filter (c) -> !c.holdingAccount
|
|
|
|
|
|
|
|
contacts = contacts.map(ContactsController._formatContact)
|
2015-10-08 17:17:53 +00:00
|
|
|
|
|
|
|
Modules.hooks.fire "getContacts", user_id, contacts, (error, additional_contacts) ->
|
|
|
|
return next(error) if error?
|
|
|
|
contacts = contacts.concat(additional_contacts...)
|
|
|
|
res.send({
|
|
|
|
contacts: contacts
|
|
|
|
})
|
2015-10-07 16:32:35 +00:00
|
|
|
|
|
|
|
_formatContact: (contact) ->
|
|
|
|
return {
|
|
|
|
id: contact._id?.toString()
|
|
|
|
email: contact.email
|
|
|
|
first_name: contact.first_name
|
|
|
|
last_name: contact.last_name
|
2015-10-08 17:17:53 +00:00
|
|
|
type: "user"
|
2015-10-07 16:32:35 +00:00
|
|
|
}
|