feature/IO-3000-Migrate-MSG-to-Sockets - Major Progress
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
const { admin } = require("../firebase/firebase-handler");
|
||||
const { MARK_MESSAGES_AS_READ, GET_CONVERSATIONS, GET_CONVERSATION_DETAILS } = require("../graphql-client/queries");
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
|
||||
const redisSocketEvents = ({
|
||||
io,
|
||||
@@ -113,6 +115,7 @@ const redisSocketEvents = ({
|
||||
socket.on("leave-bodyshop-room", leaveBodyshopRoom);
|
||||
socket.on("broadcast-to-bodyshop", broadcastToBodyshopRoom);
|
||||
};
|
||||
|
||||
// Disconnect Events
|
||||
const registerDisconnectEvents = (socket) => {
|
||||
const disconnect = () => {
|
||||
@@ -129,10 +132,57 @@ const redisSocketEvents = ({
|
||||
|
||||
socket.on("disconnect", disconnect);
|
||||
};
|
||||
// Messaging Events
|
||||
const registerMessagingEvents = (socket) => {
|
||||
const broadcastNewMessage = async (message) => {
|
||||
const room = `conversation-${message.conversationId}`;
|
||||
io.to(room).emit("new-message", message);
|
||||
};
|
||||
|
||||
const openMessaging = async (bodyshopUUID) => {
|
||||
try {
|
||||
const conversations = await client.request(GET_CONVERSATIONS, { bodyshopId: bodyshopUUID });
|
||||
socket.emit("messaging-list", { conversations });
|
||||
} catch (error) {
|
||||
console.dir(error);
|
||||
logger.log("error", "Failed to fetch conversations", error);
|
||||
socket.emit("error", { message: "Failed to fetch conversations" });
|
||||
}
|
||||
};
|
||||
|
||||
const joinConversation = async (conversationId) => {
|
||||
try {
|
||||
const room = `conversation-${conversationId}`;
|
||||
socket.join(room);
|
||||
|
||||
// Fetch conversation details and messages
|
||||
const data = await client.request(GET_CONVERSATION_DETAILS, { conversationId });
|
||||
socket.emit("conversation-details", data); // Send data to the client
|
||||
} catch (error) {
|
||||
logger.log("error", "Failed to join conversation", error);
|
||||
socket.emit("error", { message: "Failed to join conversation" });
|
||||
}
|
||||
};
|
||||
|
||||
const markAsRead = async ({ conversationId, userId }) => {
|
||||
try {
|
||||
await client.request(MARK_MESSAGES_AS_READ, { conversationId, userId });
|
||||
io.to(`conversation-${conversationId}`).emit("read-updated", { conversationId });
|
||||
} catch (error) {
|
||||
logger.log("error", "Failed to mark messages as read", error);
|
||||
socket.emit("error", { message: "Failed to mark messages as read" });
|
||||
}
|
||||
};
|
||||
// Mark Messages as Read
|
||||
socket.on("mark-as-read", markAsRead);
|
||||
socket.on("join-conversation", joinConversation);
|
||||
socket.on("open-messaging", openMessaging);
|
||||
};
|
||||
|
||||
// Call Handlers
|
||||
registerRoomAndBroadcastEvents(socket);
|
||||
registerUpdateEvents(socket);
|
||||
registerMessagingEvents(socket);
|
||||
registerDisconnectEvents(socket);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user