Chat finish; add invitation; friend; see online users if he is your friend

This commit is contained in:
2024-01-24 16:03:50 +01:00
parent fe47a4d633
commit 0f7953b2f3
12 changed files with 532 additions and 79 deletions

View File

@ -4,17 +4,16 @@ import { client } from "../index.js"
export default class extends AbstractView {
constructor(params) {
super(params, "Profile ");
this.user_id = params.id;
this.id = Number(params.id);
}
async postInit()
{
let profile = await client.profiles.getProfile(this.user_id);
this.profile = await client.profiles.getProfile(this.id);
if (profile === null)
if (this.profile === null)
return 404;
this.profile = await client.profiles.getProfile(this.user_id);
this.info = document.getElementById("info");
// Username
@ -32,6 +31,14 @@ export default class extends AbstractView {
this.info.appendChild(avatar);
await this.blockButton();
await this.friendButton();
client.notice.rewrite_profile = async () => {
let result = await this.profile.getFriend();
await this.profile.getBlock()
await this.friendButton();
}
}
async blockButton() {
@ -39,23 +46,81 @@ export default class extends AbstractView {
if (await client.isAuthentificate() === false)
return;
if (client.me.id != this.user_id) {
let block = document.getElementById("block") || document.createElement("a");
if (client.me.id != this.id) {
let block = document.getElementById("block");
if (block == undefined) {
block = document.createElement("p");
this.info.appendChild(block);
}
block.id = "block";
block.innerText = "";
block.onclick = async () => {
if (!this.profile.isBlocked)
await client.profiles.block(this.user_id);
if (this.profile.isBlocked)
await client.profiles.deblock(this.id);
else
await client.profiles.deblock(this.user_id);
this.profile = await client.profiles.getProfile(this.user_id);
await client.profiles.block(this.id);
this.profile = await client.profiles.getProfile(this.id);
this.blockButton();
};
if (this.profile.isBlocked)
block.appendChild(document.createTextNode("Deblock"));
block.textContent = "Deblock";
else
block.appendChild(document.createTextNode("Block"));
this.info.appendChild(block);
block.textContent = "Block";
}
}
async friendButton() {
if (await client.isAuthentificate() === false)
return;
if (client.me.id != this.id) {
let yes = document.getElementById("yes") || document.createElement("p");
let no = document.getElementById("no") || document.createElement("p");
let friend = document.getElementById("friend") || document.createElement("p");
if (client.notice.data["asker"].includes(this.id)) {
if (friend)
friend.remove();
yes.id = "yes";
yes.textContent = "Accept Friend";
yes.onclick = async () => {
client.notice.accept_friend(this.id);
}
no.id = "no";
no.textContent = "Refuse Friend";
no.onclick = async () => {
client.notice.refuse_friend(this.id);
}
this.info.appendChild(yes);
this.info.appendChild(document.createTextNode(" "));
this.info.appendChild(no);
}
else {
if (yes && no)
yes.remove(); no.remove();
friend.id = "friend"
friend.onclick = async () => {
if (this.profile.isFriend)
await client.notice.remove_friend(this.id);
else
await client.notice.ask_friend(this.id);
this.profile = await client.profiles.getProfile(this.id);
this.friendButton();
};
if (this.profile.isFriend)
friend.textContent = "Remove Friend";
else {
friend.textContent = "Ask Friend";
}
this.info.appendChild(friend);
}
}
}

View File

@ -10,12 +10,12 @@ export default class extends AbstractView {
async wait_get_online_users() {
return new Promise((resolve) => {
const checkInterval = setInterval(() => {
//console.log(client.notice.data["online"].length);
if (client.notice.data["online"].length > 0) {
console.log(client.notice.data["online"]);
if (Object.keys(client.notice.data["online"]).length > 0) {
clearInterval(checkInterval);
resolve();
}
}, 100);
}, 1);
});
}
@ -29,7 +29,7 @@ export default class extends AbstractView {
return console.log("Error");
//await client.notice.getOnlineUser();
await this.wait_get_online_users();
//await this.wait_get_online_users();
client.notice.rewrite_usernames = this.rewrite_usernames;
client.notice.rewrite_invite = this.display_invite;
@ -68,7 +68,14 @@ export default class extends AbstractView {
username.setAttribute('data-link', '');
username.id = `username${user.id}`
username.href = `/profiles/${user.id}`;
username.style.color = client.notice.data["online"].includes(user.id.toString()) ? "green" : "red";
if (user.id == client.me.id)
username.style.color = "green";
else {
let online = client.notice.data["online"][user.id];
username.style.color = online !== undefined ? online : "gray";
}
username.appendChild(document.createTextNode(user.username));
new_user.appendChild(username);
@ -137,8 +144,14 @@ export default class extends AbstractView {
profiles.filter(user => user.username.toLowerCase().startsWith(search) == true).forEach((user) => {
let username = document.getElementById(`username${user.id}`);
if (username !== null)
username.style.color = client.notice.data["online"].includes(user.id.toString()) ? "green" : "red";
if (username !== null) {
if (user.id == client.me.id)
username.style.color = "green";
else {
let online = client.notice.data["online"][user.id];
username.style.color = online !== undefined ? online : "gray";
}
}
});
}
@ -259,9 +272,10 @@ export default class extends AbstractView {
usernames += (profiles.filter(user => user.id == member_id)[0].username);
}
});
members.appendChild(document.createTextNode(usernames));
members.textContent = usernames;
chat.appendChild(members);
return members
}
@ -299,7 +313,7 @@ export default class extends AbstractView {
invite.innerText = "invite";
invite.title = "Invite to play a game"
invite.onclick = async () => {
await client.notice.sendInvite(others);
await client.notice.send_invite(others);
};
chat.appendChild(invite);
}
@ -312,14 +326,14 @@ export default class extends AbstractView {
yes.style.background = "green";
yes.title = "Accept to play a game"
yes.onclick = async () => {
await client.notice.acceptInvite(invitedBy);
await client.notice.accept_invite(invitedBy);
};
no.id = "no";
no.style.background = "red";
no.title = "Refuse to play a game"
no.onclick = async () => {
await client.notice.refuseInvite(invitedBy);
await client.notice.refuse_invite(invitedBy);
};
chat.appendChild(yes);