ProjectController.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. 'hotjar',
  316. 'reviewer-role',
  317. 'editor-redesign',
  318. 'paywall-change-compile-timeout',
  319. 'overleaf-assist-bundle',
  320. 'wf-feature-rebrand',
  321. 'word-count-client',
  322. 'editor-popup-ux-survey',
  323. ].filter(Boolean)
  324. const getUserValues = async userId =>
  325. pProps(
  326. _.mapValues({
  327. user: (async () => {
  328. const user = await User.findById(
  329. userId,
  330. 'email first_name last_name referal_id signUpDate featureSwitches features featuresEpoch refProviders alphaProgram betaProgram isAdmin ace labsProgram labsExperiments completedTutorials writefull aiErrorAssistant'
  331. ).exec()
  332. // Handle case of deleted user
  333. if (!user) {
  334. UserController.logout(req, res, next)
  335. return
  336. }
  337. logger.debug({ projectId, userId }, 'got user')
  338. return FeaturesUpdater.featuresEpochIsCurrent(user)
  339. ? user
  340. : await ProjectController._refreshFeatures(req, user)
  341. })(),
  342. learnedWords: SpellingHandler.promises.getUserDictionary(userId),
  343. projectTags: TagsHandler.promises.getTagsForProject(
  344. userId,
  345. projectId
  346. ),
  347. userHasInstitutionLicence: InstitutionsFeatures.promises
  348. .hasLicence(userId)
  349. .catch(err => {
  350. logger.error({ err, userId }, 'failed to get institution licence')
  351. return false
  352. }),
  353. affiliations: InstitutionsGetter.promises
  354. .getCurrentAffiliations(userId)
  355. .catch(err => {
  356. logger.error({ err, userId }, 'failed to get institution licence')
  357. return false
  358. }),
  359. subscription:
  360. SubscriptionLocator.promises.getUsersSubscription(userId),
  361. isTokenMember: CollaboratorsGetter.promises.userIsTokenMember(
  362. userId,
  363. projectId
  364. ),
  365. isInvitedMember:
  366. CollaboratorsGetter.promises.isUserInvitedMemberOfProject(
  367. userId,
  368. projectId
  369. ),
  370. odcRole: OnboardingDataCollectionManager.getOnboardingDataValue(
  371. userId,
  372. 'role'
  373. ).catch(err => {
  374. logger.error({ err, userId })
  375. return null
  376. }),
  377. })
  378. )
  379. const splitTestAssignments = {}
  380. try {
  381. const responses = await pProps({
  382. userValues: userId ? getUserValues(userId) : defaultUserValues(),
  383. splitTestAssignments: Promise.all(
  384. splitTests.map(async splitTest => {
  385. splitTestAssignments[splitTest] =
  386. await SplitTestHandler.promises.getAssignment(req, res, splitTest)
  387. })
  388. ),
  389. project: ProjectGetter.promises.getProject(projectId, {
  390. name: 1,
  391. lastUpdated: 1,
  392. track_changes: 1,
  393. owner_ref: 1,
  394. brandVariationId: 1,
  395. overleaf: 1,
  396. tokens: 1,
  397. tokenAccessReadAndWrite_refs: 1, // used for link sharing analytics
  398. collaberator_refs: 1, // used for link sharing analytics
  399. pendingEditor_refs: 1, // used for link sharing analytics
  400. reviewer_refs: 1,
  401. }),
  402. userIsMemberOfGroupSubscription: sessionUser
  403. ? (async () =>
  404. (
  405. await LimitationsManager.promises.userIsMemberOfGroupSubscription(
  406. sessionUser
  407. )
  408. ).isMember)()
  409. : false,
  410. _flushToTpds:
  411. TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(projectId),
  412. _activate:
  413. InactiveProjectManager.promises.reactivateProjectIfRequired(
  414. projectId
  415. ),
  416. })
  417. const { project, userValues, userIsMemberOfGroupSubscription } = responses
  418. const {
  419. user,
  420. learnedWords,
  421. projectTags,
  422. userHasInstitutionLicence,
  423. subscription,
  424. isTokenMember,
  425. isInvitedMember,
  426. odcRole,
  427. } = userValues
  428. const brandVariation = project?.brandVariationId
  429. ? await BrandVariationsHandler.promises.getBrandVariationById(
  430. project.brandVariationId
  431. )
  432. : undefined
  433. const anonRequestToken = TokenAccessHandler.getRequestToken(
  434. req,
  435. projectId
  436. )
  437. const allowedImageNames = ProjectHelper.getAllowedImagesForUser(user)
  438. const privilegeLevel =
  439. await AuthorizationManager.promises.getPrivilegeLevelForProject(
  440. userId,
  441. projectId,
  442. anonRequestToken
  443. )
  444. const reviewerRoleAssignment =
  445. await SplitTestHandler.promises.getAssignmentForUser(
  446. project.owner_ref,
  447. 'reviewer-role'
  448. )
  449. await Modules.promises.hooks.fire('enforceCollaboratorLimit', projectId)
  450. if (isTokenMember) {
  451. // Check explicitly that the user is in read write token refs, while this could be inferred
  452. // from the privilege level, the privilege level of token members might later be restricted
  453. const isReadWriteTokenMember =
  454. await CollaboratorsGetter.promises.userIsReadWriteTokenMember(
  455. userId,
  456. projectId
  457. )
  458. if (isReadWriteTokenMember) {
  459. // Check for an edge case where a user is both in read write token access refs but also
  460. // an invited read write member. Ensure they are not redirected to the sharing updates page
  461. // We could also delete the token access ref if the user is already a member of the project
  462. const isInvitedReadWriteMember =
  463. await CollaboratorsGetter.promises.isUserInvitedReadWriteMemberOfProject(
  464. userId,
  465. projectId
  466. )
  467. if (!isInvitedReadWriteMember) {
  468. return res.redirect(`/project/${projectId}/sharing-updates`)
  469. }
  470. }
  471. }
  472. if (privilegeLevel == null || privilegeLevel === PrivilegeLevels.NONE) {
  473. return res.sendStatus(401)
  474. }
  475. const allowedFreeTrial =
  476. subscription == null ||
  477. isStandaloneAiAddOnPlanCode(subscription.planCode)
  478. let wsUrl = Settings.wsUrl
  479. let metricName = 'load-editor-ws'
  480. if (user.betaProgram && Settings.wsUrlBeta !== undefined) {
  481. wsUrl = Settings.wsUrlBeta
  482. metricName += '-beta'
  483. } else if (
  484. Settings.wsUrlV2 &&
  485. Settings.wsUrlV2Percentage > 0 &&
  486. (new ObjectId(projectId).getTimestamp() / 1000) % 100 <
  487. Settings.wsUrlV2Percentage
  488. ) {
  489. wsUrl = Settings.wsUrlV2
  490. metricName += '-v2'
  491. }
  492. if (req.query && req.query.ws === 'fallback') {
  493. // `?ws=fallback` will connect to the bare origin, and ignore
  494. // the custom wsUrl. Hence it must load the client side
  495. // javascript from there too.
  496. // Not resetting it here would possibly load a socket.io v2
  497. // client and connect to a v0 endpoint.
  498. wsUrl = undefined
  499. metricName += '-fallback'
  500. }
  501. metrics.inc(metricName)
  502. // don't need to wait for these to complete
  503. ProjectUpdateHandler.promises
  504. .markAsOpened(projectId)
  505. .catch(err =>
  506. logger.error({ err, projectId }, 'failed to mark project as opened')
  507. )
  508. SplitTestSessionHandler.promises
  509. .sessionMaintenance(req, userId ? user : null)
  510. .catch(err =>
  511. logger.error({ err }, 'failed to update split test info in session')
  512. )
  513. const ownerFeatures = await UserGetter.promises.getUserFeatures(
  514. project.owner_ref
  515. )
  516. if (userId) {
  517. const planLimit = ownerFeatures?.collaborators || 0
  518. const namedEditors = project.collaberator_refs?.length || 0
  519. const pendingEditors = project.pendingEditor_refs?.length || 0
  520. const exceedAtLimit = planLimit > -1 && namedEditors >= planLimit
  521. let mode = 'edit'
  522. if (privilegeLevel === PrivilegeLevels.READ_ONLY) {
  523. mode = 'view'
  524. } else if (
  525. project.track_changes === true ||
  526. project.track_changes?.[userId] === true
  527. ) {
  528. mode = 'review'
  529. }
  530. const projectOpenedSegmentation = {
  531. role: privilegeLevel,
  532. mode,
  533. ownerId: project.owner_ref,
  534. projectId: project._id,
  535. namedEditors,
  536. pendingEditors,
  537. tokenEditors: project.tokenAccessReadAndWrite_refs?.length || 0,
  538. planLimit,
  539. exceedAtLimit,
  540. }
  541. AnalyticsManager.recordEventForUserInBackground(
  542. userId,
  543. 'project-opened',
  544. projectOpenedSegmentation
  545. )
  546. User.updateOne(
  547. { _id: new ObjectId(userId) },
  548. { $set: { lastActive: new Date() } }
  549. )
  550. .exec()
  551. .catch(err =>
  552. logger.error(
  553. { err, userId },
  554. 'failed to update lastActive for user'
  555. )
  556. )
  557. }
  558. const isAdminOrTemplateOwner =
  559. hasAdminAccess(user) || Settings.templates?.user_id === userId
  560. const showTemplatesServerPro =
  561. Features.hasFeature('templates-server-pro') && isAdminOrTemplateOwner
  562. const debugPdfDetach = shouldDisplayFeature('debug_pdf_detach')
  563. const detachRole = req.params.detachRole
  564. const showSymbolPalette =
  565. !Features.hasFeature('saas') ||
  566. (user.features && user.features.symbolPalette)
  567. const userInNonIndividualSub =
  568. userIsMemberOfGroupSubscription || userHasInstitutionLicence
  569. const userHasPremiumSub =
  570. subscription && !isStandaloneAiAddOnPlanCode(subscription.planCode)
  571. // Persistent upgrade prompts
  572. // in header & in share project modal
  573. const showUpgradePrompt =
  574. Features.hasFeature('saas') &&
  575. userId &&
  576. !userHasPremiumSub &&
  577. !userInNonIndividualSub
  578. let aiFeaturesAllowed = false
  579. if (userId && Features.hasFeature('saas')) {
  580. try {
  581. // exit early if the user couldnt use ai anyways, since permissions checks are expensive
  582. const canUserWriteOrReviewProjectContent =
  583. privilegeLevel === PrivilegeLevels.READ_AND_WRITE ||
  584. privilegeLevel === PrivilegeLevels.OWNER ||
  585. privilegeLevel === PrivilegeLevels.REVIEW
  586. if (canUserWriteOrReviewProjectContent) {
  587. // check permissions for user and project owner, to see if they allow AI on the project
  588. const permissionsResults = await Modules.promises.hooks.fire(
  589. 'projectAllowsCapability',
  590. project,
  591. userId,
  592. ['use-ai']
  593. )
  594. const aiAllowed = permissionsResults.every(
  595. result => result === true
  596. )
  597. aiFeaturesAllowed = aiAllowed
  598. }
  599. } catch (err) {
  600. // still allow users to access project if we cant get their permissions, but disable AI feature
  601. aiFeaturesAllowed = false
  602. }
  603. }
  604. const hasNonRecurlySubscription =
  605. subscription && !subscription.recurlySubscription_id
  606. const hasManuallyCollectedSubscription =
  607. subscription?.collectionMethod === 'manual'
  608. const canPurchaseAddons = !(
  609. hasNonRecurlySubscription || hasManuallyCollectedSubscription
  610. )
  611. const assistantDisabled = user.aiErrorAssistant?.enabled === false // the assistant has been manually disabled by the user
  612. const canUseErrorAssistant =
  613. (user.features?.aiErrorAssistant || canPurchaseAddons) &&
  614. !assistantDisabled
  615. let featureUsage = {}
  616. if (Features.hasFeature('saas')) {
  617. const usagesLeft = await Modules.promises.hooks.fire(
  618. 'remainingFeatureAllocation',
  619. userId
  620. )
  621. usagesLeft?.forEach(usage => {
  622. featureUsage = { ...featureUsage, ...usage }
  623. })
  624. }
  625. let inEnterpriseCommons = false
  626. const affiliations = userValues.affiliations || []
  627. for (const affiliation of affiliations) {
  628. inEnterpriseCommons =
  629. inEnterpriseCommons || affiliation.institution?.enterpriseCommons
  630. }
  631. // check if a user has never tried writefull before (writefull.enabled will be null)
  632. // if they previously accepted writefull, or are have been already assigned to a trial, user.writefull will be true,
  633. // if they explicitly disabled it, user.writefull will be false
  634. if (
  635. aiFeaturesAllowed &&
  636. user.writefull?.enabled === null &&
  637. !userIsMemberOfGroupSubscription &&
  638. !inEnterpriseCommons
  639. ) {
  640. const { variant } = await SplitTestHandler.promises.getAssignment(
  641. req,
  642. res,
  643. 'writefull-auto-account-creation'
  644. )
  645. if (variant === 'enabled') {
  646. await UserUpdater.promises.updateUser(userId, {
  647. $set: {
  648. writefull: { enabled: true, autoCreatedAccount: true },
  649. },
  650. })
  651. user.writefull.enabled = true
  652. user.writefull.autoCreatedAccount = true
  653. } else {
  654. const { variant } = await SplitTestHandler.promises.getAssignment(
  655. req,
  656. res,
  657. 'writefull-auto-load'
  658. )
  659. if (variant === 'enabled') {
  660. await UserUpdater.promises.updateUser(userId, {
  661. $set: {
  662. writefull: { enabled: true },
  663. },
  664. })
  665. user.writefull.enabled = true
  666. user.writefull.firstAutoLoad = true
  667. }
  668. }
  669. }
  670. const template =
  671. detachRole === 'detached'
  672. ? 'project/ide-react-detached'
  673. : 'project/ide-react'
  674. let chatEnabled
  675. if (Features.hasFeature('saas')) {
  676. chatEnabled =
  677. Features.hasFeature('chat') && req.capabilitySet.has('chat')
  678. } else {
  679. chatEnabled = Features.hasFeature('chat')
  680. }
  681. const isOverleafAssistBundleEnabled =
  682. splitTestAssignments['overleaf-assist-bundle']?.variant === 'enabled'
  683. let fullFeatureSet = user?.features
  684. if (!anonymous) {
  685. // generate users feature set including features added, or overriden via modules
  686. const moduleFeatures =
  687. (await Modules.promises.hooks.fire(
  688. 'getModuleProvidedFeatures',
  689. userId
  690. )) || []
  691. fullFeatureSet = FeaturesHelper.computeFeatureSet([
  692. user.features,
  693. ...moduleFeatures,
  694. ])
  695. }
  696. const isPaywallChangeCompileTimeoutEnabled =
  697. splitTestAssignments['paywall-change-compile-timeout']?.variant ===
  698. 'enabled'
  699. const paywallPlans =
  700. isPaywallChangeCompileTimeoutEnabled &&
  701. (await ProjectController._getPaywallPlansPrices(req, res))
  702. const customerIoEnabled =
  703. await SplitTestHandler.promises.hasUserBeenAssignedToVariant(
  704. req,
  705. userId,
  706. 'customer-io-trial-conversion',
  707. 'enabled',
  708. true
  709. )
  710. const addonPrices =
  711. isOverleafAssistBundleEnabled &&
  712. (await ProjectController._getAddonPrices(req, res))
  713. let planCode = subscription?.planCode
  714. if (!planCode && !userInNonIndividualSub) {
  715. planCode = 'personal'
  716. }
  717. const planDetails = Settings.plans.find(p => p.planCode === planCode)
  718. res.render(template, {
  719. title: project.name,
  720. priority_title: true,
  721. bodyClasses: ['editor'],
  722. project_id: project._id,
  723. projectName: project.name,
  724. projectOwnerHasPremiumOnPageLoad:
  725. ownerFeatures?.compileGroup === 'priority',
  726. user: {
  727. id: userId,
  728. email: user.email,
  729. first_name: user.first_name,
  730. last_name: user.last_name,
  731. referal_id: user.referal_id,
  732. signUpDate: user.signUpDate,
  733. allowedFreeTrial,
  734. hasRecurlySubscription: subscription?.recurlySubscription_id != null,
  735. featureSwitches: user.featureSwitches,
  736. features: fullFeatureSet,
  737. featureUsage,
  738. refProviders: _.mapValues(user.refProviders, Boolean),
  739. writefull: {
  740. enabled: Boolean(user.writefull?.enabled && aiFeaturesAllowed),
  741. autoCreatedAccount: Boolean(user.writefull?.autoCreatedAccount),
  742. firstAutoLoad: Boolean(user.writefull?.firstAutoLoad),
  743. },
  744. alphaProgram: user.alphaProgram,
  745. betaProgram: user.betaProgram,
  746. labsProgram: user.labsProgram,
  747. inactiveTutorials: TutorialHandler.getInactiveTutorials(user),
  748. isAdmin: hasAdminAccess(user),
  749. planCode,
  750. planName: planDetails?.name,
  751. isAnnualPlan: planCode && planDetails?.annual,
  752. isMemberOfGroupSubscription: userIsMemberOfGroupSubscription,
  753. hasInstitutionLicence: userHasInstitutionLicence,
  754. },
  755. userSettings: {
  756. mode: user.ace.mode,
  757. editorTheme: user.ace.theme,
  758. fontSize: user.ace.fontSize,
  759. autoComplete: user.ace.autoComplete,
  760. autoPairDelimiters: user.ace.autoPairDelimiters,
  761. pdfViewer: user.ace.pdfViewer,
  762. syntaxValidation: user.ace.syntaxValidation,
  763. fontFamily: user.ace.fontFamily || 'lucida',
  764. lineHeight: user.ace.lineHeight || 'normal',
  765. overallTheme: user.ace.overallTheme,
  766. mathPreview: user.ace.mathPreview,
  767. referencesSearchMode: user.ace.referencesSearchMode,
  768. enableNewEditor: user.ace.enableNewEditor ?? true,
  769. },
  770. labsExperiments: user.labsExperiments ?? [],
  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. odcRole:
  812. // only use the ODC role value if the split test is enabled
  813. splitTestAssignments['paywall-change-compile-timeout']?.variant ===
  814. 'enabled'
  815. ? odcRole
  816. : null,
  817. isSaas: Features.hasFeature('saas'),
  818. shouldLoadHotjar: splitTestAssignments.hotjar?.variant === 'enabled',
  819. isReviewerRoleEnabled:
  820. reviewerRoleAssignment?.variant === 'enabled' ||
  821. Object.keys(project.reviewer_refs || {}).length > 0,
  822. isPaywallChangeCompileTimeoutEnabled,
  823. isOverleafAssistBundleEnabled,
  824. paywallPlans,
  825. customerIoEnabled,
  826. addonPrices,
  827. })
  828. timer.done()
  829. } catch (err) {
  830. OError.tag(err, 'error getting details for project page')
  831. return next(err)
  832. }
  833. },
  834. async _getPaywallPlansPrices(
  835. req,
  836. res,
  837. paywallPlans = ['collaborator', 'student']
  838. ) {
  839. const plansData = {}
  840. const locale = req.i18n.language
  841. const { currency } = await SubscriptionController.getRecommendedCurrency(
  842. req,
  843. res
  844. )
  845. paywallPlans.forEach(plan => {
  846. const planPrice = Settings.localizedPlanPricing[currency][plan].monthly
  847. const formattedPlanPrice = formatCurrency(
  848. planPrice,
  849. currency,
  850. locale,
  851. true
  852. )
  853. plansData[plan] = formattedPlanPrice
  854. })
  855. return plansData
  856. },
  857. async _getAddonPrices(req, res, addonPlans = ['assistBundle']) {
  858. const plansData = {}
  859. const locale = req.i18n.language
  860. const { currency } = await SubscriptionController.getRecommendedCurrency(
  861. req,
  862. res
  863. )
  864. addonPlans.forEach(plan => {
  865. const annualPrice = Settings.localizedAddOnsPricing[currency][plan].annual
  866. const monthlyPrice =
  867. Settings.localizedAddOnsPricing[currency][plan].monthly
  868. plansData[plan] = {
  869. annual: formatCurrency(annualPrice, currency, locale, true),
  870. monthly: formatCurrency(monthlyPrice, currency, locale, true),
  871. }
  872. })
  873. return plansData
  874. },
  875. async _refreshFeatures(req, user) {
  876. // If the feature refresh has failed in this session, don't retry
  877. // it - require the user to log in again.
  878. if (req.session.feature_refresh_failed) {
  879. metrics.inc('features-refresh', 1, {
  880. path: 'load-editor',
  881. status: 'skipped',
  882. })
  883. return user
  884. }
  885. // If the refresh takes too long then return the current
  886. // features. Note that the user.features property may still be
  887. // updated in the background after the promise is resolved.
  888. const abortController = new AbortController()
  889. const refreshTimeoutHandler = async () => {
  890. await setTimeout(5000, { signal: abortController.signal })
  891. req.session.feature_refresh_failed = {
  892. reason: 'timeout',
  893. at: new Date(),
  894. }
  895. metrics.inc('features-refresh', 1, {
  896. path: 'load-editor',
  897. status: 'timeout',
  898. })
  899. return user
  900. }
  901. // try to refresh user features now
  902. const timer = new metrics.Timer('features-refresh-on-load-editor')
  903. return Promise.race([
  904. refreshTimeoutHandler(),
  905. (async () => {
  906. try {
  907. user.features = await FeaturesUpdater.promises.refreshFeatures(
  908. user._id,
  909. 'load-editor'
  910. )
  911. metrics.inc('features-refresh', 1, {
  912. path: 'load-editor',
  913. status: 'success',
  914. })
  915. } catch (err) {
  916. // keep a record to prevent unneceary retries and leave
  917. // the original features unmodified if the refresh failed
  918. req.session.feature_refresh_failed = {
  919. reason: 'error',
  920. at: new Date(),
  921. }
  922. metrics.inc('features-refresh', 1, {
  923. path: 'load-editor',
  924. status: 'error',
  925. })
  926. }
  927. abortController.abort()
  928. timer.done()
  929. return user
  930. })(),
  931. ])
  932. },
  933. _buildProjectList(allProjects, userId) {
  934. let project
  935. const {
  936. owned,
  937. review,
  938. readAndWrite,
  939. readOnly,
  940. tokenReadAndWrite,
  941. tokenReadOnly,
  942. } = allProjects
  943. const projects = []
  944. for (project of owned) {
  945. projects.push(
  946. ProjectController._buildProjectViewModel(
  947. project,
  948. 'owner',
  949. Sources.OWNER,
  950. userId
  951. )
  952. )
  953. }
  954. // Invite-access
  955. for (project of readAndWrite) {
  956. projects.push(
  957. ProjectController._buildProjectViewModel(
  958. project,
  959. 'readWrite',
  960. Sources.INVITE,
  961. userId
  962. )
  963. )
  964. }
  965. for (project of review) {
  966. projects.push(
  967. ProjectController._buildProjectViewModel(
  968. project,
  969. 'review',
  970. Sources.INVITE,
  971. userId
  972. )
  973. )
  974. }
  975. for (project of readOnly) {
  976. projects.push(
  977. ProjectController._buildProjectViewModel(
  978. project,
  979. 'readOnly',
  980. Sources.INVITE,
  981. userId
  982. )
  983. )
  984. }
  985. // Token-access
  986. // Only add these projects if they're not already present, this gives us cascading access
  987. // from 'owner' => 'token-read-only'
  988. for (project of tokenReadAndWrite) {
  989. if (
  990. projects.filter(p => p.id.toString() === project._id.toString())
  991. .length === 0
  992. ) {
  993. projects.push(
  994. ProjectController._buildProjectViewModel(
  995. project,
  996. 'readAndWrite',
  997. Sources.TOKEN,
  998. userId
  999. )
  1000. )
  1001. }
  1002. }
  1003. for (project of tokenReadOnly) {
  1004. if (
  1005. projects.filter(p => p.id.toString() === project._id.toString())
  1006. .length === 0
  1007. ) {
  1008. projects.push(
  1009. ProjectController._buildProjectViewModel(
  1010. project,
  1011. 'readOnly',
  1012. Sources.TOKEN,
  1013. userId
  1014. )
  1015. )
  1016. }
  1017. }
  1018. return projects
  1019. },
  1020. _buildProjectViewModel(project, accessLevel, source, userId) {
  1021. const archived = ProjectHelper.isArchived(project, userId)
  1022. // If a project is simultaneously trashed and archived, we will consider it archived but not trashed.
  1023. const trashed = ProjectHelper.isTrashed(project, userId) && !archived
  1024. const model = {
  1025. id: project._id,
  1026. name: project.name,
  1027. lastUpdated: project.lastUpdated,
  1028. lastUpdatedBy: project.lastUpdatedBy,
  1029. publicAccessLevel: project.publicAccesLevel,
  1030. accessLevel,
  1031. source,
  1032. archived,
  1033. trashed,
  1034. owner_ref: project.owner_ref,
  1035. isV1Project: false,
  1036. }
  1037. if (accessLevel === PrivilegeLevels.READ_ONLY && source === Sources.TOKEN) {
  1038. model.owner_ref = null
  1039. model.lastUpdatedBy = null
  1040. }
  1041. return model
  1042. },
  1043. _buildPortalTemplatesList(affiliations) {
  1044. if (affiliations == null) {
  1045. affiliations = []
  1046. }
  1047. const portalTemplates = []
  1048. for (const aff of affiliations) {
  1049. if (
  1050. aff.portal &&
  1051. aff.portal.slug &&
  1052. aff.portal.templates_count &&
  1053. aff.portal.templates_count > 0
  1054. ) {
  1055. const portalPath = aff.institution.isUniversity ? '/edu/' : '/org/'
  1056. portalTemplates.push({
  1057. name: aff.institution.name,
  1058. url: Settings.siteUrl + portalPath + aff.portal.slug,
  1059. })
  1060. }
  1061. }
  1062. return portalTemplates
  1063. },
  1064. }
  1065. const defaultSettingsForAnonymousUser = userId => ({
  1066. id: userId,
  1067. ace: {
  1068. mode: 'none',
  1069. theme: 'textmate',
  1070. fontSize: '12',
  1071. autoComplete: true,
  1072. spellCheckLanguage: '',
  1073. pdfViewer: '',
  1074. syntaxValidation: true,
  1075. },
  1076. subscription: {
  1077. freeTrial: {
  1078. allowed: true,
  1079. },
  1080. },
  1081. featureSwitches: {
  1082. github: false,
  1083. },
  1084. alphaProgram: false,
  1085. betaProgram: false,
  1086. writefull: {
  1087. enabled: false,
  1088. },
  1089. })
  1090. const defaultUserValues = () => ({
  1091. user: defaultSettingsForAnonymousUser(null),
  1092. learnedWords: [],
  1093. projectTags: [],
  1094. userHasInstitutionLicence: false,
  1095. subscription: undefined,
  1096. isTokenMember: false,
  1097. isInvitedMember: false,
  1098. })
  1099. const THEME_LIST = [
  1100. 'cobalt',
  1101. 'dracula',
  1102. 'eclipse',
  1103. 'monokai',
  1104. 'overleaf',
  1105. 'textmate',
  1106. ]
  1107. const LEGACY_THEME_LIST = [
  1108. 'ambiance',
  1109. 'chaos',
  1110. 'chrome',
  1111. 'clouds',
  1112. 'clouds_midnight',
  1113. 'crimson_editor',
  1114. 'dawn',
  1115. 'dreamweaver',
  1116. 'github',
  1117. 'gob',
  1118. 'gruvbox',
  1119. 'idle_fingers',
  1120. 'iplastic',
  1121. 'katzenmilch',
  1122. 'kr_theme',
  1123. 'kuroir',
  1124. 'merbivore',
  1125. 'merbivore_soft',
  1126. 'mono_industrial',
  1127. 'nord_dark',
  1128. 'pastel_on_dark',
  1129. 'solarized_dark',
  1130. 'solarized_light',
  1131. 'sqlserver',
  1132. 'terminal',
  1133. 'tomorrow',
  1134. 'tomorrow_night',
  1135. 'tomorrow_night_blue',
  1136. 'tomorrow_night_bright',
  1137. 'tomorrow_night_eighties',
  1138. 'twilight',
  1139. 'vibrant_ink',
  1140. 'xcode',
  1141. ]
  1142. const ProjectController = {
  1143. archiveProject: expressify(_ProjectController.archiveProject),
  1144. cloneProject: expressify(_ProjectController.cloneProject),
  1145. deleteProject: expressify(_ProjectController.deleteProject),
  1146. expireDeletedProject: expressify(_ProjectController.expireDeletedProject),
  1147. expireDeletedProjectsAfterDuration: expressify(
  1148. _ProjectController.expireDeletedProjectsAfterDuration
  1149. ),
  1150. loadEditor: expressify(_ProjectController.loadEditor),
  1151. newProject: expressify(_ProjectController.newProject),
  1152. projectEntitiesJson: expressify(_ProjectController.projectEntitiesJson),
  1153. renameProject: expressify(_ProjectController.renameProject),
  1154. restoreProject: expressify(_ProjectController.restoreProject),
  1155. trashProject: expressify(_ProjectController.trashProject),
  1156. unarchiveProject: expressify(_ProjectController.unarchiveProject),
  1157. untrashProject: expressify(_ProjectController.untrashProject),
  1158. updateProjectAdminSettings: expressify(
  1159. _ProjectController.updateProjectAdminSettings
  1160. ),
  1161. updateProjectSettings: expressify(_ProjectController.updateProjectSettings),
  1162. userProjectsJson: expressify(_ProjectController.userProjectsJson),
  1163. _buildProjectList: _ProjectController._buildProjectList,
  1164. _buildProjectViewModel: _ProjectController._buildProjectViewModel,
  1165. _injectProjectUsers: _ProjectController._injectProjectUsers,
  1166. _isInPercentageRollout: _ProjectController._isInPercentageRollout,
  1167. _refreshFeatures: _ProjectController._refreshFeatures,
  1168. _getPaywallPlansPrices: _ProjectController._getPaywallPlansPrices,
  1169. _getAddonPrices: _ProjectController._getAddonPrices,
  1170. }
  1171. module.exports = ProjectController