banner/accent_color support for profiles

This commit is contained in:
MathMan05 2024-08-16 20:10:57 -05:00
parent 1eea6cc005
commit d4a8393f69
10 changed files with 365 additions and 23 deletions

View file

@ -65,6 +65,24 @@ class Channel {
}, null, (_) => {
return _.hasPermission("CREATE_INSTANT_INVITE") && _.type !== 4;
});
/*
this.contextmenu.addbutton("Test button",function(){
this.localuser.ws.send(JSON.stringify({
"op": 14,
"d": {
"guild_id": this.guild.id,
"channels": {
[this.id]: [
[
0,
99
]
]
}
}
}))
},null);
/**/
}
createInvite() {
const div = document.createElement("div");

View file

@ -52,6 +52,7 @@ class Localuser {
this.guilds = [];
this.guildids = new Map();
this.user = new User(ready.d.user, this);
this.user.setstatus("online");
this.mfa_enabled = ready.d.user.mfa_enabled;
this.userinfo.username = this.user.username;
this.userinfo.pfpsrc = this.user.getpfpsrc();
@ -609,6 +610,30 @@ class Localuser {
});
};
}
updatebanner(file) {
if (file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
fetch(this.info.api + "/users/@me", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
banner: reader.result,
})
});
};
}
else {
fetch(this.info.api + "/users/@me", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
banner: null,
})
});
}
}
updateProfile(json) {
fetch(this.info.api + "/users/@me/profile", {
method: "PATCH",
@ -664,9 +689,10 @@ class Localuser {
let newpronouns = undefined;
let newbio = undefined;
let hypouser = this.user.clone();
function regen() {
let color;
async function regen() {
hypotheticalProfile.textContent = "";
const hypoprofile = hypouser.buildprofile(-1, -1);
const hypoprofile = await hypouser.buildprofile(-1, -1);
hypotheticalProfile.appendChild(hypoprofile);
}
regen();
@ -687,9 +713,31 @@ class Localuser {
regen();
}
});
let bfile = undefined;
const binput = settingsLeft.addFileInput("Upload banner:", _ => {
if (bfile !== undefined) {
this.updatebanner(bfile);
}
});
binput.watchForChange(_ => {
if (_.length) {
bfile = _[0];
const blob = URL.createObjectURL(bfile);
hypouser.banner = blob;
hypouser.hypotheticalbanner = true;
regen();
}
});
const bclear = settingsLeft.addButtonInput("Clear banner", "Clear", () => {
bfile = null;
hypouser.banner = null;
settingsLeft.changed();
regen();
});
let changed = false;
const pronounbox = settingsLeft.addTextInput("Pronouns", _ => {
if (newpronouns || newbio) {
this.updateProfile({ pronouns: newpronouns, bio: newbio });
if (newpronouns || newbio || changed) {
this.updateProfile({ pronouns: newpronouns, bio: newbio, accent_color: parseInt("0x" + color.substr(1), 16) });
}
}, { initText: this.user.pronouns });
pronounbox.watchForChange(_ => {
@ -704,6 +752,20 @@ class Localuser {
hypouser.bio = new MarkDown(_, this);
regen();
});
if (this.user.accent_color) {
color = "#" + this.user.accent_color.toString(16);
}
else {
color = "transparent";
}
const colorPicker = settingsLeft.addColorInput("Profile color", (_) => { }, { initColor: color });
colorPicker.watchForChange(_ => {
console.log();
color = _;
hypouser.accent_color = parseInt("0x" + _.substr(1), 16);
changed = true;
regen();
});
}
{
const tas = settings.addButton("Themes & sounds");
@ -1022,6 +1084,7 @@ class Localuser {
}
//---------- resolving members code -----------
waitingmembers = new Map();
presences = new Map();
async resolvemember(id, guildid) {
if (!this.waitingmembers.has(guildid)) {
this.waitingmembers.set(guildid, new Map());
@ -1038,6 +1101,9 @@ class Localuser {
noncemap = new Map();
noncebuild = new Map();
async gotChunk(chunk) {
for (const thing of chunk.presences) {
this.presences.set(thing.user.id, thing);
}
console.log(chunk);
chunk.members ??= [];
const arr = this.noncebuild.get(chunk.nonce);

View file

@ -101,6 +101,9 @@ class Member {
}
else {
const member = new Member(membjson, guild);
const map = guild.localuser.presences;
member.getPresence(map.get(member.id));
map.delete(member.id);
res(member);
return member;
}
@ -112,6 +115,9 @@ class Member {
return maybe;
}
}
getPresence(presence) {
this.user.getPresence(presence);
}
/**
* @todo
*/

View file

@ -17,11 +17,13 @@ class User {
public_flags;
accent_color;
banner;
hypotheticalbanner;
premium_since;
premium_type;
theme_colors;
badge_ids;
members = new WeakMap();
status;
clone() {
return new User({
username: this.username,
@ -40,6 +42,25 @@ class User {
badge_ids: this.badge_ids
}, this.owner);
}
getPresence(presence) {
if (presence) {
this.setstatus(presence.status);
}
else {
this.setstatus("offline");
}
}
setstatus(status) {
this.status = status;
}
async getStatus() {
if (this.status) {
return this.status;
}
else {
return "offline";
}
}
get id() {
return this.snowflake.id;
}
@ -107,6 +128,27 @@ class User {
pfp.classList.add("userid:" + this.id);
return pfp;
}
async buildstatuspfp() {
const div = document.createElement("div");
div.style.position = "relative";
const pfp = this.buildpfp();
div.append(pfp);
{
const status = document.createElement("div");
status.classList.add("statusDiv");
switch (await this.getStatus()) {
case "offline":
status.classList.add("offlinestatus");
break;
case "online":
default:
status.classList.add("onlinestatus");
break;
}
div.append(status);
}
return div;
}
userupdate(json) {
if (json.avatar !== this.avatar) {
console.log;
@ -149,7 +191,7 @@ class User {
return this.avatar;
}
if (this.avatar != null) {
return this.info.cdn + "/avatars/" + this.id + "/" + this.avatar + ".png";
return this.info.cdn + "/avatars/" + this.id.replace("#clone", "") + "/" + this.avatar + ".png";
}
else {
const int = new Number((BigInt(this.id) >> 22n) % 6n);
@ -159,21 +201,42 @@ class User {
createjankpromises() {
new Promise(_ => { });
}
buildprofile(x, y) {
async buildprofile(x, y) {
if (Contextmenu.currentmenu != "") {
Contextmenu.currentmenu.remove();
}
const div = document.createElement("div");
if (this.accent_color) {
div.style.setProperty("--accent_color", "#" + this.accent_color.toString(16).padStart(6, "0"));
}
else {
div.style.setProperty("--accent_color", "transparent");
}
if (this.banner) {
const banner = document.createElement("img");
let src;
if (!this.hypotheticalbanner) {
src = this.info.cdn + "/avatars/" + this.id.replace("#clone", "") + "/" + this.banner + ".png";
}
else {
src = this.banner;
}
console.log(src, this.banner);
banner.src = src;
banner.classList.add("banner");
div.append(banner);
}
if (x !== -1) {
div.style.left = x + "px";
div.style.top = y + "px";
div.classList.add("profile", "flexttb");
}
else {
this.setstatus("online");
div.classList.add("hypoprofile", "flexttb");
}
{
const pfp = this.buildpfp();
const pfp = await this.buildstatuspfp();
div.appendChild(pfp);
}
{