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