fix url bugs and improve account switcher

This commit is contained in:
MathMan05 2024-08-11 15:16:49 -05:00
parent 0fe2966ad1
commit 5148e1b77b
19 changed files with 186 additions and 175 deletions

View file

@ -361,7 +361,7 @@ class Channel {
if (!this.hasunreads) {
return;
}
fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/messages/" + this.lastmessageid + "/ack", {
fetch(this.info.api + "/channels/" + this.snowflake + "/messages/" + this.lastmessageid + "/ack", {
method: "POST",
headers: this.headers,
body: JSON.stringify({})
@ -429,7 +429,7 @@ class Channel {
return div;
}
createChannel(name, type) {
fetch(this.info.api.toString() + "/guilds/" + this.guild.id + "/channels", {
fetch(this.info.api + "/guilds/" + this.guild.id + "/channels", {
method: "POST",
headers: this.headers,
body: JSON.stringify({
@ -452,7 +452,7 @@ class Channel {
["mdbox", "Channel topic:", this.topic, function () { topic = this.value; }],
["checkbox", "NSFW Channel", this.nsfw, function () { nsfw = this.checked; }],
["button", "", "submit", () => {
fetch(this.info.api.toString() + "/channels/" + thisid, {
fetch(this.info.api + "/channels/" + thisid, {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
@ -475,7 +475,7 @@ class Channel {
console.log(full);
}
deleteChannel() {
fetch(this.info.api.toString() + "/channels/" + this.snowflake, {
fetch(this.info.api + "/channels/" + this.snowflake, {
method: "DELETE",
headers: this.headers
});
@ -518,7 +518,7 @@ class Channel {
return snowflake.getObject();
}
else {
const gety = await fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/messages?limit=1&around=" + id, { headers: this.headers });
const gety = await fetch(this.info.api + "/channels/" + this.snowflake + "/messages?limit=1&around=" + id, { headers: this.headers });
const json = await gety.json();
return new Message(json[0], this);
}
@ -554,7 +554,7 @@ class Channel {
return;
}
;
const j = await fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/messages?limit=100", {
const j = await fetch(this.info.api + "/channels/" + this.snowflake + "/messages?limit=100", {
headers: this.headers,
});
const response = await j.json();
@ -591,7 +591,7 @@ class Channel {
if (id === this.lastmessage.id) {
return;
}
await fetch(this.info.api.toString() + "/channels/" + this.id + "/messages?limit=100&after=" + id, {
await fetch(this.info.api + "/channels/" + this.id + "/messages?limit=100&after=" + id, {
headers: this.headers
}).then((j) => { return j.json(); }).then(response => {
let previd = SnowFlake.getSnowFlakeFromID(id, Message);
@ -622,7 +622,7 @@ class Channel {
if (this.topid && id === this.topid.id) {
return;
}
await fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/messages?before=" + id + "&limit=100", {
await fetch(this.info.api + "/channels/" + this.snowflake + "/messages?before=" + id + "&limit=100", {
headers: this.headers
}).then((j) => { return j.json(); }).then((response) => {
if (response.length < 100) {
@ -725,7 +725,7 @@ class Channel {
return;
}
this.typing = new Date().getTime() + 6000;
fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/typing", {
fetch(this.info.api + "/channels/" + this.snowflake + "/typing", {
method: "POST",
headers: this.headers
});
@ -768,7 +768,7 @@ class Channel {
body.message_reference = replyjson;
}
console.log(body);
return await fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/messages", {
return await fetch(this.info.api + "/channels/" + this.snowflake + "/messages", {
method: "POST",
headers: this.headers,
body: JSON.stringify(body)
@ -789,7 +789,7 @@ class Channel {
console.log(attachments[i]);
formData.append("files[" + i + "]", attachments[i]);
}
return await fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/messages", {
return await fetch(this.info.api + "/channels/" + this.snowflake + "/messages", {
method: 'POST',
body: formData,
headers: { "Authorization": this.headers.Authorization }
@ -877,7 +877,7 @@ class Channel {
}
}
async addRoleToPerms(role) {
await fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/permissions/" + role.snowflake, {
await fetch(this.info.api + "/channels/" + this.snowflake + "/permissions/" + role.snowflake, {
method: "PUT",
headers: this.headers,
body: JSON.stringify({
@ -895,7 +895,7 @@ class Channel {
const permission = this.permission_overwrites.get(id);
permission.allow = perms.allow;
permission.deny = perms.deny;
await fetch(this.info.api.toString() + "/channels/" + this.snowflake + "/permissions/" + id, {
await fetch(this.info.api + "/channels/" + this.snowflake + "/permissions/" + id, {
method: "PUT",
headers: this.headers,
body: JSON.stringify({

View file

@ -117,7 +117,7 @@ class Guild {
noti
],
["button", "", "submit", _ => {
fetch(this.info.api.toString() + "/v9/users/@me/guilds/settings", {
fetch(this.info.api + "/v9/users/@me/guilds/settings", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
@ -161,7 +161,7 @@ class Guild {
full.show();
}
async leave() {
return fetch(this.info.api.toString() + "/users/@me/guilds/" + this.snowflake, {
return fetch(this.info.api + "/users/@me/guilds/" + this.snowflake, {
method: "DELETE",
headers: this.headers
});
@ -211,7 +211,7 @@ class Guild {
if (serverbug) {
for (const thing of build) {
console.log(build, thing);
fetch(this.info.api.toString() + "/v9/guilds/" + this.snowflake + "/channels", {
fetch(this.info.api + "/v9/guilds/" + this.snowflake + "/channels", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify([thing])
@ -219,7 +219,7 @@ class Guild {
}
}
else {
fetch(this.info.api.toString() + "/v9/guilds/" + this.snowflake + "/channels", {
fetch(this.info.api + "/v9/guilds/" + this.snowflake + "/channels", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify(build)
@ -245,7 +245,7 @@ class Guild {
if (this.properties.icon != null) {
const img = document.createElement("img");
img.classList.add("pfp", "servericon");
img.src = this.info.cdn.toString() + "/icons/" + this.properties.id + "/" + this.properties.icon + ".png";
img.src = this.info.cdn + "/icons/" + this.properties.id + "/" + this.properties.icon + ".png";
divy.appendChild(img);
img.onclick = () => {
console.log(this.loadGuild);
@ -311,7 +311,7 @@ class Guild {
full.show();
}
async delete() {
return fetch(this.info.api.toString() + "/guilds/" + this.snowflake + "/delete", {
return fetch(this.info.api + "/guilds/" + this.snowflake + "/delete", {
method: "POST",
headers: this.headers,
});
@ -364,7 +364,7 @@ class Guild {
}
}
this.unreads();
fetch(this.info.api.toString() + "/v9/read-states/ack-bulk", {
fetch(this.info.api + "/v9/read-states/ack-bulk", {
method: "POST",
headers: this.headers,
body: JSON.stringify(build)
@ -487,14 +487,14 @@ class Guild {
this.printServers();
}
createChannel(name, type) {
fetch(this.info.api.toString() + "/guilds/" + this.snowflake + "/channels", {
fetch(this.info.api + "/guilds/" + this.snowflake + "/channels", {
method: "POST",
headers: this.headers,
body: JSON.stringify({ name: name, type: type })
});
}
async createRole(name) {
const fetched = await fetch(this.info.api.toString() + "/guilds/" + this.snowflake + "roles", {
const fetched = await fetch(this.info.api + "/guilds/" + this.snowflake + "roles", {
method: "POST",
headers: this.headers,
body: JSON.stringify({
@ -513,7 +513,7 @@ class Guild {
const role = this.roleids[id];
role.permissions.allow = perms.allow;
role.permissions.deny = perms.deny;
await fetch(this.info.api.toString() + "/guilds/" + this.snowflake + "/roles/" + this.snowflake, {
await fetch(this.info.api + "/guilds/" + this.snowflake + "/roles/" + this.snowflake, {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({

View file

@ -26,36 +26,27 @@ thisuser.initwebsocket().then(_ => {
const userinfo = document.getElementById("userinfo");
const userdock = document.getElementById("userdock");
userinfo.addEventListener("click", function (event) {
const table = document.createElement("table");
const table = document.createElement("div");
for (const thing of Object.values(users.users)) {
const specialuser = thing;
console.log(specialuser.pfpsrc);
const tr = document.createElement("tr");
const td = document.createElement("td");
const userinfo = document.createElement("table");
userinfo.classList.add("switchtable");
const row = document.createElement("tr");
userinfo.append(row);
const pfpcell = document.createElement("td");
row.append(pfpcell);
const userinfo = document.createElement("div");
userinfo.classList.add("flexltr", "switchtable");
const pfp = document.createElement("img");
pfpcell.append(pfp);
const usertd = document.createElement("td");
row.append(usertd);
userinfo.append(pfp);
const user = document.createElement("div");
usertd.append(user);
userinfo.append(user);
user.append(specialuser.username);
user.append(document.createElement("br"));
const span = document.createElement("span");
span.textContent = specialuser.serverurls.wellknown.hostname;
span.textContent = specialuser.serverurls.wellknown.replace("https://", "").replace("http://", "");
user.append(span);
user.classList.add("userinfo");
span.classList.add("serverURL");
pfp.src = specialuser.pfpsrc;
pfp.classList.add("pfp");
td.append(userinfo);
tr.append(td);
table.append(tr);
tr.addEventListener("click", _ => {
table.append(userinfo);
userinfo.addEventListener("click", _ => {
thisuser.unload();
document.getElementById("loading").classList.remove("doneloading");
document.getElementById("loading").classList.add("loading");
@ -72,14 +63,13 @@ thisuser.initwebsocket().then(_ => {
});
}
{
const tr = document.createElement("tr");
const td = document.createElement("td");
tr.append(td);
const td = document.createElement("div");
td.classList.add("switchtable");
td.append("Switch accounts ⇌");
td.addEventListener("click", _ => {
window.location.href = "/login.html";
});
table.append(tr);
table.append(td);
}
table.classList.add("accountSwitcher");
if (Contextmenu.currentmenu != "") {
@ -87,7 +77,7 @@ thisuser.initwebsocket().then(_ => {
}
Contextmenu.currentmenu = table;
console.log(table);
userdock.before(table);
document.body.append(table);
event.stopImmediatePropagation();
});
}

View file

@ -469,7 +469,7 @@ class Localuser {
else {
parsed = inviteurl;
}
fetch(this.info.api.toString() + "/v9/invites/" + parsed, {
fetch(this.info.api + "/v9/invites/" + parsed, {
method: "POST",
headers: this.headers,
}).then(r => r.json()).then(_ => {
@ -492,7 +492,7 @@ class Localuser {
content.textContent = "Loading...";
const full = new Fullscreen(["html", content]);
full.show();
const res = await fetch(this.info.api.toString() + "/v9/discoverable-guilds?limit=50", {
const res = await fetch(this.info.api + "/v9/discoverable-guilds?limit=50", {
headers: this.headers
});
const json = await res.json();
@ -509,7 +509,7 @@ class Localuser {
const banner = document.createElement("img");
banner.classList.add("banner");
banner.crossOrigin = "anonymous";
banner.src = this.info.cdn.toString() + "icons/" + guild.id + "/" + guild.banner + ".png?size=256";
banner.src = this.info.cdn + "icons/" + guild.id + "/" + guild.banner + ".png?size=256";
banner.alt = "";
content.appendChild(banner);
}
@ -518,7 +518,7 @@ class Localuser {
const img = document.createElement("img");
img.classList.add("icon");
img.crossOrigin = "anonymous";
img.src = this.info.cdn.toString() + (guild.icon ? ("/icons/" + guild.id + "/" + guild.icon + ".png?size=48") : "/embed/avatars/3.png");
img.src = this.info.cdn + (guild.icon ? ("/icons/" + guild.id + "/" + guild.icon + ".png?size=48") : "/embed/avatars/3.png");
img.alt = "";
nameContainer.appendChild(img);
const name = document.createElement("h3");
@ -529,7 +529,7 @@ class Localuser {
desc.textContent = guild.description;
content.appendChild(desc);
content.addEventListener("click", async () => {
const joinRes = await fetch(this.info.api.toString() + "/v9/guilds/" + guild.id + "/members/@me", {
const joinRes = await fetch(this.info.api + "/v9/guilds/" + guild.id + "/members/@me", {
method: "PUT",
headers: this.headers
});
@ -588,7 +588,7 @@ class Localuser {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
fetch(this.info.api.toString() + "/users/@me", {
fetch(this.info.api + "/users/@me", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
@ -598,7 +598,7 @@ class Localuser {
};
}
updatepronouns(pronouns) {
fetch(this.info.api.toString() + "/users/@me/profile", {
fetch(this.info.api + "/users/@me/profile", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
@ -607,7 +607,7 @@ class Localuser {
});
}
updatebio(bio) {
fetch(this.info.api.toString() + "/v9/users/@me/profile", {
fetch(this.info.api + "/v9/users/@me/profile", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
@ -730,7 +730,7 @@ class Localuser {
const security = settings.addButton("Account Security");
if (this.mfa_enabled) {
security.addTextInput("Disable 2FA, totp code:", _ => {
fetch(this.info.api.toString() + "/users/@me/mfa/totp/disable", {
fetch(this.info.api + "/users/@me/mfa/totp/disable", {
method: "POST",
headers: this.headers,
body: JSON.stringify({
@ -762,7 +762,7 @@ class Localuser {
["textbox", "Account password:", "", function () { password = this.value; }],
["textbox", "Code:", "", function () { code = this.value; }],
["button", "", "Submit", () => {
fetch(this.info.api.toString() + "/users/@me/mfa/totp/enable/", {
fetch(this.info.api + "/users/@me/mfa/totp/enable/", {
method: "POST",
headers: this.headers,
body: JSON.stringify({
@ -800,7 +800,7 @@ class Localuser {
connectionContainer
], () => { }, async () => {
connectionContainer.innerHTML = "";
const res = await fetch(this.info.api.toString() + "/v9/connections", {
const res = await fetch(this.info.api + "/v9/connections", {
headers: this.headers
});
const json = await res.json();
@ -810,7 +810,7 @@ class Localuser {
container.textContent = key.charAt(0).toUpperCase() + key.slice(1);
if (connection.enabled) {
container.addEventListener("click", async () => {
const connectionRes = await fetch(this.info.api.toString() + "/v9/connections/" + key + "/authorize", {
const connectionRes = await fetch(this.info.api + "/v9/connections/" + key + "/authorize", {
headers: this.headers
});
const connectionJSON = await connectionRes.json();
@ -838,7 +838,7 @@ class Localuser {
async () => {
if (appName.trim().length == 0)
return alert("Please enter a name for the application.");
const res = await fetch(this.info.api.toString() + "/v9/applications", {
const res = await fetch(this.info.api + "/v9/applications", {
method: "POST",
headers: this.headers,
body: JSON.stringify({
@ -856,7 +856,7 @@ class Localuser {
]
], () => { }, async () => {
appListContainer.innerHTML = "";
const res = await fetch(this.info.api.toString() + "/v9/applications", {
const res = await fetch(this.info.api + "/v9/applications", {
headers: this.headers
});
const json = await res.json();
@ -865,7 +865,7 @@ class Localuser {
if (application.cover_image) {
const cover = document.createElement("img");
cover.crossOrigin = "anonymous";
cover.src = this.info.cdn.toString() + "/app-icons/" + application.id + "/" + application.cover_image + ".png?size=256";
cover.src = this.info.cdn + "/app-icons/" + application.id + "/" + application.cover_image + ".png?size=256";
cover.alt = "";
cover.loading = "lazy";
container.appendChild(cover);
@ -882,7 +882,7 @@ class Localuser {
});
}
async manageApplication(appId = "") {
const res = await fetch(this.info.api.toString() + "/v9/applications/" + appId, {
const res = await fetch(this.info.api + "/v9/applications/" + appId, {
headers: this.headers
});
const json = await res.json();
@ -899,7 +899,7 @@ class Localuser {
fields.description = event.target.value;
}],
["vdiv",
json.icon ? ["img", this.info.cdn.toString() + "/app-icons/" + appId + "/" + json.icon + ".png?size=128", [128, 128]] : ["text", "No icon"],
json.icon ? ["img", this.info.cdn + "/app-icons/" + appId + "/" + json.icon + ".png?size=128", [128, 128]] : ["text", "No icon"],
["fileupload", "Application icon:", event => {
const reader = new FileReader();
reader.readAsDataURL(event.target.files[0]);
@ -930,7 +930,7 @@ class Localuser {
"",
"Save changes",
async () => {
const updateRes = await fetch(this.info.api.toString() + "/v9/applications/" + appId, {
const updateRes = await fetch(this.info.api + "/v9/applications/" + appId, {
method: "PATCH",
headers: this.headers,
body: JSON.stringify(fields)
@ -950,7 +950,7 @@ class Localuser {
if (!json.bot) {
if (!confirm("Are you sure you want to add a bot to this application? There's no going back."))
return;
const updateRes = await fetch(this.info.api.toString() + "/v9/applications/" + appId + "/bot", {
const updateRes = await fetch(this.info.api + "/v9/applications/" + appId + "/bot", {
method: "POST",
headers: this.headers
});
@ -966,7 +966,7 @@ class Localuser {
appDialog.show();
}
async manageBot(appId = "") {
const res = await fetch(this.info.api.toString() + "/v9/applications/" + appId, {
const res = await fetch(this.info.api + "/v9/applications/" + appId, {
headers: this.headers
});
const json = await res.json();
@ -974,7 +974,7 @@ class Localuser {
return alert("For some reason, this application doesn't have a bot (yet).");
const fields = {
username: json.bot.username,
avatar: json.bot.avatar ? (this.info.cdn.toString() + "/app-icons/" + appId + "/" + json.bot.avatar + ".png?size=256") : ""
avatar: json.bot.avatar ? (this.info.cdn + "/app-icons/" + appId + "/" + json.bot.avatar + ".png?size=256") : ""
};
const botDialog = new Fullscreen(["vdiv",
["title",
@ -1000,7 +1000,7 @@ class Localuser {
"",
"Save changes",
async () => {
const updateRes = await fetch(this.info.api.toString() + "/v9/applications/" + appId + "/bot", {
const updateRes = await fetch(this.info.api + "/v9/applications/" + appId + "/bot", {
method: "PATCH",
headers: this.headers,
body: JSON.stringify(fields)
@ -1019,7 +1019,7 @@ class Localuser {
async () => {
if (!confirm("Are you sure you want to reset the bot token? Your bot will stop working until you update it."))
return;
const updateRes = await fetch(this.info.api.toString() + "/v9/applications/" + appId + "/bot/reset", {
const updateRes = await fetch(this.info.api + "/v9/applications/" + appId + "/bot/reset", {
method: "POST",
headers: this.headers
});

View file

@ -65,10 +65,16 @@ class Specialuser {
console.error("specialuser can't construct from another specialuser");
}
this.serverurls = json.serverurls;
this.serverurls.api = new URL(this.serverurls.api);
this.serverurls.cdn = new URL(this.serverurls.cdn);
this.serverurls.gateway = new URL(this.serverurls.gateway);
this.serverurls.wellknown = new URL(this.serverurls.wellknown);
let apistring = new URL(json.serverurls.api).toString();
apistring = apistring.replace(/\/(v\d+\/?)?$/, "") + "/v9";
this.serverurls.api = apistring;
this.serverurls.cdn = new URL(json.serverurls.cdn).toString().replace(/\/$/, "");
this.serverurls.gateway = new URL(json.serverurls.gateway).toString().replace(/\/$/, "");
;
this.serverurls.wellknown = new URL(json.serverurls.wellknown).toString().replace(/\/$/, "");
;
this.serverurls.login = new URL(json.serverurls.login).toString().replace(/\/$/, "");
;
this.email = json.email;
this.token = json.token;
this.loggedin = json.loggedin;

View file

@ -455,7 +455,7 @@ class MarkDown {
emojiElem.classList.add("md-emoji");
emojiElem.classList.add(isEmojiOnly ? "bigemoji" : "smallemoji");
emojiElem.crossOrigin = "anonymous";
emojiElem.src = this.info.cdn.toString() + "emojis/" + parts[2] + "." + (parts[1] ? "gif" : "png") + "?size=32";
emojiElem.src = this.info.cdn + "emojis/" + parts[2] + "." + (parts[1] ? "gif" : "png") + "?size=32";
emojiElem.alt = buildjoin;
emojiElem.loading = "lazy";
span.appendChild(emojiElem);

View file

@ -16,7 +16,7 @@ class Member {
navigator.clipboard.writeText(this.id);
});
this.contextmenu.addbutton("Message user", function () {
fetch(this.info.api.toString() + "/v9/users/@me/channels", { method: "POST",
fetch(this.info.api + "/v9/users/@me/channels", { method: "POST",
body: JSON.stringify({ "recipients": [this.id] }),
headers: this.headers
});

View file

@ -79,7 +79,7 @@ class Message {
break;
}
}
fetch(this.info.api.toString() + "/channels/" + this.channel.id + "/messages/" + this.id + "/reactions/" + encodeURIComponent(emoji) + "/@me", {
fetch(this.info.api + "/channels/" + this.channel.id + "/messages/" + this.id + "/reactions/" + encodeURIComponent(emoji) + "/@me", {
method: remove ? "DELETE" : "PUT",
headers: this.headers,
});
@ -184,14 +184,14 @@ class Message {
return build;
}
async edit(content) {
return await fetch(this.info.api.toString() + "/channels/" + this.channel.snowflake + "/messages/" + this.id, {
return await fetch(this.info.api + "/channels/" + this.channel.snowflake + "/messages/" + this.id, {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({ content: content })
});
}
delete() {
fetch(`${this.info.api.toString()}/channels/${this.channel.snowflake}/messages/${this.id}`, {
fetch(`${this.info.api}/channels/${this.channel.snowflake}/messages/${this.id}`, {
headers: this.headers,
method: "DELETE",
});

View file

@ -48,7 +48,7 @@ class User {
navigator.clipboard.writeText(this.id.id);
});
this.contextmenu.addbutton("Message user", function () {
fetch(this.info.api.toString() + "/v9/users/@me/channels", { method: "POST",
fetch(this.info.api + "/v9/users/@me/channels", { method: "POST",
body: JSON.stringify({ "recipients": [this.id.id] }),
headers: this.localuser.headers
});
@ -141,10 +141,10 @@ class User {
return this.avatar;
}
if (this.avatar != null) {
return this.info.cdn.toString() + "/avatars/" + this.id + "/" + this.avatar + ".png";
return this.info.cdn + "/avatars/" + this.id + "/" + this.avatar + ".png";
}
else {
return this.info.cdn.toString() + "/embed/avatars/3.png";
return this.info.cdn + "/embed/avatars/3.png";
}
}
createjankpromises() {