HistoryManager.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const { callbackify } = require('util')
  2. const request = require('request-promise-native')
  3. const settings = require('@overleaf/settings')
  4. const OError = require('@overleaf/o-error')
  5. const UserGetter = require('../User/UserGetter')
  6. module.exports = {
  7. initializeProject: callbackify(initializeProject),
  8. flushProject: callbackify(flushProject),
  9. resyncProject: callbackify(resyncProject),
  10. deleteProject: callbackify(deleteProject),
  11. injectUserDetails: callbackify(injectUserDetails),
  12. promises: {
  13. initializeProject,
  14. flushProject,
  15. resyncProject,
  16. deleteProject,
  17. injectUserDetails,
  18. },
  19. }
  20. async function initializeProject() {
  21. if (
  22. !(
  23. settings.apis.project_history &&
  24. settings.apis.project_history.initializeHistoryForNewProjects
  25. )
  26. ) {
  27. return
  28. }
  29. try {
  30. const body = await request.post({
  31. url: `${settings.apis.project_history.url}/project`,
  32. })
  33. const project = JSON.parse(body)
  34. const overleafId = project && project.project && project.project.id
  35. if (!overleafId) {
  36. throw new Error('project-history did not provide an id', project)
  37. }
  38. return { overleaf_id: overleafId }
  39. } catch (err) {
  40. throw OError.tag(err, 'failed to initialize project history')
  41. }
  42. }
  43. async function flushProject(projectId) {
  44. try {
  45. await request.post({
  46. url: `${settings.apis.project_history.url}/project/${projectId}/flush`,
  47. })
  48. } catch (err) {
  49. throw OError.tag(err, 'failed to flush project to project history', {
  50. projectId,
  51. })
  52. }
  53. }
  54. async function resyncProject(projectId) {
  55. try {
  56. await request.post({
  57. url: `${settings.apis.project_history.url}/project/${projectId}/resync`,
  58. })
  59. } catch (err) {
  60. throw OError.tag(err, 'failed to resync project history', { projectId })
  61. }
  62. }
  63. async function deleteProject(projectId, historyId) {
  64. try {
  65. const tasks = [
  66. request.delete(
  67. `${settings.apis.project_history.url}/project/${projectId}`
  68. ),
  69. ]
  70. if (historyId != null) {
  71. tasks.push(
  72. request.delete({
  73. url: `${settings.apis.v1_history.url}/projects/${historyId}`,
  74. auth: {
  75. user: settings.apis.v1_history.user,
  76. pass: settings.apis.v1_history.pass,
  77. },
  78. })
  79. )
  80. }
  81. await Promise.all(tasks)
  82. } catch (err) {
  83. throw OError.tag(err, 'failed to clear project history', {
  84. projectId,
  85. historyId,
  86. })
  87. }
  88. }
  89. async function injectUserDetails(data) {
  90. // data can be either:
  91. // {
  92. // diff: [{
  93. // i: "foo",
  94. // meta: {
  95. // users: ["user_id", v1_user_id, ...]
  96. // ...
  97. // }
  98. // }, ...]
  99. // }
  100. // or
  101. // {
  102. // updates: [{
  103. // pathnames: ["main.tex"]
  104. // meta: {
  105. // users: ["user_id", v1_user_id, ...]
  106. // ...
  107. // },
  108. // ...
  109. // }, ...]
  110. // }
  111. // Either way, the top level key points to an array of objects with a meta.users property
  112. // that we need to replace user_ids with populated user objects.
  113. // Note that some entries in the users arrays may be v1 ids returned by the v1 history
  114. // service. v1 ids will be `numbers`
  115. let userIds = new Set()
  116. let v1UserIds = new Set()
  117. const entries = Array.isArray(data.diff)
  118. ? data.diff
  119. : Array.isArray(data.updates)
  120. ? data.updates
  121. : []
  122. for (const entry of entries) {
  123. for (const user of (entry.meta && entry.meta.users) || []) {
  124. if (typeof user === 'string') {
  125. userIds.add(user)
  126. } else if (typeof user === 'number') {
  127. v1UserIds.add(user)
  128. }
  129. }
  130. }
  131. userIds = Array.from(userIds)
  132. v1UserIds = Array.from(v1UserIds)
  133. const projection = { first_name: 1, last_name: 1, email: 1 }
  134. const usersArray = await UserGetter.promises.getUsers(userIds, projection)
  135. const users = {}
  136. for (const user of usersArray) {
  137. users[user._id.toString()] = _userView(user)
  138. }
  139. projection.overleaf = 1
  140. const v1IdentifiedUsersArray = await UserGetter.promises.getUsersByV1Ids(
  141. v1UserIds,
  142. projection
  143. )
  144. for (const user of v1IdentifiedUsersArray) {
  145. users[user.overleaf.id] = _userView(user)
  146. }
  147. for (const entry of entries) {
  148. if (entry.meta != null) {
  149. entry.meta.users = ((entry.meta && entry.meta.users) || []).map(user => {
  150. if (typeof user === 'string' || typeof user === 'number') {
  151. return users[user]
  152. } else {
  153. return user
  154. }
  155. })
  156. }
  157. }
  158. return data
  159. }
  160. function _userView(user) {
  161. const { _id, first_name: firstName, last_name: lastName, email } = user
  162. return { first_name: firstName, last_name: lastName, email, id: _id }
  163. }