feature/IO-3000-messaging-sockets-migrations2 -

- sync send
- fix status events

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-11-20 19:23:35 -08:00
parent 06afd6da5b
commit 15151cb4ac
4 changed files with 92 additions and 30 deletions

View File

@@ -1,4 +1,5 @@
import { CONVERSATION_LIST_QUERY, GET_CONVERSATION_DETAILS } from "../../graphql/conversations.queries";
import { gql } from "@apollo/client";
export const registerMessagingHandlers = ({ socket, client }) => {
if (!(socket && client)) return;
@@ -14,8 +15,6 @@ export const registerMessagingHandlers = ({ socket, client }) => {
const fullConversation = {
...newConversation,
phone_num: newConversation.phone_num,
id: newConversation.id,
updated_at: newConversation.updated_at || new Date().toISOString(),
unreadcnt: newConversation.unreadcnt || 0,
archived: newConversation.archived || false,
@@ -77,21 +76,43 @@ export const registerMessagingHandlers = ({ socket, client }) => {
};
const handleMessageChanged = (message) => {
// Find the message in the cache and update all fields dynamically
client.cache.modify({
id: client.cache.identify({
__typename: "messages",
id: message.id
}),
id: client.cache.identify({ __typename: "conversations", id: message.conversationid }),
fields: {
// Dynamically update all fields based on the incoming message object
__typename: (existingType) => existingType || "messages", // Ensure __typename is preserved
...Object.fromEntries(
Object.entries(message).map(([key, value]) => [
key,
(cached) => (value !== undefined ? value : cached) // Update with new value or keep existing
])
)
...(message.type === "status-changed" && {
messages(existing = [], { readField }) {
return existing.map((messageRef) => {
// Match the message by ID
if (readField("id", messageRef) === message.id) {
const currentStatus = readField("status", messageRef);
// Prevent overwriting if the current status is already "delivered"
if (currentStatus === "delivered") {
return messageRef;
}
// Update the existing message fields
return client.cache.writeFragment({
id: messageRef.__ref,
fragment: gql`
fragment UpdatedMessage on messages {
id
status
conversationid
__typename
}
`,
data: {
__typename: "messages",
...message // Only update the fields provided in the message object
}
});
}
return messageRef; // Keep other messages unchanged
});
}
})
}
});
};
@@ -113,6 +134,7 @@ export const registerMessagingHandlers = ({ socket, client }) => {
client.cache.modify({
id: cacheId,
fields: {
// This is a catch-all for just sending it fields off conversation
...Object.fromEntries(
Object.entries(fields).map(([key, value]) => [
key,
@@ -138,6 +160,30 @@ export const registerMessagingHandlers = ({ socket, client }) => {
});
};
const handleNewMessage = ({ conversationId, message }) => {
client.cache.modify({
id: client.cache.identify({ __typename: "conversations", id: conversationId }),
fields: {
messages(existing = []) {
const newMessageRef = client.cache.writeFragment({
data: message,
fragment: gql`
fragment NewMessage on messages {
id
body
createdAt
selectedMedia
imexshopid
}
`
});
return [...existing, newMessageRef];
}
}
});
};
socket.on("new-message", handleNewMessage);
socket.on("new-message-summary", handleNewMessageSummary);
socket.on("new-message-detailed", handleNewMessageDetailed);
socket.on("message-changed", handleMessageChanged);
@@ -146,9 +192,9 @@ export const registerMessagingHandlers = ({ socket, client }) => {
export const unregisterMessagingHandlers = ({ socket }) => {
if (!socket) return;
socket.off("new-message");
socket.off("new-message-summary");
socket.off("new-message-detailed");
socket.off("message-changed");
socket.off("message-changed");
socket.off("conversation-changed");
};