Skip to content

Commit 2120db0

Browse files
committed
chats: query actual message data
1 parent c2775e4 commit 2120db0

2 files changed

Lines changed: 176 additions & 10 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { MessagesDocumentQuery } from "./MessagesDocumentQuery";
2+
import { graph, parse, sym } from "rdflib";
3+
4+
describe(MessagesDocumentQuery.name, () => {
5+
describe("query messages", () => {
6+
it("returns empty array if store is empty", () => {
7+
const result = new MessagesDocumentQuery(
8+
sym("https://pod.example/chat/1/index.ttl#this"),
9+
sym("https://pod.example/chat/1/2024/07/30/chat.ttl"),
10+
graph(),
11+
).queryMessages();
12+
expect(result).toEqual([]);
13+
});
14+
15+
it("returns the message linked to the chat", () => {
16+
const store = graph();
17+
parse(
18+
`@prefix wf: <http://www.w3.org/2005/01/wf/flow#> .
19+
20+
<https://pod.example/chat/1/index.ttl#this>
21+
wf:message <#message-1> .
22+
23+
<#message-1>
24+
<http://rdfs.org/sioc/ns#content> "A chat message" ;
25+
<http://purl.org/dc/terms/created> "2024-07-01T17:47:14Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;
26+
<http://xmlns.com/foaf/0.1/maker> <http://localhost:3000/alice/profile/card#me> ;
27+
.
28+
`,
29+
store,
30+
"https://pod.example/chat/1/2024/07/30/chat.ttl",
31+
);
32+
33+
const result = new MessagesDocumentQuery(
34+
sym("https://pod.example/chat/1/index.ttl#this"),
35+
sym("https://pod.example/chat/1/2024/07/30/chat.ttl"),
36+
store,
37+
).queryMessages();
38+
expect(result).toEqual([
39+
{
40+
uri: "https://pod.example/chat/1/2024/07/30/chat.ttl#message-1",
41+
text: "A chat message",
42+
date: new Date("2024-07-01T17:47:14Z"),
43+
authorWebId: "http://localhost:3000/alice/profile/card#me",
44+
},
45+
]);
46+
});
47+
48+
it("returns all messages linked to the chat", () => {
49+
const store = graph();
50+
parse(
51+
`@prefix wf: <http://www.w3.org/2005/01/wf/flow#> .
52+
53+
<https://pod.example/chat/1/index.ttl#this>
54+
wf:message <#message-1>, <#message-2> .
55+
56+
<#message-1>
57+
<http://rdfs.org/sioc/ns#content> "First message" ;
58+
<http://purl.org/dc/terms/created> "2024-07-01T17:47:14Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;
59+
<http://xmlns.com/foaf/0.1/maker> <http://localhost:3000/alice/profile/card#me> .
60+
61+
<#message-2>
62+
<http://rdfs.org/sioc/ns#content> "Second message" ;
63+
<http://purl.org/dc/terms/created> "2024-07-01T17:48:14Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;
64+
<http://xmlns.com/foaf/0.1/maker> <http://localhost:3000/bob/profile/card#me> ;
65+
.
66+
`,
67+
store,
68+
"https://pod.example/chat/1/2024/07/30/chat.ttl",
69+
);
70+
71+
const result = new MessagesDocumentQuery(
72+
sym("https://pod.example/chat/1/index.ttl#this"),
73+
sym("https://pod.example/chat/1/2024/07/30/chat.ttl"),
74+
store,
75+
).queryMessages();
76+
expect(result).toEqual([
77+
{
78+
uri: "https://pod.example/chat/1/2024/07/30/chat.ttl#message-1",
79+
text: "First message",
80+
date: new Date("2024-07-01T17:47:14Z"),
81+
authorWebId: "http://localhost:3000/alice/profile/card#me",
82+
},
83+
{
84+
uri: "https://pod.example/chat/1/2024/07/30/chat.ttl#message-2",
85+
text: "Second message",
86+
date: new Date("2024-07-01T17:48:14Z"),
87+
authorWebId: "http://localhost:3000/bob/profile/card#me",
88+
},
89+
]);
90+
});
91+
92+
describe("ignores messages", () => {
93+
it("that are not linked to the chat", () => {
94+
const store = graph();
95+
parse(
96+
`@prefix wf: <http://www.w3.org/2005/01/wf/flow#> .
97+
98+
<#message-1>
99+
<http://rdfs.org/sioc/ns#content> "A chat message" ;
100+
<http://purl.org/dc/terms/created> "2024-07-01T17:47:14Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;
101+
<http://xmlns.com/foaf/0.1/maker> <http://localhost:3000/alice/profile/card#me> ;
102+
.
103+
`,
104+
store,
105+
"https://pod.example/chat/1/2024/07/30/chat.ttl",
106+
);
107+
108+
const result = new MessagesDocumentQuery(
109+
sym("https://pod.example/chat/1/index.ttl#this"),
110+
sym("https://pod.example/chat/1/2024/07/30/chat.ttl"),
111+
store,
112+
).queryMessages();
113+
expect(result).toEqual([]);
114+
});
115+
116+
it("that are linked to the chat in the wrong document", () => {
117+
const store = graph();
118+
parse(
119+
`@prefix wf: <http://www.w3.org/2005/01/wf/flow#> .
120+
121+
<https://pod.example/chat/1/index.ttl#this>
122+
wf:message <chat.ttl#message-1> .`,
123+
store,
124+
"https://pod.example/chat/1/2024/07/30/wrong.ttl",
125+
);
126+
parse(
127+
`@prefix wf: <http://www.w3.org/2005/01/wf/flow#> .
128+
129+
<#message-1>
130+
<http://rdfs.org/sioc/ns#content> "A chat message" ;
131+
<http://purl.org/dc/terms/created> "2024-07-01T17:47:14Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;
132+
<http://xmlns.com/foaf/0.1/maker> <http://localhost:3000/alice/profile/card#me> ;
133+
.
134+
`,
135+
store,
136+
"https://pod.example/chat/1/2024/07/30/chat.ttl",
137+
);
138+
139+
const result = new MessagesDocumentQuery(
140+
sym("https://pod.example/chat/1/index.ttl#this"),
141+
sym("https://pod.example/chat/1/2024/07/30/chat.ttl"),
142+
store,
143+
).queryMessages();
144+
expect(result).toEqual([]);
145+
});
146+
});
147+
});
148+
});
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { IndexedFormula, NamedNode } from "rdflib";
2-
import { Message } from '../../index.js';
3-
import { wf } from '../namespaces.js';
1+
import { IndexedFormula, NamedNode, sym } from "rdflib";
2+
import { Message } from "../../index.js";
3+
import { wf } from "../namespaces.js";
44

