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/share/get-public-paths.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
/*
7
Get the public paths associated to a given project. Unlisted paths are NOT included.
8
*/
9
10
import getPool, { timeInSeconds } from "@cocalc/database/pool";
11
import { PublicPath } from "./types";
12
import { isUUID } from "./util";
13
import isCollaborator from "@cocalc/server/projects/is-collaborator";
14
import getAccountId from "lib/account/get-account";
15
import { getProjectAvatarTiny } from "./project-avatar-image";
16
17
export default async function getPublicPaths(
18
project_id: string,
19
req // use to get account_id if necessary
20
): Promise<PublicPath[]> {
21
if (!isUUID(project_id)) {
22
throw Error("project_id must be a uuid");
23
}
24
// short: user might create a new public path then want to look at it shortly thereafter
25
const pool = getPool("short");
26
const result = await pool.query(
27
`SELECT id, path, description, ${timeInSeconds(
28
"last_edited"
29
)}, disabled, unlisted, authenticated,
30
counter::INT,
31
(SELECT COUNT(*)::INT FROM public_path_stars WHERE public_path_id=id) AS stars
32
FROM public_paths WHERE project_id=$1 ORDER BY stars DESC, last_edited DESC`,
33
[project_id]
34
);
35
36
const v = await filterNonPublicAndNotAuthenticated(
37
result.rows,
38
project_id,
39
req
40
);
41
const avatar_image_tiny = await getProjectAvatarTiny(project_id);
42
if (avatar_image_tiny) {
43
for (const x of v) {
44
x.avatar_image_tiny = avatar_image_tiny;
45
}
46
}
47
return v;
48
}
49
50
async function filterNonPublicAndNotAuthenticated(
51
rows: PublicPath[],
52
project_id,
53
req
54
): Promise<PublicPath[]> {
55
const v: any[] = [];
56
let isCollab: boolean | undefined = undefined;
57
let isAuthenticated: boolean | undefined = undefined;
58
for (const row of rows) {
59
if (!row.disabled && !row.unlisted && !row.authenticated) {
60
v.push(row);
61
continue;
62
}
63
if (isCollab == null) {
64
const account_id = await getAccountId(req);
65
isAuthenticated = account_id != null;
66
if (account_id) {
67
isCollab = await isCollaborator({
68
account_id,
69
project_id,
70
});
71
} else {
72
isCollab = false;
73
}
74
}
75
if (isCollab) {
76
v.push(row);
77
} else if (row.authenticated && isAuthenticated) {
78
v.push(row);
79
}
80
}
81
return v;
82
}
83
84