2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let V1SubscriptionManager
|
|
|
|
const UserGetter = require('../User/UserGetter')
|
2021-10-27 04:24:27 -04:00
|
|
|
const request = require('requestretry')
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
const { V1ConnectionError, NotFoundError } = require('../Errors/Errors')
|
2021-09-20 08:20:14 -04:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = V1SubscriptionManager = {
|
|
|
|
getSubscriptionsFromV1(userId, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return V1SubscriptionManager._v1Request(
|
|
|
|
userId,
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url(v1Id) {
|
|
|
|
return `/api/v1/sharelatex/users/${v1Id}/subscriptions`
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getSubscriptionStatusFromV1(userId, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return V1SubscriptionManager._v1Request(
|
|
|
|
userId,
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url(v1Id) {
|
|
|
|
return `/api/v1/sharelatex/users/${v1Id}/subscription_status`
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelV1Subscription(userId, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return V1SubscriptionManager._v1Request(
|
|
|
|
userId,
|
|
|
|
{
|
|
|
|
method: 'DELETE',
|
|
|
|
url(v1Id) {
|
|
|
|
return `/api/v1/sharelatex/users/${v1Id}/subscription`
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
v1IdForUser(userId, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return UserGetter.getUser(
|
|
|
|
userId,
|
|
|
|
{ 'overleaf.id': 1 },
|
|
|
|
function (err, user) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const v1Id = __guard__(
|
|
|
|
user != null ? user.overleaf : undefined,
|
|
|
|
x => x.id
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return callback(null, v1Id)
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// v1 accounts created before migration to v2 had github and mendeley for free
|
|
|
|
// but these are now paid-for features for new accounts (v1id > cutoff)
|
|
|
|
getGrandfatheredFeaturesForV1User(v1Id) {
|
|
|
|
const cutoff = settings.v1GrandfatheredFeaturesUidCutoff
|
|
|
|
if (cutoff == null) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
if (v1Id == null) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v1Id < cutoff) {
|
|
|
|
return settings.v1GrandfatheredFeatures || {}
|
|
|
|
} else {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_v1Request(userId, options, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-07-15 09:09:04 -04:00
|
|
|
if (!settings.apis.v1.url) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(null, null)
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return V1SubscriptionManager.v1IdForUser(userId, function (err, v1Id) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (v1Id == null) {
|
|
|
|
return callback(null, null, null)
|
|
|
|
}
|
2020-09-28 06:50:23 -04:00
|
|
|
const url = options.url(v1Id)
|
2021-10-27 04:24:27 -04:00
|
|
|
const requestOptions = {
|
|
|
|
baseUrl: settings.apis.v1.url,
|
|
|
|
url,
|
|
|
|
method: options.method,
|
|
|
|
auth: {
|
|
|
|
user: settings.apis.v1.user,
|
|
|
|
pass: settings.apis.v1.pass,
|
|
|
|
sendImmediately: true,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-10-27 04:24:27 -04:00
|
|
|
json: true,
|
2022-03-07 09:13:00 -05:00
|
|
|
timeout: settings.apis.v1.timeout,
|
2021-10-27 04:24:27 -04:00
|
|
|
}
|
|
|
|
if (options.method === 'GET') {
|
|
|
|
requestOptions.maxAttempts = 3
|
|
|
|
requestOptions.retryDelay = 500
|
|
|
|
} else {
|
|
|
|
requestOptions.maxAttempts = 0
|
|
|
|
}
|
|
|
|
request(requestOptions, function (error, response, body) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(
|
|
|
|
new V1ConnectionError({
|
|
|
|
message: 'no v1 connection',
|
|
|
|
info: { url },
|
|
|
|
}).withCause(error)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (response && response.statusCode >= 500) {
|
|
|
|
return callback(
|
|
|
|
new V1ConnectionError({
|
|
|
|
message: 'error from v1',
|
|
|
|
info: {
|
|
|
|
status: response.statusCode,
|
|
|
|
body: body,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
|
|
return callback(null, body, v1Id)
|
|
|
|
} else {
|
|
|
|
if (response.statusCode === 404) {
|
|
|
|
return callback(new NotFoundError(`v1 user not found: ${userId}`))
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2021-10-27 04:24:27 -04:00
|
|
|
return callback(
|
|
|
|
new Error(
|
|
|
|
`non-success code from v1: ${response.statusCode} ${
|
|
|
|
options.method
|
|
|
|
} ${options.url(v1Id)}`
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2021-10-27 04:24:27 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 04:24:27 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|
2021-09-20 08:20:14 -04:00
|
|
|
|
|
|
|
module.exports.promises = promisifyAll(module.exports, {
|
|
|
|
without: ['getGrandfatheredFeaturesForV1User'],
|
|
|
|
})
|