CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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