| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055 |
- const _ = require('lodash')
- const OError = require('@overleaf/o-error')
- const crypto = require('crypto')
- const { setTimeout } = require('timers/promises')
- const pProps = require('p-props')
- const logger = require('@overleaf/logger')
- const { expressify } = require('@overleaf/promise-utils')
- const { ObjectId } = require('mongodb-legacy')
- const ProjectDeleter = require('./ProjectDeleter')
- const ProjectDuplicator = require('./ProjectDuplicator')
- const ProjectCreationHandler = require('./ProjectCreationHandler')
- const EditorController = require('../Editor/EditorController')
- const ProjectHelper = require('./ProjectHelper')
- const metrics = require('@overleaf/metrics')
- const { User } = require('../../models/User')
- const SubscriptionLocator = require('../Subscription/SubscriptionLocator')
- const LimitationsManager = require('../Subscription/LimitationsManager')
- const Settings = require('@overleaf/settings')
- const AuthorizationManager = require('../Authorization/AuthorizationManager')
- const InactiveProjectManager = require('../InactiveData/InactiveProjectManager')
- const ProjectUpdateHandler = require('./ProjectUpdateHandler')
- const ProjectGetter = require('./ProjectGetter')
- const PrivilegeLevels = require('../Authorization/PrivilegeLevels')
- const SessionManager = require('../Authentication/SessionManager')
- const Sources = require('../Authorization/Sources')
- const TokenAccessHandler = require('../TokenAccess/TokenAccessHandler')
- const CollaboratorsGetter = require('../Collaborators/CollaboratorsGetter')
- const ProjectEntityHandler = require('./ProjectEntityHandler')
- const TpdsProjectFlusher = require('../ThirdPartyDataStore/TpdsProjectFlusher')
- const Features = require('../../infrastructure/Features')
- const BrandVariationsHandler = require('../BrandVariations/BrandVariationsHandler')
- const UserController = require('../User/UserController')
- const AnalyticsManager = require('../Analytics/AnalyticsManager')
- const SplitTestHandler = require('../SplitTests/SplitTestHandler')
- const SplitTestSessionHandler = require('../SplitTests/SplitTestSessionHandler')
- const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
- const SpellingHandler = require('../Spelling/SpellingHandler')
- const { hasAdminAccess } = require('../Helpers/AdminAuthorizationHelper')
- const InstitutionsFeatures = require('../Institutions/InstitutionsFeatures')
- const ProjectAuditLogHandler = require('./ProjectAuditLogHandler')
- const PublicAccessLevels = require('../Authorization/PublicAccessLevels')
- const TagsHandler = require('../Tags/TagsHandler')
- const TutorialHandler = require('../Tutorial/TutorialHandler')
- const OnboardingDataCollectionManager = require('../OnboardingDataCollection/OnboardingDataCollectionManager')
- const UserUpdater = require('../User/UserUpdater')
- const Modules = require('../../infrastructure/Modules')
- const UserGetter = require('../User/UserGetter')
- /**
- * @import { GetProjectsRequest, GetProjectsResponse, Project } from "./types"
- */
- const _ProjectController = {
- _isInPercentageRollout(rolloutName, objectId, percentage) {
- if (Settings.bypassPercentageRollouts === true) {
- return true
- }
- const data = `${rolloutName}:${objectId.toString()}`
- const md5hash = crypto.createHash('md5').update(data).digest('hex')
- const counter = parseInt(md5hash.slice(26, 32), 16)
- return counter % 100 < percentage
- },
- async updateProjectSettings(req, res) {
- const projectId = req.params.Project_id
- if (req.body.compiler != null) {
- await EditorController.promises.setCompiler(projectId, req.body.compiler)
- }
- if (req.body.imageName != null) {
- await EditorController.promises.setImageName(
- projectId,
- req.body.imageName
- )
- }
- if (req.body.name != null) {
- await EditorController.promises.renameProject(projectId, req.body.name)
- }
- if (req.body.spellCheckLanguage != null) {
- await EditorController.promises.setSpellCheckLanguage(
- projectId,
- req.body.spellCheckLanguage
- )
- }
- if (req.body.rootDocId != null) {
- await EditorController.promises.setRootDoc(projectId, req.body.rootDocId)
- }
- res.sendStatus(204)
- },
- async updateProjectAdminSettings(req, res) {
- const projectId = req.params.Project_id
- const user = SessionManager.getSessionUser(req.session)
- const publicAccessLevel = req.body.publicAccessLevel
- const publicAccessLevels = [
- PublicAccessLevels.READ_ONLY,
- PublicAccessLevels.READ_AND_WRITE,
- PublicAccessLevels.PRIVATE,
- PublicAccessLevels.TOKEN_BASED,
- ]
- if (
- req.body.publicAccessLevel != null &&
- publicAccessLevels.includes(publicAccessLevel)
- ) {
- await EditorController.promises.setPublicAccessLevel(
- projectId,
- req.body.publicAccessLevel
- )
- await ProjectAuditLogHandler.promises.addEntry(
- projectId,
- 'toggle-access-level',
- user._id,
- req.ip,
- { publicAccessLevel: req.body.publicAccessLevel, status: 'OK' }
- )
- res.sendStatus(204)
- } else {
- res.sendStatus(500)
- }
- },
- async deleteProject(req, res) {
- const projectId = req.params.Project_id
- const user = SessionManager.getSessionUser(req.session)
- await ProjectDeleter.promises.deleteProject(projectId, {
- deleterUser: user,
- ipAddress: req.ip,
- })
- res.sendStatus(200)
- },
- async archiveProject(req, res) {
- const projectId = req.params.Project_id
- const userId = SessionManager.getLoggedInUserId(req.session)
- await ProjectDeleter.promises.archiveProject(projectId, userId)
- res.sendStatus(200)
- },
- async unarchiveProject(req, res) {
- const projectId = req.params.Project_id
- const userId = SessionManager.getLoggedInUserId(req.session)
- await ProjectDeleter.promises.unarchiveProject(projectId, userId)
- res.sendStatus(200)
- },
- async trashProject(req, res) {
- const projectId = req.params.project_id
- const userId = SessionManager.getLoggedInUserId(req.session)
- await ProjectDeleter.promises.trashProject(projectId, userId)
- res.sendStatus(200)
- },
- async untrashProject(req, res) {
- const projectId = req.params.project_id
- const userId = SessionManager.getLoggedInUserId(req.session)
- await ProjectDeleter.promises.untrashProject(projectId, userId)
- res.sendStatus(200)
- },
- async expireDeletedProjectsAfterDuration(_req, res) {
- await ProjectDeleter.promises.expireDeletedProjectsAfterDuration()
- res.sendStatus(200)
- },
- async expireDeletedProject(req, res) {
- const { projectId } = req.params
- await ProjectDeleter.promises.expireDeletedProject(projectId)
- res.sendStatus(200)
- },
- async restoreProject(req, res) {
- const projectId = req.params.Project_id
- await ProjectDeleter.promises.restoreProject(projectId)
- res.sendStatus(200)
- },
- async cloneProject(req, res, next) {
- res.setTimeout(5 * 60 * 1000) // allow extra time for the copy to complete
- metrics.inc('cloned-project')
- const projectId = req.params.Project_id
- const { projectName, tags } = req.body
- logger.debug({ projectId, projectName }, 'cloning project')
- if (!SessionManager.isUserLoggedIn(req.session)) {
- return res.json({ redir: '/register' })
- }
- const currentUser = SessionManager.getSessionUser(req.session)
- const { first_name: firstName, last_name: lastName, email } = currentUser
- try {
- const project = await ProjectDuplicator.promises.duplicate(
- currentUser,
- projectId,
- projectName,
- tags
- )
- res.json({
- name: project.name,
- lastUpdated: project.lastUpdated,
- project_id: project._id,
- owner_ref: project.owner_ref,
- owner: {
- first_name: firstName,
- last_name: lastName,
- email,
- _id: currentUser._id,
- },
- })
- } catch (err) {
- OError.tag(err, 'error cloning project', {
- projectId,
- userId: currentUser._id,
- })
- return next(err)
- }
- },
- async newProject(req, res) {
- const currentUser = SessionManager.getSessionUser(req.session)
- const {
- first_name: firstName,
- last_name: lastName,
- email,
- _id: userId,
- } = currentUser
- const projectName =
- req.body.projectName != null ? req.body.projectName.trim() : undefined
- const { template } = req.body
- const project = await (template === 'example'
- ? ProjectCreationHandler.promises.createExampleProject(
- userId,
- projectName
- )
- : ProjectCreationHandler.promises.createBasicProject(userId, projectName))
- res.json({
- project_id: project._id,
- owner_ref: project.owner_ref,
- owner: {
- first_name: firstName,
- last_name: lastName,
- email,
- _id: userId,
- },
- })
- },
- async renameProject(req, res) {
- const projectId = req.params.Project_id
- const newName = req.body.newProjectName
- await EditorController.promises.renameProject(projectId, newName)
- res.sendStatus(200)
- },
- async userProjectsJson(req, res) {
- const userId = SessionManager.getLoggedInUserId(req.session)
- let projects = await ProjectGetter.promises.findAllUsersProjects(
- userId,
- 'name lastUpdated publicAccesLevel archived trashed owner_ref'
- )
- // _buildProjectList already converts archived/trashed to booleans so isArchivedOrTrashed should not be used here
- projects = ProjectController._buildProjectList(projects, userId)
- .filter(p => !(p.archived || p.trashed))
- .map(p => ({ _id: p.id, name: p.name, accessLevel: p.accessLevel }))
- res.json({ projects })
- },
- async projectEntitiesJson(req, res) {
- const projectId = req.params.Project_id
- const project = await ProjectGetter.promises.getProject(projectId)
- const { docs, files } =
- ProjectEntityHandler.getAllEntitiesFromProject(project)
- const entities = docs
- .concat(files)
- // Sort by path ascending
- .sort((a, b) => (a.path > b.path ? 1 : a.path < b.path ? -1 : 0))
- .map(e => ({
- path: e.path,
- type: e.doc != null ? 'doc' : 'file',
- }))
- res.json({ project_id: projectId, entities })
- },
- async loadEditor(req, res, next) {
- const timer = new metrics.Timer('load-editor')
- if (!Settings.editorIsOpen) {
- return res.render('general/closed', { title: 'updating_site' })
- }
- let anonymous, userId, sessionUser
- if (SessionManager.isUserLoggedIn(req.session)) {
- sessionUser = SessionManager.getSessionUser(req.session)
- userId = SessionManager.getLoggedInUserId(req.session)
- anonymous = false
- } else {
- sessionUser = null
- anonymous = true
- userId = null
- }
- const projectId = req.params.Project_id
- // should not be used in place of split tests query param overrides (?my-split-test-name=my-variant)
- function shouldDisplayFeature(name, variantFlag) {
- if (req.query && req.query[name]) {
- return req.query[name] === 'true'
- } else {
- return variantFlag === true
- }
- }
- const splitTests = [
- !anonymous && 'bib-file-tpr-prompt',
- 'compile-log-events',
- 'math-preview',
- 'null-test-share-modal',
- 'paywall-cta',
- 'pdf-caching-cached-url-lookup',
- 'pdf-caching-mode',
- 'pdf-caching-prefetch-large',
- 'pdf-caching-prefetching',
- 'pdf-presentation-mode',
- 'pdfjs-40',
- 'revert-file',
- 'revert-project',
- 'review-panel-redesign',
- !anonymous && 'ro-mirror-on-client',
- 'track-pdf-download',
- !anonymous && 'writefull-oauth-promotion',
- 'ieee-stylesheet',
- 'write-and-cite',
- 'default-visual-for-beginners',
- 'password-authentication-removal',
- ].filter(Boolean)
- const getUserValues = async userId =>
- pProps(
- _.mapValues({
- user: (async () => {
- const user = await User.findById(
- userId,
- 'email first_name last_name referal_id signUpDate featureSwitches features featuresEpoch refProviders alphaProgram betaProgram isAdmin ace labsProgram completedTutorials writefull'
- ).exec()
- // Handle case of deleted user
- if (!user) {
- UserController.logout(req, res, next)
- return
- }
- logger.debug({ projectId, userId }, 'got user')
- return FeaturesUpdater.featuresEpochIsCurrent(user)
- ? user
- : await ProjectController._refreshFeatures(req, user)
- })(),
- learnedWords: SpellingHandler.promises.getUserDictionary(userId),
- projectTags: TagsHandler.promises.getTagsForProject(
- userId,
- projectId
- ),
- userHasInstitutionLicence: InstitutionsFeatures.promises
- .hasLicence(userId)
- .catch(err => {
- logger.error({ err, userId }, 'failed to get institution licence')
- return false
- }),
- subscription:
- SubscriptionLocator.promises.getUsersSubscription(userId),
- isTokenMember: CollaboratorsGetter.promises.userIsTokenMember(
- userId,
- projectId
- ),
- isInvitedMember:
- CollaboratorsGetter.promises.isUserInvitedMemberOfProject(
- userId,
- projectId
- ),
- usedLatex: OnboardingDataCollectionManager.getOnboardingDataValue(
- userId,
- 'usedLatex'
- ).catch(err => {
- logger.error({ err, userId })
- return null
- }),
- })
- )
- const splitTestAssignments = {}
- try {
- const responses = await pProps({
- userValues: userId ? getUserValues(userId) : defaultUserValues(),
- splitTestAssignments: Promise.all(
- splitTests.map(async splitTest => {
- splitTestAssignments[splitTest] =
- await SplitTestHandler.promises.getAssignment(req, res, splitTest)
- })
- ),
- project: ProjectGetter.promises.getProject(projectId, {
- name: 1,
- lastUpdated: 1,
- track_changes: 1,
- owner_ref: 1,
- brandVariationId: 1,
- overleaf: 1,
- tokens: 1,
- tokenAccessReadAndWrite_refs: 1, // used for link sharing analytics
- collaberator_refs: 1, // used for link sharing analytics
- pendingEditor_refs: 1, // used for link sharing analytics
- }),
- userIsMemberOfGroupSubscription: sessionUser
- ? (async () =>
- (
- await LimitationsManager.promises.userIsMemberOfGroupSubscription(
- sessionUser
- )
- ).isMember)()
- : false,
- _flushToTpds:
- TpdsProjectFlusher.promises.flushProjectToTpdsIfNeeded(projectId),
- _activate:
- InactiveProjectManager.promises.reactivateProjectIfRequired(
- projectId
- ),
- })
- const { project, userValues, userIsMemberOfGroupSubscription } = responses
- const {
- user,
- learnedWords,
- projectTags,
- userHasInstitutionLicence,
- subscription,
- isTokenMember,
- isInvitedMember,
- usedLatex,
- } = userValues
- const brandVariation = project?.brandVariationId
- ? await BrandVariationsHandler.promises.getBrandVariationById(
- project.brandVariationId
- )
- : undefined
- const anonRequestToken = TokenAccessHandler.getRequestToken(
- req,
- projectId
- )
- const allowedImageNames = ProjectHelper.getAllowedImagesForUser(user)
- const privilegeLevel =
- await AuthorizationManager.promises.getPrivilegeLevelForProject(
- userId,
- projectId,
- anonRequestToken
- )
- const [linkSharingChanges, linkSharingEnforcement] = await Promise.all([
- SplitTestHandler.promises.getAssignmentForUser(
- project.owner_ref,
- 'link-sharing-warning'
- ),
- SplitTestHandler.promises.getAssignmentForUser(
- project.owner_ref,
- 'link-sharing-enforcement'
- ),
- ])
- if (linkSharingChanges?.variant === 'active') {
- if (linkSharingEnforcement?.variant === 'active') {
- await Modules.promises.hooks.fire(
- 'enforceCollaboratorLimit',
- projectId
- )
- }
- if (isTokenMember) {
- // Check explicitly that the user is in read write token refs, while this could be inferred
- // from the privilege level, the privilege level of token members might later be restricted
- const isReadWriteTokenMember =
- await CollaboratorsGetter.promises.userIsReadWriteTokenMember(
- userId,
- projectId
- )
- if (isReadWriteTokenMember) {
- // Check for an edge case where a user is both in read write token access refs but also
- // an invited read write member. Ensure they are not redirected to the sharing updates page
- // We could also delete the token access ref if the user is already a member of the project
- const isInvitedReadWriteMember =
- await CollaboratorsGetter.promises.isUserInvitedReadWriteMemberOfProject(
- userId,
- projectId
- )
- if (!isInvitedReadWriteMember) {
- return res.redirect(`/project/${projectId}/sharing-updates`)
- }
- }
- }
- }
- let allowedFreeTrial = true
- if (privilegeLevel == null || privilegeLevel === PrivilegeLevels.NONE) {
- return res.sendStatus(401)
- }
- if (subscription != null) {
- allowedFreeTrial = false
- }
- let wsUrl = Settings.wsUrl
- let metricName = 'load-editor-ws'
- if (user.betaProgram && Settings.wsUrlBeta !== undefined) {
- wsUrl = Settings.wsUrlBeta
- metricName += '-beta'
- } else if (
- Settings.wsUrlV2 &&
- Settings.wsUrlV2Percentage > 0 &&
- (new ObjectId(projectId).getTimestamp() / 1000) % 100 <
- Settings.wsUrlV2Percentage
- ) {
- wsUrl = Settings.wsUrlV2
- metricName += '-v2'
- }
- if (req.query && req.query.ws === 'fallback') {
- // `?ws=fallback` will connect to the bare origin, and ignore
- // the custom wsUrl. Hence it must load the client side
- // javascript from there too.
- // Not resetting it here would possibly load a socket.io v2
- // client and connect to a v0 endpoint.
- wsUrl = undefined
- metricName += '-fallback'
- }
- metrics.inc(metricName)
- // don't need to wait for these to complete
- ProjectUpdateHandler.promises
- .markAsOpened(projectId)
- .catch(err =>
- logger.error({ err, projectId }, 'failed to mark project as opened')
- )
- SplitTestSessionHandler.promises
- .sessionMaintenance(req, userId ? user : null)
- .catch(err =>
- logger.error({ err }, 'failed to update split test info in session')
- )
- if (userId) {
- const ownerFeatures = await UserGetter.promises.getUserFeatures(
- project.owner_ref
- )
- const planLimit = ownerFeatures?.collaborators || 0
- const namedEditors = project.collaberator_refs?.length || 0
- const pendingEditors = project.pendingEditor_refs?.length || 0
- const exceedAtLimit = planLimit > -1 && namedEditors >= planLimit
- const projectOpenedSegmentation = {
- projectId: project._id,
- // temporary link sharing segmentation:
- linkSharingWarning: linkSharingChanges?.variant,
- linkSharingEnforcement: linkSharingEnforcement?.variant,
- namedEditors,
- pendingEditors,
- tokenEditors: project.tokenAccessReadAndWrite_refs?.length || 0,
- planLimit,
- exceedAtLimit,
- }
- AnalyticsManager.recordEventForUserInBackground(
- userId,
- 'project-opened',
- projectOpenedSegmentation
- )
- User.updateOne(
- { _id: new ObjectId(userId) },
- { $set: { lastActive: new Date() } }
- )
- .exec()
- .catch(err =>
- logger.error(
- { err, userId },
- 'failed to update lastActive for user'
- )
- )
- }
- const isAdminOrTemplateOwner =
- hasAdminAccess(user) || Settings.templates?.user_id === userId
- const showTemplatesServerPro =
- Features.hasFeature('templates-server-pro') && isAdminOrTemplateOwner
- const debugPdfDetach = shouldDisplayFeature('debug_pdf_detach')
- const detachRole = req.params.detachRole
- const showSymbolPalette =
- !Features.hasFeature('saas') ||
- (user.features && user.features.symbolPalette)
- // Persistent upgrade prompts
- // in header & in share project modal
- const showUpgradePrompt =
- Features.hasFeature('saas') &&
- userId &&
- !subscription &&
- !userIsMemberOfGroupSubscription &&
- !userHasInstitutionLicence
- let aiFeaturesAllowed = false
- if (userId && Features.hasFeature('saas')) {
- try {
- // exit early if the user couldnt use ai anyways, since permissions checks are expensive
- const canEditProject =
- privilegeLevel === PrivilegeLevels.READ_AND_WRITE ||
- privilegeLevel === PrivilegeLevels.OWNER
- if (canEditProject) {
- // check permissions for user and project owner, to see if they allow AI on the project
- const permissionsResults = await Modules.promises.hooks.fire(
- 'projectAllowsCapability',
- project,
- userId,
- ['use-ai']
- )
- const aiAllowed = permissionsResults.every(
- result => result === true
- )
- aiFeaturesAllowed = aiAllowed
- }
- } catch (err) {
- // still allow users to access project if we cant get their permissions, but disable AI feature
- aiFeaturesAllowed = false
- }
- }
- // check if a user has never tried writefull before (writefull.enabled will be null)
- // if they previously accepted writefull. user.writefull will be true,
- // if they explicitly disabled it, user.writefull will be false
- if (aiFeaturesAllowed && user.writefull?.enabled === null) {
- // since we are auto-enrolling users into writefull if they are part of the group, we only want to
- // auto enroll (set writefull to true) if its the first time they have entered the test
- // this ensures that they can still turn writefull off (otherwise, we would be setting writefull on every time they access their projects)
- const { variant, metadata } =
- await SplitTestHandler.promises.getAssignment(
- req,
- res,
- 'writefull-auto-load'
- )
- if (variant === 'enabled' && metadata?.isFirstNonDefaultAssignment) {
- await UserUpdater.promises.updateUser(userId, {
- $set: {
- writefull: { enabled: true },
- },
- })
- user.writefull.enabled = true
- }
- }
- const template =
- detachRole === 'detached'
- ? 'project/ide-react-detached'
- : 'project/ide-react'
- // Get the user's assignment for this page's Bootstrap 5 split test, which
- // populates splitTestVariants with a value for the split test name and allows
- // Pug to read it
- await SplitTestHandler.promises.getAssignment(req, res, 'bootstrap-5-ide')
- res.render(template, {
- title: project.name,
- priority_title: true,
- bodyClasses: ['editor'],
- project_id: project._id,
- projectName: project.name,
- user: {
- id: userId,
- email: user.email,
- first_name: user.first_name,
- last_name: user.last_name,
- referal_id: user.referal_id,
- signUpDate: user.signUpDate,
- allowedFreeTrial,
- featureSwitches: user.featureSwitches,
- features: user.features,
- refProviders: _.mapValues(user.refProviders, Boolean),
- writefull: {
- enabled: Boolean(user.writefull?.enabled && aiFeaturesAllowed),
- },
- alphaProgram: user.alphaProgram,
- betaProgram: user.betaProgram,
- labsProgram: user.labsProgram,
- inactiveTutorials: TutorialHandler.getInactiveTutorials(user),
- isAdmin: hasAdminAccess(user),
- },
- userSettings: {
- mode: user.ace.mode,
- editorTheme: user.ace.theme,
- fontSize: user.ace.fontSize,
- autoComplete: user.ace.autoComplete,
- autoPairDelimiters: user.ace.autoPairDelimiters,
- pdfViewer: user.ace.pdfViewer,
- syntaxValidation: user.ace.syntaxValidation,
- fontFamily: user.ace.fontFamily || 'lucida',
- lineHeight: user.ace.lineHeight || 'normal',
- overallTheme: user.ace.overallTheme,
- mathPreview: user.ace.mathPreview,
- },
- privilegeLevel,
- anonymous,
- isTokenMember,
- isRestrictedTokenMember: AuthorizationManager.isRestrictedUser(
- userId,
- privilegeLevel,
- isTokenMember,
- isInvitedMember
- ),
- roMirrorOnClientNoLocalStorage:
- Settings.adminOnlyLogin || project.name.startsWith('Debug: '),
- languages: Settings.languages,
- learnedWords,
- editorThemes: THEME_LIST,
- legacyEditorThemes: LEGACY_THEME_LIST,
- maxDocLength: Settings.max_doc_length,
- brandVariation,
- allowedImageNames,
- gitBridgePublicBaseUrl: Settings.gitBridgePublicBaseUrl,
- gitBridgeEnabled: Features.hasFeature('git-bridge'),
- wsUrl,
- showSupport: Features.hasFeature('support'),
- showTemplatesServerPro,
- pdfjsVariant: splitTestAssignments['pdfjs-40'].variant,
- debugPdfDetach,
- showSymbolPalette,
- symbolPaletteAvailable: Features.hasFeature('symbol-palette'),
- userRestrictions: Array.from(req.userRestrictions || []),
- showAiErrorAssistant:
- aiFeaturesAllowed && user.features?.aiErrorAssistant,
- detachRole,
- metadata: { viewport: false },
- showUpgradePrompt,
- fixedSizeDocument: true,
- useOpenTelemetry: Settings.useOpenTelemetryClient,
- hasTrackChangesFeature: Features.hasFeature('track-changes'),
- projectTags,
- linkSharingWarning: linkSharingChanges?.variant === 'active',
- linkSharingEnforcement: linkSharingEnforcement?.variant === 'active',
- usedLatex:
- // only use the usedLatex value if the split test is enabled
- splitTestAssignments['default-visual-for-beginners']?.variant ===
- 'enabled'
- ? usedLatex
- : null,
- isSaas: Features.hasFeature('saas'),
- })
- timer.done()
- } catch (err) {
- OError.tag(err, 'error getting details for project page')
- return next(err)
- }
- },
- async _refreshFeatures(req, user) {
- // If the feature refresh has failed in this session, don't retry
- // it - require the user to log in again.
- if (req.session.feature_refresh_failed) {
- metrics.inc('features-refresh', 1, {
- path: 'load-editor',
- status: 'skipped',
- })
- return user
- }
- // If the refresh takes too long then return the current
- // features. Note that the user.features property may still be
- // updated in the background after the promise is resolved.
- const abortController = new AbortController()
- const refreshTimeoutHandler = async () => {
- await setTimeout(5000, { signal: abortController.signal })
- req.session.feature_refresh_failed = {
- reason: 'timeout',
- at: new Date(),
- }
- metrics.inc('features-refresh', 1, {
- path: 'load-editor',
- status: 'timeout',
- })
- return user
- }
- // try to refresh user features now
- const timer = new metrics.Timer('features-refresh-on-load-editor')
- return Promise.race([
- refreshTimeoutHandler(),
- (async () => {
- try {
- user.features = await FeaturesUpdater.promises.refreshFeatures(
- user._id,
- 'load-editor'
- )
- metrics.inc('features-refresh', 1, {
- path: 'load-editor',
- status: 'success',
- })
- } catch (err) {
- // keep a record to prevent unneceary retries and leave
- // the original features unmodified if the refresh failed
- req.session.feature_refresh_failed = {
- reason: 'error',
- at: new Date(),
- }
- metrics.inc('features-refresh', 1, {
- path: 'load-editor',
- status: 'error',
- })
- }
- abortController.abort()
- timer.done()
- return user
- })(),
- ])
- },
- _buildProjectList(allProjects, userId) {
- let project
- const { owned, readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly } =
- allProjects
- const projects = []
- for (project of owned) {
- projects.push(
- ProjectController._buildProjectViewModel(
- project,
- 'owner',
- Sources.OWNER,
- userId
- )
- )
- }
- // Invite-access
- for (project of readAndWrite) {
- projects.push(
- ProjectController._buildProjectViewModel(
- project,
- 'readWrite',
- Sources.INVITE,
- userId
- )
- )
- }
- for (project of readOnly) {
- projects.push(
- ProjectController._buildProjectViewModel(
- project,
- 'readOnly',
- Sources.INVITE,
- userId
- )
- )
- }
- // Token-access
- // Only add these projects if they're not already present, this gives us cascading access
- // from 'owner' => 'token-read-only'
- for (project of tokenReadAndWrite) {
- if (
- projects.filter(p => p.id.toString() === project._id.toString())
- .length === 0
- ) {
- projects.push(
- ProjectController._buildProjectViewModel(
- project,
- 'readAndWrite',
- Sources.TOKEN,
- userId
- )
- )
- }
- }
- for (project of tokenReadOnly) {
- if (
- projects.filter(p => p.id.toString() === project._id.toString())
- .length === 0
- ) {
- projects.push(
- ProjectController._buildProjectViewModel(
- project,
- 'readOnly',
- Sources.TOKEN,
- userId
- )
- )
- }
- }
- return projects
- },
- _buildProjectViewModel(project, accessLevel, source, userId) {
- const archived = ProjectHelper.isArchived(project, userId)
- // If a project is simultaneously trashed and archived, we will consider it archived but not trashed.
- const trashed = ProjectHelper.isTrashed(project, userId) && !archived
- const model = {
- id: project._id,
- name: project.name,
- lastUpdated: project.lastUpdated,
- lastUpdatedBy: project.lastUpdatedBy,
- publicAccessLevel: project.publicAccesLevel,
- accessLevel,
- source,
- archived,
- trashed,
- owner_ref: project.owner_ref,
- isV1Project: false,
- }
- if (accessLevel === PrivilegeLevels.READ_ONLY && source === Sources.TOKEN) {
- model.owner_ref = null
- model.lastUpdatedBy = null
- }
- return model
- },
- _buildPortalTemplatesList(affiliations) {
- if (affiliations == null) {
- affiliations = []
- }
- const portalTemplates = []
- for (const aff of affiliations) {
- if (
- aff.portal &&
- aff.portal.slug &&
- aff.portal.templates_count &&
- aff.portal.templates_count > 0
- ) {
- const portalPath = aff.institution.isUniversity ? '/edu/' : '/org/'
- portalTemplates.push({
- name: aff.institution.name,
- url: Settings.siteUrl + portalPath + aff.portal.slug,
- })
- }
- }
- return portalTemplates
- },
- }
- const defaultSettingsForAnonymousUser = userId => ({
- id: userId,
- ace: {
- mode: 'none',
- theme: 'textmate',
- fontSize: '12',
- autoComplete: true,
- spellCheckLanguage: '',
- pdfViewer: '',
- syntaxValidation: true,
- },
- subscription: {
- freeTrial: {
- allowed: true,
- },
- },
- featureSwitches: {
- github: false,
- },
- alphaProgram: false,
- betaProgram: false,
- writefull: {
- enabled: false,
- },
- })
- const defaultUserValues = () => ({
- user: defaultSettingsForAnonymousUser(null),
- learnedWords: [],
- projectTags: [],
- userHasInstitutionLicence: false,
- subscription: undefined,
- isTokenMember: false,
- isInvitedMember: false,
- })
- const THEME_LIST = [
- 'cobalt',
- 'dracula',
- 'eclipse',
- 'monokai',
- 'overleaf',
- 'textmate',
- ]
- const LEGACY_THEME_LIST = [
- 'ambiance',
- 'chaos',
- 'chrome',
- 'clouds',
- 'clouds_midnight',
- 'crimson_editor',
- 'dawn',
- 'dreamweaver',
- 'github',
- 'gob',
- 'gruvbox',
- 'idle_fingers',
- 'iplastic',
- 'katzenmilch',
- 'kr_theme',
- 'kuroir',
- 'merbivore',
- 'merbivore_soft',
- 'mono_industrial',
- 'nord_dark',
- 'pastel_on_dark',
- 'solarized_dark',
- 'solarized_light',
- 'sqlserver',
- 'terminal',
- 'tomorrow',
- 'tomorrow_night',
- 'tomorrow_night_blue',
- 'tomorrow_night_bright',
- 'tomorrow_night_eighties',
- 'twilight',
- 'vibrant_ink',
- 'xcode',
- ]
- const ProjectController = {
- archiveProject: expressify(_ProjectController.archiveProject),
- cloneProject: expressify(_ProjectController.cloneProject),
- deleteProject: expressify(_ProjectController.deleteProject),
- expireDeletedProject: expressify(_ProjectController.expireDeletedProject),
- expireDeletedProjectsAfterDuration: expressify(
- _ProjectController.expireDeletedProjectsAfterDuration
- ),
- loadEditor: expressify(_ProjectController.loadEditor),
- newProject: expressify(_ProjectController.newProject),
- projectEntitiesJson: expressify(_ProjectController.projectEntitiesJson),
- renameProject: expressify(_ProjectController.renameProject),
- restoreProject: expressify(_ProjectController.restoreProject),
- trashProject: expressify(_ProjectController.trashProject),
- unarchiveProject: expressify(_ProjectController.unarchiveProject),
- untrashProject: expressify(_ProjectController.untrashProject),
- updateProjectAdminSettings: expressify(
- _ProjectController.updateProjectAdminSettings
- ),
- updateProjectSettings: expressify(_ProjectController.updateProjectSettings),
- userProjectsJson: expressify(_ProjectController.userProjectsJson),
- _buildProjectList: _ProjectController._buildProjectList,
- _buildProjectViewModel: _ProjectController._buildProjectViewModel,
- _injectProjectUsers: _ProjectController._injectProjectUsers,
- _isInPercentageRollout: _ProjectController._isInPercentageRollout,
- _refreshFeatures: _ProjectController._refreshFeatures,
- }
- module.exports = ProjectController
|