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();
|
||||
break;
|
||||
{
|
||||
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();
|
||||
message.giveData(temp.d);
|
||||
break;
|
||||
{
|
||||
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,8 +149,11 @@ 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;
|
||||
this.options.haschanged = false;
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
constructor(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 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue