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 import ChannelMapper
|
||||||
from snek.mapper.channel_member import ChannelMemberMapper
|
from snek.mapper.channel_member import ChannelMemberMapper
|
||||||
from snek.mapper.channel_message import ChannelMessageMapper
|
from snek.mapper.channel_message import ChannelMessageMapper
|
||||||
|
from snek.mapper.notification import NotificationMapper
|
||||||
from snek.mapper.user import UserMapper
|
from snek.mapper.user import UserMapper
|
||||||
from snek.system.object import Object
|
from snek.system.object import Object
|
||||||
|
|
||||||
@ -15,6 +16,7 @@ def get_mappers(app=None):
|
|||||||
"channel_member": ChannelMemberMapper(app=app),
|
"channel_member": ChannelMemberMapper(app=app),
|
||||||
"channel": ChannelMapper(app=app),
|
"channel": ChannelMapper(app=app),
|
||||||
"channel_message": ChannelMessageMapper(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_member import ChannelMemberService
|
||||||
from snek.service.channel_message import ChannelMessageService
|
from snek.service.channel_message import ChannelMessageService
|
||||||
from snek.service.chat import ChatService
|
from snek.service.chat import ChatService
|
||||||
|
from snek.service.notification import NotificationService
|
||||||
from snek.service.socket import SocketService
|
from snek.service.socket import SocketService
|
||||||
from snek.service.user import UserService
|
from snek.service.user import UserService
|
||||||
from snek.system.object import Object
|
from snek.system.object import Object
|
||||||
@ -19,6 +20,7 @@ def get_services(app):
|
|||||||
"channel_message": ChannelMessageService(app=app),
|
"channel_message": ChannelMessageService(app=app),
|
||||||
"chat": ChatService(app=app),
|
"chat": ChatService(app=app),
|
||||||
"socket": SocketService(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):
|
async def send(self,user_uid, channel_uid, message):
|
||||||
channel_message = await self.services.channel_message.create(
|
channel_message = await self.services.channel_message.create(
|
||||||
user_uid,
|
|
||||||
channel_uid,
|
channel_uid,
|
||||||
|
user_uid,
|
||||||
message
|
message
|
||||||
)
|
)
|
||||||
channel_message_uid = channel_message["uid"]
|
channel_message_uid = channel_message["uid"]
|
||||||
|
|
||||||
user = await self.services.user.get(uid=user_uid)
|
user = await self.services.user.get(uid=user_uid)
|
||||||
async for channel_member in self.services.channel_member.find(
|
await self.services.notification.create_channel_message(channel_message_uid)
|
||||||
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}.")
|
|
||||||
|
|
||||||
sent_to_count = await self.services.socket.broadcast(channel_uid, dict(
|
sent_to_count = await self.services.socket.broadcast(channel_uid, dict(
|
||||||
message=message,
|
message=message,
|
||||||
user_uid=user_uid,
|
user_uid=user_uid,
|
||||||
|
@ -101,11 +101,15 @@ main {
|
|||||||
font-size: 1.2em;
|
font-size: 1.2em;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
}
|
}
|
||||||
|
message-list {
|
||||||
|
flex: 1;;
|
||||||
|
height: 200px;
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
.chat-messages {
|
.chat-messages {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
overflow-y: auto;
|
height: 200px;
|
||||||
background: #1a1a1a;
|
background: #1a1a1a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ class ChatWindowElement extends HTMLElement {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.attachShadow({ mode: 'open' });
|
this.attachShadow({ mode: 'open' });
|
||||||
this.component = document.createElement('div');
|
this.component = document.createElement('section');
|
||||||
this.shadowRoot.appendChild(this.component);
|
this.shadowRoot.appendChild(this.component);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ class ChatWindowElement extends HTMLElement {
|
|||||||
link.rel = 'stylesheet'
|
link.rel = 'stylesheet'
|
||||||
link.href = '/base.css'
|
link.href = '/base.css'
|
||||||
this.component.appendChild(link)
|
this.component.appendChild(link)
|
||||||
|
this.component.classList.add("chat-area")
|
||||||
this.container = document.createElement("section")
|
this.container = document.createElement("section")
|
||||||
this.container.classList.add("chat-area")
|
this.container.classList.add("chat-area")
|
||||||
this.container.classList.add("chat-window")
|
this.container.classList.add("chat-window")
|
||||||
@ -29,12 +30,33 @@ class ChatWindowElement extends HTMLElement {
|
|||||||
chatTitle.innerText = channel.name
|
chatTitle.innerText = channel.name
|
||||||
const channelElement = document.createElement('message-list')
|
const channelElement = document.createElement('message-list')
|
||||||
channelElement.setAttribute("channel", channel.uid)
|
channelElement.setAttribute("channel", channel.uid)
|
||||||
|
//channelElement.classList.add("chat-messages")
|
||||||
this.container.appendChild(channelElement)
|
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)
|
this.component.appendChild(this.container)
|
||||||
console.info(channel)
|
console.info(channel)
|
||||||
const messages = await app.rpc.getMessages(channel.uid)
|
const messages = await app.rpc.getMessages(channel.uid)
|
||||||
console.info(messages)
|
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()
|
super()
|
||||||
this.attachShadow({ mode: 'open' });
|
this.attachShadow({ mode: 'open' });
|
||||||
this.component = document.createElement('div')
|
this.component = document.createElement('div')
|
||||||
|
|
||||||
this.shadowRoot.appendChild(this.component )
|
this.shadowRoot.appendChild(this.component )
|
||||||
}
|
}
|
||||||
createElement(message){
|
createElement(message){
|
||||||
@ -59,15 +60,22 @@ class MessageListElement extends HTMLElement {
|
|||||||
const element = this.createElement(obj)
|
const element = this.createElement(obj)
|
||||||
this.messages.push(obj)
|
this.messages.push(obj)
|
||||||
this.container.appendChild(element)
|
this.container.appendChild(element)
|
||||||
|
const me = this
|
||||||
|
this.dispatchEvent(new CustomEvent("message", {detail:obj,bubbles:true}))
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
scrollBottom(){
|
||||||
|
this.container.scrollTop = this.container.scrollHeight;
|
||||||
|
}
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const link = document.createElement('link')
|
const link = document.createElement('link')
|
||||||
link.rel = 'stylesheet'
|
link.rel = 'stylesheet'
|
||||||
link.href = '/base.css'
|
link.href = '/base.css'
|
||||||
this.component.appendChild(link)
|
this.component.appendChild(link)
|
||||||
|
this.component.classList.add("chat-messages")
|
||||||
this.container = document.createElement('div')
|
this.container = document.createElement('div')
|
||||||
this.container.classList.add("chat-messages")
|
//this.container.classList.add("chat-messages")
|
||||||
this.component.appendChild(this.container)
|
this.component.appendChild(this.container)
|
||||||
|
|
||||||
this.messages = []
|
this.messages = []
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
<script src="/models.js"></script>
|
<script src="/models.js"></script>
|
||||||
<script src="/message-list.js"></script>
|
<script src="/message-list.js"></script>
|
||||||
<script src="/message-list-manager.js"></script>
|
<script src="/message-list-manager.js"></script>
|
||||||
|
<script src="/chat-input.js"></script>
|
||||||
<script src="/chat-window.js"></script>
|
<script src="/chat-window.js"></script>
|
||||||
<link rel="stylesheet" href="base.css">
|
<link rel="stylesheet" href="base.css">
|
||||||
</head>
|
</head>
|
||||||
@ -31,14 +32,7 @@
|
|||||||
<li><a href="#">Random</a></li>
|
<li><a href="#">Random</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</aside>
|
</aside>
|
||||||
<section>
|
|
||||||
<chat-window class="chat-area"></chat-window>
|
<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>
|
</main>
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener("DOMContentLoaded",()=>{
|
document.addEventListener("DOMContentLoaded",()=>{
|
||||||
|
@ -17,12 +17,19 @@ class RPCView(BaseView):
|
|||||||
|
|
||||||
async def get_messages(self, channel_uid,offset=0):
|
async def get_messages(self, channel_uid,offset=0):
|
||||||
messages = []
|
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(
|
messages.append(dict(
|
||||||
uid=message["uid"],
|
uid=message["uid"],
|
||||||
user_uid=message["user_uid"],
|
user_uid=message["user_uid"],
|
||||||
channel_uid=message["channel_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"],
|
message=message["message"],
|
||||||
created_at=message["created_at"]
|
created_at=message["created_at"]
|
||||||
))
|
))
|
||||||
|
Loading…
Reference in New Issue
Block a user