Skip to content

Commit e3918f0

Browse files
committed
chats: insert message text, author & date to the document
1 parent b03b84b commit e3918f0

6 files changed

Lines changed: 94 additions & 12 deletions

File tree

chats/rdflib/src/e2e-tests/post-message.e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("chats", () => {
1010
authorWebId: "https://localhost:3456/profile/card#me",
1111
});
1212
const chat = await chats.readChat(chatUri);
13-
expect(chat.latestMessages).toContain(
13+
expect(chat.latestMessages).toContainEqual(
1414
expect.objectContaining({
1515
uri: messageUri,
1616
authorWebId: "https://localhost:3456/profile/card#me",

chats/rdflib/src/module/ChatsModuleRdfLib.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { ChatQuery } from "./queries/index.js";
1919
import { MessagesDocumentQuery } from "./queries/MessagesDocumentQuery.js";
2020
import { DateContainerQuery } from "./queries/DateContainerQuery.js";
2121
import { mintMessageUri } from "./uris/index.js";
22-
import { postMessage } from './update-operations/post-message.js';
22+
import { postMessage } from "./update-operations/post-message.js";
2323

2424
interface ModuleConfig {
2525
store: IndexedFormula;
@@ -103,10 +103,14 @@ export class ChatsModuleRdfLib implements ChatsModule {
103103
}
104104

105105
// eslint-disable-next-line @typescript-eslint/no-unused-vars
106-
async postMessage({ chatUri }: PostMessageCommand): Promise<string> {
106+
async postMessage({
107+
chatUri,
108+
text,
109+
authorWebId,
110+
}: PostMessageCommand): Promise<string> {
107111
const chatNode = sym(chatUri);
108112
const messageUri = mintMessageUri(chatNode);
109-
const operation = postMessage(messageUri, chatNode);
113+
const operation = postMessage(messageUri, text, authorWebId, chatNode);
110114
await executeUpdate(this.fetcher, this.updater, operation);
111115
return messageUri;
112116
}

chats/rdflib/src/module/_integration-tests/post-message.integration.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe("post message", () => {
1616

1717
// and today is a specific date
1818
jest.useFakeTimers({
19-
now: new Date("2024-07-30"),
19+
now: new Date("2024-07-30T03:04:05.678Z"),
2020
});
2121

2222
// and a chats module
@@ -64,6 +64,9 @@ _:patch
6464
6565
solid:inserts {
6666
<https://pod.test/alice/chats/abc123/index.ttl#this> <http://www.w3.org/2005/01/wf/flow#message> <https://pod.test/alice/chats/abc123/2024/07/30/chat.ttl#8c615b> .
67+
<https://pod.test/alice/chats/abc123/2024/07/30/chat.ttl#8c615b> <http://rdfs.org/sioc/ns#content> "A new message" .
68+
<https://pod.test/alice/chats/abc123/2024/07/30/chat.ttl#8c615b> <http://purl.org/dc/terms/created> "2024-07-30T03:04:05.678Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
69+
<https://pod.test/alice/chats/abc123/2024/07/30/chat.ttl#8c615b> <http://xmlns.com/foaf/0.1/maker> <https://pod.test/alice/profile/card#me> .
6770
}; a solid:InsertDeletePatch .`,
6871
);
6972
});

chats/rdflib/src/module/namespaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ export const dc = Namespace("http://purl.org/dc/elements/1.1/");
44
export const xsd = Namespace("http://www.w3.org/2001/XMLSchema#");
55
export const meeting = Namespace("http://www.w3.org/ns/pim/meeting#");
66
export const wf = Namespace("http://www.w3.org/2005/01/wf/flow#");
7+
export const sioc = Namespace("http://rdfs.org/sioc/ns#");
8+
export const foaf = Namespace("http://xmlns.com/foaf/0.1/");
9+
export const dct = Namespace("http://purl.org/dc/terms/");

chats/rdflib/src/module/update-operations/post-message.spec.ts

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,78 @@
11
import { postMessage } from "./post-message";
2-
import { st, sym } from "rdflib";
3-
import { wf } from "../namespaces";
2+
import { lit, st, sym } from "rdflib";
3+
import { dct, foaf, sioc, wf } from "../namespaces";
44

55
describe("post message", () => {
66
it("inserts a link from chat to message", () => {
77
const result = postMessage(
8-
"https://pod.test/chat/42/chat.ttl#1",
8+
"https://pod.test/chat/42/chat.ttl#message-1",
9+
"irrelevant",
10+
"https://pod.test/irrelevant",
911
sym("https://pod.test/chat/42/index.ttl#this"),
1012
);
1113
expect(result.insertions).toContainEqual(
1214
st(
1315
sym("https://pod.test/chat/42/index.ttl#this"),
1416
wf("message"),
15-
sym("https://pod.test/chat/42/chat.ttl#1"),
17+
sym("https://pod.test/chat/42/chat.ttl#message-1"),
18+
sym("https://pod.test/chat/42/chat.ttl"),
19+
),
20+
);
21+
});
22+
23+
it("inserts the message content", () => {
24+
const result = postMessage(
25+
"https://pod.test/chat/42/chat.ttl#message-1",
26+
"the text to insert",
27+
"https://pos.test/irrelevant",
28+
sym("https://pod.test/chat/42/index.ttl#this"),
29+
);
30+
expect(result.insertions).toContainEqual(
31+
st(
32+
sym("https://pod.test/chat/42/chat.ttl#message-1"),
33+
sioc("content"),
34+
lit("the text to insert"),
35+
sym("https://pod.test/chat/42/chat.ttl"),
36+
),
37+
);
38+
});
39+
40+
it("inserts the current time as creation date", () => {
41+
jest.useFakeTimers({
42+
now: new Date("2024-09-08T07:06:05.432Z"),
43+
});
44+
const result = postMessage(
45+
"https://pod.test/chat/42/chat.ttl#message-1",
46+
"irrelevant",
47+
"https://pos.test/irrelevant",
48+
sym("https://pod.test/chat/42/index.ttl#this"),
49+
);
50+
expect(result.insertions).toContainEqual(
51+
st(
52+
sym("https://pod.test/chat/42/chat.ttl#message-1"),
53+
dct("created"),
54+
lit(
55+
"2024-09-08T07:06:05.432Z",
56+
undefined,
57+
sym("http://www.w3.org/2001/XMLSchema#dateTime"),
58+
),
59+
sym("https://pod.test/chat/42/chat.ttl"),
60+
),
61+
);
62+
});
63+
64+
it("inserts the author WebID", () => {
65+
const result = postMessage(
66+
"https://pod.test/chat/42/chat.ttl#message-1",
67+
"irrelevant",
68+
"https://pos.test/alice/profile/card#me",
69+
sym("https://pod.test/chat/42/index.ttl#this"),
70+
);
71+
expect(result.insertions).toContainEqual(
72+
st(
73+
sym("https://pod.test/chat/42/chat.ttl#message-1"),
74+
foaf("maker"),
75+
sym("https://pos.test/alice/profile/card#me"),
1676
sym("https://pod.test/chat/42/chat.ttl"),
1777
),
1878
);

chats/rdflib/src/module/update-operations/post-message.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
import { NamedNode, st, sym } from "rdflib";
1+
import { lit, NamedNode, st, sym } from "rdflib";
22
import { UpdateOperation } from "@solid-data-modules/rdflib-utils";
3-
import { wf } from '../namespaces.js';
3+
import { dct, foaf, sioc, wf, xsd } from "../namespaces.js";
44

55
export function postMessage(
66
messageUri: string,
7+
text: string,
8+
authorWebId: string,
79
chatNode: NamedNode,
810
): UpdateOperation {
911
const messageNode = sym(messageUri);
1012
return {
11-
insertions: [st(chatNode, wf("message"), messageNode, messageNode.doc())],
13+
insertions: [
14+
st(chatNode, wf("message"), messageNode, messageNode.doc()),
15+
st(messageNode, sioc("content"), lit(text), messageNode.doc()),
16+
st(
17+
messageNode,
18+
dct("created"),
19+
lit(new Date().toISOString(), undefined, xsd("dateTime")),
20+
messageNode.doc(),
21+
),
22+
st(messageNode, foaf("maker"), sym(authorWebId), messageNode.doc()),
23+
],
1224
deletions: [],
1325
filesToCreate: [],
1426
};

0 commit comments

Comments
 (0)