WebsocketController.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS103: Rewrite code to no longer use __guard__
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let WebsocketController
  17. const logger = require('logger-sharelatex')
  18. const metrics = require('metrics-sharelatex')
  19. const settings = require('settings-sharelatex')
  20. const WebApiManager = require('./WebApiManager')
  21. const AuthorizationManager = require('./AuthorizationManager')
  22. const DocumentUpdaterManager = require('./DocumentUpdaterManager')
  23. const ConnectedUsersManager = require('./ConnectedUsersManager')
  24. const WebsocketLoadBalancer = require('./WebsocketLoadBalancer')
  25. const RoomManager = require('./RoomManager')
  26. module.exports = WebsocketController = {
  27. // If the protocol version changes when the client reconnects,
  28. // it will force a full refresh of the page. Useful for non-backwards
  29. // compatible protocol changes. Use only in extreme need.
  30. PROTOCOL_VERSION: 2,
  31. joinProject(client, user, project_id, callback) {
  32. if (callback == null) {
  33. callback = function (error, project, privilegeLevel, protocolVersion) {}
  34. }
  35. if (client.disconnected) {
  36. metrics.inc('editor.join-project.disconnected', 1, {
  37. status: 'immediately'
  38. })
  39. return callback()
  40. }
  41. const user_id = user != null ? user._id : undefined
  42. logger.log(
  43. { user_id, project_id, client_id: client.id },
  44. 'user joining project'
  45. )
  46. metrics.inc('editor.join-project')
  47. return WebApiManager.joinProject(project_id, user, function (
  48. error,
  49. project,
  50. privilegeLevel,
  51. isRestrictedUser
  52. ) {
  53. if (error != null) {
  54. return callback(error)
  55. }
  56. if (client.disconnected) {
  57. metrics.inc('editor.join-project.disconnected', 1, {
  58. status: 'after-web-api-call'
  59. })
  60. return callback()
  61. }
  62. if (!privilegeLevel || privilegeLevel === '') {
  63. const err = new Error('not authorized')
  64. logger.warn(
  65. { err, project_id, user_id, client_id: client.id },
  66. 'user is not authorized to join project'
  67. )
  68. return callback(err)
  69. }
  70. client.ol_context = {}
  71. client.ol_context.privilege_level = privilegeLevel
  72. client.ol_context.user_id = user_id
  73. client.ol_context.project_id = project_id
  74. client.ol_context.owner_id = __guard__(
  75. project != null ? project.owner : undefined,
  76. (x) => x._id
  77. )
  78. client.ol_context.first_name = user != null ? user.first_name : undefined
  79. client.ol_context.last_name = user != null ? user.last_name : undefined
  80. client.ol_context.email = user != null ? user.email : undefined
  81. client.ol_context.connected_time = new Date()
  82. client.ol_context.signup_date = user != null ? user.signUpDate : undefined
  83. client.ol_context.login_count = user != null ? user.loginCount : undefined
  84. client.ol_context.is_restricted_user = !!isRestrictedUser
  85. RoomManager.joinProject(client, project_id, function (err) {
  86. if (err) {
  87. return callback(err)
  88. }
  89. logger.log(
  90. { user_id, project_id, client_id: client.id },
  91. 'user joined project'
  92. )
  93. return callback(
  94. null,
  95. project,
  96. privilegeLevel,
  97. WebsocketController.PROTOCOL_VERSION
  98. )
  99. })
  100. // No need to block for setting the user as connected in the cursor tracking
  101. return ConnectedUsersManager.updateUserPosition(
  102. project_id,
  103. client.publicId,
  104. user,
  105. null,
  106. function () {}
  107. )
  108. })
  109. },
  110. // We want to flush a project if there are no more (local) connected clients
  111. // but we need to wait for the triggering client to disconnect. How long we wait
  112. // is determined by FLUSH_IF_EMPTY_DELAY.
  113. FLUSH_IF_EMPTY_DELAY: 500, // ms
  114. leaveProject(io, client, callback) {
  115. if (callback == null) {
  116. callback = function (error) {}
  117. }
  118. const { project_id, user_id } = client.ol_context
  119. if (!project_id) {
  120. return callback()
  121. } // client did not join project
  122. metrics.inc('editor.leave-project')
  123. logger.log(
  124. { project_id, user_id, client_id: client.id },
  125. 'client leaving project'
  126. )
  127. WebsocketLoadBalancer.emitToRoom(
  128. project_id,
  129. 'clientTracking.clientDisconnected',
  130. client.publicId
  131. )
  132. // We can do this in the background
  133. ConnectedUsersManager.markUserAsDisconnected(
  134. project_id,
  135. client.publicId,
  136. function (err) {
  137. if (err != null) {
  138. return logger.error(
  139. { err, project_id, user_id, client_id: client.id },
  140. 'error marking client as disconnected'
  141. )
  142. }
  143. }
  144. )
  145. RoomManager.leaveProjectAndDocs(client)
  146. return setTimeout(function () {
  147. const remainingClients = io.sockets.clients(project_id)
  148. if (remainingClients.length === 0) {
  149. // Flush project in the background
  150. DocumentUpdaterManager.flushProjectToMongoAndDelete(
  151. project_id,
  152. function (err) {
  153. if (err != null) {
  154. return logger.error(
  155. { err, project_id, user_id, client_id: client.id },
  156. 'error flushing to doc updater after leaving project'
  157. )
  158. }
  159. }
  160. )
  161. }
  162. return callback()
  163. }, WebsocketController.FLUSH_IF_EMPTY_DELAY)
  164. },
  165. joinDoc(client, doc_id, fromVersion, options, callback) {
  166. if (fromVersion == null) {
  167. fromVersion = -1
  168. }
  169. if (callback == null) {
  170. callback = function (error, doclines, version, ops, ranges) {}
  171. }
  172. if (client.disconnected) {
  173. metrics.inc('editor.join-doc.disconnected', 1, { status: 'immediately' })
  174. return callback()
  175. }
  176. metrics.inc('editor.join-doc')
  177. const { project_id, user_id, is_restricted_user } = client.ol_context
  178. if (project_id == null) {
  179. return callback(new Error('no project_id found on client'))
  180. }
  181. logger.log(
  182. { user_id, project_id, doc_id, fromVersion, client_id: client.id },
  183. 'client joining doc'
  184. )
  185. return AuthorizationManager.assertClientCanViewProject(client, function (
  186. error
  187. ) {
  188. if (error != null) {
  189. return callback(error)
  190. }
  191. // ensure the per-doc applied-ops channel is subscribed before sending the
  192. // doc to the client, so that no events are missed.
  193. return RoomManager.joinDoc(client, doc_id, function (error) {
  194. if (error != null) {
  195. return callback(error)
  196. }
  197. if (client.disconnected) {
  198. metrics.inc('editor.join-doc.disconnected', 1, {
  199. status: 'after-joining-room'
  200. })
  201. // the client will not read the response anyways
  202. return callback()
  203. }
  204. return DocumentUpdaterManager.getDocument(
  205. project_id,
  206. doc_id,
  207. fromVersion,
  208. function (error, lines, version, ranges, ops) {
  209. let err
  210. if (error != null) {
  211. return callback(error)
  212. }
  213. if (client.disconnected) {
  214. metrics.inc('editor.join-doc.disconnected', 1, {
  215. status: 'after-doc-updater-call'
  216. })
  217. // the client will not read the response anyways
  218. return callback()
  219. }
  220. if (
  221. is_restricted_user &&
  222. (ranges != null ? ranges.comments : undefined) != null
  223. ) {
  224. ranges.comments = []
  225. }
  226. // Encode any binary bits of data so it can go via WebSockets
  227. // See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html
  228. const encodeForWebsockets = (text) =>
  229. unescape(encodeURIComponent(text))
  230. const escapedLines = []
  231. for (let line of Array.from(lines)) {
  232. try {
  233. line = encodeForWebsockets(line)
  234. } catch (error1) {
  235. err = error1
  236. logger.err(
  237. {
  238. err,
  239. project_id,
  240. doc_id,
  241. fromVersion,
  242. line,
  243. client_id: client.id
  244. },
  245. 'error encoding line uri component'
  246. )
  247. return callback(err)
  248. }
  249. escapedLines.push(line)
  250. }
  251. if (options.encodeRanges) {
  252. try {
  253. for (const comment of Array.from(
  254. (ranges != null ? ranges.comments : undefined) || []
  255. )) {
  256. if (comment.op.c != null) {
  257. comment.op.c = encodeForWebsockets(comment.op.c)
  258. }
  259. }
  260. for (const change of Array.from(
  261. (ranges != null ? ranges.changes : undefined) || []
  262. )) {
  263. if (change.op.i != null) {
  264. change.op.i = encodeForWebsockets(change.op.i)
  265. }
  266. if (change.op.d != null) {
  267. change.op.d = encodeForWebsockets(change.op.d)
  268. }
  269. }
  270. } catch (error2) {
  271. err = error2
  272. logger.err(
  273. {
  274. err,
  275. project_id,
  276. doc_id,
  277. fromVersion,
  278. ranges,
  279. client_id: client.id
  280. },
  281. 'error encoding range uri component'
  282. )
  283. return callback(err)
  284. }
  285. }
  286. AuthorizationManager.addAccessToDoc(client, doc_id)
  287. logger.log(
  288. {
  289. user_id,
  290. project_id,
  291. doc_id,
  292. fromVersion,
  293. client_id: client.id
  294. },
  295. 'client joined doc'
  296. )
  297. return callback(null, escapedLines, version, ops, ranges)
  298. }
  299. )
  300. })
  301. })
  302. },
  303. leaveDoc(client, doc_id, callback) {
  304. // client may have disconnected, but we have to cleanup internal state.
  305. if (callback == null) {
  306. callback = function (error) {}
  307. }
  308. metrics.inc('editor.leave-doc')
  309. const { project_id, user_id } = client.ol_context
  310. logger.log(
  311. { user_id, project_id, doc_id, client_id: client.id },
  312. 'client leaving doc'
  313. )
  314. RoomManager.leaveDoc(client, doc_id)
  315. // we could remove permission when user leaves a doc, but because
  316. // the connection is per-project, we continue to allow access
  317. // after the initial joinDoc since we know they are already authorised.
  318. // # AuthorizationManager.removeAccessToDoc client, doc_id
  319. return callback()
  320. },
  321. updateClientPosition(client, cursorData, callback) {
  322. if (callback == null) {
  323. callback = function (error) {}
  324. }
  325. if (client.disconnected) {
  326. // do not create a ghost entry in redis
  327. return callback()
  328. }
  329. metrics.inc('editor.update-client-position', 0.1)
  330. const {
  331. project_id,
  332. first_name,
  333. last_name,
  334. email,
  335. user_id
  336. } = client.ol_context
  337. logger.log(
  338. { user_id, project_id, client_id: client.id, cursorData },
  339. 'updating client position'
  340. )
  341. return AuthorizationManager.assertClientCanViewProjectAndDoc(
  342. client,
  343. cursorData.doc_id,
  344. function (error) {
  345. if (error != null) {
  346. logger.warn(
  347. { err: error, client_id: client.id, project_id, user_id },
  348. "silently ignoring unauthorized updateClientPosition. Client likely hasn't called joinProject yet."
  349. )
  350. return callback()
  351. }
  352. cursorData.id = client.publicId
  353. if (user_id != null) {
  354. cursorData.user_id = user_id
  355. }
  356. if (email != null) {
  357. cursorData.email = email
  358. }
  359. // Don't store anonymous users in redis to avoid influx
  360. if (!user_id || user_id === 'anonymous-user') {
  361. cursorData.name = ''
  362. callback()
  363. } else {
  364. cursorData.name =
  365. first_name && last_name
  366. ? `${first_name} ${last_name}`
  367. : first_name || last_name || ''
  368. ConnectedUsersManager.updateUserPosition(
  369. project_id,
  370. client.publicId,
  371. {
  372. first_name,
  373. last_name,
  374. email,
  375. _id: user_id
  376. },
  377. {
  378. row: cursorData.row,
  379. column: cursorData.column,
  380. doc_id: cursorData.doc_id
  381. },
  382. callback
  383. )
  384. }
  385. return WebsocketLoadBalancer.emitToRoom(
  386. project_id,
  387. 'clientTracking.clientUpdated',
  388. cursorData
  389. )
  390. }
  391. )
  392. },
  393. CLIENT_REFRESH_DELAY: 1000,
  394. getConnectedUsers(client, callback) {
  395. if (callback == null) {
  396. callback = function (error, users) {}
  397. }
  398. if (client.disconnected) {
  399. // they are not interested anymore, skip the redis lookups
  400. return callback()
  401. }
  402. metrics.inc('editor.get-connected-users')
  403. const { project_id, user_id, is_restricted_user } = client.ol_context
  404. if (is_restricted_user) {
  405. return callback(null, [])
  406. }
  407. if (project_id == null) {
  408. return callback(new Error('no project_id found on client'))
  409. }
  410. logger.log(
  411. { user_id, project_id, client_id: client.id },
  412. 'getting connected users'
  413. )
  414. return AuthorizationManager.assertClientCanViewProject(client, function (
  415. error
  416. ) {
  417. if (error != null) {
  418. return callback(error)
  419. }
  420. WebsocketLoadBalancer.emitToRoom(project_id, 'clientTracking.refresh')
  421. return setTimeout(
  422. () =>
  423. ConnectedUsersManager.getConnectedUsers(project_id, function (
  424. error,
  425. users
  426. ) {
  427. if (error != null) {
  428. return callback(error)
  429. }
  430. callback(null, users)
  431. return logger.log(
  432. { user_id, project_id, client_id: client.id },
  433. 'got connected users'
  434. )
  435. }),
  436. WebsocketController.CLIENT_REFRESH_DELAY
  437. )
  438. })
  439. },
  440. applyOtUpdate(client, doc_id, update, callback) {
  441. // client may have disconnected, but we can submit their update to doc-updater anyways.
  442. if (callback == null) {
  443. callback = function (error) {}
  444. }
  445. const { user_id, project_id } = client.ol_context
  446. if (project_id == null) {
  447. return callback(new Error('no project_id found on client'))
  448. }
  449. return WebsocketController._assertClientCanApplyUpdate(
  450. client,
  451. doc_id,
  452. update,
  453. function (error) {
  454. if (error != null) {
  455. logger.warn(
  456. { err: error, doc_id, client_id: client.id, version: update.v },
  457. 'client is not authorized to make update'
  458. )
  459. setTimeout(
  460. () =>
  461. // Disconnect, but give the client the chance to receive the error
  462. client.disconnect(),
  463. 100
  464. )
  465. return callback(error)
  466. }
  467. if (!update.meta) {
  468. update.meta = {}
  469. }
  470. update.meta.source = client.publicId
  471. update.meta.user_id = user_id
  472. metrics.inc('editor.doc-update', 0.3)
  473. logger.log(
  474. {
  475. user_id,
  476. doc_id,
  477. project_id,
  478. client_id: client.id,
  479. version: update.v
  480. },
  481. 'sending update to doc updater'
  482. )
  483. return DocumentUpdaterManager.queueChange(
  484. project_id,
  485. doc_id,
  486. update,
  487. function (error) {
  488. if (
  489. (error != null ? error.message : undefined) ===
  490. 'update is too large'
  491. ) {
  492. metrics.inc('update_too_large')
  493. const { updateSize } = error
  494. logger.warn(
  495. { user_id, project_id, doc_id, updateSize },
  496. 'update is too large'
  497. )
  498. // mark the update as received -- the client should not send it again!
  499. callback()
  500. // trigger an out-of-sync error
  501. const message = {
  502. project_id,
  503. doc_id,
  504. error: 'update is too large'
  505. }
  506. setTimeout(function () {
  507. if (client.disconnected) {
  508. // skip the message broadcast, the client has moved on
  509. return metrics.inc('editor.doc-update.disconnected', 1, {
  510. status: 'at-otUpdateError'
  511. })
  512. }
  513. client.emit('otUpdateError', message.error, message)
  514. return client.disconnect()
  515. }, 100)
  516. return
  517. }
  518. if (error != null) {
  519. logger.error(
  520. {
  521. err: error,
  522. project_id,
  523. doc_id,
  524. client_id: client.id,
  525. version: update.v
  526. },
  527. 'document was not available for update'
  528. )
  529. client.disconnect()
  530. }
  531. return callback(error)
  532. }
  533. )
  534. }
  535. )
  536. },
  537. _assertClientCanApplyUpdate(client, doc_id, update, callback) {
  538. return AuthorizationManager.assertClientCanEditProjectAndDoc(
  539. client,
  540. doc_id,
  541. function (error) {
  542. if (error != null) {
  543. if (
  544. error.message === 'not authorized' &&
  545. WebsocketController._isCommentUpdate(update)
  546. ) {
  547. // This might be a comment op, which we only need read-only priveleges for
  548. return AuthorizationManager.assertClientCanViewProjectAndDoc(
  549. client,
  550. doc_id,
  551. callback
  552. )
  553. } else {
  554. return callback(error)
  555. }
  556. } else {
  557. return callback(null)
  558. }
  559. }
  560. )
  561. },
  562. _isCommentUpdate(update) {
  563. for (const op of Array.from(update.op)) {
  564. if (op.c == null) {
  565. return false
  566. }
  567. }
  568. return true
  569. }
  570. }
  571. function __guard__(value, transform) {
  572. return typeof value !== 'undefined' && value !== null
  573. ? transform(value)
  574. : undefined
  575. }