From 9676c605e79e6ab44fd000041008afdbe635a674 Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Mon, 27 May 2024 14:34:04 -0500 Subject: [PATCH] Adds sorting of DMs Adds support for sortings of DMs and inital work for unread messages along with a function to work arround an error in spacebar's API --- webpage/channel.js | 15 ++++++++++++++- webpage/dirrect.js | 37 +++++++++++++++++++++++++++++++++++++ webpage/guild.js | 5 ++++- webpage/index.js | 1 - webpage/localuser.js | 12 ++++++++++++ 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/webpage/channel.js b/webpage/channel.js index 855e2c9..8cf51d9 100644 --- a/webpage/channel.js +++ b/webpage/channel.js @@ -17,6 +17,16 @@ class channel{ this.topic=JSON.topic; this.nsfw=JSON.nsfw; this.position=JSON.position; + this.lastreadmessageid=null; + this.lastmessageid=JSON.last_message_id; + } + readStateInfo(json){ + this.lastreadmessageid=json.last_message_id; + this.mentions=json.mention_count; + this.lastpin=json.last_pin_timestamp; + } + get hasunreads(){ + return this.lastmessageid===this.lastreadmessageid } get canMessage(){ for(const thing of this.permission_overwrites){ @@ -368,6 +378,10 @@ class channel{ } messageCreate(messagep,focus){ const messagez=new cmessage(messagep.d); + this.lastmessageid=messagez.id; + if(messagez.user===thisuser.user){ + this.lastreadmessageid=messagez.id; + } this.messages.unshift(messagez); const scrolly=document.getElementById("messagecontainer"); this.messageids[messagez.id]=messagez; @@ -378,6 +392,5 @@ class channel{ if(shouldScroll){ scrolly.scrollTop = scrolly.scrollHeight; } - } } diff --git a/webpage/dirrect.js b/webpage/dirrect.js index 33acb22..3c6552e 100644 --- a/webpage/dirrect.js +++ b/webpage/dirrect.js @@ -26,6 +26,12 @@ class dirrect extends guild{ this.calculateReorder(); this.printServers(); } + sortchannels(){ + this.headchannels.sort((a,b)=>{ + const result=(BigInt(a.lastmessageid)-BigInt(b.lastmessageid)); + return Number(-result); + }); + } giveMember(member){ console.error("not a real guild, can't give member object") } @@ -59,6 +65,8 @@ class group extends channel{ this.guild_id="@me"; this.messageids={}; this.permission_overwrites=[]; + this.lastmessageid=JSON.last_message_id; + this.lastmessageid??=0; } createguildHTML(){ const div=document.createElement("div") @@ -80,4 +88,33 @@ class group extends channel{ history.pushState(null, null,"/channels/"+this.guild_id+"/"+this.id); document.getElementById("channelname").innerText="@"+this.name; } + messageCreate(messagep,focus){ + const messagez=new cmessage(messagep.d); + this.lastmessageid=messagez.id; + if(messagez.user===thisuser.user){ + this.lastreadmessageid=messagez.id; + } + this.messages.unshift(messagez); + const scrolly=document.getElementById("messagecontainer"); + this.messageids[messagez.id]=messagez; + if(this.owner.owner.lookingguild.prevchannel===this){ + var shouldScroll=scrolly.scrollTop+scrolly.clientHeight>scrolly.scrollHeight-20; + messages.appendChild(messagez.buildhtml(this.messages[1])); + } + if(shouldScroll){ + scrolly.scrollTop = scrolly.scrollHeight; + } + console.log(document.getElementById("channels").children) + if(thisuser.lookingguild===this.owner){ + const channellist=document.getElementById("channels").children[0] + for(const thing of channellist.children){ + if(thing.myinfo===this){ + channellist.prepend(thing); + console.log(thing.myinfo); + break; + } + console.log(thing.myinfo,this,thing.myinfo===this); + } + } + } } diff --git a/webpage/guild.js b/webpage/guild.js index 6509ad6..3714b43 100644 --- a/webpage/guild.js +++ b/webpage/guild.js @@ -92,9 +92,12 @@ class guild{ this.owner.channelfocus=id; this.channelids[id].getHTML(); } + sortchannels(){ + this.headchannels.sort((a,b)=>{return a.position-b.position;}); + } getHTML(){ //this.printServers(); - this.headchannels.sort((a,b)=>{return a.position-b.position;}); + this.sortchannels(); this.printServers(); console.log("html") const build=document.createElement("div"); diff --git a/webpage/index.js b/webpage/index.js index 3529952..2122320 100644 --- a/webpage/index.js +++ b/webpage/index.js @@ -431,7 +431,6 @@ function initwebsocket(){ break; case "READY": thisuser=new localuser(temp); - console.log(temp.d.read_state.entries) thisuser.loaduser(); READY=temp; thisuser.init(); diff --git a/webpage/localuser.js b/webpage/localuser.js index 1c90564..fb27b3b 100644 --- a/webpage/localuser.js +++ b/webpage/localuser.js @@ -21,8 +21,20 @@ class localuser{ const temp=new member(thing[0]); this.guildids[temp.guild_id].giveMember(temp); } + for(const thing of ready.d.read_state.entries){ + console.log(thing) + thing.id=this.resolveGuildidFromChannelID(thing.channel_id);//currently needed due to broken server code, remove once id is the guild id + this.guildids[thing.id].channelids[thing.channel_id].readStateInfo(thing); + } this.typing=[]; } + resolveGuildidFromChannelID(ID){ + for(const thing of this.guilds){ + if(thing.channelids[ID]){ + return thing.id; + } + } + } updateChannel(JSON){ this.guildids[JSON.guild_id].updateChannel(JSON); if(JSON.guild_id===this.lookingguild.id){