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