From 31f3fef52d20d2b6c8e0df0e55597ba5fa78efa2 Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Fri, 21 Jun 2024 16:47:45 -0500 Subject: [PATCH] Notification support Be warned, there's either a bug in the server or notification level setting isn't implemented on the server side --- webpage/channel.js | 62 ++++++++++++++++++++++++++++++++++++++++++++ webpage/dirrect.js | 14 ++++++++++ webpage/guild.js | 36 +++++++++++++++++++++++++ webpage/localuser.js | 4 +++ webpage/message.js | 25 ++++++++++++++++++ 5 files changed, 141 insertions(+) diff --git a/webpage/channel.js b/webpage/channel.js index d363007..4994729 100644 --- a/webpage/channel.js +++ b/webpage/channel.js @@ -451,6 +451,21 @@ class channel{ headers:this.headers }) } + get notification(){ + let notinumber=this.message_notifications; + if(+notinumber===3){notinumber=null;} + notinumber??=this.owner.message_notifications; + switch(+notinumber){ + case 0: + return "all"; + case 1: + return "mentions"; + case 2: + return "none"; + case 3: + return "default"; + } + } messageCreate(messagep,focus){ const messagez=new cmessage(messagep.d,this); this.lastmessageid=messagez.id; @@ -475,6 +490,53 @@ class channel{ if(shouldScroll){ scrolly.scrollTop = scrolly.scrollHeight; } + if(messagez.author===this.owner.owner.user){ + return; + } + if(this.owner.owner.lookingguild.prevchannel===this&&document.hasFocus()){ + return; + } + if(this.notification==="all"){ + this.notify(messagez); + }else if(this.notification==="mentions"&&messagez.mentionsuser(this.owner.owner.user)){ + this.notify(messagez); + } + } + notititle(message){ + return message.author.username+" > "+this.owner.properties.name+" > "+this.name; + } + notify(message){ + { + const voicy=new voice("sin",800); + voicy.play() + setTimeout(_=>{voicy.freq=1000},50); + setTimeout(_=>{voicy.freq=1300},100); + setTimeout(_=>{voicy.stop()},150); + } + if (!("Notification" in window)) { + + } else if (Notification.permission === "granted") { + let noticontent=markdown(message.content).textContent; + noticontent||=message.embeds[0].json.title; + noticontent||=markdown(message.embeds[0].json.description).textContent; + noticontent||="Blank Message"; + let imgurl=null; + const images=message.getimages(); + if(images.length){ + const image = images[0]; + imgurl||=image.proxy_url; + imgurl||=image.url; + } + const notification = new Notification(this.notititle(message),{ + body:noticontent, + icon:message.author.getpfpsrc(), + image:imgurl, + }); + } else if (Notification.permission !== "denied") { + Notification.requestPermission().then((permission) => { + this.notify(message); + }); + } } } channel.setupcontextmenu(); diff --git a/webpage/dirrect.js b/webpage/dirrect.js index fefb81b..7bfee3e 100644 --- a/webpage/dirrect.js +++ b/webpage/dirrect.js @@ -126,6 +126,20 @@ class group extends channel{ } } this.unreads(); + if(messagez.author===this.owner.owner.user){ + return; + } + if(this.owner.owner.lookingguild.prevchannel===this&&document.hasFocus()){ + return; + } + if(this.notification==="all"){ + this.notify(messagez); + }else if(this.notification==="mentions"&&messagez.mentionsuser(this.owner.owner.user)){ + this.notify(messagez); + } + } + notititle(message){ + return message.author.username; } unreads(){ const sentdms=document.getElementById("sentdms"); diff --git a/webpage/guild.js b/webpage/guild.js index 52f25b2..a42f469 100644 --- a/webpage/guild.js +++ b/webpage/guild.js @@ -11,6 +11,11 @@ class guild{ this.markAsRead(); }); + guild.contextmenu.addbutton("Notifications",function(){ + console.log(this) + this.setnotifcation(); + }); + guild.contextmenu.addbutton("Leave guild",function(){ this.confirmleave(); },null,function(_){return _.properties.owner_id!==_.member.user.id}); @@ -67,6 +72,37 @@ class guild{ } } } + notisetting(settings){ + this.message_notifications=settings.message_notifications; + } + setnotifcation(){ + let noti=this.message_notifications + const notiselect=new fullscreen( + ["vdiv", + ["radio","select notifications type", + ["all","only mentions","none"], + function(e){ + noti=["all","only mentions","none"].indexOf(e); + }, + noti + ], + ["button","","submit",_=>{ + fetch(info.api.toString()+"/v9/users/@me/guilds/settings",{ + method:"PATCH", + headers:this.headers, + body:JSON.stringify({ + "guilds":{ + [this.id]:{ + "message_notifications": noti + } + } + }) + }) + this.message_notifications=noti; + }] + ]); + notiselect.show(); + } confirmleave(){ const full= new fullscreen([ "vdiv", diff --git a/webpage/localuser.js b/webpage/localuser.js index 9a49cb8..f78d927 100644 --- a/webpage/localuser.js +++ b/webpage/localuser.js @@ -28,6 +28,10 @@ class localuser{ this.guilds.push(temp); this.guildids[temp.id]=temp; } + console.log(ready.d.user_guild_settings.entries) + for(const thing of ready.d.user_guild_settings.entries){ + this.guildids[thing.guild_id].notisetting(thing); + } for(const thing of ready.d.merged_members){ const temp=new member(thing[0]); this.guildids[temp.guild_id].giveMember(temp); diff --git a/webpage/message.js b/webpage/message.js index 7c56e03..fa61afc 100644 --- a/webpage/message.js +++ b/webpage/message.js @@ -47,11 +47,36 @@ class cmessage{ this.embeds[thing]=new embed(this.embeds[thing],this); } this.author=new user(this.author); + for(const thing in this.mentions){ + this.mentions[thing]=new user(this.mentions[thing]); + } + if(this.mentions.length||this.mention_roles.length){//currently mention_roles isn't implemented on the spacebar servers + console.log(this.mentions,this.mention_roles) + } + if(this.mentionsuser(this.owner.owner.owner.user)){ + console.log(this); + } } messageevents(obj){ cmessage.contextmenu.bind(obj,this) obj.classList.add("messagediv") } + mentionsuser(userd){ + if(userd instanceof user){ + return this.mentions.includes(userd); + }else if(userd instanceof member){ + return this.mentions.includes(userd.user); + } + } + getimages(){ + const build=[]; + for(const thing of this.attachments){ + if(thing.content_type.startsWith('image/')){ + build.push(thing); + } + } + return build; + } buildhtml(premessage){ //premessage??=messages.lastChild; const build = document.createElement('table');