|
2 | 2 |
|
3 | 3 | A chat module for solid written with LDO. |
4 | 4 |
|
| 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 | +``` |
5 | 238 |
|
6 | 239 |
|
7 | 240 | ## Funding |
|
0 commit comments