Skip to content

Commit 53609fd

Browse files
committed
Fix content type reader filters
1 parent 5e21302 commit 53609fd

3 files changed

Lines changed: 64 additions & 13 deletions

File tree

apps/obsidian/src/utils/importNodes.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ type PublishedNode = {
3131
authorId: number | undefined;
3232
};
3333

34+
type ObsidianNativeVariant = "direct" | "full";
35+
36+
const PLAIN_TEXT_CONTENT_TYPE = "text/plain";
37+
const OBSIDIAN_MARKDOWN_CONTENT_TYPE = "text/obsidian+markdown";
38+
39+
const getObsidianNativeContentType = (
40+
variant: ObsidianNativeVariant,
41+
): string =>
42+
variant === "direct"
43+
? PLAIN_TEXT_CONTENT_TYPE
44+
: OBSIDIAN_MARKDOWN_CONTENT_TYPE;
45+
46+
const isObsidianNativeContentRow = ({
47+
variant,
48+
content_type,
49+
}: {
50+
variant: string | null;
51+
content_type: string | null;
52+
}): boolean =>
53+
(variant === "direct" && content_type === PLAIN_TEXT_CONTENT_TYPE) ||
54+
(variant === "full" && content_type === OBSIDIAN_MARKDOWN_CONTENT_TYPE);
55+
3456
export const getPublishedNodesForGroups = async ({
3557
client,
3658
groupIds,
@@ -49,9 +71,14 @@ export const getPublishedNodesForGroups = async ({
4971
const { data, error } = await client
5072
.from("my_contents")
5173
.select(
52-
"source_local_id, space_id, text, created, last_modified, variant, metadata, author_id",
74+
"source_local_id, space_id, text, created, last_modified, variant, content_type, metadata, author_id",
5375
)
54-
.neq("space_id", currentSpaceId);
76+
.neq("space_id", currentSpaceId)
77+
.in("variant", ["direct", "full"])
78+
.in("content_type", [
79+
PLAIN_TEXT_CONTENT_TYPE,
80+
OBSIDIAN_MARKDOWN_CONTENT_TYPE,
81+
]);
5582

5683
if (error) {
5784
console.error("Error fetching published nodes:", error);
@@ -69,13 +96,15 @@ export const getPublishedNodesForGroups = async ({
6996
created: string | null;
7097
last_modified: string | null;
7198
variant: string | null;
99+
content_type: string | null;
72100
author_id: number | null;
73101
metadata: Json;
74102
};
75103

76104
const key = (r: Row) => `${r.space_id ?? ""}\t${r.source_local_id ?? ""}`;
77105
const groups = new Map<string, Row[]>();
78106
for (const row of data as Row[]) {
107+
if (!isObsidianNativeContentRow(row)) continue;
79108
if (row.source_local_id == null || row.space_id == null) continue;
80109
const k = key(row);
81110
if (!groups.has(k)) groups.set(k, []);
@@ -92,7 +121,10 @@ export const getPublishedNodesForGroups = async ({
92121
const latest = withDate.reduce((a, b) =>
93122
(a.last_modified ?? "") >= (b.last_modified ?? "") ? a : b,
94123
);
95-
const direct = rows.find((r) => r.variant === "direct");
124+
const direct = rows.find(
125+
(r) =>
126+
r.variant === "direct" && r.content_type === PLAIN_TEXT_CONTENT_TYPE,
127+
);
96128
const text = direct?.text ?? latest.text ?? "";
97129
const createdAt = latest.created
98130
? new Date(latest.created + "Z").valueOf()
@@ -250,6 +282,7 @@ export const fetchNodeContent = async ({
250282
.eq("source_local_id", nodeInstanceId)
251283
.eq("space_id", spaceId)
252284
.eq("variant", variant)
285+
.eq("content_type", getObsidianNativeContentType(variant))
253286
.maybeSingle();
254287

255288
if (error || !data || data.text == null) {
@@ -284,6 +317,7 @@ export const fetchNodeContentWithMetadata = async ({
284317
.eq("source_local_id", nodeInstanceId)
285318
.eq("space_id", spaceId)
286319
.eq("variant", variant)
320+
.eq("content_type", getObsidianNativeContentType(variant))
287321
.maybeSingle();
288322

289323
if (error || !data || data.text == null) {
@@ -325,10 +359,16 @@ const fetchNodeContentForImport = async ({
325359
} | null> => {
326360
const { data, error } = await client
327361
.from("my_contents")
328-
.select("text, created, last_modified, variant, metadata, author_id")
362+
.select(
363+
"text, created, last_modified, variant, content_type, metadata, author_id",
364+
)
329365
.eq("source_local_id", nodeInstanceId)
330366
.eq("space_id", spaceId)
331-
.in("variant", ["direct", "full"]);
367+
.in("variant", ["direct", "full"])
368+
.in("content_type", [
369+
PLAIN_TEXT_CONTENT_TYPE,
370+
OBSIDIAN_MARKDOWN_CONTENT_TYPE,
371+
]);
332372

333373
if (error) {
334374
console.error("Error fetching node content for import:", error);
@@ -341,10 +381,16 @@ const fetchNodeContentForImport = async ({
341381
last_modified: string | null;
342382
author_id: number | null;
343383
variant: string | null;
384+
content_type: string | null;
344385
metadata: Json;
345386
}>;
346-
const direct = rows.find((r) => r.variant === "direct");
347-
const full = rows.find((r) => r.variant === "full");
387+
const direct = rows.find(
388+
(r) => r.variant === "direct" && r.content_type === PLAIN_TEXT_CONTENT_TYPE,
389+
);
390+
const full = rows.find(
391+
(r) =>
392+
r.variant === "full" && r.content_type === OBSIDIAN_MARKDOWN_CONTENT_TYPE,
393+
);
348394
const authorId = full?.author_id ?? direct?.author_id ?? null;
349395

350396
if (
@@ -395,6 +441,7 @@ export const getSourceContentDates = async ({
395441
.eq("source_local_id", nodeInstanceId)
396442
.eq("space_id", spaceId)
397443
.eq("variant", "direct")
444+
.eq("content_type", PLAIN_TEXT_CONTENT_TYPE)
398445
.maybeSingle();
399446
if (error || !data) return null;
400447
return {
@@ -1525,6 +1572,7 @@ export const refreshImportedFile = async ({
15251572
.eq("space_id", spaceId)
15261573
.eq("source_local_id", frontmatter.nodeInstanceId)
15271574
.eq("variant", "direct")
1575+
.eq("content_type", PLAIN_TEXT_CONTENT_TYPE)
15281576
.maybeSingle();
15291577
const metadata = metadataResp.data?.metadata;
15301578
const filePath: string | undefined =

apps/obsidian/src/utils/publishNode.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import type { DiscourseNodeInVault } from "./getDiscourseNodes";
2323
import type { SupabaseContext } from "./supabaseContext";
2424
import type { TablesInsert } from "@repo/database/dbTypes";
2525

26+
const OBSIDIAN_MARKDOWN_CONTENT_TYPE = "text/obsidian+markdown";
27+
2628
export const getPublishedToGroups = (
2729
frontmatter: FrontMatterCache | Record<string, unknown>,
2830
): string[] => {
@@ -428,6 +430,7 @@ export const publishNodeToGroup = async ({
428430
.eq("source_local_id", nodeId)
429431
.eq("space_id", spaceId)
430432
.eq("variant", "full")
433+
.eq("content_type", OBSIDIAN_MARKDOWN_CONTENT_TYPE)
431434
.maybeSingle();
432435
if (idResponse.error || !idResponse.data) {
433436
throw idResponse.error || new Error("no data while fetching node");

packages/database/features/step-definitions/stepdefs.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,14 @@ Then(
308308
},
309309
);
310310

311+
/* eslint-disable max-params -- Cucumber inspects function.length for step arity. */
311312
Then(
312313
"a user logged in space {word} should see {int} content rows with variant {string} and content type {string}",
313314
async (
314-
...[spaceName, expectedCount, variant, contentType]: [
315-
string,
316-
number,
317-
ContentVariant,
318-
string,
319-
]
315+
spaceName: string,
316+
expectedCount: number,
317+
variant: ContentVariant,
318+
contentType: string,
320319
) => {
321320
const localRefs = (world.localRefs || {}) as LocalRefsType;
322321
const spaceId = localRefs[spaceName];
@@ -331,6 +330,7 @@ Then(
331330
assert.equal(response.count, expectedCount);
332331
},
333332
);
333+
/* eslint-enable max-params */
334334

335335
// invoke the upsert_accounts_in_space function, expects json
336336
Given(

0 commit comments

Comments
 (0)