get invite embeds working
This commit is contained in:
parent
7915032462
commit
1502dbec17
8 changed files with 500 additions and 46 deletions
135
.dist/embed.js
135
.dist/embed.js
|
@ -1,5 +1,7 @@
|
|||
import { Dialog } from "./dialog.js";
|
||||
import { MarkDown } from "./markdown.js";
|
||||
import { getapiurls, getInstances } from "./login.js";
|
||||
import { Guild } from "./guild.js";
|
||||
class Embed {
|
||||
type;
|
||||
owner;
|
||||
|
@ -8,8 +10,40 @@ class Embed {
|
|||
this.type = this.getType(json);
|
||||
this.owner = owner;
|
||||
this.json = json;
|
||||
console.log(this);
|
||||
}
|
||||
getType(json) {
|
||||
const instances = getInstances();
|
||||
if (instances && json.type === "link" && json.url && URL.canParse(json.url)) {
|
||||
const Url = new URL(json.url);
|
||||
for (const instance of instances) {
|
||||
if (instance.url && URL.canParse(instance.url)) {
|
||||
const IUrl = new URL(instance.url);
|
||||
const params = new URLSearchParams(Url.search);
|
||||
let host;
|
||||
if (params.has("instance")) {
|
||||
const url = params.get("instance");
|
||||
if (URL.canParse(url)) {
|
||||
host = new URL(url).host;
|
||||
}
|
||||
else {
|
||||
host = Url.host;
|
||||
}
|
||||
}
|
||||
else {
|
||||
host = Url.host;
|
||||
}
|
||||
if (IUrl.host === host) {
|
||||
const code = Url.pathname.split("/")[Url.pathname.split("/").length - 1];
|
||||
json.invite = {
|
||||
url: instance.url,
|
||||
code
|
||||
};
|
||||
return "invite";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return json.type || "rich";
|
||||
}
|
||||
generateHTML() {
|
||||
|
@ -18,6 +52,8 @@ class Embed {
|
|||
return this.generateRich();
|
||||
case "image":
|
||||
return this.generateImage();
|
||||
case "invite":
|
||||
return this.generateInvite();
|
||||
case "link":
|
||||
return this.generateLink();
|
||||
case "video":
|
||||
|
@ -185,6 +221,105 @@ class Embed {
|
|||
table.append(bottomtr);
|
||||
return table;
|
||||
}
|
||||
invcache;
|
||||
generateInvite() {
|
||||
if (this.invcache && (!this.json.invite || !this.localuser)) {
|
||||
return this.generateLink();
|
||||
}
|
||||
const div = document.createElement("div");
|
||||
div.classList.add("embed", "inviteEmbed", "flexttb");
|
||||
const json1 = this.json.invite;
|
||||
(async () => {
|
||||
let json;
|
||||
let info;
|
||||
if (!this.invcache) {
|
||||
if (!json1) {
|
||||
div.append(this.generateLink());
|
||||
return;
|
||||
}
|
||||
const tempinfo = await getapiurls(json1.url);
|
||||
;
|
||||
if (!tempinfo) {
|
||||
div.append(this.generateLink());
|
||||
return;
|
||||
}
|
||||
info = tempinfo;
|
||||
const res = await fetch(info.api + "/invites/" + json1.code);
|
||||
if (!res.ok) {
|
||||
div.append(this.generateLink());
|
||||
}
|
||||
json = await res.json();
|
||||
this.invcache = [json, info];
|
||||
}
|
||||
else {
|
||||
[json, info] = this.invcache;
|
||||
}
|
||||
if (!json) {
|
||||
div.append(this.generateLink());
|
||||
return;
|
||||
}
|
||||
if (json.guild.banner) {
|
||||
const banner = document.createElement("img");
|
||||
banner.src = this.localuser.info.cdn + "/icons/" + json.guild.id + "/" + json.guild.banner + ".png?size=256";
|
||||
banner.classList.add("banner");
|
||||
div.append(banner);
|
||||
}
|
||||
const guild = json.guild;
|
||||
guild.info = info;
|
||||
const icon = Guild.generateGuildIcon(guild);
|
||||
const iconrow = document.createElement("div");
|
||||
iconrow.classList.add("flexltr", "flexstart");
|
||||
iconrow.append(icon);
|
||||
{
|
||||
const guildinfo = document.createElement("div");
|
||||
guildinfo.classList.add("flexttb", "invguildinfo");
|
||||
const name = document.createElement("b");
|
||||
name.textContent = guild.name;
|
||||
guildinfo.append(name);
|
||||
const members = document.createElement("span");
|
||||
members.innerText = "#" + json.channel.name + " • Members: " + guild.member_count;
|
||||
guildinfo.append(members);
|
||||
members.classList.add("subtext");
|
||||
iconrow.append(guildinfo);
|
||||
}
|
||||
div.append(iconrow);
|
||||
const h2 = document.createElement("h2");
|
||||
h2.textContent = `You've been invited by ${json.inviter.username}`;
|
||||
div.append(h2);
|
||||
const button = document.createElement("button");
|
||||
button.textContent = "Accept";
|
||||
if (this.localuser.info.api.startsWith(info.api)) {
|
||||
if (this.localuser.guildids.has(guild.id)) {
|
||||
button.textContent = "Already joined";
|
||||
button.disabled = true;
|
||||
}
|
||||
}
|
||||
button.classList.add("acceptinvbutton");
|
||||
div.append(button);
|
||||
button.onclick = _ => {
|
||||
if (this.localuser.info.api.startsWith(info.api)) {
|
||||
fetch(this.localuser.info.api + "/invites/" + json.code, {
|
||||
method: "POST",
|
||||
headers: this.localuser.headers,
|
||||
}).then(r => r.json()).then(_ => {
|
||||
if (_.message) {
|
||||
alert(_.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (this.json.invite) {
|
||||
const params = new URLSearchParams("");
|
||||
params.set("instance", this.json.invite.url);
|
||||
const encoded = params.toString();
|
||||
const url = `${location.origin}/invite/${this.json.invite.code}?${encoded}`;
|
||||
window.open(url, "_blank");
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
return div;
|
||||
}
|
||||
generateArticle() {
|
||||
const colordiv = document.createElement("div");
|
||||
colordiv.style.backgroundColor = "#000000";
|
||||
|
|
|
@ -264,39 +264,62 @@ class Guild extends SnowFlake {
|
|||
return a.position - b.position;
|
||||
});
|
||||
}
|
||||
generateGuildIcon() {
|
||||
static generateGuildIcon(guild) {
|
||||
const divy = document.createElement("div");
|
||||
divy.classList.add("servernoti");
|
||||
const noti = document.createElement("div");
|
||||
noti.classList.add("unread");
|
||||
divy.append(noti);
|
||||
this.localuser.guildhtml.set(this.id, divy);
|
||||
if (this.properties.icon != null) {
|
||||
if (guild instanceof Guild) {
|
||||
guild.localuser.guildhtml.set(guild.id, divy);
|
||||
}
|
||||
let icon;
|
||||
if (guild instanceof Guild) {
|
||||
icon = guild.properties.icon;
|
||||
}
|
||||
else {
|
||||
icon = guild.icon;
|
||||
}
|
||||
if (icon !== null) {
|
||||
const img = document.createElement("img");
|
||||
img.classList.add("pfp", "servericon");
|
||||
img.src = this.info.cdn + "/icons/" + this.properties.id + "/" + this.properties.icon + ".png";
|
||||
img.src = guild.info.cdn + "/icons/" + guild.id + "/" + icon + ".png";
|
||||
divy.appendChild(img);
|
||||
img.onclick = () => {
|
||||
console.log(this.loadGuild);
|
||||
this.loadGuild();
|
||||
this.loadChannel();
|
||||
};
|
||||
Guild.contextmenu.bindContextmenu(img, this, undefined);
|
||||
if (guild instanceof Guild) {
|
||||
img.onclick = () => {
|
||||
console.log(guild.loadGuild);
|
||||
guild.loadGuild();
|
||||
guild.loadChannel();
|
||||
};
|
||||
Guild.contextmenu.bindContextmenu(img, guild, undefined);
|
||||
}
|
||||
}
|
||||
else {
|
||||
const div = document.createElement("div");
|
||||
const build = this.properties.name.replace(/'s /g, " ").replace(/\w+/g, word => word[0]).replace(/\s/g, "");
|
||||
let name;
|
||||
if (guild instanceof Guild) {
|
||||
name = guild.properties.name;
|
||||
}
|
||||
else {
|
||||
name = guild.name;
|
||||
}
|
||||
const build = name.replace(/'s /g, " ").replace(/\w+/g, word => word[0]).replace(/\s/g, "");
|
||||
div.textContent = build;
|
||||
div.classList.add("blankserver", "servericon");
|
||||
divy.appendChild(div);
|
||||
div.onclick = () => {
|
||||
this.loadGuild();
|
||||
this.loadChannel();
|
||||
};
|
||||
Guild.contextmenu.bindContextmenu(div, this, undefined);
|
||||
if (guild instanceof Guild) {
|
||||
div.onclick = () => {
|
||||
guild.loadGuild();
|
||||
guild.loadChannel();
|
||||
};
|
||||
Guild.contextmenu.bindContextmenu(div, guild, undefined);
|
||||
}
|
||||
}
|
||||
return divy;
|
||||
}
|
||||
generateGuildIcon() {
|
||||
return Guild.generateGuildIcon(this);
|
||||
}
|
||||
confirmDelete() {
|
||||
let confirmname = "";
|
||||
const full = new Dialog([
|
||||
|
|
|
@ -8,6 +8,7 @@ function setTheme() {
|
|||
}
|
||||
document.body.className = name + "-theme";
|
||||
}
|
||||
let instances;
|
||||
setTheme();
|
||||
function getBulkUsers() {
|
||||
const json = getBulkInfo();
|
||||
|
@ -418,8 +419,12 @@ trimswitcher();
|
|||
export { mobile, getBulkUsers, getBulkInfo, setTheme, Specialuser, getapiurls, adduser };
|
||||
const datalist = document.getElementById("instances");
|
||||
console.warn(datalist);
|
||||
if (datalist) {
|
||||
fetch("/instances.json").then(_ => _.json()).then((json) => {
|
||||
export function getInstances() {
|
||||
return instances;
|
||||
}
|
||||
fetch("/instances.json").then(_ => _.json()).then((json) => {
|
||||
instances = json;
|
||||
if (datalist) {
|
||||
console.warn(json);
|
||||
if (instancein && instancein.value === "") {
|
||||
instancein.value = json[0].name;
|
||||
|
@ -452,5 +457,5 @@ if (datalist) {
|
|||
datalist.append(option);
|
||||
}
|
||||
checkInstance("");
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue