get rid of the odd complexity of the snowflake class
This commit is contained in:
parent
b72d7673f4
commit
03041cf9fd
13 changed files with 237 additions and 257 deletions
|
@ -5,7 +5,7 @@ import { Contextmenu } from "./contextmenu.js";
|
|||
import { Dialog } from "./dialog.js";
|
||||
import { Permissions } from "./permissions.js";
|
||||
import { Settings } from "./settings.js";
|
||||
import { Role, RoleList } from "./role.js";
|
||||
import { RoleList } from "./role.js";
|
||||
import { InfiniteScroller } from "./infiniteScroller.js";
|
||||
import { SnowFlake } from "./snowflake.js";
|
||||
import { MarkDown } from "./markdown.js";
|
||||
|
@ -151,7 +151,7 @@ class Channel {
|
|||
}
|
||||
sortPerms() {
|
||||
this.permission_overwritesar.sort((a, b) => {
|
||||
return this.guild.roles.findIndex(_ => _.snowflake === a[0]) - this.guild.roles.findIndex(_ => _.snowflake === b[0]);
|
||||
return this.guild.roles.findIndex(_ => _ === a[0]) - this.guild.roles.findIndex(_ => _ === b[0]);
|
||||
});
|
||||
}
|
||||
setUpInfiniteScroller() {
|
||||
|
@ -219,7 +219,7 @@ class Channel {
|
|||
this.name = json.name;
|
||||
this.snowflake = new SnowFlake(json.id, this);
|
||||
if (json.parent_id) {
|
||||
this.parent_id = SnowFlake.getSnowFlakeFromID(json.parent_id, Channel);
|
||||
this.parent_id = json.parent_id;
|
||||
}
|
||||
this.parent = null;
|
||||
this.children = [];
|
||||
|
@ -234,7 +234,10 @@ class Channel {
|
|||
this.permission_overwrites.set(thing.id, new Permissions(thing.allow, thing.deny));
|
||||
const permission = this.permission_overwrites.get(thing.id);
|
||||
if (permission) {
|
||||
this.permission_overwritesar.push([SnowFlake.getSnowFlakeFromID(thing.id, Role), permission]);
|
||||
const role = this.guild.roleids.get(thing.id);
|
||||
if (role) {
|
||||
this.permission_overwritesar.push([role, permission]);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.topic = json.topic;
|
||||
|
@ -313,7 +316,7 @@ class Channel {
|
|||
});
|
||||
}
|
||||
resolveparent(guild) {
|
||||
const parentid = this.parent_id?.id;
|
||||
const parentid = this.parent_id;
|
||||
if (!parentid)
|
||||
return false;
|
||||
this.parent = guild.channelids[parentid];
|
||||
|
@ -335,7 +338,7 @@ class Channel {
|
|||
if (thing.move_id && thing.move_id !== thing.parent_id) {
|
||||
thing.parent_id = thing.move_id;
|
||||
thisthing.parent_id = thing.parent?.id;
|
||||
thing.move_id = null;
|
||||
thing.move_id = undefined;
|
||||
//console.log(this.guild.channelids[thisthing.parent_id.id]);
|
||||
}
|
||||
if (thisthing.position || thisthing.parent_id) {
|
||||
|
@ -515,7 +518,7 @@ class Channel {
|
|||
return;
|
||||
event.preventDefault();
|
||||
if (container) {
|
||||
that.move_id = this.snowflake;
|
||||
that.move_id = this.id;
|
||||
if (that.parent) {
|
||||
that.parent.children.splice(that.parent.children.indexOf(that), 1);
|
||||
}
|
||||
|
@ -782,12 +785,12 @@ class Channel {
|
|||
for (const i in response) {
|
||||
let messager;
|
||||
let willbreak = false;
|
||||
if (!SnowFlake.hasSnowFlakeFromID(response[i].id, Message)) {
|
||||
messager = new Message(response[i], this);
|
||||
if (this.messages.has(response[i].id)) {
|
||||
messager = this.messages.get(response[i].id);
|
||||
willbreak = true;
|
||||
}
|
||||
else {
|
||||
messager = SnowFlake.getSnowFlakeFromID(response[i].id, Message).getObject();
|
||||
willbreak = true;
|
||||
messager = new Message(response[i], this);
|
||||
}
|
||||
this.idToPrev.set(messager.id, previd);
|
||||
this.idToNext.set(previd, messager.id);
|
||||
|
@ -942,11 +945,11 @@ class Channel {
|
|||
const parent = this.guild.channelids[json.parent_id];
|
||||
if (parent) {
|
||||
this.parent = parent;
|
||||
this.parent_id = parent.snowflake;
|
||||
this.parent_id = parent.id;
|
||||
}
|
||||
else {
|
||||
this.parent = null;
|
||||
this.parent_id = null;
|
||||
this.parent_id = undefined;
|
||||
}
|
||||
this.children = [];
|
||||
this.guild_id = json.guild_id;
|
||||
|
@ -959,7 +962,10 @@ class Channel {
|
|||
this.permission_overwrites.set(thing.id, new Permissions(thing.allow, thing.deny));
|
||||
const permisions = this.permission_overwrites.get(thing.id);
|
||||
if (permisions) {
|
||||
this.permission_overwritesar.push([SnowFlake.getSnowFlakeFromID(thing.id, Role), permisions]);
|
||||
const role = this.guild.roleids.get(thing.id);
|
||||
if (role) {
|
||||
this.permission_overwritesar.push([role, permisions]);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.topic = json.topic;
|
||||
|
@ -1136,7 +1142,7 @@ class Channel {
|
|||
});
|
||||
const perm = new Permissions("0", "0");
|
||||
this.permission_overwrites.set(role.id, perm);
|
||||
this.permission_overwritesar.push([role.snowflake, perm]);
|
||||
this.permission_overwritesar.push([role, perm]);
|
||||
}
|
||||
async updateRolePermissions(id, perms) {
|
||||
const permission = this.permission_overwrites.get(id);
|
||||
|
|
|
@ -72,7 +72,7 @@ class Guild {
|
|||
const s1 = settings.addButton("roles");
|
||||
const permlist = [];
|
||||
for (const thing of this.roles) {
|
||||
permlist.push([thing.snowflake, thing.permissions]);
|
||||
permlist.push([thing, thing.permissions]);
|
||||
}
|
||||
s1.options.push(new RoleList(permlist, this, this.updateRolePermissions.bind(this)));
|
||||
settings.show();
|
||||
|
@ -98,7 +98,7 @@ class Guild {
|
|||
for (const roley of json.roles) {
|
||||
const roleh = new Role(roley, this);
|
||||
this.roles.push(roleh);
|
||||
this.roleids.set(roleh.snowflake, roleh);
|
||||
this.roleids.set(roleh.id, roleh);
|
||||
}
|
||||
if (member instanceof User) {
|
||||
Member.resolveMember(member, this).then(_ => {
|
||||
|
@ -220,7 +220,7 @@ class Guild {
|
|||
if (thing.move_id && thing.move_id !== thing.parent_id) {
|
||||
thing.parent_id = thing.move_id;
|
||||
thisthing.parent_id = thing.parent?.id;
|
||||
thing.move_id = null;
|
||||
thing.move_id = undefined;
|
||||
}
|
||||
if (thisthing.position || thisthing.parent_id) {
|
||||
build.push(thisthing);
|
||||
|
@ -539,7 +539,7 @@ class Guild {
|
|||
});
|
||||
const json = await fetched.json();
|
||||
const role = new Role(json, this);
|
||||
this.roleids.set(role.snowflake, role);
|
||||
this.roleids.set(role.id, role);
|
||||
this.roles.push(role);
|
||||
return role;
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@ import { Voice } from "./audio.js";
|
|||
import { User } from "./user.js";
|
||||
import { Dialog } from "./dialog.js";
|
||||
import { getapiurls, getBulkInfo, setTheme } from "./login.js";
|
||||
import { SnowFlake } from "./snowflake.js";
|
||||
import { Message } from "./message.js";
|
||||
import { Member } from "./member.js";
|
||||
import { FormError, Settings } from "./settings.js";
|
||||
import { MarkDown } from "./markdown.js";
|
||||
|
@ -127,7 +125,6 @@ class Localuser {
|
|||
if (this.ws) {
|
||||
this.ws.close(4001);
|
||||
}
|
||||
SnowFlake.clear();
|
||||
}
|
||||
swapped = false;
|
||||
async initwebsocket() {
|
||||
|
@ -310,16 +307,38 @@ class Localuser {
|
|||
}
|
||||
break;
|
||||
case "MESSAGE_DELETE":
|
||||
console.log(temp.d);
|
||||
SnowFlake.getSnowFlakeFromID(temp.d.id, Message).getObject().deleteEvent();
|
||||
{
|
||||
temp.d.guild_id ??= "@me";
|
||||
const guild = this.guildids.get(temp.d.guild_id);
|
||||
if (!guild)
|
||||
break;
|
||||
const channel = guild.channelids[temp.d.channel_id];
|
||||
if (!channel)
|
||||
break;
|
||||
const message = channel.messages.get(temp.d.id);
|
||||
if (!message)
|
||||
break;
|
||||
message.deleteEvent();
|
||||
break;
|
||||
}
|
||||
case "READY":
|
||||
this.gottenReady(temp);
|
||||
break;
|
||||
case "MESSAGE_UPDATE":
|
||||
const message = SnowFlake.getSnowFlakeFromID(temp.d.id, Message).getObject();
|
||||
{
|
||||
temp.d.guild_id ??= "@me";
|
||||
const guild = this.guildids.get(temp.d.guild_id);
|
||||
if (!guild)
|
||||
break;
|
||||
const channel = guild.channelids[temp.d.channel_id];
|
||||
if (!channel)
|
||||
break;
|
||||
const message = channel.messages.get(temp.d.id);
|
||||
if (!message)
|
||||
break;
|
||||
message.giveData(temp.d);
|
||||
break;
|
||||
}
|
||||
case "TYPING_START":
|
||||
if (this.initialized) {
|
||||
this.typingStart(temp);
|
||||
|
@ -367,10 +386,17 @@ class Localuser {
|
|||
break;
|
||||
}
|
||||
case "MESSAGE_REACTION_ADD":
|
||||
if (SnowFlake.hasSnowFlakeFromID(temp.d.message_id, Message)) {
|
||||
{
|
||||
temp.d.guild_id ??= "@me";
|
||||
const message = SnowFlake.getSnowFlakeFromID(temp.d.message_id, Message).getObject();
|
||||
const guild = SnowFlake.getSnowFlakeFromID(temp.d.guild_id, Guild).getObject();
|
||||
const guild = this.guildids.get(temp.d.guild_id);
|
||||
if (!guild)
|
||||
break;
|
||||
const channel = guild.channelids[temp.d.channel_id];
|
||||
if (!channel)
|
||||
break;
|
||||
const message = channel.messages.get(temp.d.message_id);
|
||||
if (!message)
|
||||
break;
|
||||
let thing;
|
||||
if (temp.d.member) {
|
||||
thing = await Member.new(temp.d.member, guild);
|
||||
|
@ -382,22 +408,48 @@ class Localuser {
|
|||
}
|
||||
break;
|
||||
case "MESSAGE_REACTION_REMOVE":
|
||||
if (SnowFlake.hasSnowFlakeFromID(temp.d.message_id, Message)) {
|
||||
const message = SnowFlake.getSnowFlakeFromID(temp.d.message_id, Message).getObject();
|
||||
console.log("test");
|
||||
{
|
||||
temp.d.guild_id ??= "@me";
|
||||
const guild = this.guildids.get(temp.d.guild_id);
|
||||
if (!guild)
|
||||
break;
|
||||
const channel = guild.channelids[temp.d.channel_id];
|
||||
if (!channel)
|
||||
break;
|
||||
const message = channel.messages.get(temp.d.message_id);
|
||||
if (!message)
|
||||
break;
|
||||
message.reactionRemove(temp.d.emoji, temp.d.user_id);
|
||||
}
|
||||
break;
|
||||
case "MESSAGE_REACTION_REMOVE_ALL":
|
||||
if (SnowFlake.hasSnowFlakeFromID(temp.d.message_id, Message)) {
|
||||
const messageReactionRemoveAll = SnowFlake.getSnowFlakeFromID(temp.d.message_id, Message).getObject();
|
||||
messageReactionRemoveAll.reactionRemoveAll();
|
||||
{
|
||||
temp.d.guild_id ??= "@me";
|
||||
const guild = this.guildids.get(temp.d.guild_id);
|
||||
if (!guild)
|
||||
break;
|
||||
const channel = guild.channelids[temp.d.channel_id];
|
||||
if (!channel)
|
||||
break;
|
||||
const message = channel.messages.get(temp.d.message_id);
|
||||
if (!message)
|
||||
break;
|
||||
message.reactionRemoveAll();
|
||||
}
|
||||
break;
|
||||
case "MESSAGE_REACTION_REMOVE_EMOJI":
|
||||
if (SnowFlake.hasSnowFlakeFromID(temp.d.message_id, Message)) {
|
||||
const messageReactionRemoveEmoji = SnowFlake.getSnowFlakeFromID(temp.d.message_id, Message).getObject();
|
||||
messageReactionRemoveEmoji.reactionRemoveEmoji(temp.d.emoji);
|
||||
{
|
||||
temp.d.guild_id ??= "@me";
|
||||
const guild = this.guildids.get(temp.d.guild_id);
|
||||
if (!guild)
|
||||
break;
|
||||
const channel = guild.channelids[temp.d.channel_id];
|
||||
if (!channel)
|
||||
break;
|
||||
const message = channel.messages.get(temp.d.message_id);
|
||||
if (!message)
|
||||
break;
|
||||
message.reactionRemoveEmoji(temp.d.emoji);
|
||||
}
|
||||
break;
|
||||
case "GUILD_MEMBERS_CHUNK":
|
||||
|
@ -441,7 +493,10 @@ class Localuser {
|
|||
}
|
||||
createChannel(json) {
|
||||
json.guild_id ??= "@me";
|
||||
SnowFlake.getSnowFlakeFromID(json.guild_id, Guild).getObject().createChannelpac(json);
|
||||
const guild = this.guildids.get(json.guild_id);
|
||||
if (!guild)
|
||||
return;
|
||||
guild.createChannelpac(json);
|
||||
if (json.guild_id === this.lookingguild?.id) {
|
||||
this.loadGuild(json.guild_id);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import { User } from "./user.js";
|
||||
import { Role } from "./role.js";
|
||||
import { SnowFlake } from "./snowflake.js";
|
||||
import { Dialog } from "./dialog.js";
|
||||
class Member {
|
||||
static already = {};
|
||||
|
@ -29,7 +27,9 @@ class Member {
|
|||
}
|
||||
if (thing === "roles") {
|
||||
for (const strrole of memberjson.roles) {
|
||||
const role = SnowFlake.getSnowFlakeFromID(strrole, Role).getObject();
|
||||
const role = this.guild.roleids.get(strrole);
|
||||
if (!role)
|
||||
continue;
|
||||
this.roles.push(role);
|
||||
}
|
||||
continue;
|
||||
|
|
|
@ -138,7 +138,7 @@ class RoleList extends Buttons {
|
|||
}
|
||||
for (const i of permissions) {
|
||||
console.log(i);
|
||||
this.buttons.push([i[0].getObject().name, i[0].id]); //
|
||||
this.buttons.push([i[0].name, i[0].id]);
|
||||
}
|
||||
this.options = options;
|
||||
}
|
||||
|
@ -149,9 +149,12 @@ class RoleList extends Buttons {
|
|||
const perm = arr[1];
|
||||
this.permission.deny = perm.deny;
|
||||
this.permission.allow = perm.allow;
|
||||
this.options.name = SnowFlake.getSnowFlakeFromID(str, Role).getObject().name;
|
||||
const role = this.permissions.find(e => e[0].id === str);
|
||||
if (role) {
|
||||
this.options.name = role[0].name;
|
||||
this.options.haschanged = false;
|
||||
}
|
||||
}
|
||||
return this.options.generateHTML();
|
||||
}
|
||||
save() {
|
||||
|
|
|
@ -1,83 +1,7 @@
|
|||
class SnowFlake {
|
||||
id;
|
||||
static SnowFlakes = new Map();
|
||||
static FinalizationRegistry = new FinalizationRegistry((a) => {
|
||||
SnowFlake.SnowFlakes.get(a[1]).delete(a[0]);
|
||||
});
|
||||
obj;
|
||||
constructor(id, obj) {
|
||||
if (!obj) {
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
return;
|
||||
}
|
||||
if (!SnowFlake.SnowFlakes.get(obj.constructor)) {
|
||||
SnowFlake.SnowFlakes.set(obj.constructor, new Map());
|
||||
}
|
||||
if (SnowFlake.SnowFlakes.get(obj.constructor).get(id)) {
|
||||
const snowflake = SnowFlake.SnowFlakes.get(obj.constructor).get(id).deref();
|
||||
if (snowflake) {
|
||||
snowflake.obj = obj;
|
||||
return snowflake;
|
||||
}
|
||||
else {
|
||||
SnowFlake.SnowFlakes.get(obj.constructor).delete(id);
|
||||
}
|
||||
}
|
||||
this.id = id;
|
||||
SnowFlake.SnowFlakes.get(obj.constructor).set(id, new WeakRef(this));
|
||||
SnowFlake.FinalizationRegistry.register(this, [id, obj.constructor]);
|
||||
this.obj = obj;
|
||||
}
|
||||
static clear() {
|
||||
this.SnowFlakes = new Map();
|
||||
}
|
||||
/**
|
||||
* Just to clarify bc TS, it returns a SnowFlake\<type> which is what you entered with the type parameter
|
||||
* @deprecated
|
||||
**/
|
||||
static getSnowFlakeFromID(id, type) {
|
||||
if (!SnowFlake.SnowFlakes.get(type)) {
|
||||
SnowFlake.SnowFlakes.set(type, new Map());
|
||||
}
|
||||
const snowflake = SnowFlake.SnowFlakes.get(type).get(id);
|
||||
if (snowflake) {
|
||||
const obj = snowflake.deref();
|
||||
if (obj) {
|
||||
return obj;
|
||||
}
|
||||
else {
|
||||
SnowFlake.SnowFlakes.get(type).delete(id);
|
||||
}
|
||||
}
|
||||
{
|
||||
const snowflake = new SnowFlake(id, undefined);
|
||||
SnowFlake.SnowFlakes.get(type).set(id, new WeakRef(snowflake));
|
||||
SnowFlake.FinalizationRegistry.register(this, [id, type]);
|
||||
return snowflake;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
*
|
||||
*/
|
||||
static hasSnowFlakeFromID(id, type) {
|
||||
if (!SnowFlake.SnowFlakes.get(type)) {
|
||||
return false;
|
||||
}
|
||||
const flake = SnowFlake.SnowFlakes.get(type).get(id);
|
||||
if (flake) {
|
||||
const flake2 = flake.deref()?.getObject();
|
||||
if (flake2) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
getUnixTime() {
|
||||
try {
|
||||
|
@ -91,8 +15,5 @@ class SnowFlake {
|
|||
toString() {
|
||||
return this.id;
|
||||
}
|
||||
getObject() {
|
||||
return this.obj;
|
||||
}
|
||||
}
|
||||
export { SnowFlake };
|
||||
|
|
|
@ -25,13 +25,13 @@ class Channel{
|
|||
headers:Localuser["headers"];
|
||||
name:string;
|
||||
snowflake:SnowFlake<Channel>;
|
||||
parent_id:SnowFlake<Channel>|null;
|
||||
parent_id?:string;
|
||||
parent:Channel|null;
|
||||
children:Channel[];
|
||||
guild_id:string;
|
||||
messageids:Map<SnowFlake<Message>,Message>;
|
||||
permission_overwrites:Map<string,Permissions>;
|
||||
permission_overwritesar:[SnowFlake<Role>,Permissions][];
|
||||
permission_overwritesar:[Role,Permissions][];
|
||||
topic:string;
|
||||
nsfw:boolean;
|
||||
position:number;
|
||||
|
@ -39,7 +39,7 @@ class Channel{
|
|||
lastmessageid:string|undefined;
|
||||
mentions:number;
|
||||
lastpin:string;
|
||||
move_id:SnowFlake<Channel>|null;
|
||||
move_id?:string;
|
||||
typing:number;
|
||||
message_notifications:number;
|
||||
allthewayup:boolean;
|
||||
|
@ -167,7 +167,7 @@ class Channel{
|
|||
}
|
||||
sortPerms(){
|
||||
this.permission_overwritesar.sort((a,b)=>{
|
||||
return this.guild.roles.findIndex(_=>_.snowflake===a[0])-this.guild.roles.findIndex(_=>_.snowflake===b[0]);
|
||||
return this.guild.roles.findIndex(_=>_===a[0])-this.guild.roles.findIndex(_=>_===b[0]);
|
||||
});
|
||||
}
|
||||
setUpInfiniteScroller(){
|
||||
|
@ -231,7 +231,7 @@ class Channel{
|
|||
this.name=json.name;
|
||||
this.snowflake=new SnowFlake(json.id,this);
|
||||
if(json.parent_id){
|
||||
this.parent_id=SnowFlake.getSnowFlakeFromID(json.parent_id,Channel);
|
||||
this.parent_id=json.parent_id;
|
||||
}
|
||||
this.parent=null;
|
||||
this.children=[];
|
||||
|
@ -246,7 +246,10 @@ class Channel{
|
|||
this.permission_overwrites.set(thing.id,new Permissions(thing.allow,thing.deny));
|
||||
const permission=this.permission_overwrites.get(thing.id);
|
||||
if(permission){
|
||||
this.permission_overwritesar.push([SnowFlake.getSnowFlakeFromID(thing.id,Role),permission]);
|
||||
const role=this.guild.roleids.get(thing.id);
|
||||
if(role){
|
||||
this.permission_overwritesar.push([role,permission]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,7 +328,7 @@ class Channel{
|
|||
});
|
||||
}
|
||||
resolveparent(guild:Guild){
|
||||
const parentid=this.parent_id?.id;
|
||||
const parentid=this.parent_id;
|
||||
if(!parentid)return false;
|
||||
this.parent=guild.channelids[parentid];
|
||||
this.parent??=null;
|
||||
|
@ -346,7 +349,7 @@ class Channel{
|
|||
if(thing.move_id&&thing.move_id!==thing.parent_id){
|
||||
thing.parent_id=thing.move_id;
|
||||
thisthing.parent_id=thing.parent?.id;
|
||||
thing.move_id=null;
|
||||
thing.move_id=undefined;
|
||||
//console.log(this.guild.channelids[thisthing.parent_id.id]);
|
||||
}
|
||||
if(thisthing.position||thisthing.parent_id){
|
||||
|
@ -525,7 +528,7 @@ class Channel{
|
|||
if(!that)return;
|
||||
event.preventDefault();
|
||||
if(container){
|
||||
that.move_id=this.snowflake;
|
||||
that.move_id=this.id;
|
||||
if(that.parent){
|
||||
that.parent.children.splice(that.parent.children.indexOf(that),1);
|
||||
}
|
||||
|
@ -791,11 +794,11 @@ class Channel{
|
|||
for(const i in response){
|
||||
let messager:Message;
|
||||
let willbreak=false;
|
||||
if(!SnowFlake.hasSnowFlakeFromID(response[i].id,Message)){
|
||||
messager=new Message(response[i],this);
|
||||
}else{
|
||||
messager=SnowFlake.getSnowFlakeFromID(response[i].id,Message).getObject();
|
||||
if(this.messages.has(response[i].id)){
|
||||
messager=this.messages.get(response[i].id) as Message;
|
||||
willbreak=true;
|
||||
}else{
|
||||
messager=new Message(response[i],this);
|
||||
}
|
||||
this.idToPrev.set(messager.id,previd);
|
||||
this.idToNext.set(previd,messager.id);
|
||||
|
@ -947,10 +950,10 @@ class Channel{
|
|||
const parent=this.guild.channelids[json.parent_id];
|
||||
if(parent){
|
||||
this.parent=parent;
|
||||
this.parent_id=parent.snowflake;
|
||||
this.parent_id=parent.id;
|
||||
}else{
|
||||
this.parent=null;
|
||||
this.parent_id=null;
|
||||
this.parent_id=undefined;
|
||||
}
|
||||
|
||||
this.children=[];
|
||||
|
@ -964,7 +967,10 @@ class Channel{
|
|||
this.permission_overwrites.set(thing.id,new Permissions(thing.allow,thing.deny));
|
||||
const permisions=this.permission_overwrites.get(thing.id);
|
||||
if(permisions){
|
||||
this.permission_overwritesar.push([SnowFlake.getSnowFlakeFromID(thing.id,Role),permisions]);
|
||||
const role=this.guild.roleids.get(thing.id);
|
||||
if(role){
|
||||
this.permission_overwritesar.push([role,permisions]);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.topic=json.topic;
|
||||
|
@ -1140,7 +1146,7 @@ class Channel{
|
|||
});
|
||||
const perm=new Permissions("0","0");
|
||||
this.permission_overwrites.set(role.id,perm);
|
||||
this.permission_overwritesar.push([role.snowflake,perm]);
|
||||
this.permission_overwritesar.push([role,perm]);
|
||||
}
|
||||
async updateRolePermissions(id:string,perms:Permissions){
|
||||
const permission=this.permission_overwrites.get(id);
|
||||
|
|
|
@ -18,7 +18,7 @@ class Guild{
|
|||
snowflake:SnowFlake<Guild>;
|
||||
properties;
|
||||
roles:Role[];
|
||||
roleids:Map<SnowFlake<Role>,Role>;
|
||||
roleids:Map<string,Role>;
|
||||
prevchannel:Channel|undefined;
|
||||
message_notifications:number;
|
||||
headchannels:Channel[];
|
||||
|
@ -80,9 +80,9 @@ class Guild{
|
|||
const settings=new Settings("Settings for "+this.properties.name);
|
||||
|
||||
const s1=settings.addButton("roles");
|
||||
const permlist:[SnowFlake<Role>,Permissions][]=[];
|
||||
const permlist:[Role,Permissions][]=[];
|
||||
for(const thing of this.roles){
|
||||
permlist.push([thing.snowflake,thing.permissions]);
|
||||
permlist.push([thing,thing.permissions]);
|
||||
}
|
||||
s1.options.push(new RoleList(permlist,this,this.updateRolePermissions.bind(this)));
|
||||
settings.show();
|
||||
|
@ -109,7 +109,7 @@ class Guild{
|
|||
for(const roley of json.roles){
|
||||
const roleh=new Role(roley,this);
|
||||
this.roles.push(roleh);
|
||||
this.roleids.set(roleh.snowflake,roleh);
|
||||
this.roleids.set(roleh.id,roleh);
|
||||
}
|
||||
if(member instanceof User){
|
||||
Member.resolveMember(member,this).then(_=>{
|
||||
|
@ -231,7 +231,7 @@ class Guild{
|
|||
if(thing.move_id&&thing.move_id!==thing.parent_id){
|
||||
thing.parent_id=thing.move_id;
|
||||
thisthing.parent_id=thing.parent?.id;
|
||||
thing.move_id=null;
|
||||
thing.move_id=undefined;
|
||||
}
|
||||
if(thisthing.position||thisthing.parent_id){
|
||||
build.push(thisthing);
|
||||
|
@ -554,7 +554,7 @@ class Guild{
|
|||
});
|
||||
const json=await fetched.json();
|
||||
const role=new Role(json,this);
|
||||
this.roleids.set(role.snowflake,role);
|
||||
this.roleids.set(role.id,role);
|
||||
this.roles.push(role);
|
||||
return role;
|
||||
}
|
||||
|
|
|
@ -354,11 +354,12 @@ type wsjson={
|
|||
op:0,
|
||||
d:any,
|
||||
s:number,
|
||||
t:"TYPING_START"|"USER_UPDATE"|"CHANNEL_UPDATE"|"CHANNEL_CREATE"|"CHANNEL_DELETE"|"GUILD_DELETE"|"GUILD_CREATE"|"MESSAGE_REACTION_ADD"|"MESSAGE_REACTION_REMOVE"|"MESSAGE_REACTION_REMOVE_ALL"|"MESSAGE_REACTION_REMOVE_EMOJI"
|
||||
t:"TYPING_START"|"USER_UPDATE"|"CHANNEL_UPDATE"|"CHANNEL_CREATE"|"CHANNEL_DELETE"|"GUILD_DELETE"|"GUILD_CREATE"|"MESSAGE_REACTION_REMOVE_ALL"|"MESSAGE_REACTION_REMOVE_EMOJI"
|
||||
}|{
|
||||
op:0,
|
||||
t:"GUILD_MEMBERS_CHUNK",
|
||||
d:memberChunk
|
||||
d:memberChunk,
|
||||
s:number
|
||||
}|{
|
||||
op:0,
|
||||
d:{
|
||||
|
@ -386,6 +387,29 @@ type wsjson={
|
|||
d:{
|
||||
heartbeat_interval:number
|
||||
}
|
||||
}|{
|
||||
op: 0,
|
||||
t: "MESSAGE_REACTION_ADD",
|
||||
d: {
|
||||
user_id: string,
|
||||
channel_id: string,
|
||||
message_id: string,
|
||||
guild_id?: string,
|
||||
emoji: emojijson,
|
||||
member?: memberjson
|
||||
},
|
||||
s: number
|
||||
}|{
|
||||
op: 0,
|
||||
t: "MESSAGE_REACTION_REMOVE",
|
||||
d: {
|
||||
user_id: string,
|
||||
channel_id: string,
|
||||
message_id: string,
|
||||
guild_id: string,
|
||||
emoji: emojijson
|
||||
},
|
||||
"s": 3
|
||||
}
|
||||
type memberChunk={
|
||||
guild_id: string,
|
||||
|
|
|
@ -8,7 +8,7 @@ import{Dialog}from"./dialog.js";
|
|||
import{getapiurls, getBulkInfo, setTheme, Specialuser}from"./login.js";
|
||||
import{ SnowFlake }from"./snowflake.js";
|
||||
import{ Message }from"./message.js";
|
||||
import{ channeljson, memberjson, presencejson, readyjson }from"./jsontypes.js";
|
||||
import{ channeljson, memberjson, presencejson, readyjson, wsjson }from"./jsontypes.js";
|
||||
import{ Member }from"./member.js";
|
||||
import{ FormError, Settings }from"./settings.js";
|
||||
import{ MarkDown }from"./markdown.js";
|
||||
|
@ -137,7 +137,6 @@ class Localuser{
|
|||
if(this.ws){
|
||||
this.ws.close(4001);
|
||||
}
|
||||
SnowFlake.clear();
|
||||
}
|
||||
swapped=false;
|
||||
async initwebsocket():Promise<void>{
|
||||
|
@ -230,7 +229,7 @@ class Localuser{
|
|||
if(temp.op===0&&temp.t==="READY"){
|
||||
returny();
|
||||
}
|
||||
await this.handleEvent(temp);
|
||||
await this.handleEvent(temp as readyjson);
|
||||
}catch(e){
|
||||
console.error(e);
|
||||
}finally{
|
||||
|
@ -308,7 +307,7 @@ class Localuser{
|
|||
|
||||
await promise;
|
||||
}
|
||||
async handleEvent(temp){
|
||||
async handleEvent(temp:wsjson){
|
||||
console.debug(temp);
|
||||
if(temp.s)this.lastSequence=temp.s;
|
||||
if(temp.op==0){
|
||||
|
@ -319,16 +318,32 @@ class Localuser{
|
|||
}
|
||||
break;
|
||||
case"MESSAGE_DELETE":
|
||||
console.log(temp.d);
|
||||
SnowFlake.getSnowFlakeFromID(temp.d.id,Message).getObject().deleteEvent();
|
||||
{
|
||||
temp.d.guild_id??="@me";
|
||||
const guild=this.guildids.get(temp.d.guild_id);
|
||||
if(!guild) break;
|
||||
const channel=guild.channelids[temp.d.channel_id];
|
||||
if(!channel) break;
|
||||
const message=channel.messages.get(temp.d.id);
|
||||
if(!message) break;
|
||||
message.deleteEvent();
|
||||
break;
|
||||
}
|
||||
case"READY":
|
||||
this.gottenReady(temp as readyjson);
|
||||
break;
|
||||
case"MESSAGE_UPDATE":
|
||||
const message=SnowFlake.getSnowFlakeFromID(temp.d.id,Message).getObject();
|
||||
{
|
||||
temp.d.guild_id??="@me";
|
||||
const guild=this.guildids.get(temp.d.guild_id);
|
||||
if(!guild) break;
|
||||
const channel=guild.channelids[temp.d.channel_id];
|
||||
if(!channel) break;
|
||||
const message=channel.messages.get(temp.d.id);
|
||||
if(!message) break;
|
||||
message.giveData(temp.d);
|
||||
break;
|
||||
}
|
||||
case"TYPING_START":
|
||||
if(this.initialized){
|
||||
this.typingStart(temp);
|
||||
|
@ -376,10 +391,14 @@ class Localuser{
|
|||
break;
|
||||
}
|
||||
case"MESSAGE_REACTION_ADD":
|
||||
if(SnowFlake.hasSnowFlakeFromID(temp.d.message_id,Message)){
|
||||
{
|
||||
temp.d.guild_id??="@me";
|
||||
const message=SnowFlake.getSnowFlakeFromID(temp.d.message_id,Message).getObject();
|
||||
const guild=SnowFlake.getSnowFlakeFromID(temp.d.guild_id,Guild).getObject();
|
||||
const guild=this.guildids.get(temp.d.guild_id);
|
||||
if(!guild) break;
|
||||
const channel=guild.channelids[temp.d.channel_id];
|
||||
if(!channel) break;
|
||||
const message=channel.messages.get(temp.d.message_id);
|
||||
if(!message) break;
|
||||
let thing:Member|{id:string};
|
||||
if(temp.d.member){
|
||||
thing=await Member.new(temp.d.member,guild) as Member;
|
||||
|
@ -390,22 +409,39 @@ class Localuser{
|
|||
}
|
||||
break;
|
||||
case"MESSAGE_REACTION_REMOVE":
|
||||
if(SnowFlake.hasSnowFlakeFromID(temp.d.message_id,Message)){
|
||||
const message=SnowFlake.getSnowFlakeFromID(temp.d.message_id,Message).getObject();
|
||||
console.log("test");
|
||||
{
|
||||
temp.d.guild_id??="@me";
|
||||
const guild=this.guildids.get(temp.d.guild_id);
|
||||
if(!guild) break;
|
||||
const channel=guild.channelids[temp.d.channel_id];
|
||||
if(!channel) break;
|
||||
const message=channel.messages.get(temp.d.message_id);
|
||||
if(!message) break;
|
||||
message.reactionRemove(temp.d.emoji,temp.d.user_id);
|
||||
}
|
||||
break;
|
||||
case"MESSAGE_REACTION_REMOVE_ALL":
|
||||
if(SnowFlake.hasSnowFlakeFromID(temp.d.message_id, Message)){
|
||||
const messageReactionRemoveAll = SnowFlake.getSnowFlakeFromID(temp.d.message_id, Message).getObject();
|
||||
messageReactionRemoveAll.reactionRemoveAll();
|
||||
{
|
||||
temp.d.guild_id??="@me";
|
||||
const guild=this.guildids.get(temp.d.guild_id);
|
||||
if(!guild) break;
|
||||
const channel=guild.channelids[temp.d.channel_id];
|
||||
if(!channel) break;
|
||||
const message=channel.messages.get(temp.d.message_id);
|
||||
if(!message) break;
|
||||
message.reactionRemoveAll();
|
||||
}
|
||||
break;
|
||||
case"MESSAGE_REACTION_REMOVE_EMOJI":
|
||||
if(SnowFlake.hasSnowFlakeFromID(temp.d.message_id, Message)){
|
||||
const messageReactionRemoveEmoji = SnowFlake.getSnowFlakeFromID(temp.d.message_id, Message).getObject();
|
||||
messageReactionRemoveEmoji.reactionRemoveEmoji(temp.d.emoji);
|
||||
{
|
||||
temp.d.guild_id??="@me";
|
||||
const guild=this.guildids.get(temp.d.guild_id);
|
||||
if(!guild) break;
|
||||
const channel=guild.channelids[temp.d.channel_id];
|
||||
if(!channel) break;
|
||||
const message=channel.messages.get(temp.d.message_id);
|
||||
if(!message) break;
|
||||
message.reactionRemoveEmoji(temp.d.emoji);
|
||||
}
|
||||
break;
|
||||
case"GUILD_MEMBERS_CHUNK":
|
||||
|
@ -444,7 +480,9 @@ class Localuser{
|
|||
}
|
||||
createChannel(json:channeljson):void{
|
||||
json.guild_id??="@me";
|
||||
SnowFlake.getSnowFlakeFromID(json.guild_id,Guild).getObject().createChannelpac(json);
|
||||
const guild=this.guildids.get(json.guild_id);
|
||||
if(!guild) return;
|
||||
guild.createChannelpac(json);
|
||||
if(json.guild_id===this.lookingguild?.id){
|
||||
this.loadGuild(json.guild_id);
|
||||
}
|
||||
|
|
|
@ -31,7 +31,8 @@ class Member{
|
|||
}
|
||||
if(thing==="roles"){
|
||||
for(const strrole of memberjson.roles){
|
||||
const role=SnowFlake.getSnowFlakeFromID(strrole,Role).getObject();
|
||||
const role=this.guild.roleids.get(strrole);
|
||||
if(!role) continue;
|
||||
this.roles.push(role);
|
||||
}
|
||||
continue;
|
||||
|
|
|
@ -122,7 +122,7 @@ class PermissionToggle implements OptionsElement<number>{
|
|||
}
|
||||
import{ OptionsElement,Buttons }from"./settings.js";
|
||||
class RoleList extends Buttons{
|
||||
readonly permissions:[SnowFlake<Role>,Permissions][];
|
||||
readonly permissions:[Role,Permissions][];
|
||||
permission:Permissions;
|
||||
readonly guild:Guild;
|
||||
readonly channel:boolean;
|
||||
|
@ -130,7 +130,7 @@ class RoleList extends Buttons{
|
|||
readonly options:Options;
|
||||
onchange:Function;
|
||||
curid:string;
|
||||
constructor(permissions:[SnowFlake<Role>,Permissions][],guild:Guild,onchange:Function,channel=false){
|
||||
constructor(permissions:[Role,Permissions][],guild:Guild,onchange:Function,channel=false){
|
||||
super("Roles");
|
||||
this.guild=guild;
|
||||
this.permissions=permissions;
|
||||
|
@ -147,7 +147,7 @@ class RoleList extends Buttons{
|
|||
}
|
||||
for(const i of permissions){
|
||||
console.log(i);
|
||||
this.buttons.push([i[0].getObject().name,i[0].id]);//
|
||||
this.buttons.push([i[0].name,i[0].id]);
|
||||
}
|
||||
this.options=options;
|
||||
}
|
||||
|
@ -158,9 +158,12 @@ class RoleList extends Buttons{
|
|||
const perm=arr[1];
|
||||
this.permission.deny=perm.deny;
|
||||
this.permission.allow=perm.allow;
|
||||
this.options.name=SnowFlake.getSnowFlakeFromID(str,Role).getObject().name;
|
||||
const role=this.permissions.find(e=>e[0].id===str);
|
||||
if(role){
|
||||
this.options.name=role[0].name;
|
||||
this.options.haschanged=false;
|
||||
}
|
||||
}
|
||||
return this.options.generateHTML();
|
||||
}
|
||||
save(){
|
||||
|
|
|
@ -1,81 +1,7 @@
|
|||
class SnowFlake<x extends WeakKey>{
|
||||
class SnowFlake{
|
||||
public readonly id:string;
|
||||
private static SnowFlakes:Map<any,Map<string,WeakRef<SnowFlake<any>>>>=new Map();
|
||||
private static readonly FinalizationRegistry=new FinalizationRegistry((a:[string,WeakKey])=>{
|
||||
SnowFlake.SnowFlakes.get(a[1]).delete(a[0]);
|
||||
});
|
||||
private obj:x;
|
||||
constructor(id:string,obj:x){
|
||||
if(!obj){
|
||||
constructor(id:string){
|
||||
this.id=id;
|
||||
return;
|
||||
}
|
||||
if(!SnowFlake.SnowFlakes.get(obj.constructor)){
|
||||
SnowFlake.SnowFlakes.set(obj.constructor,new Map());
|
||||
}
|
||||
if(SnowFlake.SnowFlakes.get(obj.constructor).get(id)){
|
||||
const snowflake=SnowFlake.SnowFlakes.get(obj.constructor).get(id).deref();
|
||||
if(snowflake){
|
||||
snowflake.obj=obj;
|
||||
return snowflake;
|
||||
}else{
|
||||
SnowFlake.SnowFlakes.get(obj.constructor).delete(id);
|
||||
}
|
||||
}
|
||||
this.id=id;
|
||||
SnowFlake.SnowFlakes.get(obj.constructor).set(id,new WeakRef(this));
|
||||
SnowFlake.FinalizationRegistry.register(this,[id,obj.constructor]);
|
||||
this.obj=obj;
|
||||
}
|
||||
static clear(){//this is kinda a temp solution, it should be fixed, though its not that easy to do so
|
||||
this.SnowFlakes=new Map();
|
||||
}
|
||||
/**
|
||||
* Just to clarify bc TS, it returns a SnowFlake\<type> which is what you entered with the type parameter
|
||||
* @deprecated
|
||||
**/
|
||||
static getSnowFlakeFromID<T extends {}>(id:string,type: abstract new(...args: never) => T): SnowFlake<T>{
|
||||
if(!SnowFlake.SnowFlakes.get(type)){
|
||||
SnowFlake.SnowFlakes.set(type,new Map());
|
||||
}
|
||||
const snowflake=SnowFlake.SnowFlakes.get(type).get(id);
|
||||
if(snowflake){
|
||||
const obj=snowflake.deref();
|
||||
if(obj){
|
||||
return obj;
|
||||
}else{
|
||||
SnowFlake.SnowFlakes.get(type).delete(id);
|
||||
}
|
||||
}
|
||||
{
|
||||
const snowflake=new SnowFlake(id,undefined);
|
||||
|
||||
SnowFlake.SnowFlakes.get(type).set(id,new WeakRef(snowflake));
|
||||
SnowFlake.FinalizationRegistry.register(this,[id,type]);
|
||||
|
||||
return snowflake;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
*
|
||||
*/
|
||||
static hasSnowFlakeFromID(id:string,type:any){
|
||||
if(!SnowFlake.SnowFlakes.get(type)){
|
||||
return false;
|
||||
}
|
||||
const flake=SnowFlake.SnowFlakes.get(type).get(id);
|
||||
if(flake){
|
||||
const flake2=flake.deref()?.getObject();
|
||||
if(flake2){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
getUnixTime():number{
|
||||
try{
|
||||
|
@ -88,8 +14,5 @@ class SnowFlake<x extends WeakKey>{
|
|||
toString(){
|
||||
return this.id;
|
||||
}
|
||||
getObject():x{
|
||||
return this.obj;
|
||||
}
|
||||
}
|
||||
export{SnowFlake};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue