feature/IO-3000-messaging-sockets-migrations2 -
- Checkpoint, Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -142,14 +142,47 @@ export const registerMessagingHandlers = ({ socket, client }) => {
|
||||
});
|
||||
};
|
||||
|
||||
const handleConversationChanged = (data) => {
|
||||
const handleConversationChanged = async (data) => {
|
||||
if (!data) return;
|
||||
|
||||
const { conversationId, type, job_conversations, ...fields } = data;
|
||||
|
||||
logLocal("handleConversationChanged", data);
|
||||
|
||||
// Identify the conversation in the Apollo cache
|
||||
const updatedAt = new Date().toISOString();
|
||||
|
||||
const updateConversationList = (newConversation) => {
|
||||
try {
|
||||
const existingList = client.cache.readQuery({
|
||||
query: CONVERSATION_LIST_QUERY,
|
||||
variables: { offset: 0 }
|
||||
});
|
||||
|
||||
const updatedList = existingList?.conversations
|
||||
? [
|
||||
newConversation,
|
||||
...existingList.conversations.filter(
|
||||
(conv) => conv.id !== newConversation.id // Prevent duplicates
|
||||
)
|
||||
]
|
||||
: [newConversation];
|
||||
|
||||
client.cache.writeQuery({
|
||||
query: CONVERSATION_LIST_QUERY,
|
||||
variables: { offset: 0 },
|
||||
data: {
|
||||
conversations: updatedList
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error updating conversation list in the cache:", error);
|
||||
}
|
||||
};
|
||||
|
||||
if (type === "conversation-created") {
|
||||
updateConversationList({ ...fields, job_conversations, updated_at: updatedAt });
|
||||
return;
|
||||
}
|
||||
|
||||
const cacheId = client.cache.identify({
|
||||
__typename: "conversations",
|
||||
id: conversationId
|
||||
@@ -161,20 +194,20 @@ export const registerMessagingHandlers = ({ socket, client }) => {
|
||||
}
|
||||
|
||||
if (type === "conversation-archived") {
|
||||
// Remove all messages associated with this conversation
|
||||
const messageRefs = client.cache.readFragment({
|
||||
id: cacheId,
|
||||
fragment: gql`
|
||||
fragment ConversationMessages on conversations {
|
||||
messages {
|
||||
id
|
||||
try {
|
||||
// Evict messages associated with the conversation
|
||||
const messageRefs = client.cache.readFragment({
|
||||
id: cacheId,
|
||||
fragment: gql`
|
||||
fragment ConversationMessages on conversations {
|
||||
messages {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
});
|
||||
`
|
||||
});
|
||||
|
||||
if (messageRefs?.messages) {
|
||||
messageRefs.messages.forEach((message) => {
|
||||
messageRefs?.messages?.forEach((message) => {
|
||||
const messageCacheId = client.cache.identify({
|
||||
__typename: "messages",
|
||||
id: message.id
|
||||
@@ -183,39 +216,84 @@ export const registerMessagingHandlers = ({ socket, client }) => {
|
||||
client.cache.evict({ id: messageCacheId });
|
||||
}
|
||||
});
|
||||
|
||||
// Evict the conversation itself
|
||||
client.cache.evict({ id: cacheId });
|
||||
client.cache.gc(); // Trigger garbage collection
|
||||
} catch (error) {
|
||||
console.error("Error archiving conversation:", error);
|
||||
}
|
||||
|
||||
// Evict the conversation itself
|
||||
client.cache.evict({ id: cacheId });
|
||||
client.cache.gc(); // Trigger garbage collection to clean up unused entries
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "conversation-unarchived") {
|
||||
try {
|
||||
// Fetch the conversation from the database if not already in the cache
|
||||
const existingConversation = client.cache.readQuery({
|
||||
query: GET_CONVERSATION_DETAILS,
|
||||
variables: { conversationId }
|
||||
});
|
||||
|
||||
if (!existingConversation) {
|
||||
const { data: fetchedData } = await client.query({
|
||||
query: GET_CONVERSATION_DETAILS,
|
||||
variables: { conversationId },
|
||||
fetchPolicy: "network-only"
|
||||
});
|
||||
|
||||
if (fetchedData?.conversations_by_pk) {
|
||||
const conversationData = fetchedData.conversations_by_pk;
|
||||
|
||||
// Enrich conversation data
|
||||
const enrichedConversation = {
|
||||
...conversationData,
|
||||
messages_aggregate: {
|
||||
__typename: "messages_aggregate",
|
||||
aggregate: {
|
||||
__typename: "messages_aggregate_fields",
|
||||
count: conversationData.messages.filter((message) => !message.read && !message.isoutbound).length
|
||||
}
|
||||
},
|
||||
updated_at: updatedAt
|
||||
};
|
||||
|
||||
updateConversationList(enrichedConversation);
|
||||
}
|
||||
}
|
||||
|
||||
// Mark the conversation as unarchived in the cache
|
||||
client.cache.modify({
|
||||
id: cacheId,
|
||||
fields: {
|
||||
archived: () => false,
|
||||
updated_at: () => updatedAt
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error unarchiving conversation:", error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle other types of updates (e.g., marked read, tags added/removed)
|
||||
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,
|
||||
(cached) => (value !== undefined ? value : cached) // Update with new value or keep existing
|
||||
])
|
||||
Object.entries(fields).map(([key, value]) => [key, (cached) => (value !== undefined ? value : cached)])
|
||||
),
|
||||
...(type === "conversation-marked-read" && {
|
||||
messages_aggregate: () => ({
|
||||
aggregate: { count: 0 } // Reset unread count
|
||||
__typename: "messages_aggregate",
|
||||
aggregate: { __typename: "messages_aggregate_fields", count: 0 }
|
||||
})
|
||||
}),
|
||||
...(type === "tag-added" && {
|
||||
job_conversations: (existing = []) => {
|
||||
// Merge existing job_conversations with new ones
|
||||
return [...existing, ...job_conversations];
|
||||
}
|
||||
job_conversations: (existing = []) => [...existing, ...job_conversations]
|
||||
}),
|
||||
...(type === "tag-removed" && {
|
||||
job_conversations: (existing = [], { readField }) =>
|
||||
existing.filter((jobConversationRef) => readField("jobid", jobConversationRef) !== data.jobId)
|
||||
existing.filter((jobRef) => readField("jobid", jobRef) !== data.jobId)
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user