Added support for different instances

This commit is contained in:
MathMan05 2024-06-08 12:48:52 -05:00
parent e219ae7fc7
commit f6d33a7aa8
8 changed files with 96 additions and 30 deletions

View file

@ -189,7 +189,7 @@ class channel{
if(!this.hasunreads){ if(!this.hasunreads){
return; return;
} }
fetch("https://old.server.spacebar.chat/api/v9/channels/"+this.id+"/messages/"+this.lastmessageid+"/ack",{ fetch(info.api.toString()+"/v9/channels/"+this.id+"/messages/"+this.lastmessageid+"/ack",{
method:"POST", method:"POST",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}, headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
body:JSON.stringify({}) body:JSON.stringify({})
@ -259,7 +259,7 @@ class channel{
return div; return div;
} }
createChannel(name,type){ createChannel(name,type){
fetch("https://api.old.server.spacebar.chat/api/guilds/"+this.owner.id+"/channels",{ fetch(info.api.toString()+"/guilds/"+this.owner.id+"/channels",{
method:"Post", method:"Post",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}, headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
body:JSON.stringify({ body:JSON.stringify({
@ -283,7 +283,7 @@ class channel{
["mdbox","Channel topic:",this.topic,function(){topic=this.value}], ["mdbox","Channel topic:",this.topic,function(){topic=this.value}],
["checkbox","NSFW Channel",this.nsfw,function(){nsfw=this.checked}], ["checkbox","NSFW Channel",this.nsfw,function(){nsfw=this.checked}],
["button","","submit",function(){ ["button","","submit",function(){
fetch("https://api.old.server.spacebar.chat/api/v9/channels/"+thisid,{ fetch(info.api.toString()+"/v9/channels/"+thisid,{
method:"PATCH", method:"PATCH",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}, headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
body:JSON.stringify({ body:JSON.stringify({
@ -307,7 +307,7 @@ class channel{
console.log(full) console.log(full)
} }
deleteChannel(){ deleteChannel(){
fetch("https://api.old.server.spacebar.chat/api/v9/channels/"+this.id,{ fetch(info.api.toString()+"/v9/channels/"+this.id,{
method:"DELETE", method:"DELETE",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token} headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}
}) })
@ -321,7 +321,7 @@ class channel{
} }
putmessages(){ putmessages(){
const out=this; const out=this;
fetch("https://api.old.server.spacebar.chat/api/channels/"+this.id+"/messages?limit=100",{ fetch(info.api.toString()+"/channels/"+this.id+"/messages?limit=100",{
method: 'GET', method: 'GET',
headers: {Authorization:token}, headers: {Authorization:token},
}).then((j)=>{return j.json()}).then(function(responce){ }).then((j)=>{return j.json()}).then(function(responce){
@ -353,7 +353,7 @@ class channel{
} }
const out=this; const out=this;
await fetch("https://api.old.server.spacebar.chat/api/channels/"+this.id+"/messages?before="+this.messages[this.messages.length-1].id+"&limit=100",{ await fetch(info.api.toString()+"/channels/"+this.id+"/messages?before="+this.messages[this.messages.length-1].id+"&limit=100",{
method:"GET", method:"GET",
headers:{Authorization:token} headers:{Authorization:token}
}).then((j)=>{return j.json()}).then(function(responce){ }).then((j)=>{return j.json()}).then(function(responce){
@ -419,7 +419,7 @@ class channel{
return; return;
} }
this.typing=new Date().getTime()+6000; this.typing=new Date().getTime()+6000;
fetch("https://api.old.server.spacebar.chat/api/channels/"+this.id+"/typing",{ fetch(info.api.toString()+"/channels/"+this.id+"/typing",{
method:"POST", method:"POST",
headers:{Authorization:token} headers:{Authorization:token}
}) })

View file

@ -73,14 +73,14 @@ class guild{
if(serverbug){ if(serverbug){
for(const thing of build){ for(const thing of build){
console.log(build,thing) console.log(build,thing)
fetch("https://api.old.server.spacebar.chat/api/v9/guilds/"+this.id+"/channels",{ fetch(info.api.toString()+"/v9/guilds/"+this.id+"/channels",{
method:"PATCH", method:"PATCH",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}, headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
body:JSON.stringify([thing]) body:JSON.stringify([thing])
}); });
} }
}else{ }else{
fetch("https://api.old.server.spacebar.chat/api/v9/guilds/"+this.id+"/channels",{ fetch(info.api.toString()+"/v9/guilds/"+this.id+"/channels",{
method:"PATCH", method:"PATCH",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}, headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
body:JSON.stringify(build) body:JSON.stringify(build)
@ -214,7 +214,7 @@ class guild{
this.channels=build; this.channels=build;
} }
createChannel(name,type){ createChannel(name,type){
fetch("https://api.old.server.spacebar.chat/api/guilds/"+this.id+"/channels",{ fetch(info.api.toString()+"/guilds/"+this.id+"/channels",{
method:"Post", method:"Post",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}, headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
body:JSON.stringify({name: name, type: type}) body:JSON.stringify({name: name, type: type})

View file

@ -1,3 +1,8 @@
const info=JSON.parse(localStorage.getItem("instanceinfo"));
info.api=new URL(info.api);
info.cdn=new URL(info.cdn);
info.gateway=new URL(info.gateway);
info.wellknown=new URL(info.wellknown);
function setDynamicHeight() { function setDynamicHeight() {
var servertdHeight = document.getElementById('servertd').offsetHeight+document.getElementById('typebox').offsetHeight+document.getElementById('pasteimage').offsetHeight; var servertdHeight = document.getElementById('servertd').offsetHeight+document.getElementById('typebox').offsetHeight+document.getElementById('pasteimage').offsetHeight;
document.documentElement.style.setProperty('--servertd-height', servertdHeight + 'px'); document.documentElement.style.setProperty('--servertd-height', servertdHeight + 'px');
@ -200,7 +205,7 @@ function makemenu(divmessage,x,y){
const dmbutton=createbutton("Message user",null,function(){ const dmbutton=createbutton("Message user",null,function(){
console.log(this) console.log(this)
fetch("https://old.server.spacebar.chat/api/v9/users/@me/channels", fetch(info.api.toString()+"/v9/users/@me/channels",
{method:"POST", {method:"POST",
body:JSON.stringify({"recipients":[this.all.author.id]}), body:JSON.stringify({"recipients":[this.all.author.id]}),
headers: {"Content-type": "application/json; charset=UTF-8",Authorization:token} headers: {"Content-type": "application/json; charset=UTF-8",Authorization:token}
@ -306,7 +311,7 @@ async function enter(event){
if(event.key === "Enter"&&!event.shiftKey){ if(event.key === "Enter"&&!event.shiftKey){
event.preventDefault(); event.preventDefault();
if(editing){ if(editing){
fetch("https://api.old.server.spacebar.chat/api/channels/"+window.location.pathname.split("/")[3]+"/messages/"+editing,{ fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages/"+editing,{
method: "PATCH", method: "PATCH",
headers: { headers: {
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
@ -340,7 +345,7 @@ async function enter(event){
body.message_reference=replyjson; body.message_reference=replyjson;
} }
console.log(body) console.log(body)
fetch("https://api.old.server.spacebar.chat/api/channels/"+window.location.pathname.split("/")[3]+"/messages",{ fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages",{
method:"POST", method:"POST",
headers:{ headers:{
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
@ -369,7 +374,7 @@ async function enter(event){
} }
const data=formData.entries() const data=formData.entries()
console.log(data.next(),data.next(),data.next()) console.log(data.next(),data.next(),data.next())
console.log((await fetch("https://api.old.server.spacebar.chat/api/channels/"+window.location.pathname.split("/")[3]+"/messages", { console.log((await fetch(info.api.toString()+"/channels/"+window.location.pathname.split("/")[3]+"/messages", {
method: 'POST', method: 'POST',
body: formData, body: formData,
headers:{ headers:{
@ -395,7 +400,7 @@ let thisuser=null;
function initwebsocket(){ function initwebsocket(){
ws = new WebSocket('wss://gateway.old.server.spacebar.chat/?v=9&encoding=json'); ws = new WebSocket(info.gateway.toString());
ws.addEventListener('open', (event) => { ws.addEventListener('open', (event) => {
console.log('WebSocket connected'); console.log('WebSocket connected');
@ -649,10 +654,7 @@ function genusersettings(){
}) })
} }
setTheme(); setTheme();
function setTheme(){
const name=localStorage.getItem("theme");
document.body.className=name+"-theme";
}
function userSettings(){ function userSettings(){
usersettings.show(); usersettings.show();
} }

View file

@ -121,7 +121,7 @@ class localuser{
if(thing.properties.icon!=null){ if(thing.properties.icon!=null){
const img=document.createElement("img"); const img=document.createElement("img");
img.classList.add("pfp","servericon") img.classList.add("pfp","servericon")
img.src="https://cdn.old.server.spacebar.chat/icons/"+thing.properties.id+"/"+thing.properties.icon+".png"; img.src=info.cdn.toString()+"icons/"+thing.properties.id+"/"+thing.properties.icon+".png";
divy.appendChild(img) divy.appendChild(img)
img.all=thing; img.all=thing;
img.onclick=function(){ img.onclick=function(){
@ -193,7 +193,7 @@ class localuser{
var reader = new FileReader(); var reader = new FileReader();
reader.readAsDataURL(file); reader.readAsDataURL(file);
reader.onload = function () { reader.onload = function () {
fetch("https://old.server.spacebar.chat/api/v9/users/@me",{ fetch(info.api.toString()+"/v9/users/@me",{
method:"PATCH", method:"PATCH",
headers:{ headers:{
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
@ -208,7 +208,7 @@ class localuser{
} }
updatepronouns(pronouns){ updatepronouns(pronouns){
fetch("https://old.server.spacebar.chat/api/v9/users/@me/profile",{ fetch(info.api.toString()+"/v9/users/@me/profile",{
method:"PATCH", method:"PATCH",
headers:{ headers:{
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
@ -220,7 +220,7 @@ class localuser{
}); });
} }
updatebio(bio){ updatebio(bio){
fetch("https://old.server.spacebar.chat/api/v9/users/@me/profile",{ fetch(info.api.toString()+"/v9/users/@me/profile",{
method:"PATCH", method:"PATCH",
headers:{ headers:{
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",

View file

@ -5,6 +5,10 @@
<div id="logindiv"> <div id="logindiv">
<h1>Login</h1><br> <h1>Login</h1><br>
<form id="form" submit="check(e)"> <form id="form" submit="check(e)">
<label for="instance"><b>Instance:</b></label><br>
<p id="verify"></p>
<input type="text" placeholder="Instance URL" name="instance" value="https://spacebar.chat/" id="instancein" required><br><br>
<label for="uname"><b>Username:</b></label><br> <label for="uname"><b>Username:</b></label><br>
<input type="text" placeholder="Enter Username" name="uname" required><br><br> <input type="text" placeholder="Enter Username" name="uname" required><br><br>

View file

@ -1,6 +1,40 @@
function getCookie(name) { function getCookie(name) {
return localStorage.getItem(name); return localStorage.getItem(name);
} }
function setTheme(){
const name=localStorage.getItem("theme");
document.body.className=name+"-theme";
}
setTheme();
{
const instancein=document.getElementById("instancein");
let timeout=0;
if(instancein){
console.log(instancein)
instancein.addEventListener("keydown",e=>{
const verify=document.getElementById("verify");
verify.innerText="Waiting to check Instance"
clearTimeout(timeout);
timeout=setTimeout(async e=>{
try{
verify.innerText="Checking Instance"
instanceinfo=await setInstance(instancein.value)
localStorage.setItem("instanceinfo",JSON.stringify(instanceinfo));
verify.innerText="Instance is all good"
setTimeout(_=>{
console.log(verify.innerText)
verify.innerText="";
},3000);
}catch(e){
console.log("catch")
verify.innerText="Invalid Instance, try again"
}
},1000);
});
}
}
async function login(username, password){ async function login(username, password){
const options={ const options={
method: "POST", method: "POST",
@ -13,7 +47,9 @@ async function login(username, password){
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
}} }}
try{ try{
return await fetch('https://api.old.server.spacebar.chat/api/auth/login',options).then(responce=>responce.json()) const info=JSON.parse(localStorage.getItem("instanceinfo"));
url=new URL(info.login);
return await fetch(url.origin+'/api/auth/login',options).then(responce=>responce.json())
.then((response) => { .then((response) => {
console.log(response,response.message) console.log(response,response.message)
if("Invalid Form Body"===response.message){ if("Invalid Form Body"===response.message){
@ -28,6 +64,30 @@ async function login(username, password){
console.error('Error:', error); console.error('Error:', error);
}; };
} }
async function setInstance(url){
url=new URL(url);
async function attempt(aurl){
const info=await fetch(`${aurl.toString()}${aurl.pathname.includes("api") ? "" : "api"}/policies/instance/domains`)
.then((x) => x.json());
return {
api: info.apiEndpoint,
gateway: info.gateway,
cdn: info.cdn,
wellknown: url,
login:aurl.toString()
}
}
try{
return await attempt(url);
}catch(e){
}
const wellKnown = await fetch(`${url.origin}/.well-known/spacebar`)
.then((x) => x.json())
.then((x) => new URL(x.api));
return await attempt(wellKnown);
}
function gettoken(){ function gettoken(){
let temp=getCookie("token"); let temp=getCookie("token");
temp??=undefined; temp??=undefined;
@ -40,7 +100,7 @@ function gettoken(){
async function check(e){ async function check(e){
e.preventDefault(); e.preventDefault();
let h=await login(e.srcElement[0].value,e.srcElement[1].value); let h=await login(e.srcElement[1].value,e.srcElement[2].value);
document.getElementById("wrong").innerText=h; document.getElementById("wrong").innerText=h;
console.log(h); console.log(h);
} }

View file

@ -36,7 +36,7 @@ class cmessage{
line2.classList.add("reply"); line2.classList.add("reply");
line.classList.add("startreply"); line.classList.add("startreply");
replyline.classList.add("replyflex") replyline.classList.add("replyflex")
fetch("https://old.server.spacebar.chat/api/v9/channels/"+this.message_reference.channel_id+"/messages?limit=1&around="+this.message_reference.message_id,{headers:{Authorization:token}}).then(responce=>responce.json()).then(responce=>{ fetch(info.api.toString()+"/v9/channels/"+this.message_reference.channel_id+"/messages?limit=1&around="+this.message_reference.message_id,{headers:{Authorization:token}}).then(responce=>responce.json()).then(responce=>{
const author=new user(responce[0].author); const author=new user(responce[0].author);
reply.appendChild(markdown(responce[0].content)); reply.appendChild(markdown(responce[0].content));
@ -107,7 +107,7 @@ class cmessage{
const attatch = document.createElement("tr") const attatch = document.createElement("tr")
for(const thing of this.attachments){ for(const thing of this.attachments){
const array=thing.url.split("/");array.shift();array.shift();array.shift(); const array=thing.url.split("/");array.shift();array.shift();array.shift();
const src="https://cdn.old.server.spacebar.chat/"+array.join("/"); const src=info.cdn.toString()+array.join("/");
if(thing.content_type.startsWith('image/')){ if(thing.content_type.startsWith('image/')){
const img=document.createElement("img"); const img=document.createElement("img");
img.classList.add("messageimg") img.classList.add("messageimg")

View file

@ -28,7 +28,7 @@ class user{
}else{ }else{
const tempy=new Promise((resolve, reject) => { const tempy=new Promise((resolve, reject) => {
usercache[this.id+"+"+guild.id]={done:false}; usercache[this.id+"+"+guild.id]={done:false};
fetch("https://old.server.spacebar.chat/api/v9/users/"+this.id+"/profile?with_mutual_guilds=true&with_mutual_friends_count=false&guild_id="+guild.id).then(json).then(str=>{ fetch(info.api.toString()+"/v9/users/"+this.id+"/profile?with_mutual_guilds=true&with_mutual_friends_count=false&guild_id="+guild.id).then(json).then(str=>{
return new member(str); return new member(str);
}); });
}); });
@ -62,9 +62,9 @@ class user{
return this.avatar; return this.avatar;
} }
if(this.avatar!=null){ if(this.avatar!=null){
return "https://cdn.old.server.spacebar.chat/avatars/"+this.id+"/"+this.avatar+".png"; return info.cdn.toString()+"avatars/"+this.id+"/"+this.avatar+".png";
}else{ }else{
return "https://cdn.old.server.spacebar.chat/embed/avatars/3.png"; return info.cdn.toString()+"embed/avatars/3.png";
} }
} }
createjankpromises(){ createjankpromises(){