Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/lib/with-customize.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import getCustomize from "@cocalc/database/settings/customize";6import getAccountId from "lib/account/get-account";7import { getName } from "lib/share/get-account-info";8import isCollaborator from "@cocalc/server/projects/is-collaborator";9import { KUCALC_COCALC_COM } from "@cocalc/util/db-schema/site-defaults";10import { getSoftwareEnvironments } from "@cocalc/server/software-envs";11import { DEFAULT_COMPUTE_IMAGE } from "@cocalc/util/db-schema";1213import { CustomizeType } from "./customize";1415const revalidate = 30;1617interface Options {18name?: boolean; // if true and user is signed in, also puts their first_name,19// last_name, name(=username), email_address, and is_anonymous in the account field.20// This is one more db query.21}2223export default async function withCustomize(24obj: {25props?: any;26revalidate?: number;27context: any;28},29options: Options = {},30) {31let customize: CustomizeType;32try {33// NOTE: important to make a (shallow) copy, since customize gets mutated below.34customize = { ...(await getCustomize()) };35} catch (_err) {36// fallback to be empty; during static build37// this happens.38customize = {} as CustomizeType;39}4041if (obj.context?.req != null) {42const account_id = await getAccountId(obj.context.req);43customize.isAuthenticated = !!account_id;44if (account_id) {45customize.account = {46account_id,47...(options.name ? await getName(account_id) : undefined),48};4950// Also, if a project id is in the props and account_id is set, it's very51// useful to know if the user is a collaborator on the project, since that52// can impact a lot about how we display things. This is typically used53// for the share pages.54const project_id = obj.props?.project_id;55if (project_id) {56customize.isCollaborator = await isCollaborator({57account_id,58project_id,59});60}61}62} else {63customize.isAuthenticated = false;64}6566customize.onCoCalcCom = customize.kucalc === KUCALC_COCALC_COM;67customize.noindex = obj.props?.unlisted ?? false;68customize.imprintOrPolicies =69(customize.imprint ?? "" + customize.policies ?? "") != "";70customize.serverTime = Date.now();7172// this is used for creating new projects from a share73const softwareEnvs = await getSoftwareEnvironments("server");74customize.defaultComputeImage =75softwareEnvs?.default ?? DEFAULT_COMPUTE_IMAGE;7677customize.enabledPages = {78auth: {79try: !customize.account && customize.anonymousSignup,80},81about: {82index: customize.landingPages,83events: customize.isCommercial,84team: customize.landingPages,85},86compute: customize.computeServersEnabled,87contact: !!customize.contactEmail,88features: customize.landingPages,89info: true,90legal: !customize.landingPages && !!customize.termsOfServiceURL,91licenses: customize.isCommercial,92news: true,93onPrem: customize.onCoCalcCom,94organization: !!customize.organizationURL,95policies: {96index: customize.landingPages || customize.imprintOrPolicies,97imprint: !!customize.imprint,98},99pricing: customize.isCommercial,100share: customize.shareServer,101software: customize.landingPages,102store: customize.landingPages && customize.isCommercial,103support: true, // always enabled, but for on-prem && settings.support, we render a different page104systemActivity: true,105status: customize.onCoCalcCom,106termsOfService: !customize.landingPages && !!customize.termsOfServiceURL,107};108109if (obj == null) {110return { props: { customize } };111}112if (obj.revalidate != null) {113obj.revalidate = Math.min(revalidate, obj.revalidate);114}115if (obj.props == null) {116obj.props = { customize };117} else {118obj.props.customize = customize;119}120delete obj.context;121return obj;122}123124125