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/project.ts
Views: 687
1
/*
2
Get basic information about a user or organization
3
from the database. This should be enough to render
4
a nice "homepage" for that user or organization.
5
6
*/
7
8
import getPool from "@cocalc/database/pool";
9
import getOwner from "./owner";
10
import getProxyProjectId from "lib/share/proxy/project";
11
12
// Throws an exception if there is no project with this name.
13
// TODO: take into account redirects for when name is changed.
14
export default async function getProjectId(
15
owner: string,
16
project: string
17
): Promise<string> {
18
if (owner == "github") {
19
return await getProxyProjectId();
20
}
21
22
const { owner_id } = await getOwner(owner);
23
const pool = getPool("long"); // map from owner/project --> project_id unlikely to change and when it does, stale data is ok.
24
25
// NOTE: it's not enough that owner is a collab on the project -- they have to be the owner.
26
// We do users ? $2 also, since it's fast/indexed, then dig in to the JSON...
27
// Note that we have put owner_id in the query string explicitly (as far as I know),
28
// though it is NOT user supplied so that's safe.
29
const result = await pool.query(
30
`SELECT project_id FROM projects WHERE LOWER(name)=$1 AND users ? $2 AND users#>>'{${owner_id},group}' = 'owner'`,
31
[project.toLowerCase(), owner_id]
32
);
33
if (result.rows.length > 0) {
34
return result.rows[0].project_id;
35
}
36
throw Error(`no project ${owner}/${project}`);
37
}
38
39