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
10 changes: 8 additions & 2 deletions apps/obsidian/src/utils/__tests__/conceptConversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,19 @@ const NODE_TYPE: DiscourseNode = {
describe("discourseNodeSchemaToLocalConcept format", () => {
it("publishes the format at the top level of literal content", () => {
expect(
discourseNodeSchemaToLocalConcept({ context: CONTEXT, node: NODE_TYPE }),
discourseNodeSchemaToLocalConcept({
accountLocalId: "account-1",
node: NODE_TYPE,
}),
).toMatchObject({ literal_content: { format: "CLM - {content}" } });
});

it("keeps the other metadata in source data without the format", () => {
expect(
discourseNodeSchemaToLocalConcept({ context: CONTEXT, node: NODE_TYPE }),
discourseNodeSchemaToLocalConcept({
accountLocalId: "account-1",
node: NODE_TYPE,
}),
).toHaveProperty("literal_content.source_data", {
shortcut: "C",
color: "#7DA13E",
Expand Down
22 changes: 8 additions & 14 deletions apps/obsidian/src/utils/__tests__/syncDgNodesToSupabase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ describe("what Obsidian sync sends to upsert_concepts", () => {
id: "source-type",
name: "Source",
format: "SRC - {content}",
template: "",
...schemaTimes,
};
const IMPORTED_EVIDENCE_TYPE: DiscourseNode = {
Expand Down Expand Up @@ -602,10 +603,9 @@ describe("what Obsidian sync sends to upsert_concepts", () => {

it("sends a node type as a schema Concept with its format, settings and template", () => {
expect(sentRow("claim-type")).toStrictEqual({
space_id: SPACE_ID,
source_local_id: "claim-type",
is_schema: true,
author_id: USER_ID,
author_local_id: ACCOUNT_LOCAL_ID,
name: "Claim",
description: "An assertion that evidence can support",
created: SCHEMA_CREATED,
Expand All @@ -628,10 +628,9 @@ describe("what Obsidian sync sends to upsert_concepts", () => {

it("sends template_content null for a node type with no template", () => {
expect(sentRow("source-type")).toStrictEqual({
space_id: SPACE_ID,
source_local_id: "source-type",
is_schema: true,
author_id: USER_ID,
author_local_id: ACCOUNT_LOCAL_ID,
name: "Source",
created: SCHEMA_CREATED,
last_modified: SCHEMA_MODIFIED,
Expand All @@ -646,10 +645,9 @@ describe("what Obsidian sync sends to upsert_concepts", () => {

it("sends an imported node type with its origin RID, authored by this space's user", () => {
expect(sentRow("evidence-type")).toStrictEqual({
space_id: SPACE_ID,
source_local_id: "evidence-type",
is_schema: true,
author_id: USER_ID,
author_local_id: ACCOUNT_LOCAL_ID,
name: "Evidence",
created: SCHEMA_CREATED,
last_modified: SCHEMA_MODIFIED,
Expand All @@ -665,10 +663,9 @@ describe("what Obsidian sync sends to upsert_concepts", () => {

it("sends a relation type as a schema Concept named by its label", () => {
expect(sentRow("cites-type")).toStrictEqual({
space_id: SPACE_ID,
source_local_id: "cites-type",
is_schema: true,
author_id: USER_ID,
author_local_id: ACCOUNT_LOCAL_ID,
name: "cites",
created: SCHEMA_CREATED,
last_modified: SCHEMA_MODIFIED,
Expand All @@ -683,10 +680,9 @@ describe("what Obsidian sync sends to upsert_concepts", () => {

it("sends an accepted imported relation type with its origin RID and without its status", () => {
expect(sentRow("opposes-type")).toStrictEqual({
space_id: SPACE_ID,
source_local_id: "opposes-type",
is_schema: true,
author_id: USER_ID,
author_local_id: ACCOUNT_LOCAL_ID,
name: "opposes",
created: SCHEMA_CREATED,
last_modified: SCHEMA_MODIFIED,
Expand All @@ -702,10 +698,9 @@ describe("what Obsidian sync sends to upsert_concepts", () => {

it("names a relation triple '<source type> -<label>-> <destination type>' and references its types", () => {
expect(sentRow("claim-cites-source")).toStrictEqual({
space_id: SPACE_ID,
source_local_id: "claim-cites-source",
is_schema: true,
author_id: USER_ID,
author_local_id: ACCOUNT_LOCAL_ID,
name: "Claim -cites-> Source",
created: SCHEMA_CREATED,
last_modified: SCHEMA_MODIFIED,
Expand All @@ -724,10 +719,9 @@ describe("what Obsidian sync sends to upsert_concepts", () => {

it("sends an accepted imported relation triple with its origin RID", () => {
expect(sentRow("claim-opposes-claim")).toStrictEqual({
space_id: SPACE_ID,
source_local_id: "claim-opposes-claim",
is_schema: true,
author_id: USER_ID,
author_local_id: ACCOUNT_LOCAL_ID,
name: "Claim -opposes-> Claim",
created: SCHEMA_CREATED,
last_modified: SCHEMA_MODIFIED,
Expand Down
135 changes: 63 additions & 72 deletions apps/obsidian/src/utils/conceptConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import type { DiscourseNodeInVault } from "./getDiscourseNodes";
import type { LocalConceptDataInput } from "@repo/database/inputTypes";
import type { ObsidianDiscourseNodeData } from "./syncDgNodesToSupabase";
import type { Json } from "@repo/database/dbTypes";
import {
crossAppNodeSchemaToDbConcept,
crossAppRelationTripleSchemaToDbConcept,
crossAppRelationTypeSchemaToDbConcept,
} from "@repo/database/lib/crossAppConverters";
import { extractContentFromTitle } from "@repo/database/lib/extractContentFromTitle";
import { SOURCE_SLOT } from "~/constants";
import {
nodeTypeToCrossApp,
relationTripleToCrossApp,
relationTypeToCrossApp,
} from "./obsidianToCrossAppConverters";

/**
* Get extra data (author, timestamps) from file metadata
Expand All @@ -31,12 +41,15 @@ const getNodeExtraData = (
};
};

// The shared converters build each schema row from its CrossApp type. The values
// set after them have no CrossApp field, so each one needs a field, or has to stop
// being stored, before sync can send CrossApp objects instead of rows.
export const discourseNodeSchemaToLocalConcept = ({
context,
accountLocalId,
node,
templateContent,
}: {
context: SupabaseContext;
accountLocalId: string;
node: DiscourseNode;
templateContent?: string;
}): LocalConceptDataInput => {
Expand All @@ -51,33 +64,32 @@ export const discourseNodeSchemaToLocalConcept = ({
importedFromRid,
...otherData
} = node;
const literal_content: Record<string, Json> = {
label: name,
format,
source_data: otherData,
};
if (template) literal_content.template = template;
literal_content.template_content = templateContent || null;
if (importedFromRid) literal_content.importedFromRid = importedFromRid;
const concept = crossAppNodeSchemaToDbConcept(
nodeTypeToCrossApp({
nodeType: { id, name, format, template, created, modified },
templateContent,
accountLocalId,
}),
);
return {
space_id: context.spaceId,
name,
source_local_id: id,
is_schema: true,
author_id: context.userId,
created: new Date(created).toISOString(),
last_modified: new Date(modified).toISOString(),
description: description,
literal_content,
...concept,
description,
literal_content: {
...(concept.literal_content as Record<string, Json>),
...(templateContent ? {} : { template_content: null }),
source_data: otherData,
...(importedFromRid ? { importedFromRid } : {}),
},
};
};

const STANDARD_ROLES = ["source", "destination"];

export const discourseRelationTypeToLocalConcept = (
context: SupabaseContext,
relationType: DiscourseRelationType,
): LocalConceptDataInput => {
export const discourseRelationTypeToLocalConcept = ({
accountLocalId,
relationType,
}: {
accountLocalId: string;
relationType: DiscourseRelationType;
}): LocalConceptDataInput => {
const {
id,
label,
Expand All @@ -89,71 +101,50 @@ export const discourseRelationTypeToLocalConcept = (
status, //destructuring status to not upload it to the database
...otherData
} = relationType;
const literal_content: Record<string, Json> = {
roles: STANDARD_ROLES,
label,
complement,
source_data: otherData,
};
if (importedFromRid) literal_content.importedFromRid = importedFromRid;

const concept = crossAppRelationTypeSchemaToDbConcept(
relationTypeToCrossApp({
relationType: { id, label, complement, created, modified },
accountLocalId,
}),
);
return {
space_id: context.spaceId,
name: label,
source_local_id: id,
is_schema: true,
author_id: context.userId,
created: new Date(created).toISOString(),
last_modified: new Date(modified).toISOString(),
literal_content,
...concept,
literal_content: {
...(concept.literal_content as Record<string, Json>),
source_data: otherData,
...(importedFromRid ? { importedFromRid } : {}),
},
};
};

export const discourseRelationTripleSchemaToLocalConcept = ({
context,
accountLocalId,
relation,
nodeTypesById,
relationTypesById,
}: {
context: SupabaseContext;
accountLocalId: string;
relation: DiscourseRelation;
nodeTypesById: Record<string, DiscourseNode>;
relationTypesById: Record<string, DiscourseRelationType>;
}): LocalConceptDataInput | null => {
const {
id,
relationshipTypeId,
sourceId,
destinationId,
created,
modified,
importedFromRid,
} = relation;
const { relationshipTypeId, sourceId, destinationId, importedFromRid } =
relation;
const sourceName = nodeTypesById[sourceId]?.name ?? sourceId;
const destinationName = nodeTypesById[destinationId]?.name ?? destinationId;
const relationType = relationTypesById[relationshipTypeId];
if (!relationType) return null;
const { label, complement } = relationType;
const literal_content: Record<string, Json> = {
roles: STANDARD_ROLES,
label,
complement,
};
if (importedFromRid) literal_content.importedFromRid = importedFromRid;

const concept = crossAppRelationTripleSchemaToDbConcept(
relationTripleToCrossApp({ relation, accountLocalId }),
relationTypeToCrossApp({ relationType, accountLocalId }),
);
if (concept === undefined) return null;
return {
space_id: context.spaceId,
name: `${sourceName} -${label}-> ${destinationName}`,
source_local_id: id,
is_schema: true,
author_id: context.userId,
created: new Date(created).toISOString(),
last_modified: new Date(modified).toISOString(),
literal_content,
local_reference_content: {
relation_type: relationshipTypeId,
source: sourceId,
destination: destinationId,
...concept,
name: `${sourceName} -${relationType.label}-> ${destinationName}`,
literal_content: {
...(concept.literal_content as Record<string, Json>),
...(importedFromRid ? { importedFromRid } : {}),
},
};
};
Expand Down
66 changes: 66 additions & 0 deletions apps/obsidian/src/utils/obsidianToCrossAppConverters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type {
CrossAppNodeSchema,
CrossAppRelationTripleSchema,
CrossAppRelationTypeSchema,
} from "@repo/database/crossAppContracts";
import type {
DiscourseNode,
DiscourseRelation,
DiscourseRelationType,
} from "~/types";

export const nodeTypeToCrossApp = ({
nodeType,
templateContent,
accountLocalId,
}: {
nodeType: Pick<
DiscourseNode,
"id" | "name" | "format" | "template" | "created" | "modified"
>;
templateContent?: string;
accountLocalId: string;
}): CrossAppNodeSchema => ({
localId: nodeType.id,
label: nodeType.name,
format: nodeType.format,
templateTitle: nodeType.template || undefined,
template: templateContent || undefined,
authorId: accountLocalId,
createdAt: new Date(nodeType.created),
modifiedAt: new Date(nodeType.modified),
});

export const relationTypeToCrossApp = ({
relationType,
accountLocalId,
}: {
relationType: Pick<
DiscourseRelationType,
"id" | "label" | "complement" | "created" | "modified"
>;
accountLocalId: string;
}): CrossAppRelationTypeSchema => ({
localId: relationType.id,
label: relationType.label,
complement: relationType.complement,
authorId: accountLocalId,
createdAt: new Date(relationType.created),
modifiedAt: new Date(relationType.modified),
});

export const relationTripleToCrossApp = ({
relation,
accountLocalId,
}: {
relation: DiscourseRelation;
accountLocalId: string;
}): CrossAppRelationTripleSchema => ({
localId: relation.id,
relation: relation.relationshipTypeId,
sourceType: relation.sourceId,
destinationType: relation.destinationId,
authorId: accountLocalId,
createdAt: new Date(relation.created),
modifiedAt: new Date(relation.modified),
});
6 changes: 3 additions & 3 deletions apps/obsidian/src/utils/syncDgNodesToSupabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ const convertDgToSupabaseConcepts = async ({
}
}
return discourseNodeSchemaToLocalConcept({
context,
accountLocalId,
node: nodeType,
templateContent,
});
Expand All @@ -802,7 +802,7 @@ const convertDgToSupabaseConcepts = async ({
missingSchemaIds.has(relationType.id),
)
.map((relationType) =>
discourseRelationTypeToLocalConcept(context, relationType),
discourseRelationTypeToLocalConcept({ accountLocalId, relationType }),
);

const discourseRelationTriplesToLocalConcepts = discourseRelations
Expand All @@ -821,7 +821,7 @@ const convertDgToSupabaseConcepts = async ({
)
.map((relation) =>
discourseRelationTripleSchemaToLocalConcept({
context,
accountLocalId,
relation,
nodeTypesById,
relationTypesById,
Expand Down
Loading
Loading