HistoryManager.js 6.2 KB

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