HttpController.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. import logger from '@overleaf/logger'
  2. import OError from '@overleaf/o-error'
  3. import request from 'request'
  4. import * as UpdatesProcessor from './UpdatesProcessor.js'
  5. import * as SummarizedUpdatesManager from './SummarizedUpdatesManager.js'
  6. import * as DiffManager from './DiffManager.js'
  7. import * as HistoryStoreManager from './HistoryStoreManager.js'
  8. import * as WebApiManager from './WebApiManager.js'
  9. import * as SnapshotManager from './SnapshotManager.js'
  10. import * as HealthChecker from './HealthChecker.js'
  11. import * as SyncManager from './SyncManager.js'
  12. import * as ErrorRecorder from './ErrorRecorder.js'
  13. import * as RedisManager from './RedisManager.js'
  14. import * as LabelsManager from './LabelsManager.js'
  15. import * as HistoryApiManager from './HistoryApiManager.js'
  16. import * as RetryManager from './RetryManager.js'
  17. import * as FlushManager from './FlushManager.js'
  18. import { pipeline } from 'node:stream'
  19. import { RequestFailedError } from '@overleaf/fetch-utils'
  20. import { z, zz, validateReq } from '@overleaf/validation-tools'
  21. const ONE_DAY_IN_SECONDS = 24 * 60 * 60
  22. const getProjectBlobSchema = z.object({
  23. params: z.object({
  24. history_id: zz.objectId(),
  25. hash: z.string(),
  26. }),
  27. })
  28. export function getProjectBlob(req, res, next) {
  29. const { params } = validateReq(req, getProjectBlobSchema)
  30. const historyId = params.history_id
  31. const blobHash = params.hash
  32. HistoryStoreManager.getProjectBlobStream(
  33. historyId,
  34. blobHash,
  35. (err, stream) => {
  36. if (err != null) {
  37. if (err instanceof RequestFailedError && err.response.status === 404) {
  38. return res.status(404).end()
  39. }
  40. return next(OError.tag(err))
  41. }
  42. res.setHeader('Cache-Control', `private, max-age=${ONE_DAY_IN_SECONDS}`)
  43. pipeline(stream, res, err => {
  44. if (err) next(err)
  45. // res.end() is already called via 'end' event by pipeline.
  46. })
  47. }
  48. )
  49. }
  50. export function initializeProject(req, res, next) {
  51. const { historyId } = req.body
  52. HistoryStoreManager.initializeProject(historyId, (error, id) => {
  53. if (error != null) {
  54. return next(OError.tag(error))
  55. }
  56. res.json({ project: { id } })
  57. })
  58. }
  59. const flushProjectSchema = z.object({
  60. params: z.object({
  61. project_id: zz.objectId(),
  62. }),
  63. query: z.object({
  64. debug: z.stringbool().default(false),
  65. bisect: z.stringbool().default(false),
  66. }),
  67. })
  68. export function flushProject(req, res, next) {
  69. const { query, params } = validateReq(req, flushProjectSchema)
  70. const projectId = params.project_id
  71. if (query.debug) {
  72. logger.debug(
  73. { projectId },
  74. 'compressing project history in single-step mode'
  75. )
  76. UpdatesProcessor.processSingleUpdateForProject(projectId, error => {
  77. if (error != null) {
  78. return next(OError.tag(error))
  79. }
  80. res.sendStatus(204)
  81. })
  82. } else if (query.bisect) {
  83. logger.debug({ projectId }, 'compressing project history in bisect mode')
  84. UpdatesProcessor.processUpdatesForProjectUsingBisect(
  85. projectId,
  86. UpdatesProcessor.REDIS_READ_BATCH_SIZE,
  87. error => {
  88. if (error != null) {
  89. return next(OError.tag(error))
  90. }
  91. res.sendStatus(204)
  92. }
  93. )
  94. } else {
  95. logger.debug({ projectId }, 'compressing project history')
  96. UpdatesProcessor.processUpdatesForProject(projectId, error => {
  97. if (error != null) {
  98. return next(OError.tag(error))
  99. }
  100. res.sendStatus(204)
  101. })
  102. }
  103. }
  104. const dumpProjectSchema = z.object({
  105. params: z.object({
  106. project_id: zz.objectId(),
  107. }),
  108. query: z.object({
  109. count: z.coerce.number().int().optional(),
  110. }),
  111. })
  112. export function dumpProject(req, res, next) {
  113. const { query, params } = validateReq(req, dumpProjectSchema)
  114. const projectId = params.project_id
  115. const batchSize = query.count || UpdatesProcessor.REDIS_READ_BATCH_SIZE
  116. logger.debug({ projectId }, 'retrieving raw updates')
  117. UpdatesProcessor.getRawUpdates(projectId, batchSize, (error, rawUpdates) => {
  118. if (error != null) {
  119. return next(OError.tag(error))
  120. }
  121. res.json(rawUpdates)
  122. })
  123. }
  124. const flushOldSchema = z.object({
  125. query: z.object({
  126. // flush projects with queued ops older than this
  127. maxAge: z.coerce
  128. .number()
  129. .int()
  130. .default(6 * 3600),
  131. // pause this amount of time between checking queues
  132. queueDelay: z.coerce.number().int().default(100),
  133. // maximum number of queues to check
  134. limit: z.coerce.number().int().default(1000),
  135. // maximum amount of time allowed
  136. timeout: z.coerce
  137. .number()
  138. .int()
  139. .default(60 * 1000),
  140. // whether to run in the background
  141. background: z.stringbool().default(false),
  142. }),
  143. })
  144. export function flushOld(req, res, next) {
  145. const { query } = validateReq(req, flushOldSchema)
  146. const { maxAge, queueDelay, limit, timeout, background } = query
  147. const options = { maxAge, queueDelay, limit, timeout, background }
  148. FlushManager.flushOldOps(options, (error, results) => {
  149. if (error != null) {
  150. return next(OError.tag(error))
  151. }
  152. res.send(results)
  153. })
  154. }
  155. const getDiffSchema = z.object({
  156. params: z.object({
  157. project_id: zz.objectId(),
  158. }),
  159. query: z.object({
  160. pathname: z.string(),
  161. from: z.coerce.number().int(),
  162. to: z.coerce.number().int(),
  163. }),
  164. })
  165. export function getDiff(req, res, next) {
  166. const { query, params } = validateReq(req, getDiffSchema)
  167. const { pathname, from, to } = query
  168. const projectId = params.project_id
  169. logger.debug({ projectId, pathname, from, to }, 'getting diff')
  170. DiffManager.getDiff(projectId, pathname, from, to, (error, diff) => {
  171. if (error != null) {
  172. return next(OError.tag(error))
  173. }
  174. res.json({ diff })
  175. })
  176. }
  177. const getFileTreeDiffSchema = z.object({
  178. params: z.object({
  179. project_id: zz.objectId(),
  180. }),
  181. query: z.object({
  182. from: z.coerce.number().int(),
  183. to: z.coerce.number().int(),
  184. }),
  185. })
  186. export function getFileTreeDiff(req, res, next) {
  187. const { query, params } = validateReq(req, getFileTreeDiffSchema)
  188. const { from, to } = query
  189. const projectId = params.project_id
  190. DiffManager.getFileTreeDiff(projectId, from, to, (error, diff) => {
  191. if (error != null) {
  192. return next(OError.tag(error))
  193. }
  194. res.json({ diff })
  195. })
  196. }
  197. const getUpdatesSchema = z.object({
  198. params: z.object({
  199. project_id: zz.objectId(),
  200. }),
  201. query: z.object({
  202. before: z.coerce.number().int().optional(),
  203. min_count: z.coerce.number().int().optional(),
  204. }),
  205. })
  206. export function getUpdates(req, res, next) {
  207. const { query, params } = validateReq(req, getUpdatesSchema)
  208. const projectId = params.project_id
  209. const { before, min_count: minCount } = query
  210. SummarizedUpdatesManager.getSummarizedProjectUpdates(
  211. projectId,
  212. { before, min_count: minCount },
  213. (error, updates, nextBeforeTimestamp) => {
  214. if (error != null) {
  215. return next(OError.tag(error))
  216. }
  217. for (const update of updates) {
  218. // Sets don't JSONify, so convert to arrays
  219. update.pathnames = Array.from(update.pathnames || []).sort()
  220. }
  221. res.json({
  222. updates,
  223. nextBeforeTimestamp,
  224. })
  225. }
  226. )
  227. }
  228. const latestVersionSchema = z.object({
  229. params: z.object({
  230. project_id: zz.objectId(),
  231. }),
  232. })
  233. export function latestVersion(req, res, next) {
  234. const { params } = validateReq(req, latestVersionSchema)
  235. const projectId = params.project_id
  236. logger.debug({ projectId }, 'compressing project history and getting version')
  237. UpdatesProcessor.processUpdatesForProject(projectId, error => {
  238. if (error != null) {
  239. return next(OError.tag(error))
  240. }
  241. WebApiManager.getHistoryId(projectId, (error, historyId) => {
  242. if (error != null) {
  243. return next(OError.tag(error))
  244. }
  245. HistoryStoreManager.getMostRecentVersion(
  246. projectId,
  247. historyId,
  248. (error, version, projectStructureAndDocVersions, lastChange) => {
  249. if (error != null) {
  250. return next(OError.tag(error))
  251. }
  252. res.json({
  253. version,
  254. timestamp: lastChange != null ? lastChange.timestamp : undefined,
  255. v2Authors: lastChange != null ? lastChange.v2Authors : undefined,
  256. })
  257. }
  258. )
  259. })
  260. })
  261. }
  262. const getFileSnapshotSchema = z.object({
  263. params: z.object({
  264. project_id: zz.objectId(),
  265. version: z.coerce.number().int(),
  266. pathname: z.string(),
  267. }),
  268. })
  269. export function getFileSnapshot(req, res, next) {
  270. const { params } = validateReq(req, getFileSnapshotSchema)
  271. const { project_id: projectId, version, pathname } = params
  272. SnapshotManager.getFileSnapshotStream(
  273. projectId,
  274. version,
  275. pathname,
  276. (error, stream) => {
  277. if (error != null) {
  278. return next(OError.tag(error))
  279. }
  280. pipeline(stream, res, err => {
  281. if (err) next(err)
  282. // res.end() is already called via 'end' event by pipeline.
  283. })
  284. }
  285. )
  286. }
  287. const getRangesSnapshotSchema = z.object({
  288. params: z.object({
  289. project_id: zz.objectId(),
  290. version: z.coerce.number().int(),
  291. pathname: z.string(),
  292. }),
  293. })
  294. export function getRangesSnapshot(req, res, next) {
  295. const { params } = validateReq(req, getRangesSnapshotSchema)
  296. const { project_id: projectId, version, pathname } = params
  297. SnapshotManager.getRangesSnapshot(
  298. projectId,
  299. version,
  300. pathname,
  301. (err, ranges) => {
  302. if (err) {
  303. return next(OError.tag(err))
  304. }
  305. res.json(ranges)
  306. }
  307. )
  308. }
  309. const getFileMetadataSnapshotSchema = z.object({
  310. params: z.object({
  311. project_id: zz.objectId(),
  312. version: z.coerce.number().int(),
  313. pathname: z.string(),
  314. }),
  315. })
  316. export function getFileMetadataSnapshot(req, res, next) {
  317. const { params } = validateReq(req, getFileMetadataSnapshotSchema)
  318. const { project_id: projectId, version, pathname } = params
  319. SnapshotManager.getFileMetadataSnapshot(
  320. projectId,
  321. version,
  322. pathname,
  323. (err, data) => {
  324. if (err) {
  325. return next(OError.tag(err))
  326. }
  327. res.json(data)
  328. }
  329. )
  330. }
  331. const getLatestSnapshotSchema = z.object({
  332. params: z.object({
  333. project_id: zz.objectId(),
  334. }),
  335. })
  336. export function getLatestSnapshot(req, res, next) {
  337. const { params } = validateReq(req, getLatestSnapshotSchema)
  338. const { project_id: projectId } = params
  339. WebApiManager.getHistoryId(projectId, (error, historyId) => {
  340. if (error) return next(OError.tag(error))
  341. SnapshotManager.getLatestSnapshot(
  342. projectId,
  343. historyId,
  344. (error, details) => {
  345. if (error != null) {
  346. return next(error)
  347. }
  348. const { snapshot, version } = details
  349. res.json({ snapshot: snapshot.toRaw(), version })
  350. }
  351. )
  352. })
  353. }
  354. const getChangesInChunkSinceSchema = z.object({
  355. params: z.object({
  356. project_id: zz.objectId(),
  357. }),
  358. query: z.object({
  359. since: z.coerce.number().int().min(0),
  360. }),
  361. })
  362. export function getChangesInChunkSince(req, res, next) {
  363. const { query, params } = validateReq(req, getChangesInChunkSinceSchema)
  364. const { project_id: projectId } = params
  365. const { since } = query
  366. WebApiManager.getHistoryId(projectId, (error, historyId) => {
  367. if (error) return next(OError.tag(error))
  368. SnapshotManager.getChangesInChunkSince(
  369. projectId,
  370. historyId,
  371. since,
  372. (error, details) => {
  373. if (error != null) {
  374. return next(error)
  375. }
  376. const { latestStartVersion, changes } = details
  377. res.json({
  378. latestStartVersion,
  379. changes: changes.map(c => c.toRaw()),
  380. })
  381. }
  382. )
  383. })
  384. }
  385. const getProjectSnapshotSchema = z.object({
  386. params: z.object({
  387. project_id: zz.objectId(),
  388. version: z.coerce.number().int(),
  389. }),
  390. })
  391. export function getProjectSnapshot(req, res, next) {
  392. const { params } = validateReq(req, getProjectSnapshotSchema)
  393. const { project_id: projectId, version } = params
  394. SnapshotManager.getProjectSnapshot(
  395. projectId,
  396. version,
  397. (error, snapshotData) => {
  398. if (error != null) {
  399. return next(error)
  400. }
  401. res.json(snapshotData)
  402. }
  403. )
  404. }
  405. const getPathsAtVersionSchema = z.object({
  406. params: z.object({
  407. project_id: zz.objectId(),
  408. version: z.coerce.number().int(),
  409. }),
  410. })
  411. export function getPathsAtVersion(req, res, next) {
  412. const { params } = validateReq(req, getPathsAtVersionSchema)
  413. const { project_id: projectId, version } = params
  414. SnapshotManager.getPathsAtVersion(projectId, version, (error, result) => {
  415. if (error != null) {
  416. return next(error)
  417. }
  418. res.json(result)
  419. })
  420. }
  421. export function healthCheck(req, res) {
  422. HealthChecker.check(err => {
  423. if (err != null) {
  424. logger.err({ err }, 'error performing health check')
  425. res.sendStatus(500)
  426. } else {
  427. res.sendStatus(200)
  428. }
  429. })
  430. }
  431. export function checkLock(req, res) {
  432. HealthChecker.checkLock(err => {
  433. if (err != null) {
  434. logger.err({ err }, 'error performing lock check')
  435. res.sendStatus(500)
  436. } else {
  437. res.sendStatus(200)
  438. }
  439. })
  440. }
  441. const resyncProjectSchema = z.object({
  442. params: z.object({
  443. project_id: zz.objectId(),
  444. }),
  445. query: z.object({
  446. force: z.stringbool().default(false),
  447. }),
  448. body: z.object({
  449. force: z.boolean().default(false),
  450. origin: z
  451. .object({
  452. kind: z.string(),
  453. })
  454. .optional(),
  455. historyRangesMigration: z.enum(['forwards', 'backwards']).optional(),
  456. }),
  457. })
  458. export function resyncProject(req, res, next) {
  459. const { query, params, body } = validateReq(req, resyncProjectSchema)
  460. const projectId = params.project_id
  461. const options = {}
  462. if (body.origin) {
  463. options.origin = body.origin
  464. }
  465. if (body.historyRangesMigration) {
  466. options.historyRangesMigration = body.historyRangesMigration
  467. }
  468. if (query.force || body.force) {
  469. // this will delete the queue and clear the sync state
  470. // use if the project is completely broken
  471. SyncManager.startHardResync(projectId, options, error => {
  472. if (error != null) {
  473. return next(error)
  474. }
  475. // flush the sync operations
  476. UpdatesProcessor.processUpdatesForProject(projectId, error => {
  477. if (error != null) {
  478. return next(error)
  479. }
  480. res.sendStatus(204)
  481. })
  482. })
  483. } else {
  484. SyncManager.startResync(projectId, options, error => {
  485. if (error != null) {
  486. return next(error)
  487. }
  488. // flush the sync operations
  489. UpdatesProcessor.processUpdatesForProject(projectId, error => {
  490. if (error != null) {
  491. return next(error)
  492. }
  493. res.sendStatus(204)
  494. })
  495. })
  496. }
  497. }
  498. const forceDebugProjectSchema = z.object({
  499. params: z.object({
  500. project_id: zz.objectId(),
  501. }),
  502. query: z.object({
  503. clear: z.stringbool().default(false),
  504. }),
  505. })
  506. export function forceDebugProject(req, res, next) {
  507. const { query, params } = validateReq(req, forceDebugProjectSchema)
  508. const projectId = params.project_id
  509. // set the debug flag to true unless we see ?clear=true
  510. const state = !query.clear
  511. ErrorRecorder.setForceDebug(projectId, state, error => {
  512. if (error != null) {
  513. return next(error)
  514. }
  515. // display the failure record to help debugging
  516. ErrorRecorder.getFailureRecord(projectId, (error, result) => {
  517. if (error != null) {
  518. return next(error)
  519. }
  520. res.send(result)
  521. })
  522. })
  523. }
  524. export function getFailures(req, res, next) {
  525. ErrorRecorder.getFailures((error, result) => {
  526. if (error != null) {
  527. return next(error)
  528. }
  529. res.send({ failures: result })
  530. })
  531. }
  532. export function getQueueCounts(req, res, next) {
  533. RedisManager.getProjectIdsWithHistoryOpsCount((err, queuedProjectsCount) => {
  534. if (err != null) {
  535. return next(err)
  536. }
  537. res.send({ queuedProjects: queuedProjectsCount })
  538. })
  539. }
  540. const getLabelsSchema = z.object({
  541. params: z.object({
  542. project_id: zz.objectId(),
  543. }),
  544. })
  545. export function getLabels(req, res, next) {
  546. const { params } = validateReq(req, getLabelsSchema)
  547. const projectId = params.project_id
  548. HistoryApiManager.shouldUseProjectHistory(
  549. projectId,
  550. (error, shouldUseProjectHistory) => {
  551. if (error != null) {
  552. return next(error)
  553. }
  554. if (shouldUseProjectHistory) {
  555. LabelsManager.getLabels(projectId, (error, labels) => {
  556. if (error != null) {
  557. return next(error)
  558. }
  559. res.json(labels)
  560. })
  561. } else {
  562. res.sendStatus(409)
  563. }
  564. }
  565. )
  566. }
  567. const createLabelSchema = z.object({
  568. params: z.object({
  569. project_id: zz.objectId(),
  570. user_id: zz.objectId().optional(),
  571. }),
  572. body: z.object({
  573. version: z.number().int(),
  574. comment: z.string(),
  575. created_at: z.string().optional(),
  576. validate_exists: z.boolean().default(true),
  577. user_id: zz.objectId().nullable().optional(),
  578. }),
  579. })
  580. export function createLabel(req, res, next) {
  581. const { params, body } = validateReq(req, createLabelSchema)
  582. const { project_id: projectId, user_id: userIdParam } = params
  583. const {
  584. version,
  585. comment,
  586. user_id: userIdBody,
  587. created_at: createdAt,
  588. validate_exists: validateExists,
  589. } = body
  590. // Temporarily looking up both params and body while rolling out changes
  591. // in the router path - https://github.com/overleaf/internal/pull/20200
  592. const userId = userIdParam || userIdBody
  593. HistoryApiManager.shouldUseProjectHistory(
  594. projectId,
  595. (error, shouldUseProjectHistory) => {
  596. if (error != null) {
  597. return next(error)
  598. }
  599. if (shouldUseProjectHistory) {
  600. LabelsManager.createLabel(
  601. projectId,
  602. userId,
  603. version,
  604. comment,
  605. createdAt,
  606. validateExists,
  607. (error, label) => {
  608. if (error != null) {
  609. return next(error)
  610. }
  611. res.json(label)
  612. }
  613. )
  614. } else {
  615. logger.error(
  616. {
  617. projectId,
  618. userId,
  619. version,
  620. comment,
  621. createdAt,
  622. validateExists,
  623. },
  624. 'not using v2 history'
  625. )
  626. res.sendStatus(409)
  627. }
  628. }
  629. )
  630. }
  631. /**
  632. * This will delete a label if it is owned by the current user. If you wish to
  633. * delete a label regardless of the current user, then use `deleteLabel` instead.
  634. */
  635. const deleteLabelForUserSchema = z.object({
  636. params: z.object({
  637. project_id: zz.objectId(),
  638. user_id: zz.objectId(),
  639. label_id: zz.objectId(),
  640. }),
  641. })
  642. export function deleteLabelForUser(req, res, next) {
  643. const { params } = validateReq(req, deleteLabelForUserSchema)
  644. const { project_id: projectId, user_id: userId, label_id: labelId } = params
  645. LabelsManager.deleteLabelForUser(projectId, userId, labelId, error => {
  646. if (error != null) {
  647. return next(error)
  648. }
  649. res.sendStatus(204)
  650. })
  651. }
  652. const deleteLabelSchema = z.object({
  653. params: z.object({
  654. project_id: zz.objectId(),
  655. label_id: zz.objectId(),
  656. }),
  657. })
  658. export function deleteLabel(req, res, next) {
  659. const { params } = validateReq(req, deleteLabelSchema)
  660. const { project_id: projectId, label_id: labelId } = params
  661. LabelsManager.deleteLabel(projectId, labelId, error => {
  662. if (error != null) {
  663. return next(error)
  664. }
  665. res.sendStatus(204)
  666. })
  667. }
  668. const retryFailuresSchema = z.object({
  669. query: z.object({
  670. failureType: z.enum(['soft', 'hard']).optional(),
  671. // bail out after this time limit
  672. timeout: z.coerce.number().int().default(300),
  673. // maximum number of projects to check
  674. limit: z.coerce.number().int().default(100),
  675. callbackUrl: z.string().optional(),
  676. }),
  677. })
  678. export function retryFailures(req, res, next) {
  679. const { query } = validateReq(req, retryFailuresSchema)
  680. const { failureType, timeout, limit, callbackUrl } = query
  681. if (callbackUrl) {
  682. // send response but run in background when callbackUrl provided
  683. res.send({ retryStatus: 'running retryFailures in background' })
  684. }
  685. RetryManager.retryFailures(
  686. { failureType, timeout, limit },
  687. (error, result) => {
  688. if (callbackUrl) {
  689. // if present, notify the callbackUrl on success
  690. if (!error) {
  691. // Needs Node 12
  692. // const callbackHeaders = Object.fromEntries(Object.entries(req.headers || {}).filter(([k,v]) => k.match(/^X-CALLBACK-/i)))
  693. const callbackHeaders = {}
  694. for (const key of Object.getOwnPropertyNames(
  695. req.headers || {}
  696. ).filter(key => key.match(/^X-CALLBACK-/i))) {
  697. const found = key.match(/^X-CALLBACK-(.*)/i)
  698. callbackHeaders[found[1]] = req.headers[key]
  699. }
  700. request({ url: callbackUrl, headers: callbackHeaders })
  701. }
  702. } else {
  703. if (error != null) {
  704. return next(error)
  705. }
  706. res.send({ retryStatus: result })
  707. }
  708. }
  709. )
  710. }
  711. const transferLabelsSchema = z.object({
  712. params: z.object({
  713. from_user: zz.objectId(),
  714. to_user: zz.objectId(),
  715. }),
  716. })
  717. export function transferLabels(req, res, next) {
  718. const { params } = validateReq(req, transferLabelsSchema)
  719. const { from_user: fromUser, to_user: toUser } = params
  720. LabelsManager.transferLabels(fromUser, toUser, error => {
  721. if (error != null) {
  722. return next(error)
  723. }
  724. res.sendStatus(204)
  725. })
  726. }
  727. const deleteProjectSchema = z.object({
  728. params: z.object({
  729. project_id: zz.objectId(),
  730. }),
  731. })
  732. export function deleteProject(req, res, next) {
  733. const { params } = validateReq(req, deleteProjectSchema)
  734. const { project_id: projectId } = params
  735. // clear the timestamp before clearing the queue,
  736. // because the queue location is used in the migration
  737. RedisManager.clearFirstOpTimestamp(projectId, err => {
  738. if (err) {
  739. return next(err)
  740. }
  741. RedisManager.clearCachedHistoryId(projectId, err => {
  742. if (err) {
  743. return next(err)
  744. }
  745. RedisManager.destroyDocUpdatesQueue(projectId, err => {
  746. if (err) {
  747. return next(err)
  748. }
  749. SyncManager.clearResyncState(projectId, err => {
  750. if (err) {
  751. return next(err)
  752. }
  753. ErrorRecorder.clearError(projectId, err => {
  754. if (err) {
  755. return next(err)
  756. }
  757. res.sendStatus(204)
  758. })
  759. })
  760. })
  761. })
  762. })
  763. }