HistoryManager.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const { callbackify } = require('util')
  2. const fetch = require('node-fetch')
  3. const settings = require('@overleaf/settings')
  4. const OError = require('@overleaf/o-error')
  5. const UserGetter = require('../User/UserGetter')
  6. async function initializeProject(projectId) {
  7. const response = await fetch(`${settings.apis.project_history.url}/project`, {
  8. method: 'POST',
  9. headers: {
  10. 'Content-Type': 'application/json',
  11. Accept: 'application/json',
  12. },
  13. body: JSON.stringify({ historyId: projectId.toString() }),
  14. })
  15. if (!response.ok) {
  16. throw new OError('failed to initialize project history', {
  17. statusCode: response.status,
  18. })
  19. }
  20. const body = await response.json()
  21. const historyId = body && body.project && body.project.id
  22. if (!historyId) {
  23. throw new OError('project-history did not provide an id', { body })
  24. }
  25. return historyId
  26. }
  27. async function flushProject(projectId) {
  28. const response = await fetch(
  29. `${settings.apis.project_history.url}/project/${projectId}/flush`,
  30. { method: 'POST' }
  31. )
  32. if (!response.ok) {
  33. throw new OError('failed to flush project to project history', {
  34. projectId,
  35. statusCode: response.status,
  36. })
  37. }
  38. }
  39. async function deleteProjectHistory(projectId) {
  40. const response = await fetch(
  41. `${settings.apis.project_history.url}/project/${projectId}`,
  42. { method: 'DELETE' }
  43. )
  44. if (!response.ok) {
  45. throw new OError('failed to delete project history', {
  46. projectId,
  47. statusCode: response.status,
  48. })
  49. }
  50. }
  51. async function resyncProject(projectId, options = {}) {
  52. const body = {}
  53. if (options.force) {
  54. body.force = options.force
  55. }
  56. if (options.origin) {
  57. body.origin = options.origin
  58. }
  59. const response = await fetch(
  60. `${settings.apis.project_history.url}/project/${projectId}/resync`,
  61. {
  62. method: 'POST',
  63. headers: {
  64. 'Content-Type': 'application/json',
  65. },
  66. body: JSON.stringify(body),
  67. signal: AbortSignal.timeout(6 * 60 * 1000),
  68. }
  69. )
  70. if (!response.ok) {
  71. throw new OError('failed to resync project history', {
  72. projectId,
  73. statusCode: response.status,
  74. })
  75. }
  76. }
  77. async function deleteProject(projectId, historyId) {
  78. const tasks = []
  79. tasks.push(_deleteProjectInProjectHistory(projectId))
  80. if (historyId != null) {
  81. tasks.push(_deleteProjectInFullProjectHistory(historyId))
  82. }
  83. await Promise.all(tasks)
  84. }
  85. async function _deleteProjectInProjectHistory(projectId) {
  86. const response = await fetch(
  87. `${settings.apis.project_history.url}/project/${projectId}`,
  88. { method: 'DELETE' }
  89. )
  90. if (!response.ok) {
  91. throw new OError('failed to clear project history in project-history', {
  92. projectId,
  93. statusCode: response.status,
  94. })
  95. }
  96. }
  97. async function _deleteProjectInFullProjectHistory(historyId) {
  98. const response = await fetch(
  99. `${settings.apis.v1_history.url}/projects/${historyId}`,
  100. {
  101. method: 'DELETE',
  102. headers: {
  103. Authorization:
  104. 'Basic ' +
  105. Buffer.from(
  106. `${settings.apis.v1_history.user}:${settings.apis.v1_history.pass}`
  107. ).toString('base64'),
  108. },
  109. }
  110. )
  111. if (!response.ok) {
  112. throw new OError('failed to clear project history', {
  113. historyId,
  114. statusCode: response.status,
  115. })
  116. }
  117. }
  118. async function injectUserDetails(data) {
  119. // data can be either:
  120. // {
  121. // diff: [{
  122. // i: "foo",
  123. // meta: {
  124. // users: ["user_id", v1_user_id, ...]
  125. // ...
  126. // }
  127. // }, ...]
  128. // }
  129. // or
  130. // {
  131. // updates: [{
  132. // pathnames: ["main.tex"]
  133. // meta: {
  134. // users: ["user_id", v1_user_id, ...]
  135. // ...
  136. // },
  137. // ...
  138. // }, ...]
  139. // }
  140. // Either way, the top level key points to an array of objects with a meta.users property
  141. // that we need to replace user_ids with populated user objects.
  142. // Note that some entries in the users arrays may be v1 ids returned by the v1 history
  143. // service. v1 ids will be `numbers`
  144. let userIds = new Set()
  145. let v1UserIds = new Set()
  146. const entries = Array.isArray(data.diff)
  147. ? data.diff
  148. : Array.isArray(data.updates)
  149. ? data.updates
  150. : []
  151. for (const entry of entries) {
  152. for (const user of (entry.meta && entry.meta.users) || []) {
  153. if (typeof user === 'string') {
  154. userIds.add(user)
  155. } else if (typeof user === 'number') {
  156. v1UserIds.add(user)
  157. }
  158. }
  159. }
  160. userIds = Array.from(userIds)
  161. v1UserIds = Array.from(v1UserIds)
  162. const projection = { first_name: 1, last_name: 1, email: 1 }
  163. const usersArray = await UserGetter.promises.getUsers(userIds, projection)
  164. const users = {}
  165. for (const user of usersArray) {
  166. users[user._id.toString()] = _userView(user)
  167. }
  168. projection.overleaf = 1
  169. const v1IdentifiedUsersArray = await UserGetter.promises.getUsersByV1Ids(
  170. v1UserIds,
  171. projection
  172. )
  173. for (const user of v1IdentifiedUsersArray) {
  174. users[user.overleaf.id] = _userView(user)
  175. }
  176. for (const entry of entries) {
  177. if (entry.meta != null) {
  178. entry.meta.users = ((entry.meta && entry.meta.users) || []).map(user => {
  179. if (typeof user === 'string' || typeof user === 'number') {
  180. return users[user]
  181. } else {
  182. return user
  183. }
  184. })
  185. }
  186. }
  187. return data
  188. }
  189. function _userView(user) {
  190. const { _id, first_name: firstName, last_name: lastName, email } = user
  191. return { first_name: firstName, last_name: lastName, email, id: _id }
  192. }
  193. module.exports = {
  194. initializeProject: callbackify(initializeProject),
  195. flushProject: callbackify(flushProject),
  196. resyncProject: callbackify(resyncProject),
  197. deleteProject: callbackify(deleteProject),
  198. deleteProjectHistory: callbackify(deleteProjectHistory),
  199. injectUserDetails: callbackify(injectUserDetails),
  200. promises: {
  201. initializeProject,
  202. flushProject,
  203. resyncProject,
  204. deleteProject,
  205. injectUserDetails,
  206. deleteProjectHistory,
  207. },
  208. }