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/frontend/collaborators/handle-project-invite.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { QueryParams } from "../misc/query-params";6import { webapp_client } from "../webapp-client";7import { alert_message } from "../alerts";8import { redux } from "../app-framework";9import { delay } from "awaiting";1011export const PROJECT_INVITE_QUERY_PARAM = "project-invite";1213async function handle_project_invite() {14webapp_client.removeListener("signed_in", handle_project_invite); // only try at most once the first time.15const token_id = QueryParams.get(PROJECT_INVITE_QUERY_PARAM);16if (!token_id) return;17QueryParams.remove(PROJECT_INVITE_QUERY_PARAM);18const account_id = webapp_client.account_id;19if (!account_id) return;20add_self_to_project_using_token(token_id);21}2223async function init() {24await delay(0); // has to be after page loads...25webapp_client.on("signed_in", handle_project_invite);26}27init();2829export async function add_self_to_project_using_token(token_id) {30if (webapp_client.account_id == null) return;3132const actions = redux.getActions("page");33if (34!(await actions.popconfirm({35title: "Would you like to accept this project invitation?",36description:37"If you are visiting a link from somebody you trust, click 'Yes, accept invitation'. If this seems suspicious, click 'No'. You can always open the invite link again if you change your mind.",38okText: "Yes, accept invitation",39}))40) {41return;42}43try {44const resp = await webapp_client.project_collaborators.add_collaborator({45account_id: webapp_client.account_id,46token_id,47});48const project_id = resp.project_id;49if (typeof project_id == "string") {50alert_message({51type: "info",52message: "You have been successfully added to the project!",53timeout: 10,54});55// Wait until the project is available in the store:56const store = redux.getStore("projects");57await store.async_wait({58until: () => store.getIn(["project_map", project_id]),59timeout: 120,60});61// Now actually open it.62redux.getActions("projects").open_project({ project_id });63} else {64throw Error("something went wrong (this shouldn't happen)"); // should never happen.65}66} catch (err) {67alert_message({ type: "error", message: err.toString(), timeout: 30 });68}69}707172