2019-11-20 06:35:55 -05:00
|
|
|
const request = require('request')
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-11-20 06:35:55 -05:00
|
|
|
const OError = require('@overleaf/o-error')
|
|
|
|
|
|
|
|
const TIMEOUT = 10 * 1000
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getUserDictionary(userId, callback) {
|
|
|
|
const url = `${Settings.apis.spelling.url}/user/${userId}`
|
|
|
|
request.get({ url: url, timeout: TIMEOUT }, (error, response) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(
|
2020-08-11 05:28:29 -04:00
|
|
|
OError.tag(error, 'error getting user dictionary', { error, userId })
|
2019-11-20 06:35:55 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
|
|
return callback(
|
2020-08-11 05:28:29 -04:00
|
|
|
new OError(
|
|
|
|
'Non-success code from spelling API when getting user dictionary',
|
|
|
|
{ userId, statusCode: response.statusCode }
|
|
|
|
)
|
2019-11-20 06:35:55 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, JSON.parse(response.body))
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteWordFromUserDictionary(userId, word, callback) {
|
|
|
|
const url = `${Settings.apis.spelling.url}/user/${userId}/unlearn`
|
|
|
|
request.post(
|
|
|
|
{
|
|
|
|
url: url,
|
|
|
|
json: {
|
2021-04-27 03:52:58 -04:00
|
|
|
word,
|
2019-11-20 06:35:55 -05:00
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
timeout: TIMEOUT,
|
2019-11-20 06:35:55 -05:00
|
|
|
},
|
|
|
|
(error, response) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(
|
2020-08-11 05:28:29 -04:00
|
|
|
OError.tag(error, 'error deleting word from user dictionary', {
|
|
|
|
userId,
|
2021-04-27 03:52:58 -04:00
|
|
|
word,
|
2020-08-11 05:28:29 -04:00
|
|
|
})
|
2019-11-20 06:35:55 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
|
|
return callback(
|
2020-08-11 05:28:29 -04:00
|
|
|
new OError(
|
|
|
|
'Non-success code from spelling API when removing word from user dictionary',
|
|
|
|
{ userId, word, statusCode: response.statusCode }
|
|
|
|
)
|
2019-11-20 06:35:55 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteUserDictionary(userId, callback) {
|
|
|
|
const url = `${Settings.apis.spelling.url}/user/${userId}`
|
|
|
|
request.delete({ url: url, timeout: TIMEOUT }, (error, response) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(
|
2020-08-11 05:28:29 -04:00
|
|
|
OError.tag(error, 'error deleting user dictionary', { userId })
|
2019-11-20 06:35:55 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
|
|
return callback(
|
2020-08-11 05:28:29 -04:00
|
|
|
new OError(
|
|
|
|
'Non-success code from spelling API when removing user dictionary',
|
|
|
|
{ userId, statusCode: response.statusCode }
|
|
|
|
)
|
2019-11-20 06:35:55 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
callback()
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-11-20 06:35:55 -05:00
|
|
|
}
|