Skip to content

Commit

Permalink
chats: insert message text, author & date to the document
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo-v committed Sep 10, 2024
1 parent b03b84b commit e3918f0
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
2 changes: 1 addition & 1 deletion chats/rdflib/src/e2e-tests/post-message.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("chats", () => {
authorWebId: "https://localhost:3456/profile/card#me",
});
const chat = await chats.readChat(chatUri);
expect(chat.latestMessages).toContain(
expect(chat.latestMessages).toContainEqual(
expect.objectContaining({
uri: messageUri,
authorWebId: "https://localhost:3456/profile/card#me",
Expand Down
10 changes: 7 additions & 3 deletions chats/rdflib/src/module/ChatsModuleRdfLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ChatQuery } from "./queries/index.js";
import { MessagesDocumentQuery } from "./queries/MessagesDocumentQuery.js";
import { DateContainerQuery } from "./queries/DateContainerQuery.js";
import { mintMessageUri } from "./uris/index.js";
import { postMessage } from './update-operations/post-message.js';
import { postMessage } from "./update-operations/post-message.js";

interface ModuleConfig {
store: IndexedFormula;
Expand Down Expand Up @@ -103,10 +103,14 @@ export class ChatsModuleRdfLib implements ChatsModule {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async postMessage({ chatUri }: PostMessageCommand): Promise<string> {
async postMessage({
chatUri,
text,
authorWebId,
}: PostMessageCommand): Promise<string> {
const chatNode = sym(chatUri);
const messageUri = mintMessageUri(chatNode);
const operation = postMessage(messageUri, chatNode);
const operation = postMessage(messageUri, text, authorWebId, chatNode);
await executeUpdate(this.fetcher, this.updater, operation);
return messageUri;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("post message", () => {

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

// and a chats module
Expand Down Expand Up @@ -64,6 +64,9 @@ _:patch
solid:inserts {
<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> .
<https://pod.test/alice/chats/abc123/2024/07/30/chat.ttl#8c615b> <http://rdfs.org/sioc/ns#content> "A new message" .
<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> .
<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> .
}; a solid:InsertDeletePatch .`,
);
});
Expand Down
3 changes: 3 additions & 0 deletions chats/rdflib/src/module/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export const dc = Namespace("http://purl.org/dc/elements/1.1/");
export const xsd = Namespace("http://www.w3.org/2001/XMLSchema#");
export const meeting = Namespace("http://www.w3.org/ns/pim/meeting#");
export const wf = Namespace("http://www.w3.org/2005/01/wf/flow#");
export const sioc = Namespace("http://rdfs.org/sioc/ns#");
export const foaf = Namespace("http://xmlns.com/foaf/0.1/");
export const dct = Namespace("http://purl.org/dc/terms/");
68 changes: 64 additions & 4 deletions chats/rdflib/src/module/update-operations/post-message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,78 @@
import { postMessage } from "./post-message";
import { st, sym } from "rdflib";
import { wf } from "../namespaces";
import { lit, st, sym } from "rdflib";
import { dct, foaf, sioc, wf } from "../namespaces";

describe("post message", () => {
it("inserts a link from chat to message", () => {
const result = postMessage(
"https://pod.test/chat/42/chat.ttl#1",
"https://pod.test/chat/42/chat.ttl#message-1",
"irrelevant",
"https://pod.test/irrelevant",
sym("https://pod.test/chat/42/index.ttl#this"),
);
expect(result.insertions).toContainEqual(
st(
sym("https://pod.test/chat/42/index.ttl#this"),
wf("message"),
sym("https://pod.test/chat/42/chat.ttl#1"),
sym("https://pod.test/chat/42/chat.ttl#message-1"),
sym("https://pod.test/chat/42/chat.ttl"),
),
);
});

it("inserts the message content", () => {
const result = postMessage(
"https://pod.test/chat/42/chat.ttl#message-1",
"the text to insert",
"https://pos.test/irrelevant",
sym("https://pod.test/chat/42/index.ttl#this"),
);
expect(result.insertions).toContainEqual(
st(
sym("https://pod.test/chat/42/chat.ttl#message-1"),
sioc("content"),
lit("the text to insert"),
sym("https://pod.test/chat/42/chat.ttl"),
),
);
});

it("inserts the current time as creation date", () => {
jest.useFakeTimers({
now: new Date("2024-09-08T07:06:05.432Z"),
});
const result = postMessage(
"https://pod.test/chat/42/chat.ttl#message-1",
"irrelevant",
"https://pos.test/irrelevant",
sym("https://pod.test/chat/42/index.ttl#this"),
);
expect(result.insertions).toContainEqual(
st(
sym("https://pod.test/chat/42/chat.ttl#message-1"),
dct("created"),
lit(
"2024-09-08T07:06:05.432Z",
undefined,
sym("http://www.w3.org/2001/XMLSchema#dateTime"),
),
sym("https://pod.test/chat/42/chat.ttl"),
),
);
});

it("inserts the author WebID", () => {
const result = postMessage(
"https://pod.test/chat/42/chat.ttl#message-1",
"irrelevant",
"https://pos.test/alice/profile/card#me",
sym("https://pod.test/chat/42/index.ttl#this"),
);
expect(result.insertions).toContainEqual(
st(
sym("https://pod.test/chat/42/chat.ttl#message-1"),
foaf("maker"),
sym("https://pos.test/alice/profile/card#me"),
sym("https://pod.test/chat/42/chat.ttl"),
),
);
Expand Down
18 changes: 15 additions & 3 deletions chats/rdflib/src/module/update-operations/post-message.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { NamedNode, st, sym } from "rdflib";
import { lit, NamedNode, st, sym } from "rdflib";
import { UpdateOperation } from "@solid-data-modules/rdflib-utils";
import { wf } from '../namespaces.js';
import { dct, foaf, sioc, wf, xsd } from "../namespaces.js";

export function postMessage(
messageUri: string,
text: string,
authorWebId: string,
chatNode: NamedNode,
): UpdateOperation {
const messageNode = sym(messageUri);
return {
insertions: [st(chatNode, wf("message"), messageNode, messageNode.doc())],
insertions: [
st(chatNode, wf("message"), messageNode, messageNode.doc()),
st(messageNode, sioc("content"), lit(text), messageNode.doc()),
st(
messageNode,
dct("created"),
lit(new Date().toISOString(), undefined, xsd("dateTime")),
messageNode.doc(),
),
st(messageNode, foaf("maker"), sym(authorWebId), messageNode.doc()),
],
deletions: [],
filesToCreate: [],
};
Expand Down

0 comments on commit e3918f0

Please sign in to comment.