diff --git a/src/snek/service/chat.py b/src/snek/service/chat.py index 9346e40..1de149c 100644 --- a/src/snek/service/chat.py +++ b/src/snek/service/chat.py @@ -13,7 +13,7 @@ class ChatService(BaseService): channel["last_message_on"] = now() await self.services.channel.save(channel) await self.services.socket.broadcast( - channel_uid, + channel['uid'], { "message": channel_message["message"], "html": channel_message["html"], diff --git a/src/snek/static/app.js b/src/snek/static/app.js index 8810952..b839ddb 100644 --- a/src/snek/static/app.js +++ b/src/snek/static/app.js @@ -160,11 +160,15 @@ export class App extends EventHandler { typeLock = null; typeListener = null; typeEventChannelUid = null; - async set_typing(channel_uid) { + _debug = false + async set_typing(channel_uid) { this.typeEventChannel_uid = channel_uid; } - - async ping(...args) { + debug() { + this._debug = !this._debug; + this.ws._debug = this._debug; + } + async ping(...args) { if (this.is_pinging) return false; this.is_pinging = true; await this.rpc.ping(...args); @@ -202,8 +206,10 @@ export class App extends EventHandler { this.ws.addEventListener("channel-message", (data) => { me.emit("channel-message", data); }); - this.ws.addEventListener("event", (data) => { - console.info("aaaa"); + this.ws.addEventListener("data", (data) => { + if(this._debug){ + console.debug(data) + } }); this.rpc.getUser(null).then((user) => { me.user = user; diff --git a/src/snek/static/chat-input.js b/src/snek/static/chat-input.js index cab4285..9e6fa03 100644 --- a/src/snek/static/chat-input.js +++ b/src/snek/static/chat-input.js @@ -179,9 +179,6 @@ levenshteinDistance(a, b) { if (e.key === "Enter" && !e.shiftKey) { this.value = ""; e.target.value = ""; - if(this.messageUid){ - app.rpc.finalizeMessage(this.messageUid) - } return; } this.value = e.target.value; @@ -220,7 +217,7 @@ levenshteinDistance(a, b) { } this.updateMessage() - + app.rpc.finalizeMessage(this.messageUid) this.value = ""; this.previousValue = ""; this.messageUid = null; diff --git a/src/snek/static/message-list.js b/src/snek/static/message-list.js index 5cc8756..70c3573 100644 --- a/src/snek/static/message-list.js +++ b/src/snek/static/message-list.js @@ -51,8 +51,6 @@ class MessageList extends HTMLElement { return this.isElementVisible(this.querySelector(".message-list-bottom")); } scrollToBottom(force) { - console.info("Scrolling down") - // if (force) { this.scrollTop = this.scrollHeight; this.querySelector(".message-list-bottom").scrollIntoView(); @@ -61,7 +59,6 @@ class MessageList extends HTMLElement { this.scrollTop = this.scrollHeight; this.querySelector(".message-list-bottom").scrollIntoView(); },200) - // } } updateMessageText(uid, message) { const messageDiv = this.querySelector('div[data-uid="' + uid + '"]'); @@ -69,12 +66,15 @@ class MessageList extends HTMLElement { if (!messageDiv) { return; } + const scrollToBottom = this.isScrolledToBottom(); const receivedHtml = document.createElement("div"); receivedHtml.innerHTML = message.html; const html = receivedHtml.querySelector(".text").innerHTML; const textElement = messageDiv.querySelector(".text"); textElement.innerHTML = html; textElement.style.display = message.text == "" ? "none" : "block"; + if(scrollToBottom) + this.scrollToBottom(true) } triggerGlow(uid,color) { app.starField.glowColor(color) diff --git a/src/snek/static/socket.js b/src/snek/static/socket.js index 77fbd3c..1131e8f 100644 --- a/src/snek/static/socket.js +++ b/src/snek/static/socket.js @@ -17,6 +17,8 @@ export class Socket extends EventHandler { shouldReconnect = true; + _debug = false; + get isConnected() { return this.ws && this.ws.readyState === WebSocket.OPEN; } @@ -112,7 +114,12 @@ export class Socket extends EventHandler { get(_, prop) { return (...args) => { const functionName = me._camelToSnake(prop); - return me.call(functionName, ...args); + if(me._debug){ + const call = {} + call[functionName] = args + console.debug(call) + } + return me.call(functionName, ...args); }; }, }, diff --git a/src/snek/templates/web.html b/src/snek/templates/web.html index 6a1fe10..03b9444 100644 --- a/src/snek/templates/web.html +++ b/src/snek/templates/web.html @@ -13,7 +13,7 @@ {% endfor %}
- + {% include "dialog_help.html" %} {% include "dialog_online.html" %} @@ -167,24 +167,35 @@ app.ws.addEventListener("starfield.render_word", (data) => { // --- Channel message event --- app.addEventListener("channel-message", (data) => { + let display = data.text && data.text.trim() ? 'block' : 'none'; - if (data.channel_uid !== channelUid) { - if (!isMentionForSomeoneElse(data.message)) { - channelSidebar.notify(data); - app.playSound("messageOtherChannel"); + if(data.is_final){ + if (data.channel_uid !== channelUid) { + if (!isMentionForSomeoneElse(data.message)) { + channelSidebar.notify(data); + app.playSound("messageOtherChannel"); + } + return; } - return; - } - if (data.username !== username) { - if (isMentionToMe(data.message)) { - app.playSound("mention"); - } else if (!isMentionForSomeoneElse(data.message)) { - app.playSound("message"); + if (data.username !== username) { + if (isMentionToMe(data.message)) { + app.playSound("mention"); + } else if (!isMentionForSomeoneElse(data.message)) { + app.playSound("message"); + } } } const lastElement = messagesContainer.querySelector(".message-list-bottom"); const doScrollDown = messagesContainer.isScrolledToBottom(); + + const oldMessage = messagesContainer.querySelector(`.message[data-uid="${data.uid}"]`); + if (oldMessage) { + oldMessage.remove(); + } + const message = document.createElement("div"); + + message.innerHTML = data.html; message.style.display = display; messagesContainer.insertBefore(message.firstChild, lastElement); diff --git a/src/snek/view/rpc.py b/src/snek/view/rpc.py index 3251035..3dda8e8 100644 --- a/src/snek/view/rpc.py +++ b/src/snek/view/rpc.py @@ -196,6 +196,9 @@ class RPCView(BaseView): async def finalize_message(self, message_uid): self._require_login() message = await self.services.channel_message.get(message_uid) + if not message: + return False + if message["user_uid"] != self.user_uid: raise Exception("Not allowed") @@ -203,7 +206,7 @@ class RPCView(BaseView): if not message['is_final']: await self.services.chat.finalize(message['uid']) - return {"success": True} + return True async def update_message_text(self,message_uid, text): self._require_login()