Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/lib/share/get-public-paths.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Get the public paths associated to a given project. Unlisted paths are NOT included.7*/89import getPool, { timeInSeconds } from "@cocalc/database/pool";10import { PublicPath } from "./types";11import { isUUID } from "./util";12import isCollaborator from "@cocalc/server/projects/is-collaborator";13import getAccountId from "lib/account/get-account";14import { getProjectAvatarTiny } from "./project-avatar-image";1516export default async function getPublicPaths(17project_id: string,18req // use to get account_id if necessary19): Promise<PublicPath[]> {20if (!isUUID(project_id)) {21throw Error("project_id must be a uuid");22}23// short: user might create a new public path then want to look at it shortly thereafter24const pool = getPool("short");25const result = await pool.query(26`SELECT id, path, description, ${timeInSeconds(27"last_edited"28)}, disabled, unlisted, authenticated,29counter::INT,30(SELECT COUNT(*)::INT FROM public_path_stars WHERE public_path_id=id) AS stars31FROM public_paths WHERE project_id=$1 ORDER BY stars DESC, last_edited DESC`,32[project_id]33);3435const v = await filterNonPublicAndNotAuthenticated(36result.rows,37project_id,38req39);40const avatar_image_tiny = await getProjectAvatarTiny(project_id);41if (avatar_image_tiny) {42for (const x of v) {43x.avatar_image_tiny = avatar_image_tiny;44}45}46return v;47}4849async function filterNonPublicAndNotAuthenticated(50rows: PublicPath[],51project_id,52req53): Promise<PublicPath[]> {54const v: any[] = [];55let isCollab: boolean | undefined = undefined;56let isAuthenticated: boolean | undefined = undefined;57for (const row of rows) {58if (!row.disabled && !row.unlisted && !row.authenticated) {59v.push(row);60continue;61}62if (isCollab == null) {63const account_id = await getAccountId(req);64isAuthenticated = account_id != null;65if (account_id) {66isCollab = await isCollaborator({67account_id,68project_id,69});70} else {71isCollab = false;72}73}74if (isCollab) {75v.push(row);76} else if (row.authenticated && isAuthenticated) {77v.push(row);78}79}80return v;81}828384