Merge branch 'Chatte' into server

This commit is contained in:
2023-12-27 09:59:19 +01:00
10 changed files with 155 additions and 41 deletions

View File

@ -54,13 +54,14 @@ class Channel {
return new_messages;
}
async sendMessageChannel(message) {
async sendMessageChannel(message, receivers_id) {
if (this.chatSocket == undefined)
return;
this.chatSocket.send(JSON.stringify({
'message':message
'message':message,
'receivers_id':receivers_id,
}));
}

View File

@ -38,10 +38,11 @@ class Client
return this.logged;
}
async _get(uri)
async _get(uri, data)
{
let response = await fetch(this._url + uri, {
method: "GET",
body: JSON.stringify(data),
});
return response;
}

View File

@ -12,8 +12,9 @@ class Profile
*/
this.client = client;
this.username = username;
this.avatar_url = avatar_url
this.user_id = user_id
this.avatar_url = avatar_url;
this.user_id = user_id;
this.isBlocked = false;
}
async init(user_id)
@ -28,7 +29,21 @@ class Profile
this.user_id = response_data.user_id;
this.username = response_data.username;
this.avatar_url = response_data.avatar_url;
let block_response = await this.client._get("/api/profiles/block");
if (block_response.status == 404)
return
let block_data = await block_response.json();
let block_list = JSON.parse(block_data);
block_list.forEach(block => {
let blocker = block.fields.blocker;
let blocked = block.fields.blocked;
if (blocker == this.client.me.user_id && blocked == user_id)
return this.isBlocked = true;
});
}
}
export {Profile}
export {Profile}

View File

@ -36,13 +36,30 @@ class Profiles
async block(user_id) {
// blocker & blocked
let response = await this.client._post("/api/block/",
[this.client.me.user_id, user_id],
);
let response = await this.client._post("/api/profiles/block", {
users_id:[this.client.me.user_id, user_id],
});
let data = await response.json();
console.log(response.status);
console.log(data);
return data;
}
async deblock(user_id) {
// blocker & blocked
let response = await this.client._delete("/api/profiles/block", {
users_id:[this.client.me.user_id, user_id],
});
let data = await response.json();
console.log(response.status);
console.log(data);
return data;
}
}
export {Profiles}

View File

@ -14,33 +14,48 @@ export default class extends AbstractView {
if (profile === null)
return 1;
let info = document.getElementById("info");
this.profile = await client.profiles.getProfile(this.user_id);
this.info = document.getElementById("info");
// Username
let username = document.createElement("a");
username.id = "username";
username.appendChild(document.createTextNode(profile.username));
info.appendChild(username);
username.appendChild(document.createTextNode(this.profile.username));
this.info.appendChild(username);
info.appendChild(document.createElement("br"));
this.info.appendChild(document.createElement("br"));
// Avatar
let avatar = document.createElement("img");
avatar.id = "avatar";
avatar.src = profile.avatar_url;
info.appendChild(avatar);
// Block option
let block = document.createElement("a");
block.id = "block";
block.addEventListener("click", async () => {
if (client.me.user_id != user.user_id) {
}
});
block.appendChild(document.createTextNode("Block"));
info.appendChild(block);
avatar.src = this.profile.avatar_url;
this.info.appendChild(avatar);
await this.blockButton();
}
async blockButton() {
// Block option
if (client.me.user_id != this.user_id) {
let block = document.getElementById("block") || document.createElement("a");
block.id = "block";
block.innerText = "";
block.onclick = async () => {
if (!this.profile.isBlocked)
await client.profiles.block(this.user_id);
else
await client.profiles.deblock(this.user_id);
this.profile = await client.profiles.getProfile(this.user_id);
this.blockButton();
};
if (this.profile.isBlocked)
block.appendChild(document.createTextNode("Deblock"));
else
block.appendChild(document.createTextNode("Block"));
this.info.appendChild(block);
}
}
async getHtml() {
return `
<link rel="stylesheet" href="/static/css/profile.css">

View File

@ -100,6 +100,7 @@ export default class extends AbstractView {
async chat() {
let users = await client.profiles.all();
let logged = await client.isAuthentificate();
/*let reload = document.getElementById("messages");
if (reload != null)
@ -135,7 +136,7 @@ export default class extends AbstractView {
// les messages, réecriture seulement du dernier
let i = 0;
client.channel.messages.forEach((message) => {
if (messages[i] == null || message != messages.children[i].innerText) {
if (messages.children[i] == null || message.content != messages.children[i].innerText) {
let text = document.createElement("p");
text.appendChild(document.createTextNode(message.content));
if (message.author_id == client.me.user_id)
@ -158,21 +159,25 @@ export default class extends AbstractView {
chat_input.maxLength=255;
chat.appendChild(chat_input);
chat_input.addEventListener("keydown", async () => {
chat_input.onkeydown = async () => {
if (event.keyCode == 13 && client.channel != undefined) {
//let chat_input = document.getElementById("input_chat");
let chat_text = chat_input.value;
await client.channel.sendMessageChannel(chat_text)
let receivers_id = [];
client.channel.members_id.forEach((member_id) => {
if (member_id != client.me.user_id)
receivers_id.push(users.filter(user => user.user_id == member_id)[0].user_id);
});
await client.channel.sendMessageChannel(chat_text, receivers_id)
// Reset
chat_input.value = "";
}
});
};
}
// nom des membres du chat
let users = await client.profiles.all();
let members = document.createElement("h2");
members.id = "members";
let usernames = "";