ProjectController.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. const _ = require('lodash')
  2. const OError = require('@overleaf/o-error')
  3. const crypto = require('crypto')
  4. const { setTimeout } = require('timers/promises')
  5. const pProps = require('p-props')
  6. const logger = require('@overleaf/logger')
  7. const { expressify } = require('@overleaf/promise-utils')
  8. const { ObjectId } = require('mongodb-legacy')
  9. const ProjectDeleter = require('./ProjectDeleter')
  10. const ProjectDuplicator = require('./ProjectDuplicator')
  11. const ProjectCreationHandler = require('./ProjectCreationHandler')
  12. const EditorController = require('../Editor/EditorController')
  13. const ProjectHelper = require('./ProjectHelper')
  14. const metrics = require('@overleaf/metrics')
  15. const { User } = require('../../models/User')
  16. const SubscriptionLocator = require('../Subscription/SubscriptionLocator')
  17. const LimitationsManager = require('../Subscription/LimitationsManager')
  18. const Settings = require('@overleaf/settings')
  19. const AuthorizationManager = require('../Authorization/AuthorizationManager')
  20. const InactiveProjectManager = require('../InactiveData/InactiveProjectManager')
  21. const ProjectUpdateHandler = require('./ProjectUpdateHandler')
  22. const ProjectGetter = require('./ProjectGetter')
  23. const PrivilegeLevels = require('../Authorization/PrivilegeLevels')
  24. const SessionManager = require('../Authentication/SessionManager')
  25. const Sources = require('../Authorization/Sources')
  26. const TokenAccessHandler = require('../TokenAccess/TokenAccessHandler')
  27. const CollaboratorsGetter = require('../Collaborators/CollaboratorsGetter')
  28. const ProjectEntityHandler = require('./ProjectEntityHandler')
  29. const TpdsProjectFlusher = require('../ThirdPartyDataStore/TpdsProjectFlusher')
  30. const Features = require('../../infrastructure/Features')
  31. const BrandVariationsHandler = require('../BrandVariations/BrandVariationsHandler')
  32. const UserController = require('../User/UserController')
  33. const AnalyticsManager = require('../Analytics/AnalyticsManager')
  34. const SplitTestHandler = require('../SplitTests/SplitTestHandler')
  35. const SplitTestSessionHandler = require('../SplitTests/SplitTestSessionHandler')
  36. const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
  37. const SpellingHandler = require('../Spelling/SpellingHandler')
  38. const { hasAdminAccess } = require('../Helpers/AdminAuthorizationHelper')
  39. const InstitutionsFeatures = require('../Institutions/InstitutionsFeatures')
  40. const ProjectAuditLogHandler = require('./ProjectAuditLogHandler')
  41. const PublicAccessLevels = require('../Authorization/PublicAccessLevels')
  42. const TagsHandler = require('../Tags/TagsHandler')
  43. const TutorialHandler = require('../Tutorial/TutorialHandler')
  44. const OnboardingDataCollectionManager = require('../OnboardingDataCollection/OnboardingDataCollectionManager')
  45. const UserUpdater = require('../User/UserUpdater')
  46. const Modules = require('../../infrastructure/Modules')
  47. const UserGetter = require('../User/UserGetter')
  48. /**
  49. * @typedef {import("./types").GetProjectsRequest} GetProjectsRequest
  50. * @typedef {import("./types").GetProjectsResponse} GetProjectsResponse
  51. * @typedef {import("./types").Project} Project
  52. */
  53. const _ProjectController = {
  54. _isInPercentageRollout(rolloutName, objectId, percentage) {
  55. if (Settings.bypassPercentageRollouts === true) {
  56. return true
  57. }
  58. const data = `${rolloutName}:${objectId.toString()}`
  59. const md5hash = crypto.createHash('md5').update(data).digest('hex')
  60. const counter = parseInt(md5hash.slice(26, 32), 16)
  61. return counter % 100 < percentage
  62. },
  63. async updateProjectSettings(req, res) {
  64. const projectId = req.params.Project_id
  65. if (req.body.compiler != null) {
  66. await EditorController.promises.setCompiler(projectId, req.body.compiler)
  67. }
  68. if (req.body.imageName != null) {
  69. await EditorController.promises.setImageName(
  70. projectId,
  71. req.body.imageName
  72. )
  73. }
  74. if (req.body.name != null) {
  75. await EditorController.promises.renameProject(projectId, req.body.name)
  76. }
  77. if (req.body.spellCheckLanguage != null) {
  78. await EditorController.promises.setSpellCheckLanguage(
  79. projectId,
  80. req.body.spellCheckLanguage
  81. )
  82. }
  83. if (req.body.rootDocId != null) {
  84. await EditorController.promises.setRootDoc(projectId, req.body.rootDocId)
  85. }
  86. res.sendStatus(204)
  87. },
  88. async updateProjectAdminSettings(req, res) {
  89. const projectId = req.params.Project_id
  90. const user = SessionManager.getSessionUser(req.session)
  91. const publicAccessLevel = req.body.publicAccessLevel
  92. const publicAccessLevels = [
  93. PublicAccessLevels.READ_ONLY,
  94. PublicAccessLevels.READ_AND_WRITE,
  95. PublicAccessLevels.PRIVATE,
  96. PublicAccessLevels.TOKEN_BASED,
  97. ]
  98. if (
  99. req.body.publicAccessLevel != null &&
  100. publicAccessLevels.includes(publicAccessLevel)
  101. ) {
  102. await EditorController.promises.setPublicAccessLevel(
  103. projectId,
  104. req.body.publicAccessLevel
  105. )
  106. await ProjectAuditLogHandler.promises.addEntry(
  107. projectId,
  108. 'toggle-access-level',
  109. user._id,
  110. req.ip,
  111. { publicAccessLevel: req.body.publicAccessLevel, status: 'OK' }
  112. )
  113. res.sendStatus(204)
  114. } else {
  115. res.sendStatus(500)
  116. }
  117. },
  118. async deleteProject(req, res) {
  119. const projectId = req.params.Project_id
  120. const user = SessionManager.getSessionUser(req.session)
  121. await ProjectDeleter.promises.deleteProject(projectId, {
  122. deleterUser: user,
  123. ipAddress: req.ip,
  124. })
  125. res.sendStatus(200)
  126. },
  127. async archiveProject(req, res) {
  128. const projectId = req.params.Project_id
  129. const userId = SessionManager.getLoggedInUserId(req.session)
  130. await ProjectDeleter.promises.archiveProject(projectId, userId)
  131. res.sendStatus(200)
  132. },
  133. async unarchiveProject(req, res) {
  134. const projectId = req.params.Project_id
  135. const userId = SessionManager.getLoggedInUserId(req.session)
  136. await ProjectDeleter.promises.unarchiveProject(projectId, userId)
  137. res.sendStatus(200)
  138. },
  139. async trashProject(req, res) {
  140. const projectId = req.params.project_id
  141. const userId = SessionManager.getLoggedInUserId(req.session)
  142. await ProjectDeleter.promises.trashProject(projectId, userId)
  143. res.sendStatus(200)
  144. },
  145. async untrashProject(req, res) {
  146. const projectId = req.params.project_id
  147. const userId = SessionManager.getLoggedInUserId(req.session)
  148. await ProjectDeleter.promises.untrashProject(projectId, userId)
  149. res.sendStatus(200)
  150. },
  151. async expireDeletedProjectsAfterDuration(_req, res) {
  152. await ProjectDeleter.promises.expireDeletedProjectsAfterDuration()
  153. res.sendStatus(200)
  154. },
  155. async expireDeletedProject(req, res) {
  156. const { projectId } = req.params
  157. await ProjectDeleter.promises.expireDeletedProject(projectId)
  158. res.sendStatus(200)
  159. },
  160. async restoreProject(req, res) {
  161. const projectId = req.params.Project_id
  162. await ProjectDeleter.promises.restoreProject(projectId)
  163. res.sendStatus(200)
  164. },
  165. async cloneProject(req, res, next) {
  166. res.setTimeout(5 * 60 * 1000) // allow extra time for the copy to complete
  167. metrics.inc('cloned-project')
  168. const projectId = req.params.Project_id
  169. const { projectName, tags } = req.body
  170. logger.debug({ projectId, projectName }, 'cloning project')
  171. if (!SessionManager.isUserLoggedIn(req.session)) {
  172. return res.json({ redir: '/register' })
  173. }
  174. const currentUser = SessionManager.getSessionUser(req.session)
  175. const { first_name: firstName, last_name: lastName, email } = currentUser
  176. try {
  177. const project = await ProjectDuplicator.promises.duplicate(
  178. currentUser,
  179. projectId,
  180. projectName,
  181. tags
  182. )
  183. res.json({
  184. name: project.name,
  185. lastUpdated: project.lastUpdated,
  186. project_id: project._id,
  187. owner_ref: project.owner_ref,
  188. owner: {
  189. first_name: firstName,
  190. last_name: lastName,
  191. email,
  192. _id: currentUser._id,
  193. },
  194. })
  195. } catch (err) {
  196. OError.tag(err, 'error cloning project', {
  197. projectId,
  198. userId: currentUser._id,
  199. })
  200. return next(err)
  201. }
  202. },
  203. async newProject(req, res) {
  204. const currentUser = SessionManager.getSessionUser(req.session)
  205. const {
  206. first_name: firstName,
  207. last_name: lastName,
  208. email,
  209. _id: userId,
  210. } = currentUser
  211. const projectName =
  212. req.body.projectName != null ? req.body.projectName.trim() : undefined
  213. const { template } = req.body
  214. const project = await (template === 'example'
  215. ? ProjectCreationHandler.promises.createExampleProject(
  216. userId,
  217. projectName
  218. )
  219. : ProjectCreationHandler.promises.createBasicProject(userId, projectName))
  220. res.json({
  221. project_id: project._id,
  222. owner_ref: project.owner_ref,
  223. owner: {
  224. first_name: firstName,
  225. last_name: lastName,
  226. email,
  227. _id: userId,
  228. },
  229. })
  230. },
  231. async renameProject(req, res) {
  232. const projectId = req.params.Project_id
  233. const newName = req.body.newProjectName
  234. await EditorController.promises.renameProject(projectId, newName)
  235. res.sendStatus(200)
  236. },
  237. async userProjectsJson(req, res) {
  238. const userId = SessionManager.getLoggedInUserId(req.session)
  239. let projects = await ProjectGetter.promises.findAllUsersProjects(
  240. userId,
  241. 'name lastUpdated publicAccesLevel archived trashed owner_ref'
  242. )
  243. // _buildProjectList already converts archived/trashed to booleans so isArchivedOrTrashed should not be used here
  244. projects = ProjectController._buildProjectList(projects, userId)
  245. .filter(p => !(p.archived || p.trashed))
  246. .map(p => ({ _id: p.id, name: p.name, accessLevel: p.accessLevel }))
  247. res.json({ projects })
  248. },
  249. async projectEntitiesJson(req, res) {
  250. const projectId = req.params.Project_id
  251. const project = await ProjectGetter.promises.getProject(projectId)
  252. const { docs, files } =
  253. ProjectEntityHandler.getAllEntitiesFromProject(project)
  254. const entities = docs
  255. .concat(files)
  256. // Sort by path ascending
  257. .sort((a, b) => (a.path > b.path ? 1 : a.path < b.path ? -1 : 0))
  258. .map(e => ({
  259. path: e.path,
  260. type: e.doc != null ? 'doc' : 'file',
  261. }))
  262. res.json({ project_id: projectId, entities })
  263. },
  264. async loadEditor(req, res, next) {
  265. const timer = new metrics.Timer('load-editor')
  266. if (!Settings.editorIsOpen) {
  267. return res.render('general/closed', { title: 'updating_site' })
  268. }
  269. let anonymous, userId, sessionUser
  270. if (SessionManager.isUserLoggedIn(req.session)) {
  271. sessionUser = SessionManager.getSessionUser(req.session)
  272. userId = SessionManager.getLoggedInUserId(req.session)
  273. anonymous = false
  274. } else {
  275. sessionUser = null
  276. anonymous = true
  277. userId = null
  278. }
  279. const projectId = req.params.Project_id
  280. // should not be used in place of split tests query param overrides (?my-split-test-name=my-variant)
  281. function shouldDisplayFeature(name, variantFlag) {
  282. if (req.query && req.query[name]) {
  283. return req.query[name] === 'true'
  284. } else {
  285. return variantFlag === true
  286. }
  287. }
  288. const splitTests = [
  289. !anonymous && 'bib-file-tpr-prompt',
  290. 'compile-log-events',
  291. 'math-preview',
  292. 'null-test-share-modal',
  293. 'paywall-cta',
  294. 'pdf-caching-cached-url-lookup',
  295. 'pdf-caching-mode',
  296. 'pdf-caching-prefetch-large',
  297. 'pdf-caching-prefetching',
  298. 'pdf-presentation-mode',
  299. 'pdfjs-40',
  300. 'personal-access-token',
  301. 'revert-file',
  302. 'revert-project',
  303. 'review-panel-redesign',
  304. !anonymous && 'ro-mirror-on-client',
  305. 'track-pdf-download',
  306. !anonymous && 'writefull-oauth-promotion',
  307. 'ieee-stylesheet',
  308. 'write-and-cite',
  309. 'default-visual-for-beginners',
  310. ].filter(Boolean)
  311. const getUserValues = async userId =>
  312. pProps(
  313. _.mapValues({
  314. user: (async () => {
  315. const user = await User.findById(
  316. userId,
  317. 'email first_name last_name referal_id signUpDate featureSwitches features featuresEpoch refProviders alphaProgram betaProgram isAdmin ace labsProgram completedTutorials writefull'
  318. ).exec()
  319. // Handle case of deleted user
  320. if (!user) {
  321. UserController.logout(req, res, next)
  322. return
  323. }
  324. logger.debug({ projectId, userId }, 'got user')
  325. return FeaturesUpdater.featuresEpochIsCurrent(user)
  326. ? user
  327. : await ProjectController._refreshFeatures(req, user)
  328. })(),
  329. learnedWords: SpellingHandler.promises.getUserDictionary(userId),
  330. projectTags: TagsHandler.promises.getTagsForProject(
  331. userId,
  332. projectId
  333. ),
  334. userHasInstitutionLicence: InstitutionsFeatures.promises
  335. .hasLicence(userId)
  336. .catch(err => {
  337. logger.error({ err, userId }, 'failed to get institution licence')
  338. return false
  339. }),
  340. subscription:
  341. SubscriptionLocator.promises.getUsersSubscription(userId),
  342. isTokenMember: CollaboratorsGetter.promises.userIsTokenMember(
  343. userId,
  344. projectId
  345. ),
  346. isInvitedMember:
  347. CollaboratorsGetter.promises.isUserInvitedMemberOfProject(
  348. userId,
  349. projectId
  350. ),
  351. usedLatex: OnboardingDataCollectionManager.getOnboardingDataValue(
  352. userId,
  353. 'usedLatex'
  354. ).catch(err => {
  355. logger.error({ err, userId })
  356. return null
  357. }),
  358. })
  359. )
  360. const splitTestAssignments = {}
  361. try {
  362. const responses = await pProps({
  363. userValues: userId ? getUserValues(userId) : defaultUserValues(),
  364. splitTestAssignments: Promise.all(
  365. splitTests.map(async splitTest => {
  366. splitTestAssignments[splitTest] =
  367. await SplitTestHandler.promises.getAssignment(req, res, splitTest)
  368. })
  369. ),
  370. project: ProjectGetter.promises.getProject(projectId, {
  371. name: 1,
  372. lastUpdated: 1,
  373. track_changes: 1,
  374. owner_ref: 1,
  375. brandVariationId: 1,
  376. overleaf: 1,
  377. tokens: 1,
  378. tokenAccessReadAndWrite_refs: 1, // used for link sharing analytics
  379. collaberator_refs: 1, // used for link sharing analytics
  380. pendingEditor_refs: 1, // used for link sharing analytics
  381. }),
  382. userIsMemberOfGroupSubscription: sessionUser
  383. ? (async () =>
  384. (
  385. await LimitationsManager.promises.userIsMemberOfGroupSubscription(
  386. sessionUser
  387. )
  388. ).isMember)()
  389. : false,
  390. _flushToTpds:
  391. TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(projectId),
  392. _activate:
  393. InactiveProjectManager.promises.reactivateProjectIfRequired(
  394. projectId
  395. ),
  396. })
  397. const { project, userValues, userIsMemberOfGroupSubscription } = responses
  398. const {
  399. user,
  400. learnedWords,
  401. projectTags,
  402. userHasInstitutionLicence,
  403. subscription,
  404. isTokenMember,
  405. isInvitedMember,
  406. usedLatex,
  407. } = userValues
  408. // check if a user is not in the writefull-oauth-promotion, in which case they may be part of the auto trial group
  409. if (
  410. !anonymous &&
  411. splitTestAssignments['writefull-oauth-promotion']?.variant === 'default'
  412. ) {
  413. // since we are auto-enrolling users into writefull if they are part of the group, we only want to
  414. // auto enroll (set writefull to true) if its the first time they have entered the test
  415. // this ensures that they can still turn writefull off (otherwise, we would be setting writefull on every time they access their projects)
  416. const { variant, metadata } =
  417. await SplitTestHandler.promises.getAssignment(
  418. req,
  419. res,
  420. 'writefull-auto-load'
  421. )
  422. if (variant === 'enabled' && metadata?.isFirstNonDefaultAssignment) {
  423. await UserUpdater.promises.updateUser(userId, {
  424. $set: {
  425. writefull: { enabled: true },
  426. },
  427. })
  428. user.writefull.enabled = true
  429. }
  430. }
  431. const brandVariation = project?.brandVariationId
  432. ? await BrandVariationsHandler.promises.getBrandVariationById(
  433. project.brandVariationId
  434. )
  435. : undefined
  436. const anonRequestToken = TokenAccessHandler.getRequestToken(
  437. req,
  438. projectId
  439. )
  440. const allowedImageNames = ProjectHelper.getAllowedImagesForUser(user)
  441. const privilegeLevel =
  442. await AuthorizationManager.promises.getPrivilegeLevelForProject(
  443. userId,
  444. projectId,
  445. anonRequestToken
  446. )
  447. const [linkSharingChanges, linkSharingEnforcement] = await Promise.all([
  448. SplitTestHandler.promises.getAssignmentForUser(
  449. project.owner_ref,
  450. 'link-sharing-warning'
  451. ),
  452. SplitTestHandler.promises.getAssignmentForUser(
  453. project.owner_ref,
  454. 'link-sharing-enforcement'
  455. ),
  456. ])
  457. if (linkSharingChanges?.variant === 'active') {
  458. if (linkSharingEnforcement?.variant === 'active') {
  459. await Modules.promises.hooks.fire(
  460. 'enforceCollaboratorLimit',
  461. projectId
  462. )
  463. }
  464. if (isTokenMember) {
  465. // Check explicitly that the user is in read write token refs, while this could be inferred
  466. // from the privilege level, the privilege level of token members might later be restricted
  467. const isReadWriteTokenMember =
  468. await CollaboratorsGetter.promises.userIsReadWriteTokenMember(
  469. userId,
  470. projectId
  471. )
  472. if (isReadWriteTokenMember) {
  473. // Check for an edge case where a user is both in read write token access refs but also
  474. // an invited read write member. Ensure they are not redirected to the sharing updates page
  475. // We could also delete the token access ref if the user is already a member of the project
  476. const isInvitedReadWriteMember =
  477. await CollaboratorsGetter.promises.isUserInvitedReadWriteMemberOfProject(
  478. userId,
  479. projectId
  480. )
  481. if (!isInvitedReadWriteMember) {
  482. return res.redirect(`/project/${projectId}/sharing-updates`)
  483. }
  484. }
  485. }
  486. }
  487. let allowedFreeTrial = true
  488. if (privilegeLevel == null || privilegeLevel === PrivilegeLevels.NONE) {
  489. return res.sendStatus(401)
  490. }
  491. if (subscription != null) {
  492. allowedFreeTrial = false
  493. }
  494. let wsUrl = Settings.wsUrl
  495. let metricName = 'load-editor-ws'
  496. if (user.betaProgram && Settings.wsUrlBeta !== undefined) {
  497. wsUrl = Settings.wsUrlBeta
  498. metricName += '-beta'
  499. } else if (
  500. Settings.wsUrlV2 &&
  501. Settings.wsUrlV2Percentage > 0 &&
  502. (new ObjectId(projectId).getTimestamp() / 1000) % 100 <
  503. Settings.wsUrlV2Percentage
  504. ) {
  505. wsUrl = Settings.wsUrlV2
  506. metricName += '-v2'
  507. }
  508. if (req.query && req.query.ws === 'fallback') {
  509. // `?ws=fallback` will connect to the bare origin, and ignore
  510. // the custom wsUrl. Hence it must load the client side
  511. // javascript from there too.
  512. // Not resetting it here would possibly load a socket.io v2
  513. // client and connect to a v0 endpoint.
  514. wsUrl = undefined
  515. metricName += '-fallback'
  516. }
  517. metrics.inc(metricName)
  518. // don't need to wait for these to complete
  519. ProjectUpdateHandler.promises
  520. .markAsOpened(projectId)
  521. .catch(err =>
  522. logger.error({ err, projectId }, 'failed to mark project as opened')
  523. )
  524. SplitTestSessionHandler.promises
  525. .sessionMaintenance(req, userId ? user : null)
  526. .catch(err =>
  527. logger.error({ err }, 'failed to update split test info in session')
  528. )
  529. if (userId) {
  530. const ownerFeatures = await UserGetter.promises.getUserFeatures(
  531. project.owner_ref
  532. )
  533. const planLimit = ownerFeatures?.collaborators || 0
  534. const namedEditors = project.collaberator_refs?.length || 0
  535. const pendingEditors = project.pendingEditor_refs?.length || 0
  536. const exceedAtLimit = planLimit > -1 && namedEditors >= planLimit
  537. const projectOpenedSegmentation = {
  538. projectId: project._id,
  539. // temporary link sharing segmentation:
  540. linkSharingWarning: linkSharingChanges?.variant,
  541. namedEditors,
  542. pendingEditors,
  543. tokenEditors: project.tokenAccessReadAndWrite_refs?.length || 0,
  544. planLimit,
  545. exceedAtLimit,
  546. }
  547. AnalyticsManager.recordEventForUserInBackground(
  548. userId,
  549. 'project-opened',
  550. projectOpenedSegmentation
  551. )
  552. User.updateOne(
  553. { _id: new ObjectId(userId) },
  554. { $set: { lastActive: new Date() } }
  555. )
  556. .exec()
  557. .catch(err =>
  558. logger.error(
  559. { err, userId },
  560. 'failed to update lastActive for user'
  561. )
  562. )
  563. }
  564. const isAdminOrTemplateOwner =
  565. hasAdminAccess(user) || Settings.templates?.user_id === userId
  566. const showTemplatesServerPro =
  567. Features.hasFeature('templates-server-pro') && isAdminOrTemplateOwner
  568. const debugPdfDetach = shouldDisplayFeature('debug_pdf_detach')
  569. const detachRole = req.params.detachRole
  570. const showSymbolPalette =
  571. !Features.hasFeature('saas') ||
  572. (user.features && user.features.symbolPalette)
  573. // Persistent upgrade prompts
  574. // in header & in share project modal
  575. const showUpgradePrompt =
  576. Features.hasFeature('saas') &&
  577. userId &&
  578. !subscription &&
  579. !userIsMemberOfGroupSubscription &&
  580. !userHasInstitutionLicence
  581. const showPersonalAccessToken =
  582. userId &&
  583. (!Features.hasFeature('saas') ||
  584. req.query?.personal_access_token === 'true')
  585. const optionalPersonalAccessToken =
  586. userId &&
  587. !showPersonalAccessToken &&
  588. splitTestAssignments['personal-access-token'].variant === 'enabled' // `?personal-access-token=enabled`
  589. let showAiErrorAssistant = false
  590. if (userId && Features.hasFeature('saas')) {
  591. try {
  592. // exit early if the user couldnt use ai anyways, since permissions checks are expensive
  593. const canUseAiOnProject =
  594. user.features?.aiErrorAssistant &&
  595. (privilegeLevel === PrivilegeLevels.READ_AND_WRITE ||
  596. privilegeLevel === PrivilegeLevels.OWNER)
  597. if (canUseAiOnProject) {
  598. // check permissions for user and project owner, to see if they allow AI on the project
  599. const permissionsResults = await Modules.promises.hooks.fire(
  600. 'projectAllowsCapability',
  601. project,
  602. userId,
  603. ['use-ai']
  604. )
  605. const aiAllowed = permissionsResults.every(
  606. result => result === true
  607. )
  608. showAiErrorAssistant = aiAllowed
  609. }
  610. } catch (err) {
  611. // still allow users to access project if we cant get their permissions, but disable AI feature
  612. showAiErrorAssistant = false
  613. }
  614. }
  615. const template =
  616. detachRole === 'detached'
  617. ? 'project/ide-react-detached'
  618. : 'project/ide-react'
  619. res.render(template, {
  620. title: project.name,
  621. priority_title: true,
  622. bodyClasses: ['editor'],
  623. project_id: project._id,
  624. projectName: project.name,
  625. user: {
  626. id: userId,
  627. email: user.email,
  628. first_name: user.first_name,
  629. last_name: user.last_name,
  630. referal_id: user.referal_id,
  631. signUpDate: user.signUpDate,
  632. allowedFreeTrial,
  633. featureSwitches: user.featureSwitches,
  634. features: user.features,
  635. refProviders: _.mapValues(user.refProviders, Boolean),
  636. writefull: {
  637. enabled: Boolean(user.writefull?.enabled),
  638. },
  639. alphaProgram: user.alphaProgram,
  640. betaProgram: user.betaProgram,
  641. labsProgram: user.labsProgram,
  642. inactiveTutorials: TutorialHandler.getInactiveTutorials(user),
  643. isAdmin: hasAdminAccess(user),
  644. },
  645. userSettings: {
  646. mode: user.ace.mode,
  647. editorTheme: user.ace.theme,
  648. fontSize: user.ace.fontSize,
  649. autoComplete: user.ace.autoComplete,
  650. autoPairDelimiters: user.ace.autoPairDelimiters,
  651. pdfViewer: user.ace.pdfViewer,
  652. syntaxValidation: user.ace.syntaxValidation,
  653. fontFamily: user.ace.fontFamily || 'lucida',
  654. lineHeight: user.ace.lineHeight || 'normal',
  655. overallTheme: user.ace.overallTheme,
  656. mathPreview: user.ace.mathPreview,
  657. },
  658. privilegeLevel,
  659. anonymous,
  660. isTokenMember,
  661. isRestrictedTokenMember: AuthorizationManager.isRestrictedUser(
  662. userId,
  663. privilegeLevel,
  664. isTokenMember,
  665. isInvitedMember
  666. ),
  667. languages: Settings.languages,
  668. learnedWords,
  669. editorThemes: THEME_LIST,
  670. legacyEditorThemes: LEGACY_THEME_LIST,
  671. maxDocLength: Settings.max_doc_length,
  672. brandVariation,
  673. allowedImageNames,
  674. gitBridgePublicBaseUrl: Settings.gitBridgePublicBaseUrl,
  675. gitBridgeEnabled: Features.hasFeature('git-bridge'),
  676. wsUrl,
  677. showSupport: Features.hasFeature('support'),
  678. showTemplatesServerPro,
  679. pdfjsVariant: splitTestAssignments['pdfjs-40'].variant,
  680. debugPdfDetach,
  681. showSymbolPalette,
  682. symbolPaletteAvailable: Features.hasFeature('symbol-palette'),
  683. showAiErrorAssistant,
  684. detachRole,
  685. metadata: { viewport: false },
  686. showUpgradePrompt,
  687. fixedSizeDocument: true,
  688. useOpenTelemetry: Settings.useOpenTelemetryClient,
  689. showPersonalAccessToken,
  690. optionalPersonalAccessToken,
  691. hasTrackChangesFeature: Features.hasFeature('track-changes'),
  692. projectTags,
  693. linkSharingWarning: linkSharingChanges.variant === 'active',
  694. usedLatex:
  695. // only use the usedLatex value if the split test is enabled
  696. splitTestAssignments['default-visual-for-beginners']?.variant ===
  697. 'enabled'
  698. ? usedLatex
  699. : null,
  700. })
  701. timer.done()
  702. } catch (err) {
  703. OError.tag(err, 'error getting details for project page')
  704. return next(err)
  705. }
  706. },
  707. async _refreshFeatures(req, user) {
  708. // If the feature refresh has failed in this session, don't retry
  709. // it - require the user to log in again.
  710. if (req.session.feature_refresh_failed) {
  711. metrics.inc('features-refresh', 1, {
  712. path: 'load-editor',
  713. status: 'skipped',
  714. })
  715. return user
  716. }
  717. // If the refresh takes too long then return the current
  718. // features. Note that the user.features property may still be
  719. // updated in the background after the promise is resolved.
  720. const abortController = new AbortController()
  721. const refreshTimeoutHandler = async () => {
  722. await setTimeout(5000, { signal: abortController.signal })
  723. req.session.feature_refresh_failed = {
  724. reason: 'timeout',
  725. at: new Date(),
  726. }
  727. metrics.inc('features-refresh', 1, {
  728. path: 'load-editor',
  729. status: 'timeout',
  730. })
  731. return user
  732. }
  733. // try to refresh user features now
  734. const timer = new metrics.Timer('features-refresh-on-load-editor')
  735. return Promise.race([
  736. refreshTimeoutHandler(),
  737. (async () => {
  738. try {
  739. user.features = await FeaturesUpdater.promises.refreshFeatures(
  740. user._id,
  741. 'load-editor'
  742. )
  743. metrics.inc('features-refresh', 1, {
  744. path: 'load-editor',
  745. status: 'success',
  746. })
  747. } catch (err) {
  748. // keep a record to prevent unneceary retries and leave
  749. // the original features unmodified if the refresh failed
  750. req.session.feature_refresh_failed = {
  751. reason: 'error',
  752. at: new Date(),
  753. }
  754. metrics.inc('features-refresh', 1, {
  755. path: 'load-editor',
  756. status: 'error',
  757. })
  758. }
  759. abortController.abort()
  760. timer.done()
  761. return user
  762. })(),
  763. ])
  764. },
  765. _buildProjectList(allProjects, userId) {
  766. let project
  767. const { owned, readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly } =
  768. allProjects
  769. const projects = []
  770. for (project of owned) {
  771. projects.push(
  772. ProjectController._buildProjectViewModel(
  773. project,
  774. 'owner',
  775. Sources.OWNER,
  776. userId
  777. )
  778. )
  779. }
  780. // Invite-access
  781. for (project of readAndWrite) {
  782. projects.push(
  783. ProjectController._buildProjectViewModel(
  784. project,
  785. 'readWrite',
  786. Sources.INVITE,
  787. userId
  788. )
  789. )
  790. }
  791. for (project of readOnly) {
  792. projects.push(
  793. ProjectController._buildProjectViewModel(
  794. project,
  795. 'readOnly',
  796. Sources.INVITE,
  797. userId
  798. )
  799. )
  800. }
  801. // Token-access
  802. // Only add these projects if they're not already present, this gives us cascading access
  803. // from 'owner' => 'token-read-only'
  804. for (project of tokenReadAndWrite) {
  805. if (
  806. projects.filter(p => p.id.toString() === project._id.toString())
  807. .length === 0
  808. ) {
  809. projects.push(
  810. ProjectController._buildProjectViewModel(
  811. project,
  812. 'readAndWrite',
  813. Sources.TOKEN,
  814. userId
  815. )
  816. )
  817. }
  818. }
  819. for (project of tokenReadOnly) {
  820. if (
  821. projects.filter(p => p.id.toString() === project._id.toString())
  822. .length === 0
  823. ) {
  824. projects.push(
  825. ProjectController._buildProjectViewModel(
  826. project,
  827. 'readOnly',
  828. Sources.TOKEN,
  829. userId
  830. )
  831. )
  832. }
  833. }
  834. return projects
  835. },
  836. _buildProjectViewModel(project, accessLevel, source, userId) {
  837. const archived = ProjectHelper.isArchived(project, userId)
  838. // If a project is simultaneously trashed and archived, we will consider it archived but not trashed.
  839. const trashed = ProjectHelper.isTrashed(project, userId) && !archived
  840. const model = {
  841. id: project._id,
  842. name: project.name,
  843. lastUpdated: project.lastUpdated,
  844. lastUpdatedBy: project.lastUpdatedBy,
  845. publicAccessLevel: project.publicAccesLevel,
  846. accessLevel,
  847. source,
  848. archived,
  849. trashed,
  850. owner_ref: project.owner_ref,
  851. isV1Project: false,
  852. }
  853. if (accessLevel === PrivilegeLevels.READ_ONLY && source === Sources.TOKEN) {
  854. model.owner_ref = null
  855. model.lastUpdatedBy = null
  856. }
  857. return model
  858. },
  859. _buildPortalTemplatesList(affiliations) {
  860. if (affiliations == null) {
  861. affiliations = []
  862. }
  863. const portalTemplates = []
  864. for (const aff of affiliations) {
  865. if (
  866. aff.portal &&
  867. aff.portal.slug &&
  868. aff.portal.templates_count &&
  869. aff.portal.templates_count > 0
  870. ) {
  871. const portalPath = aff.institution.isUniversity ? '/edu/' : '/org/'
  872. portalTemplates.push({
  873. name: aff.institution.name,
  874. url: Settings.siteUrl + portalPath + aff.portal.slug,
  875. })
  876. }
  877. }
  878. return portalTemplates
  879. },
  880. }
  881. const defaultSettingsForAnonymousUser = userId => ({
  882. id: userId,
  883. ace: {
  884. mode: 'none',
  885. theme: 'textmate',
  886. fontSize: '12',
  887. autoComplete: true,
  888. spellCheckLanguage: '',
  889. pdfViewer: '',
  890. syntaxValidation: true,
  891. },
  892. subscription: {
  893. freeTrial: {
  894. allowed: true,
  895. },
  896. },
  897. featureSwitches: {
  898. github: false,
  899. },
  900. alphaProgram: false,
  901. betaProgram: false,
  902. writefull: {
  903. enabled: false,
  904. },
  905. })
  906. const defaultUserValues = () => ({
  907. user: defaultSettingsForAnonymousUser(null),
  908. learnedWords: [],
  909. projectTags: [],
  910. userHasInstitutionLicence: false,
  911. subscription: undefined,
  912. isTokenMember: false,
  913. isInvitedMember: false,
  914. })
  915. const THEME_LIST = [
  916. 'cobalt',
  917. 'dracula',
  918. 'eclipse',
  919. 'monokai',
  920. 'overleaf',
  921. 'textmate',
  922. ]
  923. const LEGACY_THEME_LIST = [
  924. 'ambiance',
  925. 'chaos',
  926. 'chrome',
  927. 'clouds',
  928. 'clouds_midnight',
  929. 'crimson_editor',
  930. 'dawn',
  931. 'dreamweaver',
  932. 'github',
  933. 'gob',
  934. 'gruvbox',
  935. 'idle_fingers',
  936. 'iplastic',
  937. 'katzenmilch',
  938. 'kr_theme',
  939. 'kuroir',
  940. 'merbivore',
  941. 'merbivore_soft',
  942. 'mono_industrial',
  943. 'nord_dark',
  944. 'pastel_on_dark',
  945. 'solarized_dark',
  946. 'solarized_light',
  947. 'sqlserver',
  948. 'terminal',
  949. 'tomorrow',
  950. 'tomorrow_night',
  951. 'tomorrow_night_blue',
  952. 'tomorrow_night_bright',
  953. 'tomorrow_night_eighties',
  954. 'twilight',
  955. 'vibrant_ink',
  956. 'xcode',
  957. ]
  958. const ProjectController = {
  959. archiveProject: expressify(_ProjectController.archiveProject),
  960. cloneProject: expressify(_ProjectController.cloneProject),
  961. deleteProject: expressify(_ProjectController.deleteProject),
  962. expireDeletedProject: expressify(_ProjectController.expireDeletedProject),
  963. expireDeletedProjectsAfterDuration: expressify(
  964. _ProjectController.expireDeletedProjectsAfterDuration
  965. ),
  966. loadEditor: expressify(_ProjectController.loadEditor),
  967. newProject: expressify(_ProjectController.newProject),
  968. projectEntitiesJson: expressify(_ProjectController.projectEntitiesJson),
  969. renameProject: expressify(_ProjectController.renameProject),
  970. restoreProject: expressify(_ProjectController.restoreProject),
  971. trashProject: expressify(_ProjectController.trashProject),
  972. unarchiveProject: expressify(_ProjectController.unarchiveProject),
  973. untrashProject: expressify(_ProjectController.untrashProject),
  974. updateProjectAdminSettings: expressify(
  975. _ProjectController.updateProjectAdminSettings
  976. ),
  977. updateProjectSettings: expressify(_ProjectController.updateProjectSettings),
  978. userProjectsJson: expressify(_ProjectController.userProjectsJson),
  979. _buildProjectList: _ProjectController._buildProjectList,
  980. _buildProjectViewModel: _ProjectController._buildProjectViewModel,
  981. _injectProjectUsers: _ProjectController._injectProjectUsers,
  982. _isInPercentageRollout: _ProjectController._isInPercentageRollout,
  983. _refreshFeatures: _ProjectController._refreshFeatures,
  984. }
  985. module.exports = ProjectController