ProjectHistoryClient.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 getChangesInChunkSince(projectId, since, options, callback) {
  104. request.get(
  105. {
  106. url: `http://127.0.0.1:3054/project/${projectId}/changes-in-chunk`,
  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 getLatestSnapshot(projectId, callback) {
  122. request.get(
  123. {
  124. url: `http://127.0.0.1:3054/project/${projectId}/snapshot`,
  125. json: true,
  126. },
  127. (error, res, body) => {
  128. if (error) {
  129. return callback(error)
  130. }
  131. expect(res.statusCode).to.equal(200)
  132. callback(null, body)
  133. }
  134. )
  135. }
  136. export function getSnapshot(projectId, pathname, version, options, callback) {
  137. if (typeof options === 'function') {
  138. callback = options
  139. options = null
  140. }
  141. if (!options) {
  142. options = { allowErrors: false }
  143. }
  144. request.get(
  145. {
  146. url: `http://127.0.0.1:3054/project/${projectId}/version/${version}/${encodeURIComponent(
  147. pathname
  148. )}`,
  149. },
  150. (error, res, body) => {
  151. if (error) {
  152. return callback(error)
  153. }
  154. if (!options.allowErrors) {
  155. expect(res.statusCode).to.equal(200)
  156. }
  157. callback(error, body, res.statusCode)
  158. }
  159. )
  160. }
  161. export function pushRawUpdate(projectId, update, callback) {
  162. rclient.rpush(
  163. Keys.projectHistoryOps({ project_id: projectId }),
  164. JSON.stringify(update),
  165. callback
  166. )
  167. }
  168. export function setFirstOpTimestamp(projectId, timestamp, callback) {
  169. rclient.set(
  170. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
  171. timestamp,
  172. callback
  173. )
  174. }
  175. export function getFirstOpTimestamp(projectId, callback) {
  176. rclient.get(
  177. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
  178. callback
  179. )
  180. }
  181. export function clearFirstOpTimestamp(projectId, callback) {
  182. rclient.del(
  183. Keys.projectHistoryFirstOpTimestamp({ project_id: projectId }),
  184. callback
  185. )
  186. }
  187. export function getQueueLength(projectId, callback) {
  188. rclient.llen(Keys.projectHistoryOps({ project_id: projectId }), callback)
  189. }
  190. export function getQueueCounts(callback) {
  191. return request.get(
  192. {
  193. url: 'http://127.0.0.1:3054/status/queue',
  194. json: true,
  195. },
  196. callback
  197. )
  198. }
  199. export function resyncHistory(projectId, callback) {
  200. request.post(
  201. {
  202. url: `http://127.0.0.1:3054/project/${projectId}/resync`,
  203. json: true,
  204. body: { origin: { kind: 'test-origin' } },
  205. },
  206. (error, res, body) => {
  207. if (error) {
  208. return callback(error)
  209. }
  210. expect(res.statusCode).to.equal(204)
  211. callback(error)
  212. }
  213. )
  214. }
  215. export function createLabel(
  216. projectId,
  217. userId,
  218. version,
  219. comment,
  220. createdAt,
  221. callback
  222. ) {
  223. request.post(
  224. {
  225. url: `http://127.0.0.1:3054/project/${projectId}/labels`,
  226. json: { comment, version, created_at: createdAt, user_id: userId },
  227. },
  228. (error, res, body) => {
  229. if (error) {
  230. return callback(error)
  231. }
  232. expect(res.statusCode).to.equal(200)
  233. callback(null, body)
  234. }
  235. )
  236. }
  237. export function getLabels(projectId, callback) {
  238. request.get(
  239. {
  240. url: `http://127.0.0.1:3054/project/${projectId}/labels`,
  241. json: true,
  242. },
  243. (error, res, body) => {
  244. if (error) {
  245. return callback(error)
  246. }
  247. expect(res.statusCode).to.equal(200)
  248. callback(null, body)
  249. }
  250. )
  251. }
  252. export function deleteLabelForUser(projectId, userId, labelId, callback) {
  253. request.delete(
  254. {
  255. url: `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels/${labelId}`,
  256. },
  257. (error, res, body) => {
  258. if (error) {
  259. return callback(error)
  260. }
  261. expect(res.statusCode).to.equal(204)
  262. callback(null, body)
  263. }
  264. )
  265. }
  266. export function deleteLabel(projectId, labelId, callback) {
  267. request.delete(
  268. {
  269. url: `http://127.0.0.1:3054/project/${projectId}/labels/${labelId}`,
  270. },
  271. (error, res, body) => {
  272. if (error) {
  273. return callback(error)
  274. }
  275. expect(res.statusCode).to.equal(204)
  276. callback(null, body)
  277. }
  278. )
  279. }
  280. export function setFailure(failureEntry, callback) {
  281. db.projectHistoryFailures.deleteOne(
  282. { project_id: { $exists: true } },
  283. (err, result) => {
  284. if (err) {
  285. return callback(err)
  286. }
  287. db.projectHistoryFailures.insertOne(failureEntry, callback)
  288. }
  289. )
  290. }
  291. export function getFailure(projectId, callback) {
  292. db.projectHistoryFailures.findOne({ project_id: projectId }, callback)
  293. }
  294. export function transferLabelOwnership(fromUser, toUser, callback) {
  295. request.post(
  296. {
  297. url: `http://127.0.0.1:3054/user/${fromUser}/labels/transfer/${toUser}`,
  298. },
  299. (error, res, body) => {
  300. if (error) {
  301. return callback(error)
  302. }
  303. expect(res.statusCode).to.equal(204)
  304. callback(null, body)
  305. }
  306. )
  307. }
  308. export function getDump(projectId, callback) {
  309. request.get(
  310. `http://127.0.0.1:3054/project/${projectId}/dump`,
  311. (err, res, body) => {
  312. if (err) {
  313. return callback(err)
  314. }
  315. expect(res.statusCode).to.equal(200)
  316. callback(null, JSON.parse(body))
  317. }
  318. )
  319. }
  320. export function deleteProject(projectId, callback) {
  321. request.delete(`http://127.0.0.1:3054/project/${projectId}`, (err, res) => {
  322. if (err) {
  323. return callback(err)
  324. }
  325. expect(res.statusCode).to.equal(204)
  326. callback()
  327. })
  328. }