Skip to content

Commit c0bb51c

Browse files
committed
Added docs
1 parent 6e71238 commit c0bb51c

3 files changed

Lines changed: 239 additions & 5 deletions

File tree

chats/ldo/README.md

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,239 @@
22

33
A chat module for solid written with LDO.
44

5+
Okay, I can help you update the README.md file with installation and usage documentation.
6+
7+
Here's an updated version of your README.md:
8+
Markdown
9+
10+
# Chats - Data Module (LDO)
11+
12+
A chat module for Solid written with LDO.
13+
14+
## Installation
15+
16+
To install this library, use npm:
17+
18+
```bash
19+
npm install @solid-data-modules/chats-ldo @ldo/connected-solid @ldo/connected @ldo/ldo
20+
```
21+
22+
## Usage
23+
24+
Here's how you can use the @solid-data-modules/chats-ldo library to manage chats in a Solid Pod.
25+
Importing
26+
27+
First, import the necessary components from the library:
28+
```TypeScript
29+
30+
import { Chat, ChatShape } from "@solid-data-modules/chats-ldo";
31+
import { createSolidLdoDataset, SolidConnectedPlugin } from "@ldo/connected-solid";
32+
import { ConnectedLdoDataset } from "@ldo/connected";
33+
```
34+
35+
### Initialization
36+
37+
You'll need to create a SolidLdoDataset instance. This dataset will be used to interact with Solid Pods.
38+
39+
```TypeScript
40+
const dataset: ConnectedLdoDataset<SolidConnectedPlugin[]> = createSolidLdoDataset();
41+
```
42+
43+
### Creating a Chat Instance
44+
45+
To work with a chat, create an instance of the Chat class by providing the container URI where the chat data is or will be stored, and the dataset instance.
46+
47+
```TypeScript
48+
const chatContainerUri = "http://localhost:3003/your-chat-container/"; // Replace with your chat container URI
49+
const chat = new Chat(chatContainerUri, dataset);
50+
```
51+
52+
### Creating a New Chat on a Pod
53+
54+
If the chat doesn't exist yet, you can create it.
55+
56+
```TypeScript
57+
const webId = "http://example.com/profile/card#me"; // Replace with the author's WebID
58+
59+
async function initializeChat() {
60+
try {
61+
const newChatInfo: ChatShape = {
62+
"@id": `${chatContainerUri}index.ttl#this`, // Optional: specify the ID of the chat resource
63+
type: { "@id": "LongChat" }, // Specifies the type of chat
64+
author: { "@id": webId },
65+
created: new Date().toISOString(),
66+
title: "My Awesome Chat",
67+
};
68+
await chat.createChat(newChatInfo);
69+
console.log("Chat created successfully!");
70+
} catch (error) {
71+
console.error("Error creating chat:", error);
72+
}
73+
}
74+
75+
initializeChat();
76+
```
77+
78+
For the ChatShape properties like type and author, you typically provide an object with an @id property pointing to the respective RDF term or WebID.
79+
80+
### Getting Chat Information
81+
82+
You can retrieve the information of an existing chat.
83+
84+
```TypeScript
85+
async function logChatInfo() {
86+
try {
87+
const chatInfo = await chat.getChatInfo();
88+
console.log("Chat Title:", chatInfo.title);
89+
console.log("Chat Author:", chatInfo.author?.["@id"]);
90+
console.log("Chat Created:", chatInfo.created);
91+
} catch (error) {
92+
console.error("Error getting chat info:", error);
93+
}
94+
}
95+
96+
logChatInfo();
97+
```
98+
99+
### Updating Chat Information
100+
101+
You can update the properties of a chat.
102+
103+
```TypeScript
104+
async function updateChatTitle() {
105+
try {
106+
await chat.setChatInfo({
107+
title: "My Updated Awesome Chat",
108+
});
109+
console.log("Chat title updated!");
110+
const updatedInfo = await chat.getChatInfo();
111+
console.log("New Chat Title:", updatedInfo.title);
112+
} catch (error) {
113+
console.error("Error updating chat info:", error);
114+
}
115+
}
116+
117+
updateChatTitle();
118+
```
119+
120+
### Sending a Message
121+
122+
Send a new message to the chat.
123+
124+
```TypeScript
125+
async function postMessage() {
126+
try {
127+
const senderWebId = "[http://example.com/another-profile/card#me](http://example.com/another-profile/card#me)"; // Replace with sender's WebID
128+
await chat.sendMessage("Hello Solid World!", senderWebId);
129+
console.log("Message sent!");
130+
} catch (error) {
131+
console.error("Error sending message:", error);
132+
}
133+
}
134+
135+
postMessage();
136+
```
137+
138+
### Iterating Through Messages
139+
140+
You can iterate through the messages in a chat. The iterator returns messages in groups, typically by the day they were posted, from most recent to oldest.
141+
142+
```TypeScript
143+
async function readMessages() {
144+
try {
145+
const messageIterator = chat.getMessageIterator();
146+
console.log("Chat Messages (most recent first):");
147+
for await (const messageGroup of messageIterator) {
148+
// Messages in messageGroup are sorted from most recent to least recent for that specific day/resource
149+
messageGroup.forEach(message => {
150+
console.log(`- [${message.created2}] ${message.maker?.["@id"]}: ${message.content}`);
151+
});
152+
}
153+
} catch (error) {
154+
console.error("Error reading messages:", error);
155+
}
156+
}
157+
158+
readMessages();
159+
```
160+
161+
### Subscribing to New Messages
162+
163+
You can subscribe to receive real-time updates for new messages.
164+
165+
```TypeScript
166+
async function subscribeToChatMessages() {
167+
try {
168+
console.log("Subscribing to new messages...");
169+
await chat.subscribeToMessages((newMessages) => {
170+
console.log("New message(s) received:", newMessages.length);
171+
newMessages.forEach(message => {
172+
console.log(`> [${message.created2}] ${message.maker?.["@id"]}: ${message.content}`);
173+
});
174+
});
175+
console.log("Now listening for incoming messages.");
176+
// Keep the process alive to receive messages, or integrate into your app's lifecycle.
177+
} catch (error) {
178+
console.error("Error subscribing to messages:", error);
179+
}
180+
}
181+
182+
subscribeToChatMessages();
183+
184+
// To stop listening:
185+
// await chat.unsubscribeFromMessages();
186+
// console.log("Unsubscribed from messages.");
187+
```
188+
189+
The subscribeToMessages method sets up a WebSocket connection to the message resource of the current day and listens for updates. It automatically handles transitioning to a new day's message resource.
190+
191+
### Removing a Message
192+
193+
You can remove a specific message by its ID.
194+
195+
```TypeScript
196+
async function deleteMessage(messageId: string) {
197+
try {
198+
await chat.removeMessage(messageId);
199+
console.log(`Message ${messageId} removed.`);
200+
} catch (error) {
201+
console.error("Error removing message:", error);
202+
}
203+
}
204+
205+
// Example: deleteMessage("http://localhost:3003/your-chat-container/2024/05/25/index.ttl#some-message-uuid");
206+
```
207+
208+
### Deleting a Chat
209+
210+
You can delete the entire chat container and its contents.
211+
212+
```TypeScript
213+
async function removeChat() {
214+
try {
215+
await chat.deleteChat();
216+
console.log("Chat deleted successfully.");
217+
} catch (error) {
218+
console.error("Error deleting chat:", error);
219+
}
220+
}
221+
222+
// removeChat();
223+
```
224+
225+
### Cleaning Up
226+
227+
When you are done with a Chat instance, especially if you've used subscriptions, call the destroy method to clean up any listeners or timers.
228+
229+
```TypeScript
230+
async function cleanupChat() {
231+
await chat.destroy();
232+
console.log("Chat instance cleaned up.");
233+
}
234+
235+
// Call this when the chat instance is no longer needed, e.g., when a component unmounts.
236+
// cleanupChat();
237+
```
5238

6239

7240
## Funding

chats/ldo/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from "./Chat.js";
1+
export * from "./Chat.js";
2+
export * from "./.ldo/longChat.shapeTypes.js"

chats/ldo/test/integration.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { setupServer } from "@ldo/test-solid-server";
33
import { testFiles } from "./testFiles.helper";
44
import path from "path";
55
import { fileURLToPath } from "url";
6-
import { ConnectedLdoDataset, createConnectedLdoDataset } from "@ldo/connected";
7-
import { solidConnectedPlugin, SolidConnectedPlugin } from "@ldo/connected-solid";
6+
import { ConnectedLdoDataset } from "@ldo/connected";
7+
import { createSolidLdoDataset, SolidConnectedPlugin } from "@ldo/connected-solid";
88
import { Chat } from "../src";
99
import { ChatMessageShape, ChatShape } from "../src/.ldo/longChat.typings";
1010

@@ -74,7 +74,7 @@ describe("integration", () => {
7474
let sample1Chat: Chat;
7575

7676
beforeEach(() => {
77-
dataset = createConnectedLdoDataset([solidConnectedPlugin]);
77+
dataset = createSolidLdoDataset();
7878
sample1Chat = new Chat(SAMPLE_CHAT_1_CONTAINER_URI, dataset);
7979
});
8080

@@ -125,7 +125,7 @@ describe("integration", () => {
125125
expect(chatInfo2.title).toBe("Uncool Chat");
126126
});
127127

128-
it.only("Sends a message to a chat", async () => {
128+
it("Sends a message to a chat", async () => {
129129
const sample3Chat = new Chat(`${BASE_URI}sample-chat-3/`, dataset);
130130
await sample3Chat.createChat({
131131
type: { "@id": "LongChat" },

0 commit comments

Comments
 (0)