Persistance.
This commit is contained in:
parent
aec9ffd1a1
commit
4f71f74574
@ -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),
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -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),
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
}
|
||||
|
||||
|
||||
|
@ -13,6 +13,7 @@ class MessageListElement extends HTMLElement {
|
||||
super()
|
||||
this.attachShadow({ mode: 'open' });
|
||||
this.component = document.createElement('div')
|
||||
|
||||
this.shadowRoot.appendChild(this.component )
|
||||
}
|
||||
createElement(message){
|
||||
@ -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 = []
|
||||
|
@ -8,6 +8,7 @@
|
||||
<script src="/models.js"></script>
|
||||
<script src="/message-list.js"></script>
|
||||
<script src="/message-list-manager.js"></script>
|
||||
<script src="/chat-input.js"></script>
|
||||
<script src="/chat-window.js"></script>
|
||||
<link rel="stylesheet" href="base.css">
|
||||
</head>
|
||||
@ -31,14 +32,7 @@
|
||||
<li><a href="#">Random</a></li>
|
||||
</ul>
|
||||
</aside>
|
||||
<section>
|
||||
<chat-window class="chat-area"></chat-window>
|
||||
|
||||
<div class="chat-input">
|
||||
<textarea placeholder="Type a message..." rows="2"></textarea>
|
||||
<button>Send</button>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded",()=>{
|
||||
|
@ -17,12 +17,19 @@ class RPCView(BaseView):
|
||||
|
||||
async def get_messages(self, channel_uid,offset=0):
|
||||
messages = []
|
||||
async for message in self.services.channel_message.query("SELECT channel_uid, user_uid, message, created_at FROM channel_message WHERE channel_uid = :channel_uid ORDER BY created_at DESC LIMIT 30 OFFSET :offset",{"channel_uid":channel_uid,"offset":int(offset)}):
|
||||
async for message in self.services.channel_message.query("SELECT * FROM channel_message"): #"SELECT uid, channel_uid, user_uid, message, created_at FROM channel_message WHERE channel_uid = :channel_uid ORDER BY created_at DESC LIMIT 30 OFFSET :offset",{"channel_uid":channel_uid,"offset":int(offset)}):
|
||||
print("JEEEHHH\n",flush=True)
|
||||
|
||||
user = await self.services.user.get(uid=message["user_uid"])
|
||||
if not user:
|
||||
print("User not found!",flush= True)
|
||||
continue
|
||||
|
||||
messages.append(dict(
|
||||
uid=message["uid"],
|
||||
user_uid=message["user_uid"],
|
||||
channel_uid=message["channel_uid"],
|
||||
user_nick=(await self.services.user.get(uid=message["user_uid"]))["nick"],
|
||||
user_nick=user['nick'],
|
||||
message=message["message"],
|
||||
created_at=message["created_at"]
|
||||
))
|
||||
|
Loading…
Reference in New Issue
Block a user