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/owner.ts
Views: 687
1
/* Get basic information about a user or organization
2
from the database. This should be enough to render
3
a nice "homepage" for that user or organization.
4
*/
5
6
import getPool from "@cocalc/database/pool";
7
import { isReserved } from "@cocalc/util/db-schema/name-rules";
8
9
// Throws an exception if there is no account or org with this name.
10
// TODO: take into account redirects for when name is changed.
11
12
interface Owner {
13
type: "account" | "organization";
14
owner_id: string;
15
}
16
17
export default async function getOwner(owner: string): Promise<Owner> {
18
if (isReserved(owner)) {
19
// can't be so save us the trouble of consulting database; these are common
20
// so this is a good win.
21
throw Error(`no such owner '${owner}'- reserved name`);
22
}
23
const pool = getPool("long");
24
// Is it an account?
25
let result = await pool.query(
26
"SELECT account_id FROM accounts WHERE LOWER(name)=$1",
27
[owner.toLowerCase()]
28
);
29
if (result.rows.length > 0) {
30
return { type: "account", owner_id: result.rows[0].account_id };
31
}
32
// Is it an organization?
33
result = await pool.query(
34
"SELECT title, description, organization_id FROM organizations WHERE LOWER(name)=$1",
35
[owner.toLowerCase()]
36
);
37
if (result.rows.length > 0) {
38
return { type: "organization", owner_id: result.rows[0].organization_id };
39
}
40
throw Error(`no account or organization '${owner}'`);
41
}
42
43
export async function getOwnerName(
44
owner_id: string
45
): Promise<string | undefined> {
46
const pool = getPool("long");
47
let result = await pool.query(
48
"SELECT name FROM accounts WHERE account_id=$1",
49
[owner_id]
50
);
51
if (result.rows.length > 0) {
52
const { name } = result.rows[0];
53
if (!name) return;
54
return name;
55
}
56
result = await pool.query(
57
"SELECT name FROM organizations WHERE organization_id=$1",
58
[owner_id]
59
);
60
if (result.rows.length > 0) {
61
const { name } = result.rows[0];
62
if (!name) return;
63
return name;
64
}
65
}
66
67