V1SubscriptionManager.coffee 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. UserGetter = require "../User/UserGetter"
  2. request = require "request"
  3. settings = require "settings-sharelatex"
  4. logger = require "logger-sharelatex"
  5. { V1ConnectionError } = require "../Errors/Errors"
  6. module.exports = V1SubscriptionManager =
  7. # Returned planCode = 'v1_pro' | 'v1_pro_plus' | 'v1_student' | 'v1_free' | null
  8. # For this to work, we need plans in settings with plan-codes:
  9. # - 'v1_pro'
  10. # - 'v1_pro_plus'
  11. # - 'v1_student'
  12. # - 'v1_free'
  13. getPlanCodeFromV1: (userId, callback=(err, planCode, v1Id)->) ->
  14. logger.log {userId}, "[V1SubscriptionManager] fetching v1 plan for user"
  15. V1SubscriptionManager._v1Request userId, {
  16. method: 'GET',
  17. url: (v1Id) -> "/api/v1/sharelatex/users/#{v1Id}/plan_code"
  18. }, (error, body, v1Id) ->
  19. return callback(error) if error?
  20. planName = body?.plan_name
  21. logger.log {userId, planName, body}, "[V1SubscriptionManager] fetched v1 plan for user"
  22. if planName in ['pro', 'pro_plus', 'student', 'free']
  23. planName = "v1_#{planName}"
  24. else
  25. # Throw away 'anonymous', etc as being equivalent to null
  26. planName = null
  27. return callback(null, planName, v1Id)
  28. notifyV1OfFeaturesChange: (userId, callback = (error) ->) ->
  29. V1SubscriptionManager._v1Request userId, {
  30. method: 'POST',
  31. url: (v1Id) -> "/api/v1/sharelatex/users/#{v1Id}/sync"
  32. }, callback
  33. getSubscriptionsFromV1: (userId, callback=(err, subscriptions, v1Id) ->) ->
  34. V1SubscriptionManager._v1Request userId, {
  35. method: 'GET',
  36. url: (v1Id) -> "/api/v1/sharelatex/users/#{v1Id}/subscriptions"
  37. }, callback
  38. v1IdForUser: (userId, callback=(err, v1Id) ->) ->
  39. UserGetter.getUser userId, {'overleaf.id': 1}, (err, user) ->
  40. return callback(err) if err?
  41. v1Id = user?.overleaf?.id
  42. if !v1Id?
  43. logger.log {userId}, "[V1SubscriptionManager] no v1 id found for user"
  44. callback(null, v1Id)
  45. # v1 accounts created before migration to v2 had github and mendeley for free
  46. # but these are now paid-for features for new accounts (v1id > cutoff)
  47. getGrandfatheredFeaturesForV1User: (v1Id) ->
  48. cutoff = settings.v1GrandfatheredFeaturesUidCutoff
  49. return {} if !cutoff?
  50. return {} if !v1Id?
  51. if (v1Id < cutoff)
  52. return settings.v1GrandfatheredFeatures or {}
  53. else
  54. return {}
  55. _v1Request: (userId, options, callback=(err, body, v1Id)->) ->
  56. if !settings?.apis?.v1
  57. return callback null, null
  58. V1SubscriptionManager.v1IdForUser userId, (err, v1Id) ->
  59. return callback(err) if err?
  60. return callback(null, null, null) if !v1Id?
  61. request {
  62. baseUrl: settings.apis.v1.url
  63. url: options.url(v1Id)
  64. method: options.method
  65. auth:
  66. user: settings.apis.v1.user
  67. pass: settings.apis.v1.pass
  68. sendImmediately: true
  69. json: true,
  70. timeout: 5 * 1000
  71. }, (error, response, body) ->
  72. if error?
  73. # Specially handle no connection err, so warning can be shown
  74. error = new V1ConnectionError('No V1 connection') if error.code == 'ECONNREFUSED'
  75. return callback(error)
  76. if 200 <= response.statusCode < 300
  77. return callback null, body, v1Id
  78. else
  79. return callback new Error("non-success code from v1: #{response.statusCode}")