ProjectHistoryClient.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { expect } from 'chai'
  2. import Settings from '@overleaf/settings'
  3. import RedisWrapper from '@overleaf/redis-wrapper'
  4. import { db, ObjectId } from '../../../../app/js/mongodb.js'
  5. import {
  6. fetchJson,
  7. fetchJsonWithResponse,
  8. fetchNothing,
  9. fetchStringWithResponse,
  10. RequestFailedError,
  11. } from '@overleaf/fetch-utils'
  12. const rclient = RedisWrapper.createClient(Settings.redis.project_history)
  13. const Keys = Settings.redis.project_history.key_schema
  14. export function resetDatabase(callback) {
  15. rclient.flushdb(callback)
  16. }
  17. export async function initializeProject(historyId) {
  18. const response = await fetchJsonWithResponse(
  19. 'http://127.0.0.1:3054/project',
  20. {
  21. method: 'POST',
  22. json: { historyId },
  23. }
  24. )
  25. expect(response.response.status).to.equal(200)
  26. return response.json.project
  27. }
  28. export async function flushProject(projectId, options = {}) {
  29. try {
  30. const response = await fetchNothing(
  31. `http://127.0.0.1:3054/project/${projectId}/flush`,
  32. { method: 'POST' }
  33. )
  34. if (!options.allowErrors) {
  35. expect(response.status).to.equal(204)
  36. }
  37. return { statusCode: response.status }
  38. } catch (error) {
  39. if (options.allowErrors && error instanceof RequestFailedError) {
  40. return { statusCode: error.response.status }
  41. }
  42. throw error
  43. }
  44. }
  45. export async function getSummarizedUpdates(projectId, query) {
  46. const url = new URL(`http://127.0.0.1:3054/project/${projectId}/updates`)
  47. Object.keys(query).forEach(key => {
  48. url.searchParams.set(key, query[key])
  49. })
  50. return await fetchJson(url.toString())
  51. }
  52. export async function getDiff(projectId, pathname, from, to) {
  53. const url = new URL(`http://127.0.0.1:3054/project/${projectId}/diff`)
  54. url.searchParams.set('pathname', pathname)
  55. url.searchParams.set('from', from)
  56. url.searchParams.set('to', to)
  57. return await fetchJson(url.toString())
  58. }
  59. export async function getFileTreeDiff(projectId, from, to) {
  60. const url = new URL(
  61. `http://127.0.0.1:3054/project/${projectId}/filetree/diff`
  62. )
  63. url.searchParams.set('from', from)
  64. url.searchParams.set('to', to)
  65. try {
  66. const { response, json } = await fetchJsonWithResponse(url.toString())
  67. return { diff: json, statusCode: response.status }
  68. } catch (error) {
  69. if (error instanceof RequestFailedError) {
  70. return { diff: null, statusCode: error.response.status }
  71. }
  72. throw error
  73. }
  74. }
  75. export async function getChangesInChunkSince(projectId, since, options = {}) {
  76. const url = new URL(
  77. `http://127.0.0.1:3054/project/${projectId}/changes-in-chunk`
  78. )
  79. url.searchParams.set('since', since)
  80. try {
  81. const { response, json } = await fetchJsonWithResponse(url.toString())
  82. return { body: json, statusCode: response.status }
  83. } catch (error) {
  84. if (options.allowErrors && error instanceof RequestFailedError) {
  85. return { body: null, statusCode: error.response.status }
  86. }
  87. throw error
  88. }
  89. }
  90. export async function getLatestSnapshot(projectId) {
  91. return await fetchJson(`http://127.0.0.1:3054/project/${projectId}/snapshot`)
  92. }
  93. export async function getSnapshot(projectId, pathname, version, options = {}) {
  94. const url = `http://127.0.0.1:3054/project/${projectId}/version/${version}/${encodeURIComponent(
  95. pathname
  96. )}`
  97. try {
  98. const { response, body } = await fetchStringWithResponse(url)
  99. if (!options.allowErrors) {
  100. expect(response.status).to.equal(200)
  101. }
  102. return { body, statusCode: response.status }
  103. } catch (error) {
  104. if (options.allowErrors && error instanceof RequestFailedError) {
  105. return { body: null, statusCode: error.response.status }
  106. }
  107. throw error
  108. }
  109. }
  110. export async function pushRawUpdate(projectId, update) {
  111. await rclient.rpush(
  112. Keys.projectHistoryOps({ project_id: projectId }),
  113. JSON.stringify(update)
  114. )
  115. }
  116. export async function setFirstOpTimestamp(projectId, timestamp) {
  117. await rclient.set(
  118. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
  119. timestamp
  120. )
  121. }
  122. export async function getFirstOpTimestamp(projectId) {
  123. return await rclient.get(
  124. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId })
  125. )
  126. }
  127. export async function clearFirstOpTimestamp(projectId) {
  128. await rclient.del(
  129. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId })
  130. )
  131. }
  132. export function getQueueLength(projectId, callback) {
  133. rclient.llen(Keys.projectHistoryOps({ project_id: projectId }), callback)
  134. }
  135. export async function resyncHistory(projectId) {
  136. const response = await fetchNothing(
  137. `http://127.0.0.1:3054/project/${projectId}/resync`,
  138. {
  139. method: 'POST',
  140. json: { origin: { kind: 'test-origin' } },
  141. }
  142. )
  143. expect(response.status).to.equal(204)
  144. }
  145. export async function hardResyncHistory(
  146. projectId,
  147. { recoverCorruptedFiles } = {}
  148. ) {
  149. const params = new URLSearchParams({ force: 'true' })
  150. if (recoverCorruptedFiles) {
  151. params.set('recoverCorruptedFiles', 'true')
  152. }
  153. const response = await fetchNothing(
  154. `http://127.0.0.1:3054/project/${projectId}/resync?${params}`,
  155. {
  156. method: 'POST',
  157. json: { origin: { kind: 'test-origin' } },
  158. }
  159. )
  160. expect(response.status).to.equal(204)
  161. }
  162. export async function createLabel(
  163. projectId,
  164. userId,
  165. version,
  166. comment,
  167. createdAt
  168. ) {
  169. return await fetchJson(`http://127.0.0.1:3054/project/${projectId}/labels`, {
  170. method: 'POST',
  171. json: { comment, version, created_at: createdAt, user_id: userId },
  172. })
  173. }
  174. export async function getLabels(projectId) {
  175. return await fetchJson(`http://127.0.0.1:3054/project/${projectId}/labels`)
  176. }
  177. export async function deleteLabelForUser(projectId, userId, labelId) {
  178. const response = await fetchNothing(
  179. `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels/${labelId}`,
  180. { method: 'DELETE' }
  181. )
  182. expect(response.status).to.equal(204)
  183. }
  184. export async function deleteLabel(projectId, labelId) {
  185. const response = await fetchNothing(
  186. `http://127.0.0.1:3054/project/${projectId}/labels/${labelId}`,
  187. { method: 'DELETE' }
  188. )
  189. expect(response.status).to.equal(204)
  190. }
  191. export async function setFailures(failureEntries) {
  192. await db.projectHistoryFailures.deleteMany({})
  193. return await db.projectHistoryFailures.insertMany(failureEntries)
  194. }
  195. export function getFailure(projectId, callback) {
  196. db.projectHistoryFailures.findOne({ project_id: projectId }, callback)
  197. }
  198. export async function transferLabelOwnership(fromUser, toUser) {
  199. const response = await fetchNothing(
  200. `http://127.0.0.1:3054/user/${fromUser}/labels/transfer/${toUser}`,
  201. { method: 'POST' }
  202. )
  203. expect(response.status).to.equal(204)
  204. }
  205. export async function getDump(projectId) {
  206. return await fetchJson(`http://127.0.0.1:3054/project/${projectId}/dump`)
  207. }
  208. export async function getFailures() {
  209. const { failures } = await fetchJson('http://127.0.0.1:3054/status/failures')
  210. return failures
  211. }
  212. export async function getSyncState(projectId) {
  213. return await db.projectHistorySyncState.findOne({
  214. project_id: new ObjectId(projectId),
  215. })
  216. }
  217. export async function getResyncPending(projectId) {
  218. return await fetchJson(
  219. `http://127.0.0.1:3054/project/${projectId}/resync-pending`
  220. )
  221. }
  222. export async function injectStuckSyncState(projectId, docPaths) {
  223. await db.projectHistorySyncState.replaceOne(
  224. { project_id: new ObjectId(projectId) },
  225. {
  226. project_id: new ObjectId(projectId),
  227. resyncProjectStructure: false,
  228. resyncDocContents: docPaths,
  229. stuckClearCount: 0,
  230. history: [],
  231. },
  232. { upsert: true }
  233. )
  234. }
  235. export async function deleteProject(projectId) {
  236. const response = await fetchNothing(
  237. `http://127.0.0.1:3054/project/${projectId}`,
  238. { method: 'DELETE' }
  239. )
  240. expect(response.status).to.equal(204)
  241. }