2020-02-17 02:37:55 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 02:37:54 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 02:38:00 -05:00
|
|
|
let HttpController
|
|
|
|
const ContactManager = require('./ContactManager')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:00 -05:00
|
|
|
module.exports = HttpController = {
|
|
|
|
addContact(req, res, next) {
|
|
|
|
const { user_id } = req.params
|
|
|
|
const { contact_id } = req.body
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2020-02-17 02:38:00 -05:00
|
|
|
if (contact_id == null || contact_id === '') {
|
|
|
|
res.status(400).send('contact_id should be a non-blank string')
|
|
|
|
return
|
|
|
|
}
|
2015-10-06 11:41:35 -04:00
|
|
|
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ user_id, contact_id }, 'adding contact')
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return ContactManager.touchContact(user_id, contact_id, function (error) {
|
2020-02-17 02:38:00 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-06-04 03:49:46 -04:00
|
|
|
return ContactManager.touchContact(contact_id, user_id, function (error) {
|
2020-02-17 02:38:00 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2020-03-18 10:08:14 -04:00
|
|
|
return res.sendStatus(204)
|
2020-02-17 02:38:00 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:00 -05:00
|
|
|
CONTACT_LIMIT: 50,
|
|
|
|
getContacts(req, res, next) {
|
|
|
|
let limit
|
|
|
|
let { user_id } = req.params
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-02-17 02:38:00 -05:00
|
|
|
if ((req.query != null ? req.query.limit : undefined) != null) {
|
|
|
|
limit = parseInt(req.query.limit, 10)
|
|
|
|
} else {
|
|
|
|
limit = HttpController.CONTACT_LIMIT
|
|
|
|
}
|
|
|
|
limit = Math.min(limit, HttpController.CONTACT_LIMIT)
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ user_id }, 'getting contacts')
|
2015-10-06 12:22:11 -04:00
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return ContactManager.getContacts(user_id, function (error, contact_dict) {
|
2020-02-17 02:38:00 -05:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
let contacts = []
|
|
|
|
const object = contact_dict || {}
|
|
|
|
for (user_id in object) {
|
|
|
|
const data = object[user_id]
|
|
|
|
contacts.push({
|
|
|
|
user_id,
|
|
|
|
n: data.n,
|
2021-07-13 07:04:47 -04:00
|
|
|
ts: data.ts,
|
2020-02-17 02:38:00 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpController._sortContacts(contacts)
|
|
|
|
contacts = contacts.slice(0, limit)
|
2021-07-13 07:04:47 -04:00
|
|
|
const contact_ids = contacts.map(contact => contact.user_id)
|
2020-02-17 02:38:00 -05:00
|
|
|
|
|
|
|
return res.status(200).send({
|
2021-07-13 07:04:47 -04:00
|
|
|
contact_ids,
|
2020-02-17 02:38:00 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_sortContacts(contacts) {
|
2020-06-04 03:49:46 -04:00
|
|
|
return contacts.sort(function (a, b) {
|
2020-02-17 02:38:00 -05:00
|
|
|
// Sort by decreasing count, descreasing timestamp.
|
|
|
|
// I.e. biggest count, and most recent at front.
|
|
|
|
if (a.n > b.n) {
|
|
|
|
return -1
|
|
|
|
} else if (a.n < b.n) {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
if (a.ts > b.ts) {
|
|
|
|
return -1
|
|
|
|
} else if (a.ts < b.ts) {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-07-13 07:04:47 -04:00
|
|
|
},
|
2020-02-17 02:38:00 -05:00
|
|
|
}
|