ProjectHistoryClient.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. const rclient = RedisWrapper.createClient(Settings.redis.project_history)
  7. const Keys = Settings.redis.project_history.key_schema
  8. export function resetDatabase(callback) {
  9. rclient.flushdb(callback)
  10. }
  11. export function initializeProject(historyId, callback) {
  12. request.post(
  13. {
  14. url: 'http://127.0.0.1:3054/project',
  15. json: { historyId },
  16. },
  17. (error, res, body) => {
  18. if (error) {
  19. return callback(error)
  20. }
  21. expect(res.statusCode).to.equal(200)
  22. callback(null, body.project)
  23. }
  24. )
  25. }
  26. export function flushProject(projectId, options, callback) {
  27. if (typeof options === 'function') {
  28. callback = options
  29. options = null
  30. }
  31. if (!options) {
  32. options = { allowErrors: false }
  33. }
  34. request.post(
  35. {
  36. url: `http://127.0.0.1:3054/project/${projectId}/flush`,
  37. },
  38. (error, res, body) => {
  39. if (error) {
  40. return callback(error)
  41. }
  42. if (!options.allowErrors) {
  43. expect(res.statusCode).to.equal(204)
  44. }
  45. callback(error, res)
  46. }
  47. )
  48. }
  49. export function getSummarizedUpdates(projectId, query, callback) {
  50. request.get(
  51. {
  52. url: `http://127.0.0.1:3054/project/${projectId}/updates`,
  53. qs: query,
  54. json: true,
  55. },
  56. (error, res, body) => {
  57. if (error) {
  58. return callback(error)
  59. }
  60. expect(res.statusCode).to.equal(200)
  61. callback(error, body)
  62. }
  63. )
  64. }
  65. export function getDiff(projectId, pathname, from, to, callback) {
  66. request.get(
  67. {
  68. url: `http://127.0.0.1:3054/project/${projectId}/diff`,
  69. qs: {
  70. pathname,
  71. from,
  72. to,
  73. },
  74. json: true,
  75. },
  76. (error, res, body) => {
  77. if (error) {
  78. return callback(error)
  79. }
  80. expect(res.statusCode).to.equal(200)
  81. callback(error, body)
  82. }
  83. )
  84. }
  85. export function getFileTreeDiff(projectId, from, to, callback) {
  86. request.get(
  87. {
  88. url: `http://127.0.0.1:3054/project/${projectId}/filetree/diff`,
  89. qs: {
  90. from,
  91. to,
  92. },
  93. json: true,
  94. },
  95. (error, res, body) => {
  96. if (error) {
  97. return callback(error)
  98. }
  99. callback(error, body, res.statusCode)
  100. }
  101. )
  102. }
  103. export function getChangesSince(projectId, since, options, callback) {
  104. request.get(
  105. {
  106. url: `http://127.0.0.1:3054/project/${projectId}/changes`,
  107. qs: {
  108. since,
  109. },
  110. json: true,
  111. },
  112. (error, res, body) => {
  113. if (error) return callback(error)
  114. if (!options.allowErrors) {
  115. expect(res.statusCode).to.equal(200)
  116. }
  117. callback(null, body, res.statusCode)
  118. }
  119. )
  120. }
  121. export function getChangesInChunkSince(projectId, since, options, callback) {
  122. request.get(
  123. {
  124. url: `http://127.0.0.1:3054/project/${projectId}/changes-in-chunk`,
  125. qs: {
  126. since,
  127. },
  128. json: true,
  129. },
  130. (error, res, body) => {
  131. if (error) return callback(error)
  132. if (!options.allowErrors) {
  133. expect(res.statusCode).to.equal(200)
  134. }
  135. callback(null, body, res.statusCode)
  136. }
  137. )
  138. }
  139. export function getLatestSnapshot(projectId, callback) {
  140. request.get(
  141. {
  142. url: `http://127.0.0.1:3054/project/${projectId}/snapshot`,
  143. json: true,
  144. },
  145. (error, res, body) => {
  146. if (error) {
  147. return callback(error)
  148. }
  149. expect(res.statusCode).to.equal(200)
  150. callback(null, body)
  151. }
  152. )
  153. }
  154. export function getSnapshot(projectId, pathname, version, options, callback) {
  155. if (typeof options === 'function') {
  156. callback = options
  157. options = null
  158. }
  159. if (!options) {
  160. options = { allowErrors: false }
  161. }
  162. request.get(
  163. {
  164. url: `http://127.0.0.1:3054/project/${projectId}/version/${version}/${encodeURIComponent(
  165. pathname
  166. )}`,
  167. },
  168. (error, res, body) => {
  169. if (error) {
  170. return callback(error)
  171. }
  172. if (!options.allowErrors) {
  173. expect(res.statusCode).to.equal(200)
  174. }
  175. callback(error, body, res.statusCode)
  176. }
  177. )
  178. }
  179. export function pushRawUpdate(projectId, update, callback) {
  180. rclient.rpush(
  181. Keys.projectHistoryOps({ project_id: projectId }),
  182. JSON.stringify(update),
  183. callback
  184. )
  185. }
  186. export function setFirstOpTimestamp(projectId, timestamp, callback) {
  187. rclient.set(
  188. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
  189. timestamp,
  190. callback
  191. )
  192. }
  193. export function getFirstOpTimestamp(projectId, callback) {
  194. rclient.get(
  195. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
  196. callback
  197. )
  198. }
  199. export function clearFirstOpTimestamp(projectId, callback) {
  200. rclient.del(
  201. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
  202. callback
  203. )
  204. }
  205. export function getQueueLength(projectId, callback) {
  206. rclient.llen(Keys.projectHistoryOps({ project_id: projectId }), callback)
  207. }
  208. export function getQueueCounts(callback) {
  209. return request.get(
  210. {
  211. url: 'http://127.0.0.1:3054/status/queue',
  212. json: true,
  213. },
  214. callback
  215. )
  216. }
  217. export function resyncHistory(projectId, callback) {
  218. request.post(
  219. {
  220. url: `http://127.0.0.1:3054/project/${projectId}/resync`,
  221. json: true,
  222. body: { origin: { kind: 'test-origin' } },
  223. },
  224. (error, res, body) => {
  225. if (error) {
  226. return callback(error)
  227. }
  228. expect(res.statusCode).to.equal(204)
  229. callback(error)
  230. }
  231. )
  232. }
  233. export function createLabel(
  234. projectId,
  235. userId,
  236. version,
  237. comment,
  238. createdAt,
  239. callback
  240. ) {
  241. request.post(
  242. {
  243. url: `http://127.0.0.1:3054/project/${projectId}/labels`,
  244. json: { comment, version, created_at: createdAt, user_id: userId },
  245. },
  246. (error, res, body) => {
  247. if (error) {
  248. return callback(error)
  249. }
  250. expect(res.statusCode).to.equal(200)
  251. callback(null, body)
  252. }
  253. )
  254. }
  255. export function getLabels(projectId, callback) {
  256. request.get(
  257. {
  258. url: `http://127.0.0.1:3054/project/${projectId}/labels`,
  259. json: true,
  260. },
  261. (error, res, body) => {
  262. if (error) {
  263. return callback(error)
  264. }
  265. expect(res.statusCode).to.equal(200)
  266. callback(null, body)
  267. }
  268. )
  269. }
  270. export function deleteLabelForUser(projectId, userId, labelId, callback) {
  271. request.delete(
  272. {
  273. url: `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels/${labelId}`,
  274. },
  275. (error, res, body) => {
  276. if (error) {
  277. return callback(error)
  278. }
  279. expect(res.statusCode).to.equal(204)
  280. callback(null, body)
  281. }
  282. )
  283. }
  284. export function deleteLabel(projectId, labelId, callback) {
  285. request.delete(
  286. {
  287. url: `http://127.0.0.1:3054/project/${projectId}/labels/${labelId}`,
  288. },
  289. (error, res, body) => {
  290. if (error) {
  291. return callback(error)
  292. }
  293. expect(res.statusCode).to.equal(204)
  294. callback(null, body)
  295. }
  296. )
  297. }
  298. export function setFailure(failureEntry, callback) {
  299. db.projectHistoryFailures.deleteOne(
  300. { project_id: { $exists: true } },
  301. (err, result) => {
  302. if (err) {
  303. return callback(err)
  304. }
  305. db.projectHistoryFailures.insertOne(failureEntry, callback)
  306. }
  307. )
  308. }
  309. export function getFailure(projectId, callback) {
  310. db.projectHistoryFailures.findOne({ project_id: projectId }, callback)
  311. }
  312. export function transferLabelOwnership(fromUser, toUser, callback) {
  313. request.post(
  314. {
  315. url: `http://127.0.0.1:3054/user/${fromUser}/labels/transfer/${toUser}`,
  316. },
  317. (error, res, body) => {
  318. if (error) {
  319. return callback(error)
  320. }
  321. expect(res.statusCode).to.equal(204)
  322. callback(null, body)
  323. }
  324. )
  325. }
  326. export function getDump(projectId, callback) {
  327. request.get(
  328. `http://127.0.0.1:3054/project/${projectId}/dump`,
  329. (err, res, body) => {
  330. if (err) {
  331. return callback(err)
  332. }
  333. expect(res.statusCode).to.equal(200)
  334. callback(null, JSON.parse(body))
  335. }
  336. )
  337. }
  338. export function deleteProject(projectId, callback) {
  339. request.delete(`http://127.0.0.1:3054/project/${projectId}`, (err, res) => {
  340. if (err) {
  341. return callback(err)
  342. }
  343. expect(res.statusCode).to.equal(204)
  344. callback()
  345. })
  346. }