From 4f71f745744b1a413a729875bc42366ea3ab665d Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 27 Jan 2025 02:57:51 +0100 Subject: [PATCH] Persistance. --- src/snek/mapper/__init__.py | 2 ++ src/snek/service/__init__.py | 2 ++ src/snek/service/chat.py | 19 ++----------------- src/snek/static/base.css | 8 ++++++-- src/snek/static/chat-window.js | 26 ++++++++++++++++++++++++-- src/snek/static/message-list.js | 12 ++++++++++-- src/snek/templates/web.html | 8 +------- src/snek/view/rpc.py | 11 +++++++++-- 8 files changed, 56 insertions(+), 32 deletions(-) diff --git a/src/snek/mapper/__init__.py b/src/snek/mapper/__init__.py index be22534..1841346 100644 --- a/src/snek/mapper/__init__.py +++ b/src/snek/mapper/__init__.py @@ -3,6 +3,7 @@ import functools from snek.mapper.channel import ChannelMapper from snek.mapper.channel_member import ChannelMemberMapper from snek.mapper.channel_message import ChannelMessageMapper +from snek.mapper.notification import NotificationMapper from snek.mapper.user import UserMapper from snek.system.object import Object @@ -15,6 +16,7 @@ def get_mappers(app=None): "channel_member": ChannelMemberMapper(app=app), "channel": ChannelMapper(app=app), "channel_message": ChannelMessageMapper(app=app), + "notification": NotificationMapper(app=app), } ) diff --git a/src/snek/service/__init__.py b/src/snek/service/__init__.py index db4a00f..6a8f76c 100644 --- a/src/snek/service/__init__.py +++ b/src/snek/service/__init__.py @@ -4,6 +4,7 @@ from snek.service.channel import ChannelService from snek.service.channel_member import ChannelMemberService from snek.service.channel_message import ChannelMessageService from snek.service.chat import ChatService +from snek.service.notification import NotificationService from snek.service.socket import SocketService from snek.service.user import UserService from snek.system.object import Object @@ -19,6 +20,7 @@ def get_services(app): "channel_message": ChannelMessageService(app=app), "chat": ChatService(app=app), "socket": SocketService(app=app), + "notification": NotificationService(app=app), } ) diff --git a/src/snek/service/chat.py b/src/snek/service/chat.py index 3dd67e3..74ca94e 100644 --- a/src/snek/service/chat.py +++ b/src/snek/service/chat.py @@ -8,29 +8,14 @@ class ChatService(BaseService): async def send(self,user_uid, channel_uid, message): channel_message = await self.services.channel_message.create( - user_uid, channel_uid, + user_uid, message ) channel_message_uid = channel_message["uid"] user = await self.services.user.get(uid=user_uid) - async for channel_member in self.services.channel_member.find( - channel_uid=channel_message["channel_uid"], - is_banned=False, - is_muted=False, - deleted_at=None, - ): - model = await self.new() - model["object_uid"] = channel_message_uid - model["object_type"] = "channel_message" - model["user_uid"] = channel_member["user_uid"] - model["message"] = ( - f"New message from {user['nick']} in {channel_member['label']}." - ) - if not await self.services.channel_member.save(model): - raise Exception(f"Failed to create notification: {model.errors}.") - + await self.services.notification.create_channel_message(channel_message_uid) sent_to_count = await self.services.socket.broadcast(channel_uid, dict( message=message, user_uid=user_uid, diff --git a/src/snek/static/base.css b/src/snek/static/base.css index b674b8d..5447efb 100644 --- a/src/snek/static/base.css +++ b/src/snek/static/base.css @@ -101,11 +101,15 @@ main { font-size: 1.2em; color: #fff; } - +message-list { + flex: 1;; + height: 200px; + overflow-y: auto; +} .chat-messages { flex: 1; padding: 20px; - overflow-y: auto; + height: 200px; background: #1a1a1a; } diff --git a/src/snek/static/chat-window.js b/src/snek/static/chat-window.js index f9b7328..35e4e78 100644 --- a/src/snek/static/chat-window.js +++ b/src/snek/static/chat-window.js @@ -4,7 +4,7 @@ class ChatWindowElement extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); - this.component = document.createElement('div'); + this.component = document.createElement('section'); this.shadowRoot.appendChild(this.component); } @@ -13,6 +13,7 @@ class ChatWindowElement extends HTMLElement { link.rel = 'stylesheet' link.href = '/base.css' this.component.appendChild(link) + this.component.classList.add("chat-area") this.container = document.createElement("section") this.container.classList.add("chat-area") this.container.classList.add("chat-window") @@ -29,12 +30,33 @@ class ChatWindowElement extends HTMLElement { chatTitle.innerText = channel.name const channelElement = document.createElement('message-list') channelElement.setAttribute("channel", channel.uid) + //channelElement.classList.add("chat-messages") this.container.appendChild(channelElement) + + const chatInput = document.createElement('chat-input') + chatInput.classList.add("chat-input") + chatInput.addEventListener("submit",(e)=>{ + app.rpc.sendMessage(channel.uid,e.detail) + }) + this.container.appendChild(chatInput) + this.component.appendChild(this.container) console.info(channel) const messages = await app.rpc.getMessages(channel.uid) console.info(messages) - await app.rpc.sendMessage(channel.uid,"hello world") + messages.forEach(message=>{ + if(!message['user_nick']) + return + channelElement.addMessage(message) + }) + const me = this + channelElement.addEventListener("message",(message)=>{ + console.info("ROCKSTARTSS") + setTimeout(()=>{ + message.detail.element.scrollIntoView({behavior: 'smooth'}) + },10) + }) + await app.rpc.sendMessage(channel.uid,"Grrrrr") } diff --git a/src/snek/static/message-list.js b/src/snek/static/message-list.js index c1d7b3d..84207d2 100644 --- a/src/snek/static/message-list.js +++ b/src/snek/static/message-list.js @@ -13,11 +13,12 @@ class MessageListElement extends HTMLElement { super() this.attachShadow({ mode: 'open' }); this.component = document.createElement('div') + this.shadowRoot.appendChild(this.component ) } createElement(message){ const element = document.createElement("div") - + element.classList.add("message") const avatar = document.createElement("div") avatar.classList.add("avatar") @@ -59,15 +60,22 @@ class MessageListElement extends HTMLElement { const element = this.createElement(obj) this.messages.push(obj) this.container.appendChild(element) + const me = this + this.dispatchEvent(new CustomEvent("message", {detail:obj,bubbles:true})) + return obj } + scrollBottom(){ + this.container.scrollTop = this.container.scrollHeight; + } connectedCallback() { const link = document.createElement('link') link.rel = 'stylesheet' link.href = '/base.css' this.component.appendChild(link) + this.component.classList.add("chat-messages") this.container = document.createElement('div') - this.container.classList.add("chat-messages") + //this.container.classList.add("chat-messages") this.component.appendChild(this.container) this.messages = [] diff --git a/src/snek/templates/web.html b/src/snek/templates/web.html index f6635d3..0184a97 100644 --- a/src/snek/templates/web.html +++ b/src/snek/templates/web.html @@ -8,6 +8,7 @@ + @@ -31,14 +32,7 @@
  • Random
  • -
    - -
    - - -
    -