HttpController.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import logger from '@overleaf/logger'
  2. import * as ContactManager from './ContactManager.js'
  3. import { buildContactIds } from './contacts.js'
  4. const CONTACT_LIMIT = 50
  5. export function addContact(req, res, next) {
  6. const { user_id: userId } = req.params
  7. const { contact_id: contactId } = req.body
  8. if (contactId == null || contactId === '') {
  9. res.status(400).send('contact_id should be a non-blank string')
  10. return
  11. }
  12. logger.debug({ userId, contactId }, 'adding contact')
  13. Promise.all([
  14. ContactManager.touchContact(userId, contactId),
  15. ContactManager.touchContact(contactId, userId),
  16. ])
  17. .then(() => {
  18. res.sendStatus(204)
  19. })
  20. .catch(error => {
  21. next(error)
  22. })
  23. }
  24. export function getContacts(req, res, next) {
  25. const { user_id: userId } = req.params
  26. const { limit } = req.query
  27. const contactLimit =
  28. limit == null ? CONTACT_LIMIT : Math.min(parseInt(limit, 10), CONTACT_LIMIT)
  29. logger.debug({ userId }, 'getting contacts')
  30. ContactManager.getContacts(userId)
  31. .then(contacts => {
  32. res.json({
  33. contact_ids: buildContactIds(contacts, contactLimit),
  34. })
  35. })
  36. .catch(error => {
  37. next(error)
  38. })
  39. }