55
export class MessagesDocumentQuery {
66
constructor(
@@ -14,12 +14,30 @@ export class MessagesDocumentQuery {
1414
.each(this.chatNode, wf("message"), undefined, this.messagesDocument)
1515
.map((it) => it as NamedNode);
1616

17-
// TODO query actual message
18-
return messages.map((it) => ({
19-
uri: it.uri,
20-
text: "Hello visitor, welcome to my public chat lobby!",
21-
date: new Date("2024-07-01T17:47:14Z"),
22-
authorWebId: "http://localhost:3000/alice/profile/card#me",
23-
}));
17+
return messages.map((messageNode) => this.queryMessage(messageNode));
18+
}
19+
20+
private queryMessage(messageNode: NamedNode) {
21+
const text =
22+
this.store.anyValue(
23+
messageNode,
24+
sym("http://rdfs.org/sioc/ns#content"),
25+
) ?? "";
26+
27+
const date = this.store.anyJS(
28+
messageNode,
29+
sym("http://purl.org/dc/terms/created"),
30+
);
31+
const authorWebId =
32+
this.store.anyValue(
33+
messageNode,
34+
sym("http://xmlns.com/foaf/0.1/maker"),
35+
) ?? "";
36+
return {
37+
uri: messageNode.uri,
38+
text,
39+
date,
40+
authorWebId,
41+
};
2442
}
2543
}

0 commit comments

Comments
 (0)