snowflake and cleaning up classes

This commit is contained in:
MathMan05 2024-07-23 23:01:45 -05:00
parent 14d1c69c7d
commit 7eb3ff6cab
21 changed files with 584 additions and 361 deletions

View file

@ -6,7 +6,7 @@ import { Fullscreen } from "./fullscreen.js";
import { Permissions } from "./permissions.js";
import { Settings, RoleList } from "./settings.js";
import { InfiniteScroller } from "./infiniteScroller.js";
Settings;
import { SnowFlake } from "./snowflake.js";
class Channel {
editing;
type;
@ -35,8 +35,8 @@ class Channel {
static contextmenu = new Contextmenu("channel menu");
replyingto;
infinite;
idToPrev = {};
idToNext = {};
idToPrev = new Map();
idToNext = new Map();
static setupcontextmenu() {
this.contextmenu.addbutton("Copy channel id", function () {
console.log(this);
@ -73,22 +73,24 @@ class Channel {
setUpInfiniteScroller() {
const ids = {};
this.infinite = new InfiniteScroller(async function (id, offset) {
const snowflake = SnowFlake.getSnowFlakeFromID(id, Message);
if (offset === 1) {
if (this.idToPrev[id]) {
return this.idToPrev[id];
if (this.idToPrev.get(snowflake)) {
return this.idToPrev.get(snowflake)?.id;
}
else {
await this.grabmoremessages(id);
return this.idToPrev[id];
await this.grabBefore(id);
return this.idToPrev.get(snowflake)?.id;
}
}
else {
return this.idToNext[id];
return this.idToNext.get(snowflake)?.id;
}
}.bind(this), function (id) {
let res;
const promise = new Promise(_ => { res = _; });
const html = this.messageids[id].buildhtml(this.messageids[this.idToPrev[id]], promise);
const snowflake = SnowFlake.getSnowFlakeFromID(id, Message);
const html = this.messageids.get(snowflake).buildhtml(this.messageids.get(this.idToPrev.get(snowflake)), promise);
ids[id] = res;
return html;
}.bind(this), async function (id) {
@ -106,28 +108,27 @@ class Channel {
this.owner = owner;
this.headers = this.owner.headers;
this.name = JSON.name;
this.id = JSON.id;
this.parent_id = JSON.parent_id;
this.id = new SnowFlake(JSON.id, this);
this.parent_id = new SnowFlake(JSON.parent_id, undefined);
this.parent = null;
this.children = [];
this.guild_id = JSON.guild_id;
this.messageids = {};
this.permission_overwrites = {};
this.messageids = new Map();
this.permission_overwrites = new Map();
this.permission_overwritesar = [];
for (const thing of JSON.permission_overwrites) {
console.log(thing);
if (thing.id === "1182819038095799904" || thing.id === "1182820803700625444") {
continue;
}
;
this.permission_overwrites[thing.id] = new Permissions(thing.allow, thing.deny);
this.permission_overwritesar.push([thing.id, this.permission_overwrites[thing.id]]);
this.permission_overwrites.set(thing.id, new Permissions(thing.allow, thing.deny));
this.permission_overwritesar.push([thing.id, this.permission_overwrites.get(thing.id)]);
}
this.topic = JSON.topic;
this.nsfw = JSON.nsfw;
this.position = JSON.position;
this.lastreadmessageid = null;
this.lastmessageid = JSON.last_message_id;
this.lastmessageid = SnowFlake.getSnowFlakeFromID(JSON.last_message_id, Message);
this.setUpInfiniteScroller();
}
isAdmin() {
@ -143,7 +144,7 @@ class Channel {
return this.owner.info;
}
readStateInfo(json) {
this.lastreadmessageid = json.last_message_id;
this.lastreadmessageid = SnowFlake.getSnowFlakeFromID(json.last_message_id, Message);
this.mentions = json.mention_count;
this.mentions ??= 0;
this.lastpin = json.last_pin_timestamp;
@ -152,15 +153,15 @@ class Channel {
if (!this.hasPermission("VIEW_CHANNEL")) {
return false;
}
return this.lastmessageid !== this.lastreadmessageid && this.type !== 4;
return this.lastmessageid !== this.lastreadmessageid && this.type !== 4 && !!this.lastmessageid.id;
}
hasPermission(name, member = this.guild.member) {
if (member.isAdmin()) {
return true;
}
for (const thing of member.roles) {
if (this.permission_overwrites[thing.id]) {
let perm = this.permission_overwrites[thing.id].getPermission(name);
if (this.permission_overwrites.get(thing.id.id)) {
let perm = this.permission_overwrites.get(thing.id.id).getPermission(name);
if (perm) {
return perm === 1;
}
@ -181,7 +182,7 @@ class Channel {
this.children.sort((a, b) => { return a.position - b.position; });
}
resolveparent(guild) {
this.parent = guild.channelids[this.parent_id];
this.parent = guild.channelids[this.parent_id?.id];
this.parent ??= null;
if (this.parent !== null) {
this.parent.children.push(this);
@ -343,7 +344,7 @@ class Channel {
if (!this.hasunreads) {
return;
}
fetch(this.info.api.toString() + "/v9/channels/" + this.id + "/messages/" + this.lastmessageid + "/ack", {
fetch(this.info.api.toString() + "/channels/" + this.id + "/messages/" + this.lastmessageid + "/ack", {
method: "POST",
headers: this.headers,
body: JSON.stringify({})
@ -433,8 +434,8 @@ class Channel {
["textbox", "Channel name:", this.name, function () { name = this.value; }],
["mdbox", "Channel topic:", this.topic, function () { topic = this.value; }],
["checkbox", "NSFW Channel", this.nsfw, function () { nsfw = this.checked; }],
["button", "", "submit", function () {
fetch(this.info.api.toString() + "/v9/channels/" + thisid, {
["button", "", "submit", () => {
fetch(this.info.api.toString() + "/channels/" + thisid, {
method: "PATCH",
headers: this.headers,
body: JSON.stringify({
@ -457,7 +458,7 @@ class Channel {
console.log(full);
}
deleteChannel() {
fetch(this.info.api.toString() + "/v9/channels/" + this.id, {
fetch(this.info.api.toString() + "/channels/" + this.id, {
method: "DELETE",
headers: this.headers
});
@ -495,11 +496,12 @@ class Channel {
}
}
async getmessage(id) {
if (this.messageids[id]) {
return this.messageids[id];
const snowflake = SnowFlake.getSnowFlakeFromID(id, Message);
if (snowflake.getObject()) {
return snowflake.getObject();
}
else {
const gety = await fetch(this.info.api.toString() + "/v9/channels/" + this.id + "/messages?limit=1&around=" + id, { headers: this.headers });
const gety = await fetch(this.info.api.toString() + "/channels/" + this.id + "/messages?limit=1&around=" + id, { headers: this.headers });
const json = await gety.json();
return new Message(json[0], this);
}
@ -527,8 +529,9 @@ class Channel {
history.pushState(null, null, "/channels/" + this.guild_id + "/" + this.id);
document.getElementById("channelname").textContent = "#" + this.name;
console.log(this);
document.getElementById("typebox").disabled = !this.canMessage;
document.getElementById("typebox").contentEditable = "" + this.canMessage;
}
lastmessage;
async putmessages() {
if (this.allthewayup) {
return;
@ -545,12 +548,15 @@ class Channel {
for (const thing of response) {
const message = new Message(thing, this);
if (prev) {
this.idToNext[message.id] = prev.id;
this.idToPrev[prev.id] = message.id;
this.idToNext.set(message.id, prev.id);
this.idToPrev.set(prev.id, message.id);
}
else {
this.lastmessage = message;
}
prev = message;
if (this.messageids[message.id] === undefined) {
this.messageids[message.id] = message;
if (this.messageids.get(message.id) === undefined) {
this.messageids.set(message.id, message);
}
}
}
@ -563,7 +569,7 @@ class Channel {
}
this.children = build;
}
async grabmoremessages(id) {
async grabBefore(id) {
if (this.allthewayup) {
return;
}
@ -574,7 +580,7 @@ class Channel {
if (response.length === 0) {
this.allthewayup = true;
}
let previd = id;
let previd = SnowFlake.getSnowFlakeFromID(id, Message);
for (const i in response) {
let messager;
if (!next) {
@ -590,11 +596,11 @@ class Channel {
next = undefined;
console.log("ohno", +i + 1);
}
if (this.messageids[messager.id] === undefined) {
this.idToNext[messager.id] = previd;
this.idToPrev[previd] = messager.id;
if (this.messageids.get(messager.id) === undefined) {
this.idToNext.set(messager.id, previd);
this.idToPrev.set(previd, messager.id);
previd = messager.id;
this.messageids[messager.id] = messager;
this.messageids.set(messager.id, messager);
}
else {
console.log("How???");
@ -611,17 +617,47 @@ class Channel {
buildmessages() {
const messages = document.getElementById("channelw");
messages.innerHTML = "";
messages.append(this.infinite.getDiv(this.lastmessageid));
let id;
if (this.messageids.get(this.lastreadmessageid)) {
id = this.lastreadmessageid;
}
else if (this.lastmessage.id) {
id = this.goBackIds(this.lastmessage.id, 50);
console.log("shouldn't");
}
messages.append(this.infinite.getDiv(id.id));
}
goBackIds(id, back) {
while (back !== 0) {
const nextid = this.idToPrev.get(id);
if (nextid) {
id = nextid;
console.log(id);
back--;
}
else {
break;
}
}
return id;
}
updateChannel(JSON) {
this.type = JSON.type;
this.name = JSON.name;
this.parent_id = JSON.parent_id;
this.parent_id = new SnowFlake(JSON.parent_id, undefined);
this.parent = null;
this.children = [];
this.guild_id = JSON.guild_id;
this.messageids = {};
this.permission_overwrites = JSON.permission_overwrites;
this.messageids = new Map();
this.permission_overwrites = new Map();
for (const thing of JSON.permission_overwrites) {
if (thing.id === "1182819038095799904" || thing.id === "1182820803700625444") {
continue;
}
;
this.permission_overwrites.set(thing.id, new Permissions(thing.allow, thing.deny));
this.permission_overwritesar.push([thing.id, this.permission_overwrites.get(thing.id)]);
}
this.topic = JSON.topic;
this.nsfw = JSON.nsfw;
}
@ -706,10 +742,11 @@ class Channel {
return;
}
const messagez = new Message(messagep.d, this);
this.idToNext[this.lastmessageid] = messagez.id;
this.idToPrev[messagez.id] = this.lastmessageid;
console.log(this.lastmessageid, messagez.id, ":3");
this.idToNext.set(this.lastmessageid, messagez.id);
this.idToPrev.set(messagez.id, this.lastmessageid);
this.lastmessageid = messagez.id;
this.messageids[messagez.id] = messagez;
this.messageids.set(messagez.id, messagez);
if (messagez.author === this.localuser.user) {
this.lastreadmessageid = messagez.id;
if (this.myhtml) {
@ -789,11 +826,11 @@ class Channel {
})
});
const perm = new Permissions("0", "0");
this.permission_overwrites[role.id] = perm;
this.permission_overwrites.set(role.id.id, perm);
this.permission_overwritesar.push([role.id, perm]);
}
async updateRolePermissions(id, perms) {
const permission = this.permission_overwrites[id];
const permission = this.permission_overwrites.get(id);
permission.allow = perms.allow;
permission.deny = perms.deny;
await fetch(this.info.api.toString() + "/channels/" + this.id + "/permissions/" + id, {