Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions apps/roam/src/utils/createReifiedBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@ export const countReifiedRelations = async (): Promise<number> => {
return (r[0] || [0])[0] as number;
};

export type ReifiedRelationData = {
sourceUid: string;
destinationUid: string;
hasSchema: string;
importedFromRid?: string;
};

export type ReifiedRelationDataWithRelId = ReifiedRelationData & {
relationId: string;
};

export const getReifiedRelations = async (): Promise<
ReifiedRelationDataWithRelId[]
> => {
const pageUid = getExistingRelationPageUid();
if (pageUid === undefined) return [];
const r = await window.roamAlphaAPI.data.async.q(
`[:find ?ruid ?rdata :where
[?p :block/uid "${pageUid}"]
[?p :block/children ?c]
[?c :block/uid ?ruid]
[?c :block/props ?pr]
[(get ?pr :${DISCOURSE_GRAPH_PROP_NAME}) ?rdata] ]`,
);
return r.map((x) => ({
relationId: x[0] as string,
...(x[1] as ReifiedRelationData),
}));
};

export const createReifiedRelation = async ({
sourceUid,
relationBlockUid,
Expand Down
326 changes: 326 additions & 0 deletions apps/roam/src/utils/publishNodesToGroups.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import type { DGSupabaseClient } from "@repo/database/lib/client";
import { getAvailableGroupIds } from "@repo/database/lib/groups";
import getDiscourseRelations from "./getDiscourseRelations";
import getDiscourseNodes from "./getDiscourseNodes";
import {
getReifiedRelations,
type ReifiedRelationDataWithRelId,
} from "./createReifiedBlock";
import type {
CrossAppNodeSchema,
CrossAppRelation,
CrossAppRelationTripleSchema,
} from "@repo/database/crossAppContracts";
import type { DiscourseRelation } from "./getDiscourseRelations";
import {
crossAppRelationToDbConcept,
crossAppRelationTripleSchemaToDbConcept,
} from "@repo/database/lib/crossAppConverters";
import { spaceUriAndLocalIdToRid } from "@repo/database/lib/rid";

export type PublishNode = {
uid: string;
Expand All @@ -13,6 +30,309 @@ type PublishNodesResult = {
failedGroupIds: string[];
};

const getAllPublishedIds = async (
client: DGSupabaseClient,
spaceId: number,
groupId: string,
): Promise<string[]> => {
const response = await client
.from("ResourceAccess")
.select("source_local_id")
.eq("space_id", spaceId)
.eq("account_uid", groupId);
if (response.error) throw response.error;
return response.data.map((r) => r.source_local_id);
};

const getGroupSpaceIdAndUrls = async (
client: DGSupabaseClient,
groupId: string,
): Promise<Record<number, string>> => {
const response = await client
.from("SpaceAccess")
.select("space_id")
.eq("account_uid", groupId);
if (response.error) throw response.error;
const spaceIds = response.data.map((r) => r.space_id);
const response2 = await client
.from("Space")
.select("id, url")
.in("id", spaceIds);
if (response2.error) throw response2.error;
return Object.fromEntries(response2.data.map(({ id, url }) => [id, url]));
};

// Use readImportedSourceIdentity when it's integrated.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getSpaceIdOf = (nodeId: string): number | undefined => undefined;

const getSyncedRelationLocalIds = async (
client: DGSupabaseClient,
spaceId: number,
): Promise<string[]> => {
const response = await client
.from("my_concepts")
.select("source_local_id")
.eq("space_id", spaceId)
.eq("arity", 2); // intentionally include both schemas and instances
if (response.error) throw response.error;
return response.data
.map((r) => r.source_local_id)
.filter((id) => id !== null);
};

const ensureRelationsSynced = async ({
client,
spaceId,
relations,
existing,
isImportedFrom,
spaceUids,
}: {
client: DGSupabaseClient;
spaceId: number;
relations: ReifiedRelationDataWithRelId[];
existing: Set<string>;
isImportedFrom: (id: string) => number | undefined;
spaceUids: Record<number, string>;
}): Promise<void> => {
const missing = relations.filter((r) => !existing.has(r.relationId));
const asCrossApp: CrossAppRelation[] = missing
.map((r) => {
const sourceSpaceUid = spaceUids[isImportedFrom(r.sourceUid) || 0];
const destinationSpaceUid =
spaceUids[isImportedFrom(r.destinationUid) || 0];
const sourceId =
sourceSpaceUid === undefined
? r.sourceUid
: spaceUriAndLocalIdToRid(sourceSpaceUid, r.sourceUid);
const destinationId =
destinationSpaceUid === undefined
? r.destinationUid
: spaceUriAndLocalIdToRid(destinationSpaceUid, r.destinationUid);
const relData = window.roamAlphaAPI.pull(
"[:create/time :edit/time {:create/user [:user/uid]}]",
`[:block/uid "${r.relationId}"]`,
) as unknown as {
":create/time": number;
":edit/time": number;
":create/user": { ":user/uid": string };
};
if (relData == undefined || !relData[":create/user"]) return null;
const userUid = relData[":create/user"][":user/uid"];
Comment thread
maparent marked this conversation as resolved.

return {
localId: r.relationId,
relationType: r.hasSchema,
source: sourceId,
destination: destinationId,
authorId: userUid,
createdAt: new Date(relData[":create/time"]),
modifiedAt: new Date(relData[":edit/time"]),
};
})
.filter((s) => s !== null);
if (asCrossApp.length == 0) return;
const asDbData = asCrossApp.map((r) => crossAppRelationToDbConcept(r));
const response = await client.rpc("upsert_concepts", {
v_space_id: spaceId,
data: asDbData,
});
if (response.error) throw response.error;
asCrossApp.forEach((r) => {
existing.add(r.localId);
});
};

const ensureRelationSchemasSynced = async ({
client,
spaceId,
relationSchemas,
existing,
nodeSchemaById,
}: {
client: DGSupabaseClient;
spaceId: number;
relationSchemas: DiscourseRelation[];
existing: Set<string>;
nodeSchemaById: Record<string, CrossAppNodeSchema>;
}): Promise<void> => {
const missing = relationSchemas.filter((r) => !existing.has(r.id));
const asCrossApp: CrossAppRelationTripleSchema[] = missing
.map((r) => {
const relData = window.roamAlphaAPI.pull(
"[:create/time :edit/time {:create/user [:user/uid]}]",
`[:block/uid "${r.id}"]`,
) as unknown as {
":create/time": number;
":edit/time": number;
":create/user": { ":user/uid": string };
};
if (!relData) return null;
const userUid = relData[":create/user"][":user/uid"];

return {
localId: r.id,
sourceType: r.source,
destinationType: r.destination,
label: r.label,
complement: r.complement,
authorId: userUid,
createdAt: new Date(relData[":create/time"]),
modifiedAt: new Date(relData[":edit/time"]),
};
})
.filter((x) => x !== null);
if (asCrossApp.length == 0) return;
const asDbData = asCrossApp
.map((r) => {
const sourceSchema = nodeSchemaById[r.sourceType];
const destinationSchema = nodeSchemaById[r.destinationType];
if (!sourceSchema || !destinationSchema) return undefined;
return crossAppRelationTripleSchemaToDbConcept({
node: r,
sourceNodeSchema: sourceSchema,
destinationNodeSchema: destinationSchema,
});
Comment thread
maparent marked this conversation as resolved.
})
.filter((d) => d !== undefined);
const response = await client.rpc("upsert_concepts", {
v_space_id: spaceId,
data: asDbData,
});
if (response.error) throw response.error;
asDbData.map((d) => {
if (d.source_local_id) existing.add(d.source_local_id);
});
};

const getCrossAppNodeSchemaById = (): Record<string, CrossAppNodeSchema> => {
const allNodeSchemas = getDiscourseNodes();
const result: CrossAppNodeSchema[] = allNodeSchemas
.map((s) => {
const relData = window.roamAlphaAPI.pull(
"[:create/time :edit/time {:create/user [:user/uid]}]",
`[:block/uid "${s.type}"]`,
) as unknown as {
":create/time": number;
":edit/time": number;
":create/user": { ":user/uid": string };
};
if (!relData) return null;
const userUid = relData[":create/user"][":user/uid"];
return {
localId: s.type,
label: s.text,
authorId: userUid,
createdAt: new Date(relData[":create/time"]),
};
})
.filter((s) => s !== null);
return Object.fromEntries(result.map((r) => [r.localId, r]));
};

export const publishCorrespondingRelations = async ({
client,
spaceId,
groupIds,
forNodeIds,
}: {
client: DGSupabaseClient;
spaceId: number;
groupIds: string[];
forNodeIds?: Set<string>;
}): Promise<string[] | null> => {
const allRelationsSchemas = getDiscourseRelations();
const allRelationSchemasById = Object.fromEntries(
allRelationsSchemas.map((s) => [s.id, s]),
);
const nodeSchemaById = getCrossAppNodeSchemaById();
// Should we even handle non-reified relations?
// I need a way to know if a relation is imported.
const allRelations = await getReifiedRelations();
const failedGroupIds: string[] = [];
const sourceIdOfNodes: Record<string, number> = {};
const syncedRelationIds = new Set(
await getSyncedRelationLocalIds(client, spaceId),
);
const isImportedFrom = (nodeLocalId: string): number => {
let cached = sourceIdOfNodes[nodeLocalId];
if (cached === undefined) {
cached = sourceIdOfNodes[nodeLocalId] =
getSpaceIdOf(nodeLocalId) || spaceId;
}
return cached === spaceId ? 0 : cached;
};
Comment on lines +258 to +265

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical Logic Bug: The isImportedFrom function always returns 0 because getSpaceIdOf (line 67) always returns undefined.

// Line 259: getSpaceIdOf always returns undefined
cached = sourceIdOfNodes[nodeLocalId] = undefined || spaceId;
// So cached always equals spaceId

// Line 261: Since cached === spaceId is always true
return cached === spaceId ? 0 : cached;  // Always returns 0

This breaks the logic at lines 279-281 where (isImportedFrom(r.sourceUid) || 0) in groupSpaces evaluates to 0 in groupSpaces, checking if property "0" exists instead of the actual space ID. This will cause relations with imported nodes to be incorrectly filtered.

Similarly at lines 102-104, spaceUids[isImportedFrom(r.sourceUid) || 0] always looks up spaceUids[0] which likely doesn't exist, causing all imported nodes to fall back to local UIDs when they should use RIDs.

Impact: Relations involving imported nodes will not be published correctly to groups. The code will either skip valid relations or fail to construct proper RIDs for cross-space references.

Fix: Either implement getSpaceIdOf properly or remove the imported node logic until ENG-1856 is complete (as mentioned in the PR description).

Spotted by Graphite

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

@maparent maparent Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is valid, but it's related to function that is expected to be integrated in ENG-1865 (forthcoming.) So the bug is really a placeholder.

const relations =
forNodeIds !== undefined
? allRelations.filter(
(r) =>
r.importedFromRid === undefined &&
(forNodeIds.has(r.sourceUid) || forNodeIds.has(r.destinationUid)),
)
: allRelations.filter((r) => r.importedFromRid === undefined);
for (const groupId of groupIds) {
const groupSpaces = await getGroupSpaceIdAndUrls(client, groupId);
const publishedIds = new Set(
await getAllPublishedIds(client, spaceId, groupId),
);
const relsBetweenPublishedAll = relations.filter(
(r) =>
(publishedIds.has(r.sourceUid) ||
(isImportedFrom(r.sourceUid) || 0) in groupSpaces) &&
(publishedIds.has(r.destinationUid) ||
(isImportedFrom(r.destinationUid) || 0) in groupSpaces),
);
const relationSchemaIds = new Set(
relsBetweenPublishedAll
.map((r) => r.hasSchema)
// filter out deleted schemas
.filter((id) => id in allRelationSchemasById),
);
const relationSchemaTriples = allRelationsSchemas.filter((r) =>
relationSchemaIds.has(r.id),
);
const relsBetweenPublished = relsBetweenPublishedAll.filter((r) =>
relationSchemaIds.has(r.hasSchema),
);
await ensureRelationSchemasSynced({
client,
spaceId,
relationSchemas: relationSchemaTriples,
existing: syncedRelationIds,
nodeSchemaById,
});
await ensureRelationsSynced({
client,
spaceId,
relations: relsBetweenPublished,
existing: syncedRelationIds,
isImportedFrom,
spaceUids: groupSpaces,
});
const missingIds = [
...relsBetweenPublished
.map((r) => r.relationId)
.filter((id) => !publishedIds.has(id)),
...relationSchemaTriples
.map((r) => r.id)
.filter((id) => !publishedIds.has(id)),
];
const grantRes = await client.from("ResourceAccess").upsert(
missingIds.map((sourceLocalId) => ({
account_uid: groupId,
source_local_id: sourceLocalId,
space_id: spaceId,
})),
{ ignoreDuplicates: true },
);
if (!isIgnorableUpsertError(grantRes.error)) {
failedGroupIds.push(groupId);
}
}
return failedGroupIds.length ? failedGroupIds : null;
};

// 23505 = unique_violation: the grant already exists, which counts as success.
const isIgnorableUpsertError = (error: { code?: string } | null): boolean =>
!error || error.code === "23505";
Expand Down Expand Up @@ -121,6 +441,12 @@ export const publishNodesToGroups = async ({

result.okGroupIds.push(groupId);
}
await publishCorrespondingRelations({
client,
spaceId,
groupIds: targetGroupIds,
forNodeIds: new Set(syncedNodeUids),
});

result.publishedNodeUids = result.okGroupIds.length > 0 ? syncedNodeUids : [];
return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/inputTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type LocalAccountDataInput = Partial<
export type LocalDocumentDataInput = Partial<
Omit<
Database["public"]["CompositeTypes"]["document_local_input"],
"author_inline"
"author_inline" | "contents"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"content" is of type unknown, so we get typescript errors when using the LocalDocumentDataInput explicitly (even though indirectly through LocalConceptDataInput, LocalContentDataInput.)
It is not used yet, so just removing from input type for now.
I guess this did not arise earlier because we were building objects, not using the type explicitly.

> & { author_inline: LocalAccountDataInput }
>;
export type LocalContentDataInput = Partial<
Expand Down