Skip to content

Commit b3a0b19

Browse files
authored
ENG-420 Obtain a password from the roam props of the plugin settings. (#245)
Create it (as a UUID, which is cryptographicallly safe) and store it otherwise. Added an utility function to set Props.
1 parent 691ef0d commit b3a0b19

3 files changed

Lines changed: 81 additions & 6 deletions

File tree

apps/roam/src/utils/getBlockProps.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ export const normalizeProps = (props: json): json =>
2424
)
2525
: props;
2626

27+
export const getRawBlockProps = (uid: string) =>
28+
(window.roamAlphaAPI.pull("[:block/props]", [":block/uid", uid])?.[
29+
":block/props"
30+
] || {}) as Record<string, json>;
31+
2732
const getBlockProps = (uid: string) =>
28-
normalizeProps(
29-
(window.roamAlphaAPI.pull("[:block/props]", [":block/uid", uid])?.[
30-
":block/props"
31-
] || {}) as Record<string, json>,
32-
) as Record<string, json>;
33+
normalizeProps(getRawBlockProps(uid)) as Record<string, json>;
3334

3435
export default getBlockProps;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { type json, getRawBlockProps } from "./getBlockProps";
2+
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
3+
4+
export const deNormalizeProps = (props: json): json =>
5+
typeof props === "object"
6+
? props === null
7+
? null
8+
: Array.isArray(props)
9+
? props.map(deNormalizeProps)
10+
: Object.fromEntries(
11+
Object.entries(props).map(([k, v]) => [
12+
`:${k}`,
13+
typeof v === "object" && v !== null && !Array.isArray(v)
14+
? deNormalizeProps(v)
15+
: Array.isArray(v)
16+
? v.map(deNormalizeProps)
17+
: v,
18+
]),
19+
)
20+
: props;
21+
22+
const setBlockProps = (
23+
uid: string,
24+
newProps: Record<string, json>,
25+
denormalize: boolean = false,
26+
) => {
27+
const baseProps = getRawBlockProps(uid);
28+
if (typeof baseProps === "object" && !Array.isArray(baseProps)) {
29+
const props = {
30+
...(baseProps || {}),
31+
...(denormalize
32+
? (deNormalizeProps(newProps) as Record<string, json>)
33+
: newProps),
34+
} as Record<string, json>;
35+
window.roamAlphaAPI.data.block.update({ block: { uid, props } });
36+
return props;
37+
}
38+
return baseProps;
39+
};
40+
41+
export const testSetBlockProps = (
42+
title: string,
43+
newProps: Record<string, json>,
44+
) => {
45+
const uid = getPageUidByPageTitle(title);
46+
return uid ? setBlockProps(uid, newProps) : null;
47+
};
48+
49+
export default setBlockProps;

apps/roam/src/utils/supabaseContext.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { getNodeEnv } from "roamjs-components/util/env";
22
import getCurrentUserEmail from "roamjs-components/queries/getCurrentUserEmail";
33
import getCurrentUserDisplayName from "roamjs-components/queries/getCurrentUserDisplayName";
4+
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
45
import getRoamUrl from "roamjs-components/dom/getRoamUrl";
6+
57
import { Database } from "@repo/database/types.gen";
8+
import { DISCOURSE_CONFIG_PAGE_TITLE } from "~/utils/renderNodeConfigPage";
9+
import getBlockProps from "~/utils/getBlockProps";
10+
import setBlockProps from "~/utils/setBlockProps";
11+
12+
declare const crypto: { randomUUID: () => string };
613

714
type Platform = Database["public"]["Enums"]["Platform"];
815

916
export type SupabaseContext = {
1017
platform: Platform;
1118
spaceId: number;
1219
userId: number;
20+
spacePassword: string;
1321
};
1422

1523
let CONTEXT_CACHE: SupabaseContext | null = null;
@@ -20,6 +28,22 @@ const base_url =
2028
? "http://localhost:3000/api/supabase"
2129
: "https://discoursegraphs.com/api/supabase";
2230

31+
const settingsConfigPageUid = getPageUidByPageTitle(
32+
DISCOURSE_CONFIG_PAGE_TITLE,
33+
);
34+
35+
const getOrCreateSpacePassword = () => {
36+
const props = getBlockProps(settingsConfigPageUid);
37+
const existing: string | unknown = props["space-user-password"];
38+
if (existing && typeof existing === "string") return existing;
39+
// use a uuid as password, at least cryptographically safe
40+
const password = crypto.randomUUID();
41+
setBlockProps(settingsConfigPageUid, {
42+
"space-user-password": password,
43+
});
44+
return password;
45+
};
46+
2347
// Note: Some of this will be more typesafe if rewritten with direct supabase access eventually.
2448
// We're going through nextjs until we have settled security.
2549

@@ -100,12 +124,13 @@ export const getSupabaseContext = async (): Promise<SupabaseContext | null> => {
100124
const accountLocalId = window.roamAlphaAPI.user.uid();
101125
const personEmail = getCurrentUserEmail();
102126
const personName = getCurrentUserDisplayName();
127+
const spacePassword = getOrCreateSpacePassword();
103128
const userId = await fetchOrCreatePlatformAccount({
104129
accountLocalId,
105130
personName,
106131
personEmail,
107132
});
108-
CONTEXT_CACHE = { platform: "Roam", spaceId, userId };
133+
CONTEXT_CACHE = { platform: "Roam", spaceId, userId, spacePassword };
109134
} catch (error) {
110135
console.error(error);
111136
return null;

0 commit comments

Comments
 (0)