2022-12-16 05:42:39 -05:00
|
|
|
import logger from '@overleaf/logger'
|
|
|
|
import * as ContactManager from './ContactManager.js'
|
|
|
|
import { buildContactIds } from './contacts.js'
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
const CONTACT_LIMIT = 50
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
export function addContact(req, res, next) {
|
|
|
|
const { user_id: userId } = req.params
|
|
|
|
const { contact_id: contactId } = req.body
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
if (contactId == null || contactId === '') {
|
|
|
|
res.status(400).send('contact_id should be a non-blank string')
|
|
|
|
return
|
|
|
|
}
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
logger.debug({ user_id: userId, contact_id: contactId }, 'adding contact')
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
Promise.all([
|
|
|
|
ContactManager.touchContact(userId, contactId),
|
|
|
|
ContactManager.touchContact(contactId, userId),
|
|
|
|
])
|
|
|
|
.then(() => {
|
|
|
|
res.sendStatus(204)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
next(error)
|
|
|
|
})
|
|
|
|
}
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
export function getContacts(req, res, next) {
|
|
|
|
const { user_id: userId } = req.params
|
|
|
|
const { limit } = req.query
|
2020-02-17 02:38:00 -05:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
const contactLimit =
|
|
|
|
limit == null ? CONTACT_LIMIT : Math.min(parseInt(limit, 10), CONTACT_LIMIT)
|
2020-02-17 02:38:00 -05:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
logger.debug({ user_id: userId }, 'getting contacts')
|
2020-02-17 02:38:00 -05:00
|
|
|
|
2022-12-16 05:42:39 -05:00
|
|
|
ContactManager.getContacts(userId)
|
|
|
|
.then(contacts => {
|
|
|
|
res.json({
|
|
|
|
contact_ids: buildContactIds(contacts, contactLimit),
|
2020-02-17 02:38:00 -05:00
|
|
|
})
|
|
|
|
})
|
2022-12-16 05:42:39 -05:00
|
|
|
.catch(error => {
|
|
|
|
next(error)
|
2020-02-17 02:38:00 -05:00
|
|
|
})
|
|
|
|
}
|