ProjectHistoryClient.js 6.5 KB

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