merge with Chatte

This commit is contained in:
2023-12-16 16:15:13 +01:00
6 changed files with 39 additions and 79 deletions

View File

@ -1,56 +0,0 @@
import AbstractView from "./AbstractView.js";
export default class extends AbstractView {
constructor(params) {
super(params, "Chat");
let url = `ws://${window.location.host}/ws/chat/`
this.chatSocket = new WebSocket(url)
this.chatSocket.onmessage = function(e){
let data = JSON.parse(e.data)
console.log('Data:', data)
if (data.type === 'chat') {
let messages = document.getElementById('messages')
let username = data.username === null || data.username.length <= 0 ? "NoName" : data.username;
messages.insertAdjacentHTML('beforeend', `
<div><p>${username}: ${data.message}</p></div>
`)
}
}
}
async postInit() {
let form = document.getElementById('form')
form.addEventListener('submit', (e)=> {
e.preventDefault()
let message = e.target.message.value
this.chatSocket.send(JSON.stringify({
'message':message
}))
form.reset()
})
}
async leavePage() {
this.chatSocket.close();
}
async getHtml() {
return `
<h1>Chat</h1>
<form id="form">
<input type="text" name="message" placeholder="message"/>
</form>
<div id="messages">
</div>
`;
}
}

View File

@ -45,7 +45,9 @@ export default class extends AbstractView {
// chat
if (logged && client.me.user_id != user.user_id) {
let add_chat = document.createElement("a");
add_chat.id = "add_chat_off";
add_chat.addEventListener("click", async () => {
add_chat.id = "add_chat_off";
if (client.channel != undefined) {
client.channel.members_id.forEach((member_id) => {
if (member_id == user.user_id)
@ -60,24 +62,20 @@ export default class extends AbstractView {
client.channel = await client.channels.createChannel([client.me.user_id , user.user_id], this.chat);
this.chat();
add_chat.id = "add_chat_on";
});
add_chat.appendChild(document.createTextNode("Chat"));
new_user.appendChild(add_chat);
/*new_user.appendChild(document.createTextNode(" "));
new_user.appendChild(document.createTextNode(" "));
let remove = document.createElement("a");
remove.addEventListener("click", async () => {
let block = document.createElement("a");
block.addEventListener("click", async () => {
if (client.me.user_id != user.user_id) {
await client.channels.deleteChannel([client.me.user_id , user.user_id]);
if
client.channel.disconnect();
client.channel = undefined;
this.chat()
}
});
remove.appendChild(document.createTextNode("Remove"));
new_user.appendChild(remove);*/
block.appendChild(document.createTextNode("Block"));
new_user.appendChild(block);
}
// break line