HttpController.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. const DocumentManager = require('./DocumentManager')
  2. const HistoryManager = require('./HistoryManager')
  3. const ProjectManager = require('./ProjectManager')
  4. const RedisManager = require('./RedisManager')
  5. const Errors = require('./Errors')
  6. const logger = require('@overleaf/logger')
  7. const Settings = require('@overleaf/settings')
  8. const Metrics = require('./Metrics')
  9. const DeleteQueueManager = require('./DeleteQueueManager')
  10. const { getTotalSizeOfLines } = require('./Limits')
  11. const async = require('async')
  12. const { StringFileData } = require('overleaf-editor-core')
  13. function getDoc(req, res, next) {
  14. let fromVersion
  15. const docId = req.params.doc_id
  16. const projectId = req.params.project_id
  17. logger.debug({ projectId, docId }, 'getting doc via http')
  18. const timer = new Metrics.Timer('http.getDoc')
  19. if (req.query.fromVersion != null) {
  20. fromVersion = parseInt(req.query.fromVersion, 10)
  21. } else {
  22. fromVersion = -1
  23. }
  24. DocumentManager.getDocAndRecentOpsWithLock(
  25. projectId,
  26. docId,
  27. fromVersion,
  28. (error, lines, version, ops, ranges, pathname, _projectHistoryId, type) => {
  29. timer.done()
  30. if (error) {
  31. return next(error)
  32. }
  33. logger.debug({ projectId, docId }, 'got doc via http')
  34. if (lines == null || version == null) {
  35. return next(new Errors.NotFoundError('document not found'))
  36. }
  37. if (!Array.isArray(lines) && req.query.historyOTSupport !== 'true') {
  38. const file = StringFileData.fromRaw(lines)
  39. // TODO(24596): tc support for history-ot
  40. lines = file.getLines()
  41. }
  42. res.json({
  43. id: docId,
  44. lines,
  45. version,
  46. ops,
  47. ranges,
  48. pathname,
  49. ttlInS: RedisManager.DOC_OPS_TTL,
  50. type,
  51. })
  52. }
  53. )
  54. }
  55. function getComment(req, res, next) {
  56. const docId = req.params.doc_id
  57. const projectId = req.params.project_id
  58. const commentId = req.params.comment_id
  59. logger.debug({ projectId, docId, commentId }, 'getting comment via http')
  60. DocumentManager.getCommentWithLock(
  61. projectId,
  62. docId,
  63. commentId,
  64. (error, comment) => {
  65. if (error) {
  66. return next(error)
  67. }
  68. if (comment == null) {
  69. return next(new Errors.NotFoundError('comment not found'))
  70. }
  71. res.json(comment)
  72. }
  73. )
  74. }
  75. // return the doc from redis if present, but don't load it from mongo
  76. function peekDoc(req, res, next) {
  77. const docId = req.params.doc_id
  78. const projectId = req.params.project_id
  79. logger.debug({ projectId, docId }, 'peeking at doc via http')
  80. RedisManager.getDoc(projectId, docId, function (error, lines, version) {
  81. if (error) {
  82. return next(error)
  83. }
  84. if (lines == null || version == null) {
  85. return next(new Errors.NotFoundError('document not found'))
  86. }
  87. if (!Array.isArray(lines) && req.query.historyOTSupport !== 'true') {
  88. const file = StringFileData.fromRaw(lines)
  89. // TODO(24596): tc support for history-ot
  90. lines = file.getLines()
  91. }
  92. res.json({ id: docId, lines, version })
  93. })
  94. }
  95. function getProjectDocsAndFlushIfOld(req, res, next) {
  96. const projectId = req.params.project_id
  97. const projectStateHash = req.query.state
  98. // exclude is string of existing docs "id:version,id:version,..."
  99. const excludeItems =
  100. req.query.exclude != null ? req.query.exclude.split(',') : []
  101. logger.debug({ projectId, exclude: excludeItems }, 'getting docs via http')
  102. const timer = new Metrics.Timer('http.getAllDocs')
  103. const excludeVersions = {}
  104. for (const item of excludeItems) {
  105. const [id, version] = item.split(':')
  106. excludeVersions[id] = version
  107. }
  108. logger.debug(
  109. { projectId, projectStateHash, excludeVersions },
  110. 'excluding versions'
  111. )
  112. ProjectManager.getProjectDocsAndFlushIfOld(
  113. projectId,
  114. projectStateHash,
  115. excludeVersions,
  116. (error, result) => {
  117. timer.done()
  118. if (error instanceof Errors.ProjectStateChangedError) {
  119. res.sendStatus(409) // conflict
  120. } else if (error) {
  121. next(error)
  122. } else {
  123. logger.debug(
  124. {
  125. projectId,
  126. result: result.map(doc => `${doc._id}:${doc.v}`),
  127. },
  128. 'got docs via http'
  129. )
  130. res.send(result)
  131. }
  132. }
  133. )
  134. }
  135. function getProjectLastUpdatedAt(req, res, next) {
  136. const projectId = req.params.project_id
  137. ProjectManager.getProjectDocsTimestamps(projectId, (err, timestamps) => {
  138. if (err) return next(err)
  139. // Filter out nulls. This can happen when
  140. // - docs get flushed between the listing and getting the individual docs ts
  141. // - a doc flush failed half way (doc keys removed, project tracking not updated)
  142. timestamps = timestamps.filter(ts => !!ts)
  143. timestamps = timestamps.map(ts => parseInt(ts, 10))
  144. timestamps.sort((a, b) => (a > b ? 1 : -1))
  145. res.json({ lastUpdatedAt: timestamps.pop() })
  146. })
  147. }
  148. function clearProjectState(req, res, next) {
  149. const projectId = req.params.project_id
  150. const timer = new Metrics.Timer('http.clearProjectState')
  151. logger.debug({ projectId }, 'clearing project state via http')
  152. ProjectManager.clearProjectState(projectId, error => {
  153. timer.done()
  154. if (error) {
  155. next(error)
  156. } else {
  157. res.sendStatus(200)
  158. }
  159. })
  160. }
  161. function setDoc(req, res, next) {
  162. const docId = req.params.doc_id
  163. const projectId = req.params.project_id
  164. const { lines, source, user_id: userId, undoing } = req.body
  165. const lineSize = getTotalSizeOfLines(lines)
  166. if (lineSize > Settings.max_doc_length) {
  167. logger.warn(
  168. { projectId, docId, source, lineSize, userId },
  169. 'document too large, returning 406 response'
  170. )
  171. return res.sendStatus(406)
  172. }
  173. logger.debug(
  174. { projectId, docId, lines, source, userId, undoing },
  175. 'setting doc via http'
  176. )
  177. const timer = new Metrics.Timer('http.setDoc')
  178. DocumentManager.setDocWithLock(
  179. projectId,
  180. docId,
  181. lines,
  182. source,
  183. userId,
  184. undoing,
  185. true,
  186. (error, result) => {
  187. timer.done()
  188. if (error) {
  189. return next(error)
  190. }
  191. logger.debug({ projectId, docId }, 'set doc via http')
  192. res.json(result)
  193. }
  194. )
  195. }
  196. function appendToDoc(req, res, next) {
  197. const docId = req.params.doc_id
  198. const projectId = req.params.project_id
  199. const { lines, source, user_id: userId } = req.body
  200. const timer = new Metrics.Timer('http.appendToDoc')
  201. DocumentManager.appendToDocWithLock(
  202. projectId,
  203. docId,
  204. lines,
  205. source,
  206. userId,
  207. (error, result) => {
  208. timer.done()
  209. if (error instanceof Errors.FileTooLargeError) {
  210. logger.warn('refusing to append to file, it would become too large')
  211. return res.sendStatus(422)
  212. }
  213. if (error) {
  214. return next(error)
  215. }
  216. logger.debug(
  217. { projectId, docId, lines, source, userId },
  218. 'appending to doc via http'
  219. )
  220. res.json(result)
  221. }
  222. )
  223. }
  224. function flushDocIfLoaded(req, res, next) {
  225. const docId = req.params.doc_id
  226. const projectId = req.params.project_id
  227. logger.debug({ projectId, docId }, 'flushing doc via http')
  228. const timer = new Metrics.Timer('http.flushDoc')
  229. DocumentManager.flushDocIfLoadedWithLock(projectId, docId, error => {
  230. timer.done()
  231. if (error) {
  232. return next(error)
  233. }
  234. logger.debug({ projectId, docId }, 'flushed doc via http')
  235. res.sendStatus(204) // No Content
  236. })
  237. }
  238. function deleteDoc(req, res, next) {
  239. const docId = req.params.doc_id
  240. const projectId = req.params.project_id
  241. const ignoreFlushErrors = req.query.ignore_flush_errors === 'true'
  242. const timer = new Metrics.Timer('http.deleteDoc')
  243. logger.debug({ projectId, docId }, 'deleting doc via http')
  244. DocumentManager.flushAndDeleteDocWithLock(
  245. projectId,
  246. docId,
  247. { ignoreFlushErrors },
  248. error => {
  249. timer.done()
  250. // There is no harm in flushing project history if the previous call
  251. // failed and sometimes it is required
  252. HistoryManager.flushProjectChangesAsync(projectId)
  253. if (error) {
  254. return next(error)
  255. }
  256. logger.debug({ projectId, docId }, 'deleted doc via http')
  257. res.sendStatus(204) // No Content
  258. }
  259. )
  260. }
  261. function flushProject(req, res, next) {
  262. const projectId = req.params.project_id
  263. logger.debug({ projectId }, 'flushing project via http')
  264. const timer = new Metrics.Timer('http.flushProject')
  265. ProjectManager.flushProjectWithLocks(projectId, error => {
  266. timer.done()
  267. if (error) {
  268. return next(error)
  269. }
  270. logger.debug({ projectId }, 'flushed project via http')
  271. res.sendStatus(204) // No Content
  272. })
  273. }
  274. function deleteProject(req, res, next) {
  275. const projectId = req.params.project_id
  276. logger.debug({ projectId }, 'deleting project via http')
  277. const options = {}
  278. if (req.query.background) {
  279. options.background = true
  280. } // allow non-urgent flushes to be queued
  281. if (req.query.shutdown) {
  282. options.skip_history_flush = true
  283. } // don't flush history when realtime shuts down
  284. if (req.query.background) {
  285. ProjectManager.queueFlushAndDeleteProject(projectId, error => {
  286. if (error) {
  287. return next(error)
  288. }
  289. logger.debug({ projectId }, 'queue delete of project via http')
  290. res.sendStatus(204)
  291. }) // No Content
  292. } else {
  293. const timer = new Metrics.Timer('http.deleteProject')
  294. ProjectManager.flushAndDeleteProjectWithLocks(projectId, options, error => {
  295. timer.done()
  296. if (error) {
  297. return next(error)
  298. }
  299. logger.debug({ projectId }, 'deleted project via http')
  300. res.sendStatus(204) // No Content
  301. })
  302. }
  303. }
  304. function deleteMultipleProjects(req, res, next) {
  305. const projectIds = req.body.project_ids || []
  306. logger.debug({ projectIds }, 'deleting multiple projects via http')
  307. async.eachSeries(
  308. projectIds,
  309. (projectId, cb) => {
  310. logger.debug({ projectId }, 'queue delete of project via http')
  311. ProjectManager.queueFlushAndDeleteProject(projectId, cb)
  312. },
  313. error => {
  314. if (error) {
  315. return next(error)
  316. }
  317. res.sendStatus(204) // No Content
  318. }
  319. )
  320. }
  321. function acceptChanges(req, res, next) {
  322. const { project_id: projectId, doc_id: docId } = req.params
  323. let changeIds = req.body.change_ids
  324. if (changeIds == null) {
  325. changeIds = [req.params.change_id]
  326. }
  327. logger.debug(
  328. { projectId, docId },
  329. `accepting ${changeIds.length} changes via http`
  330. )
  331. const timer = new Metrics.Timer('http.acceptChanges')
  332. DocumentManager.acceptChangesWithLock(projectId, docId, changeIds, error => {
  333. timer.done()
  334. if (error) {
  335. return next(error)
  336. }
  337. logger.debug(
  338. { projectId, docId },
  339. `accepted ${changeIds.length} changes via http`
  340. )
  341. res.sendStatus(204) // No Content
  342. })
  343. }
  344. function resolveComment(req, res, next) {
  345. const {
  346. project_id: projectId,
  347. doc_id: docId,
  348. comment_id: commentId,
  349. } = req.params
  350. const userId = req.body.user_id
  351. logger.debug({ projectId, docId, commentId }, 'resolving comment via http')
  352. DocumentManager.updateCommentStateWithLock(
  353. projectId,
  354. docId,
  355. commentId,
  356. userId,
  357. true,
  358. error => {
  359. if (error) {
  360. return next(error)
  361. }
  362. logger.debug({ projectId, docId, commentId }, 'resolved comment via http')
  363. res.sendStatus(204) // No Content
  364. }
  365. )
  366. }
  367. function reopenComment(req, res, next) {
  368. const {
  369. project_id: projectId,
  370. doc_id: docId,
  371. comment_id: commentId,
  372. } = req.params
  373. const userId = req.body.user_id
  374. logger.debug({ projectId, docId, commentId }, 'reopening comment via http')
  375. DocumentManager.updateCommentStateWithLock(
  376. projectId,
  377. docId,
  378. commentId,
  379. userId,
  380. false,
  381. error => {
  382. if (error) {
  383. return next(error)
  384. }
  385. logger.debug({ projectId, docId, commentId }, 'reopened comment via http')
  386. res.sendStatus(204) // No Content
  387. }
  388. )
  389. }
  390. function deleteComment(req, res, next) {
  391. const {
  392. project_id: projectId,
  393. doc_id: docId,
  394. comment_id: commentId,
  395. } = req.params
  396. const userId = req.body.user_id
  397. logger.debug({ projectId, docId, commentId }, 'deleting comment via http')
  398. const timer = new Metrics.Timer('http.deleteComment')
  399. DocumentManager.deleteCommentWithLock(
  400. projectId,
  401. docId,
  402. commentId,
  403. userId,
  404. error => {
  405. timer.done()
  406. if (error) {
  407. return next(error)
  408. }
  409. logger.debug({ projectId, docId, commentId }, 'deleted comment via http')
  410. res.sendStatus(204) // No Content
  411. }
  412. )
  413. }
  414. function updateProject(req, res, next) {
  415. const timer = new Metrics.Timer('http.updateProject')
  416. const projectId = req.params.project_id
  417. const { projectHistoryId, userId, updates = [], version, source } = req.body
  418. logger.debug({ projectId, updates, version }, 'updating project via http')
  419. ProjectManager.updateProjectWithLocks(
  420. projectId,
  421. projectHistoryId,
  422. userId,
  423. updates,
  424. version,
  425. source,
  426. error => {
  427. timer.done()
  428. if (error) {
  429. return next(error)
  430. }
  431. logger.debug({ projectId }, 'updated project via http')
  432. res.sendStatus(204) // No Content
  433. }
  434. )
  435. }
  436. function resyncProjectHistory(req, res, next) {
  437. const projectId = req.params.project_id
  438. const {
  439. projectHistoryId,
  440. docs,
  441. files,
  442. historyRangesMigration,
  443. resyncProjectStructureOnly,
  444. } = req.body
  445. logger.debug(
  446. { projectId, docs, files },
  447. 'queuing project history resync via http'
  448. )
  449. const opts = {}
  450. if (historyRangesMigration) {
  451. opts.historyRangesMigration = historyRangesMigration
  452. }
  453. if (resyncProjectStructureOnly) {
  454. opts.resyncProjectStructureOnly = resyncProjectStructureOnly
  455. }
  456. HistoryManager.resyncProjectHistory(
  457. projectId,
  458. projectHistoryId,
  459. docs,
  460. files,
  461. opts,
  462. error => {
  463. if (error) {
  464. return next(error)
  465. }
  466. logger.debug({ projectId }, 'queued project history resync via http')
  467. res.sendStatus(204)
  468. }
  469. )
  470. }
  471. function flushQueuedProjects(req, res, next) {
  472. res.setTimeout(10 * 60 * 1000)
  473. const options = {
  474. limit: req.query.limit || 1000,
  475. timeout: 5 * 60 * 1000,
  476. min_delete_age: req.query.min_delete_age || 5 * 60 * 1000,
  477. }
  478. DeleteQueueManager.flushAndDeleteOldProjects(options, (err, flushed) => {
  479. if (err) {
  480. logger.err({ err }, 'error flushing old projects')
  481. res.sendStatus(500)
  482. } else {
  483. logger.info({ flushed }, 'flush of queued projects completed')
  484. res.send({ flushed })
  485. }
  486. })
  487. }
  488. /**
  489. * Block a project from getting loaded in docupdater
  490. *
  491. * The project is blocked only if it's not already loaded in docupdater. The
  492. * response indicates whether the project has been blocked or not.
  493. */
  494. function blockProject(req, res, next) {
  495. const projectId = req.params.project_id
  496. RedisManager.blockProject(projectId, (err, blocked) => {
  497. if (err) {
  498. return next(err)
  499. }
  500. res.json({ blocked })
  501. })
  502. }
  503. /**
  504. * Unblock a project
  505. */
  506. function unblockProject(req, res, next) {
  507. const projectId = req.params.project_id
  508. RedisManager.unblockProject(projectId, (err, wasBlocked) => {
  509. if (err) {
  510. return next(err)
  511. }
  512. res.json({ wasBlocked })
  513. })
  514. }
  515. module.exports = {
  516. getDoc,
  517. peekDoc,
  518. getProjectDocsAndFlushIfOld,
  519. getProjectLastUpdatedAt,
  520. clearProjectState,
  521. appendToDoc,
  522. setDoc,
  523. flushDocIfLoaded,
  524. deleteDoc,
  525. flushProject,
  526. deleteProject,
  527. deleteMultipleProjects,
  528. acceptChanges,
  529. resolveComment,
  530. reopenComment,
  531. deleteComment,
  532. updateProject,
  533. resyncProjectHistory,
  534. flushQueuedProjects,
  535. blockProject,
  536. unblockProject,
  537. getComment,
  538. }