ProjectController.js 37 KB

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