This commit is contained in:
Mekan1206
2026-08-16 20:06:17 +05:00
parent c9f584ce2a
commit aa23e73b34

View File

@@ -330,6 +330,21 @@
if (!body || !state.activeId || !conversation || conversation.can_reply === false) return;
sendEl.disabled = true;
inputEl.value = "";
// 1. OPTIMISTIC UPDATE: Render local message immediately
const tempId = "temp-" + Date.now();
const optimisticMsg = {
id: tempId,
conversation_id: state.activeId,
body: body,
created_at: new Date().toISOString(),
user: { id: config.userId || "current" }
};
state.messages.push(optimisticMsg);
renderMessages();
try {
const payload = await request(
`${config.messagesUrl}/${state.activeId}/messages`,
@@ -339,16 +354,24 @@
body: JSON.stringify({ body }),
}
);
const message = payload.data;
if (message && message.id) {
soundedIds.add(messageKey(message));
}
if (message && !state.messages.some((item) => String(item.id) === String(message.id))) {
state.messages.push(message);
const serverMsg = payload.data;
if (serverMsg && serverMsg.id) {
soundedIds.add(messageKey(serverMsg));
// Replace temp message with server message
const idx = state.messages.findIndex(m => m.id === tempId);
if (idx !== -1) {
state.messages[idx] = serverMsg;
} else if (!state.messages.some(m => String(m.id) === String(serverMsg.id))) {
state.messages.push(serverMsg);
}
renderMessages();
}
inputEl.value = "";
await loadInbox();
} catch (err) {
// Remove temp message if request failed
state.messages = state.messages.filter(m => m.id !== tempId);
renderMessages();
alert("Failed to send message");
} finally {
sendEl.disabled = false;
inputEl.focus();
@@ -405,18 +428,23 @@
function bootEcho() {
const pusher = config.pusher || {};
const EchoClass = window.Echo;
if (typeof EchoClass !== "function" || !window.Pusher || !pusher.key) return;
if (typeof EchoClass !== "function" || !window.Pusher || !pusher.key) {
console.warn("Echo/Pusher assets missing.");
return;
}
const endpoint = echoEndpoint(pusher);
const isHttps = window.location.protocol === "https:";
const host = window.location.hostname;
try {
state.echo = new EchoClass({
broadcaster: "reverb",
key: pusher.key,
wsHost: endpoint.host,
wsPort: endpoint.port,
wssPort: endpoint.port,
forceTLS: endpoint.forceTLS,
wsHost: host,
// Match standard HTTPS (443) or custom port passed from backend
wsPort: pusher.port || (isHttps ? 443 : 80),
wssPort: pusher.port || (isHttps ? 443 : 80),
forceTLS: isHttps,
enabledTransports: ["ws", "wss"],
disableStats: true,
cluster: "mt1",
@@ -429,37 +457,21 @@
"X-Requested-With": "XMLHttpRequest",
},
},
authorizer: (channel) => ({
authorize: (socketId, callback) => {
fetch(config.broadcastAuthPath || config.broadcastAuth || "/panel/broadcasting/auth", {
method: "POST",
credentials: "same-origin",
headers: headers(true),
body: JSON.stringify({
socket_id: socketId,
channel_name: channel.name,
}),
})
.then(async (response) => {
let data = {};
try {
data = await response.json();
} catch (error) {
data = {};
}
if (!response.ok) {
callback(true, data);
return;
}
callback(false, data);
})
.catch((error) => callback(true, error));
},
}),
});
// Debugging connection states in console
if (state.echo.connector && state.echo.connector.pusher) {
state.echo.connector.pusher.connection.bind("state_change", (states) => {
console.log("[Reverb WS State]:", states.current);
});
state.echo.connector.pusher.connection.bind("error", (err) => {
console.error("[Reverb WS Error]:", err);
});
}
state.echo.private(config.inboxChannel || "chat.support").listen(".message.sent", upsertIncoming);
} catch (err) {
console.error("Failed to initialize Echo:", err);
state.echo = null;
}
}