ProjectController.js 40 KB

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