ProjectController.js 41 KB

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