ProjectHistoryClient.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { expect } from 'chai'
  2. import Settings from '@overleaf/settings'
  3. import RedisWrapper from '@overleaf/redis-wrapper'
  4. import { db } 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 createLabel(
  146. projectId,
  147. userId,
  148. version,
  149. comment,
  150. createdAt
  151. ) {
  152. return await fetchJson(`http://127.0.0.1:3054/project/${projectId}/labels`, {
  153. method: 'POST',
  154. json: { comment, version, created_at: createdAt, user_id: userId },
  155. })
  156. }
  157. export async function getLabels(projectId) {
  158. return await fetchJson(`http://127.0.0.1:3054/project/${projectId}/labels`)
  159. }
  160. export async function deleteLabelForUser(projectId, userId, labelId) {
  161. const response = await fetchNothing(
  162. `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels/${labelId}`,
  163. { method: 'DELETE' }
  164. )
  165. expect(response.status).to.equal(204)
  166. }
  167. export async function deleteLabel(projectId, labelId) {
  168. const response = await fetchNothing(
  169. `http://127.0.0.1:3054/project/${projectId}/labels/${labelId}`,
  170. { method: 'DELETE' }
  171. )
  172. expect(response.status).to.equal(204)
  173. }
  174. export async function setFailure(failureEntry) {
  175. await db.projectHistoryFailures.deleteOne({ project_id: { $exists: true } })
  176. return await db.projectHistoryFailures.insertOne(failureEntry)
  177. }
  178. export function getFailure(projectId, callback) {
  179. db.projectHistoryFailures.findOne({ project_id: projectId }, callback)
  180. }
  181. export async function transferLabelOwnership(fromUser, toUser) {
  182. const response = await fetchNothing(
  183. `http://127.0.0.1:3054/user/${fromUser}/labels/transfer/${toUser}`,
  184. { method: 'POST' }
  185. )
  186. expect(response.status).to.equal(204)
  187. }
  188. export async function getDump(projectId) {
  189. return await fetchJson(`http://127.0.0.1:3054/project/${projectId}/dump`)
  190. }
  191. export async function deleteProject(projectId) {
  192. const response = await fetchNothing(
  193. `http://127.0.0.1:3054/project/${projectId}`,
  194. { method: 'DELETE' }
  195. )
  196. expect(response.status).to.equal(204)
  197. }