Skip to content

Commit 685651c

Browse files
committed
extract query logic for latest date container to dedicated query class
1 parent d181496 commit 685651c

3 files changed

Lines changed: 104 additions & 12 deletions

File tree

chats/rdflib/src/module/ChatsModuleRdfLib.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
import { generateId } from "@solid-data-modules/rdflib-utils/identifier";
1212
import { createChat } from "./update-operations/index.js";
1313
import { ChatQuery } from "./queries/index.js";
14-
import { MessagesDocumentQuery } from './queries/MessagesDocumentQuery.js';
14+
import { MessagesDocumentQuery } from "./queries/MessagesDocumentQuery.js";
15+
import { DateContainerQuery } from "./queries/DateContainerQuery.js";
1516

1617
interface ModuleConfig {
1718
store: IndexedFormula;
@@ -62,7 +63,11 @@ export class ChatsModuleRdfLib implements ChatsModule {
6263
if (!latestYear) return [];
6364

6465
const latestMonth = await this.fetchLatestSubContainer(latestYear);
66+
if (!latestMonth) return [];
67+
6568
const latestDay = await this.fetchLatestSubContainer(latestMonth);
69+
if (!latestDay) return [];
70+
6671
const messagesDocument = await this.fetchLatestDocument(latestDay);
6772

6873
return new MessagesDocumentQuery(
@@ -74,17 +79,7 @@ export class ChatsModuleRdfLib implements ChatsModule {
7479

7580
private async fetchLatestSubContainer(container: NamedNode) {
7681
await this.support.fetchNode(container);
77-
78-
const contents = new ContainerQuery(container, this.store).queryContents();
79-
const childContainers = contents.filter((it) => {
80-
return this.store.holds(
81-
it,
82-
rdf("type"),
83-
ldp("Container"),
84-
container.doc(),
85-
);
86-
});
87-
return childContainers[0]; // TODO actually get latest
82+
return new DateContainerQuery(container, this.store).queryLatest();
8883
}
8984

9085
private async fetchLatestDocument(container: NamedNode) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DateContainerQuery } from "./DateContainerQuery";
2+
import { graph, sym } from "rdflib";
3+
4+
describe(DateContainerQuery.name, () => {
5+
describe("query latest", () => {
6+
it("returns null if store is empty", () => {
7+
const store = graph();
8+
const result = new DateContainerQuery(
9+
sym("https://pod.test/container/"),
10+
store,
11+
).queryLatest();
12+
expect(result).toBe(null);
13+
});
14+
15+
it("returns the only container", () => {
16+
const store = graph();
17+
store.add(
18+
sym("https://pod.test/container/"),
19+
sym("http://www.w3.org/ns/ldp#contains"),
20+
sym("https://pod.test/container/2024/"),
21+
sym("https://pod.test/container/"),
22+
);
23+
store.add(
24+
sym("https://pod.test/container/2024/"),
25+
sym("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
26+
sym("http://www.w3.org/ns/ldp#Container"),
27+
sym("https://pod.test/container/"),
28+
);
29+
const result = new DateContainerQuery(
30+
sym("https://pod.test/container/"),
31+
store,
32+
).queryLatest();
33+
expect(result).toEqual(sym("https://pod.test/container/2024/"));
34+
});
35+
36+
it("returns null if all contents are not containers", () => {
37+
const store = graph();
38+
store.add(
39+
sym("https://pod.test/container/"),
40+
sym("http://www.w3.org/ns/ldp#contains"),
41+
sym("https://pod.test/container/2024"),
42+
sym("https://pod.test/container/"),
43+
);
44+
45+
const result = new DateContainerQuery(
46+
sym("https://pod.test/container/"),
47+
store,
48+
).queryLatest();
49+
expect(result).toEqual(null);
50+
});
51+
it("returns null if container type is in wrong document", () => {
52+
const store = graph();
53+
store.add(
54+
sym("https://pod.test/container/"),
55+
sym("http://www.w3.org/ns/ldp#contains"),
56+
sym("https://pod.test/container/2024/"),
57+
sym("https://pod.test/container/"),
58+
);
59+
store.add(
60+
sym("https://pod.test/container/2024/"),
61+
sym("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
62+
sym("http://www.w3.org/ns/ldp#Container"),
63+
sym("https://pod.test/wrong/"),
64+
);
65+
const result = new DateContainerQuery(
66+
sym("https://pod.test/container/"),
67+
store,
68+
).queryLatest();
69+
expect(result).toEqual(null);
70+
});
71+
});
72+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { IndexedFormula, NamedNode } from "rdflib";
2+
import { ContainerQuery, ldp, rdf } from "@solid-data-modules/rdflib-utils";
3+
4+
export class DateContainerQuery {
5+
constructor(
6+
private container: NamedNode,
7+
private store: IndexedFormula,
8+
) {}
9+
10+
queryLatest(): NamedNode | null {
11+
const contents = new ContainerQuery(
12+
this.container,
13+
this.store,
14+
).queryContents();
15+
const childContainers = contents.filter((it) => {
16+
return this.store.holds(
17+
it,
18+
rdf("type"),
19+
ldp("Container"),
20+
this.container.doc(),
21+
);
22+
});
23+
return childContainers[0] ?? null; // TODO actually get latest
24+
}
25+
}

0 commit comments

Comments
 (0)