Various changes cleaning up how user data is stored

This commit is contained in:
MathMan05 2024-06-15 00:22:50 -05:00
parent 618cbf6167
commit 3ed09d615d
7 changed files with 163 additions and 50 deletions

View file

@ -22,11 +22,14 @@ class channel{
},null,_=>{return _.isAdmin()});
}
constructor(JSON,owner){
if(JSON===-1){
return;
}
this.type=JSON.type;
this.owner=owner;
this.headers=this.owner.headers;
console.log(this.headers)
this.messages=[];
this.name=JSON.name;
this.id=JSON.id;
@ -216,7 +219,7 @@ class channel{
}
fetch(info.api.toString()+"/v9/channels/"+this.id+"/messages/"+this.lastmessageid+"/ack",{
method:"POST",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
headers:this.headers,
body:JSON.stringify({})
});
this.lastreadmessageid=this.lastmessageid;
@ -286,7 +289,7 @@ class channel{
createChannel(name,type){
fetch(info.api.toString()+"/guilds/"+this.owner.id+"/channels",{
method:"Post",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
headers:this.headers,
body:JSON.stringify({
name: name,
type: type,
@ -310,7 +313,7 @@ class channel{
["button","","submit",function(){
fetch(info.api.toString()+"/v9/channels/"+thisid,{
method:"PATCH",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
headers:this.headers,
body:JSON.stringify({
"name": name,
"type": thistype,
@ -334,7 +337,7 @@ class channel{
deleteChannel(){
fetch(info.api.toString()+"/v9/channels/"+this.id,{
method:"DELETE",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token}
headers:this.headers
})
}
getHTML(){
@ -348,13 +351,13 @@ class channel{
const out=this;
fetch(info.api.toString()+"/channels/"+this.id+"/messages?limit=100",{
method: 'GET',
headers: {Authorization:token},
}).then((j)=>{return j.json()}).then(function(responce){
headers: this.headers,
}).then((j)=>{return j.json()}).then(responce=>{
messages.innerHTML = '';
//responce.reverse()
messagelist=[];
for(const thing of responce){
const messager=new cmessage(thing)
const messager=new cmessage(thing,this)
if(out.messageids[messager.id]==undefined){
out.messageids[messager.id]=messager;
out.messages.push(messager);
@ -391,12 +394,12 @@ class channel{
for(const i in responce){
let messager
if(!next){
messager=new cmessage(responce[i])
messager=new cmessage(responce[i],this)
}else{
messager=next;
}
if(responce[+i+1]!==undefined){
next=new cmessage(responce[+i+1]);
next=new cmessage(responce[+i+1],this);
}else{
next=undefined;
console.log("ohno",+i+1)
@ -450,7 +453,7 @@ class channel{
})
}
messageCreate(messagep,focus){
const messagez=new cmessage(messagep.d);
const messagez=new cmessage(messagep.d,this);
this.lastmessageid=messagez.id;
if(messagez.author===this.owner.owner.user){
this.lastreadmessageid=messagez.id;

View file

@ -3,6 +3,7 @@ class dirrect extends guild{
super(-1);
console.log(JSON);
this.owner=owner;
this.headers={"Content-type": "application/json; charset=UTF-8",Authorization:this.owner.userinfo.token};
if(!this.owner){
console.error("Owner was not included, please fix")
}
@ -56,6 +57,7 @@ class group extends channel{
constructor(JSON,owner){
super(-1);
this.owner=owner;
this.headers=this.owner.headers;
this.messages=[];
console.log(JSON.recipients,JSON)
this.name=JSON.recipients[0]?.username;

View file

@ -26,11 +26,13 @@ class guild{
*/
}
constructor(JSON,owner){
if(JSON===-1){
return;
}
console.log(JSON);
this.owner=owner;
this.headers={"Content-type": "application/json; charset=UTF-8",Authorization:this.owner.userinfo.token};
if(!this.owner){
console.error("Owner was not included, please fix")
}
@ -103,14 +105,14 @@ class guild{
console.log(build,thing)
fetch(info.api.toString()+"/v9/guilds/"+this.id+"/channels",{
method:"PATCH",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
headers:this.headers,
body:JSON.stringify([thing])
});
}
}else{
fetch(info.api.toString()+"/v9/guilds/"+this.id+"/channels",{
method:"PATCH",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
headers:this.headers,
body:JSON.stringify(build)
});
}
@ -171,7 +173,7 @@ class guild{
this.unreads();
fetch(info.api.toString()+"/v9/read-states/ack-bulk",{
method:"POST",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
headers:this.headers,
body:JSON.stringify(build)
})
}
@ -260,7 +262,7 @@ class guild{
createChannel(name,type){
fetch(info.api.toString()+"/guilds/"+this.id+"/channels",{
method:"Post",
headers:{"Content-type": "application/json; charset=UTF-8",Authorization:token},
headers:this.headers,
body:JSON.stringify({name: name, type: type})
})
}

View file

@ -1,8 +1,3 @@
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() {
var servertdHeight = document.getElementById('servertd').offsetHeight+document.getElementById('typebox').offsetHeight+document.getElementById('pasteimage').offsetHeight;
document.documentElement.style.setProperty('--servertd-height', servertdHeight + 'px');
@ -15,12 +10,24 @@ resizeObserver.observe(document.getElementById('typebox'));
resizeObserver.observe(document.getElementById('pasteimage'));
setDynamicHeight();
let token=gettoken();
let ws
//initwebsocket();
const users=getBulkUsers();
if(!users.currentuser){
window.location.href = '/login.html';
}
const info=users.users[users.currentuser].serverurls;
console.log(users)
let token=users.users[users.currentuser].token;
let READY;
let thisuser=new localuser({token:token});
thisuser.initwebsocket().then(_=>{
thisuser.loaduser();
thisuser.init();
document.getElementById("loading").classList.add("doneloading");
document.getElementById("loading").classList.remove("loading");
console.log("done loading")
});
var currentmenu="";
document.addEventListener('click', function(event) {
if(currentmenu==""){
@ -259,11 +266,10 @@ async function enter(event){
let packets=1;
let serverz=0;
let serverid=[];
let thisuser=null;
thisuser=new localuser();
let cchanel=0;

View file

@ -1,6 +1,7 @@
class localuser{
constructor(){
this.initwebsocket();
constructor(userinfo){
this.token=userinfo.token;
this.userinfo=userinfo;
this.initialized=false;
}
gottenReady(ready){
@ -38,14 +39,31 @@ class localuser{
}
this.typing=[];
}
initwebsocket(){
outoffocus(){
document.getElementById("servers").textContent="";
document.getElementById("channels").textContent="";
document.getElementById("messages").textContent="";
this.lookingguild=null;
this.channelfocus=null;
}
unload(){
this.initialized=false;
clearInterval(this.wsinterval);
this.outoffocus();
this.guilds=[];
this.guildids={};
this.ws.close(4000)
}
async initwebsocket(){
let returny=null
const promise=new Promise((res)=>{returny=res});
this.ws = new WebSocket(info.gateway.toString());
this.ws.addEventListener('open', (event) => {
console.log('WebSocket connected');
this.ws.send(JSON.stringify({
"op": 2,
"d": {
"token":token,
"token":this.token,
"capabilities": 16381,
"properties": {
"browser": "Jank Client",
@ -79,12 +97,9 @@ class localuser{
break;
case "READY":
this.gottenReady(temp);
this.loaduser();
READY=temp;
this.init();
genusersettings();
document.getElementById("loading").classList.add("doneloading");
document.getElementById("loading").classList.remove("loading")
returny();
break;
case "MESSAGE_UPDATE":
if(this.initialized){
@ -135,7 +150,7 @@ class localuser{
}else if(temp.op===10){
console.log("heartbeat down")
setInterval(_=>{
this.wsinterval=setInterval(_=>{
this.ws.send(JSON.stringify({op:1,d:packets}))
},temp.d.heartbeat_interval)
packets=1;
@ -149,9 +164,25 @@ class localuser{
});
this.ws.addEventListener('close', (event) => {
clearInterval(this.wsinterval);
console.log('WebSocket closed');
console.warn(event);
if(event.code!==4000&&thisuser===this){
this.unload();
document.getElementById("loading").classList.remove("doneloading");
document.getElementById("loading").classList.add("loading");
this.initwebsocket().then(_=>{
thisuser.loaduser();
thisuser.init();
document.getElementById("loading").classList.add("doneloading");
document.getElementById("loading").classList.remove("loading");
console.log("done loading")
});
}
});
await promise;
return;
}
resolveGuildidFromChannelID(ID){
let resolve=this.guilds.find(guild => guild.channelids[ID])
resolve??=undefined;

View file

@ -1,6 +1,3 @@
function getCookie(name) {
return localStorage.getItem(name);
}
function setTheme(){
const name=localStorage.getItem("theme");
if(!name){
@ -10,7 +7,83 @@ function setTheme(){
document.body.className=name+"-theme";
}
setTheme();
function getBulkUsers(){
const json=JSON.parse(localStorage.getItem("userinfos"));
for(const thing in json.users){
json.users[thing]=new specialuser(json.users[thing]);
}
return json;
}
function getBulkInfo(){
return JSON.parse(localStorage.getItem("userinfos"));
}
function setDefaults(){
let userinfos=localStorage.getItem("userinfos");
if(!userinfos){
localStorage.setItem("userinfos",JSON.stringify({
currentuser:null,
users:{},
preferances:
{
theme:"Dark",
notifcations:false,
},
}));
}
}
setDefaults();
class specialuser{
constructor(json){
console.log(json)
if(typeof json==="specialuser"){
return json;
}
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);
this.email=json.email;
this.token=json.token;
this.loggedin=json.loggedin;
this.json=json;
if(!this.serverurls||!this.email||!this.token){
console.error("There are fundamentally missing pieces of info missing from this user");
}
}
set pfpsrc(e){
this.json.pfpsrc=e;
this.updateLocal();
}
get pfpsrc(){
return this.json.pfpsrc;
}
set username(e){
this.json.username=e;
this.updateLocal();
}
get username(){
return this.json.username;
}
get uid(){
return this.email+this.serverurls.wellknown;
}
toJSON(){
return this.json;
}
updateLocal(){
const info=getBulkInfo();
info.users[this.uid]=this.toJSON();
localStorage.setItem("userinfos",JSON.stringify(info));
}
}
function adduser(user){
user=new specialuser(user);
const info=getBulkInfo();
info.users[user.uid]=user;
info.currentuser=user.uid;
localStorage.setItem("userinfos",JSON.stringify(info));
}
const instancein=document.getElementById("instancein");
let timeout=0;
async function checkInstance(e){
@ -67,7 +140,8 @@ async function login(username, password){
return response.errors.login._errors[0].message;
console.log("test")
}
localStorage.setItem("token",response.token);
//this.serverurls||!this.email||!this.token
adduser({serverurls:JSON.parse(localStorage.getItem("instanceinfo")),email:username,token:response.token});
window.location.href = '/channels/@me';
return response.token;
})
@ -99,14 +173,6 @@ async function setInstance(url){
return await attempt(wellKnown);
}
function gettoken(){
let temp=getCookie("token");
temp??=undefined;
if(temp===undefined){
window.location.href = '/login.html';
}
return temp;
}
async function check(e){

View file

@ -36,7 +36,10 @@ class cmessage{
document.getElementById("typebox").value=this.content;
},null,_=>{return _.author.id==READY.d.user.id});
}
constructor(messagejson){
constructor(messagejson,owner){
console.log(owner)
this.owner=owner;
this.headers=this.owner.headers;
for(const thing of Object.keys(messagejson)){
this[thing]=messagejson[thing];
}
@ -70,7 +73,7 @@ class cmessage{
line2.classList.add("reply");
line.classList.add("startreply");
replyline.classList.add("replyflex")
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=>{
fetch(info.api.toString()+"/v9/channels/"+this.message_reference.channel_id+"/messages?limit=1&around="+this.message_reference.message_id,{headers:this.headers}).then(responce=>responce.json()).then(responce=>{
const author=new user(responce[0].author);
reply.appendChild(markdown(responce[0].content));