make DMs less special and fix bug

This commit is contained in:
MathMan05
2025-10-07 10:31:46 -05:00
parent 24aa107b9e
commit 2ce4c673f2
4 changed files with 29 additions and 159 deletions
+14 -14
View File
@@ -46,7 +46,7 @@ class Channel extends SnowFlake {
children!: Channel[];
guild_id!: string;
permission_overwrites!: Map<string, Permissions>;
permission_overwritesar!: [Role, Permissions][];
permission_overwritesar: [Role, Permissions][] = [];
topic!: string;
nsfw!: boolean;
position: number = 0;
@@ -435,13 +435,16 @@ class Channel extends SnowFlake {
last_pin_timestamp?: string;
constructor(json: channeljson | -1, owner: Guild, id: string = json === -1 ? "" : json.id) {
super(id);
this.owner = owner;
this.headers = this.owner.headers;
if (json === -1) {
return;
}
this.editing;
this.type = json.type;
this.owner = owner;
this.headers = this.owner.headers;
this.name = json.name;
if (json.parent_id) {
this.parent_id = json.parent_id;
@@ -452,9 +455,6 @@ class Channel extends SnowFlake {
this.permission_overwrites = new Map();
this.permission_overwritesar = [];
for (const thing of json.permission_overwrites) {
if (thing.id === "1182819038095799904" || thing.id === "1182820803700625444") {
continue;
}
if (!this.permission_overwrites.has(thing.id)) {
//either a bug in the server requires this, or the API is cursed
this.permission_overwrites.set(thing.id, new Permissions(thing.allow, thing.deny));
@@ -2163,15 +2163,16 @@ class Channel extends SnowFlake {
return "default";
}
}
fakeMessages = new Map<Message, HTMLElement>();
fakeMessages = new WeakMap<Message, HTMLElement>();
nonceMap = new Map<string, string>();
destroyFakeMessage(id: string) {
const message = this.messages.get(id);
if (!message) return;
message.deleteEvent();
const div = this.fakeMessages.get(message);
div?.remove();
this.fakeMessages.delete(message);
message.deleteEvent();
this.messages.delete(id);
for (const {url} of message.attachments) {
@@ -2455,12 +2456,6 @@ class Channel extends SnowFlake {
if (this.lastmessageid) this.infinite.focus(this.lastmessageid, false, true);
}
async messageCreate(messagep: messageCreateJson): Promise<void> {
if (this.localuser.channelfocus !== this) {
const id = this.nonceMap.get(messagep.d.nonce);
if (id) {
this.destroyFakeMessage(id);
}
}
if (!this.hasPermission("VIEW_CHANNEL")) {
return;
}
@@ -2469,6 +2464,11 @@ class Channel extends SnowFlake {
if (this.lastmessageid) {
this.idToNext.set(this.lastmessageid, messagez.id);
this.idToPrev.set(messagez.id, this.lastmessageid);
if (!this.messages.has(this.lastmessageid)) {
console.error("something bad happened");
}
} else {
console.error("something bad happened");
}
if (messagez.mentionsuser(this.localuser.user) && messagez.author !== this.localuser.user) {
this.mentions++;
+7 -137
View File
@@ -3,16 +3,14 @@ import {Channel} from "./channel.js";
import {Message} from "./message.js";
import {Localuser} from "./localuser.js";
import {User} from "./user.js";
import {channeljson, dirrectjson, memberjson, messagejson} from "./jsontypes.js";
import {channeljson, dirrectjson, memberjson} from "./jsontypes.js";
import {Permissions} from "./permissions.js";
import {SnowFlake} from "./snowflake.js";
import {Contextmenu} from "./contextmenu.js";
import {I18n} from "./i18n.js";
import {Float, FormError} from "./settings.js";
import {CustomHTMLDivElement} from "./index.js";
class Direct extends Guild {
declare channelids: {[key: string]: Group};
channels: Group[];
getUnixTime(): number {
throw new Error("Do not call this for Direct, it does not make sense");
@@ -20,10 +18,7 @@ class Direct extends Guild {
constructor(json: dirrectjson[], owner: Localuser) {
super(-1, owner, null);
this.message_notifications = 0;
this.owner = owner;
this.headers = this.localuser.headers;
this.channels = [];
this.channelids = {};
// @ts-ignore it's a hack, but it's a hack that works
this.properties = {};
this.roles = [];
@@ -33,14 +28,12 @@ class Direct extends Guild {
for (const thing of json) {
const temp = new Group(thing, this);
this.channels.push(temp);
this.channelids[temp.id] = temp;
this.localuser.channelids.set(temp.id, temp);
}
this.headchannels = this.channels;
}
createChannelpac(json: any) {
const thischannel = new Group(json, this);
this.channelids[thischannel.id] = thischannel;
this.channels.push(thischannel);
this.localuser.channelids.set(thischannel.id, thischannel);
this.sortchannels();
@@ -48,7 +41,7 @@ class Direct extends Guild {
return thischannel;
}
delChannel(json: channeljson) {
const channel = this.channelids[json.id];
const channel = this.localuser.channelids.get(json.id) as Group;
super.delChannel(json);
if (channel) {
channel.del();
@@ -311,10 +304,10 @@ class Direct extends Guild {
giveMember(_member: memberjson) {
throw new Error("not a real guild, can't give member object");
}
getRole(/* ID: string */) {
getRole() {
return null;
}
hasRole(/* r: string */) {
hasRole() {
return false;
}
isAdmin() {
@@ -392,8 +385,7 @@ class Group extends Channel {
}
constructor(json: dirrectjson, owner: Direct) {
super(-1, owner, json.id);
this.owner = owner;
this.headers = this.guild.headers;
this.name = json.recipients[0]?.username;
const userSet = new Set(json.recipients.map((user) => new User(user, this.localuser)));
@@ -418,6 +410,7 @@ class Group extends Channel {
this.updatePosition();
}
updatePosition() {
//TODO see if fake messages break this
if (this.lastmessageid) {
this.position = SnowFlake.stringToUnixTime(this.lastmessageid);
} else {
@@ -444,127 +437,6 @@ class Group extends Channel {
return div;
}
async getHTML(addstate = true, _: boolean | void = undefined, aroundMessage?: string) {
if (this.localuser.channelfocus) {
this.localuser.channelfocus.collectBox();
}
const typebox = document.getElementById("typebox") as CustomHTMLDivElement;
const md = typebox.markdown;
typebox.textContent = this.textSave;
md.boxupdate(Infinity);
this.localuser.fileExtange(this.files, this.htmls);
const pinnedM = document.getElementById("pinnedMDiv");
if (pinnedM) {
if (this.unreadPins()) {
pinnedM.classList.add("unreadPin");
} else {
pinnedM.classList.remove("unreadPin");
}
}
const id = ++Channel.genid;
if (this.localuser.channelfocus) {
this.localuser.channelfocus.infinite.delete();
}
if (this.guild !== this.localuser.lookingguild) {
this.guild.loadGuild();
}
this.guild.prevchannel = this;
this.localuser.channelfocus = this;
const prom = this.infinite.delete();
if (addstate) {
history.pushState(
[this.guild_id, this.id, aroundMessage],
"",
"/channels/" + this.guild_id + "/" + this.id + (aroundMessage ? `/${aroundMessage}` : ""),
);
}
this.localuser.pageTitle("@" + this.name);
(document.getElementById("channelTopic") as HTMLElement).setAttribute("hidden", "");
const loading = document.getElementById("loadingdiv") as HTMLDivElement;
Channel.regenLoadingMessages();
loading.classList.add("loading");
this.rendertyping();
(document.getElementById("typebox") as HTMLDivElement).contentEditable = "" + true;
(document.getElementById("upload") as HTMLElement).style.visibility = "visible";
(document.getElementById("typediv") as HTMLElement).style.visibility = "visible";
(document.getElementById("typebox") as HTMLDivElement).focus();
await this.putmessages();
await prom;
this.localuser.getSidePannel();
if (id !== Channel.genid) {
return;
}
this.buildmessages(aroundMessage);
}
async messageCreate(messagep: {d: messagejson}) {
if (this.localuser.channelfocus !== this) {
const id = this.nonceMap.get(messagep.d.nonce);
if (id) {
this.destroyFakeMessage(id);
}
}
this.mentions++;
const messagez = new Message(messagep.d, this);
if (this.lastmessageid) {
this.idToNext.set(this.lastmessageid, messagez.id);
this.idToPrev.set(messagez.id, this.lastmessageid);
}
this.idToNext.set(messagez.id, undefined);
this.lastmessageid = messagez.id;
if (messagez.author === this.localuser.user) {
const next = this.messages.get(this.idToNext.get(this.lastreadmessageid as string) as string);
this.lastreadmessageid = messagez.id;
if (next) {
next.generateMessage();
}
}
if (messagez.author === this.localuser.user) {
this.lastreadmessageid = messagez.id;
if (this.myhtml) {
this.myhtml.classList.remove("cunread");
}
} else {
if (this.myhtml) {
this.myhtml.classList.add("cunread");
}
}
this.unreads();
this.updatePosition();
this.infinite.addedBottom();
this.guild.sortchannels();
if (this.myhtml) {
const parrent = this.myhtml.parentElement as HTMLElement;
parrent.prepend(this.myhtml);
}
if (this === this.localuser.channelfocus) {
if (!this.infinitefocus) {
await this.tryfocusinfinate();
}
await this.infinite.addedBottom();
}
this.unreads();
if (messagez.author === this.localuser.user) {
this.mentions = 0;
return;
}
if (this.localuser.lookingguild?.prevchannel === this && document.hasFocus()) {
return;
}
if (this.notification === "all") {
this.notify(messagez);
} else if (this.notification === "mentions" && messagez.mentionsuser(this.localuser.user)) {
this.notify(messagez);
}
}
notititle(message: Message) {
return message.author.username;
}
@@ -617,9 +489,7 @@ class Group extends Channel {
} else {
}
}
isAdmin(): boolean {
return false;
}
hasPermission(name: string): boolean {
return dmPermissions.hasPermission(name);
}
+6 -6
View File
@@ -1230,19 +1230,21 @@ class Guild extends SnowFlake {
this.banner = json.banner;
}
constructor(json: guildjson | -1, owner: Localuser, member: memberjson | User | null) {
super(typeof json === "number" ? "@me" : json.id);
this.owner = owner;
this.perminfo ??= {channels: {}};
this.headers = this.owner.headers;
if (json === -1 || member === null) {
super("@me");
return;
}
if (json.stickers.length) {
console.log(json.stickers, ":3");
}
super(json.id);
this.owner = owner;
this.large = json.large;
this.member_count = json.member_count;
this.emojis = json.emojis || [];
this.headers = this.owner.headers;
this.channels = [];
if (json.properties) {
this.properties = json.properties;
@@ -1259,7 +1261,6 @@ class Guild extends SnowFlake {
}
this.message_notifications = 0;
this.sortRoles();
if (member instanceof User) {
console.warn(member);
@@ -1278,7 +1279,6 @@ class Guild extends SnowFlake {
});
}
this.perminfo ??= {channels: {}};
for (const thing of json.channels) {
const temp = new Channel(thing, this);
this.channels.push(temp);
+2 -2
View File
@@ -471,6 +471,7 @@ class Message extends SnowFlake {
});
}
deleteEvent() {
if (!this.channel.messages.has(this.id)) return;
console.log("deleted");
if (this.div) {
this.div.remove();
@@ -479,8 +480,7 @@ class Message extends SnowFlake {
}
const prev = this.channel.idToPrev.get(this.id);
const next = this.channel.idToNext.get(this.id);
this.channel.idToPrev.delete(this.id);
this.channel.idToNext.delete(this.id);
this.channel.messages.delete(this.id);
if (prev && next) {
this.channel.idToPrev.set(next, prev);