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/names/public-path.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/* Get basic information about a user or organization
7
from the database. This should be enough to render
8
a nice "homepage" for that user or organization.
9
*/
10
import getPool from "@cocalc/database/pool";
11
import getProjectId from "./project";
12
import { getOwnerName } from "lib/names/owner";
13
import getProxyPublicPath, {
14
shouldUseProxy,
15
} from "lib/share/proxy/get-public-path";
16
import { join } from "path";
17
18
export default async function getPublicPathId(
19
owner: string,
20
project: string,
21
public_path: string[] // this is the entire actual path
22
): Promise<string> {
23
if (shouldUseProxy(owner)) {
24
// special case -- proxy urls...
25
const { id } = await getProxyPublicPath({
26
url: join(owner, project, ...public_path),
27
});
28
return id;
29
}
30
31
const project_id = await getProjectId(owner, project);
32
const pool = getPool("long");
33
const result = await pool.query(
34
"SELECT id FROM public_paths WHERE LOWER(name)=$1 AND project_id=$2",
35
[public_path[0]?.toLowerCase() ?? "", project_id]
36
);
37
if (result.rows.length > 0) {
38
return result.rows[0].id;
39
}
40
throw Error(`no public_path ${owner}/${project}/${public_path[0]}`);
41
}
42
43
// Given the id of a public path, returns owner name, project name, and public_path name
44
// if they are all defined and nonempty. Otherwise returns undefined.
45
export async function getPublicPathNames(
46
id: string
47
): Promise<
48
{ owner: string; project: string; public_path: string } | undefined
49
> {
50
const pool = getPool("medium");
51
let result = await pool.query(
52
"SELECT project_id, name FROM public_paths WHERE id=$1",
53
[id]
54
);
55
if (result.rows.length == 0) return;
56
const { project_id, name: public_path } = result.rows[0];
57
if (!public_path) return;
58
59
// Having to get users is pretty stupid -- see comment in lib/project/get-owner.ts
60
result = await pool.query(
61
"SELECT name, users FROM projects WHERE project_id=$1 AND name IS NOT NULL AND name != ''",
62
[project_id]
63
);
64
if (result.rows.length == 0) return;
65
66
const { name: project, users } = result.rows[0];
67
let owner_id: string = "";
68
for (const account_id in users) {
69
if (users[account_id].group == "owner") {
70
owner_id = account_id;
71
break;
72
}
73
}
74
if (!owner_id) return; // shouldn't be possible
75
const owner = await getOwnerName(owner_id);
76
if (!owner) return;
77
return { owner, project, public_path };
78
}
79
80