various updates

This commit is contained in:
MathMan05 2024-09-09 17:01:07 -05:00
parent 164aa48ea4
commit cd15064d3a
16 changed files with 312 additions and 150 deletions

View file

@ -212,7 +212,7 @@ class Channel extends SnowFlake {
if (json.parent_id) {
this.parent_id = json.parent_id;
}
this.parent = null;
this.parent = undefined;
this.children = [];
this.guild_id = json.guild_id;
this.permission_overwrites = new Map();
@ -312,12 +312,12 @@ class Channel extends SnowFlake {
const parentid = this.parent_id;
if (!parentid)
return false;
this.parent = guild.channelids[parentid];
this.parent ??= null;
if (this.parent !== null) {
this.parent = this.localuser.channelids.get(parentid);
this.parent ??= undefined;
if (this.parent !== undefined) {
this.parent.children.push(this);
}
return this.parent !== null;
return this.parent !== undefined;
}
calculateReorder() {
let position = -1;
@ -967,13 +967,13 @@ class Channel extends SnowFlake {
updateChannel(json) {
this.type = json.type;
this.name = json.name;
const parent = this.guild.channelids[json.parent_id];
const parent = this.localuser.channelids.get(json.parent_id);
if (parent) {
this.parent = parent;
this.parent_id = parent.id;
}
else {
this.parent = null;
this.parent = undefined;
this.parent_id = undefined;
}
this.children = [];

View file

@ -10,7 +10,6 @@ class Guild extends SnowFlake {
owner;
headers;
channels;
channelids;
properties;
member_count;
roles;
@ -86,7 +85,6 @@ class Guild extends SnowFlake {
this.owner = owner;
this.headers = this.owner.headers;
this.channels = [];
this.channelids = {};
this.properties = json.properties;
this.roles = [];
this.roleids = new Map();
@ -117,7 +115,7 @@ class Guild extends SnowFlake {
for (const thing of json.channels) {
const temp = new Channel(thing, this);
this.channels.push(temp);
this.channelids[temp.id] = temp;
this.localuser.channelids.set(temp.id, temp);
}
this.headchannels = [];
for (const thing of this.channels) {
@ -126,7 +124,7 @@ class Guild extends SnowFlake {
this.headchannels.push(thing);
}
}
this.prevchannel = this.channelids[this.perminfo.prevchannel];
this.prevchannel = this.localuser.channelids.get(this.perminfo.prevchannel);
}
get perminfo() {
return this.localuser.perminfo.guilds[this.id];
@ -429,9 +427,12 @@ class Guild extends SnowFlake {
return this.member.hasRole(r);
}
loadChannel(ID) {
if (ID && this.channelids[ID]) {
this.channelids[ID].getHTML();
return;
if (ID) {
const channel = this.localuser.channelids.get(ID);
if (channel) {
channel.getHTML();
return;
}
}
if (this.prevchannel) {
console.log(this.prevchannel);
@ -449,7 +450,7 @@ class Guild extends SnowFlake {
this.localuser.loadGuild(this.id);
}
updateChannel(json) {
const channel = this.channelids[json.id];
const channel = this.localuser.channelids.get(json.id);
if (channel) {
channel.updateChannel(json);
this.headchannels = [];
@ -468,7 +469,7 @@ class Guild extends SnowFlake {
}
createChannelpac(json) {
const thischannel = new Channel(json, this);
this.channelids[json.id] = thischannel;
this.localuser.channelids.set(json.id, thischannel);
this.channels.push(thischannel);
thischannel.resolveparent(this);
if (!thischannel.parent) {
@ -515,8 +516,10 @@ class Guild extends SnowFlake {
channelselect.show();
}
delChannel(json) {
const channel = this.channelids[json.id];
delete this.channelids[json.id];
const channel = this.localuser.channelids.get(json.id);
this.localuser.channelids.delete(json.id);
if (!channel)
return;
this.channels.splice(this.channels.indexOf(channel), 1);
const indexy = this.headchannels.indexOf(channel);
if (indexy !== -1) {

View file

@ -24,6 +24,9 @@ class InfiniteScroller {
this.div = scroll;
this.div.addEventListener("scroll", _ => {
this.checkscroll();
if (this.scrollBottom < 5) {
this.scrollBottom = 5;
}
if (this.timeout === null) {
this.timeout = setTimeout(this.updatestuff.bind(this), 300);
}

View file

@ -30,6 +30,7 @@ class Localuser {
ws;
connectionSucceed = 0;
errorBackoff = 0;
channelids = new Map();
userMap = new Map();
instancePing = {
name: "Unknown",
@ -64,8 +65,8 @@ class Localuser {
this.userinfo.username = this.user.username;
this.userinfo.pfpsrc = this.user.getpfpsrc();
this.status = this.ready.d.user_settings.status;
this.channelfocus = null;
this.lookingguild = null;
this.channelfocus = undefined;
this.lookingguild = undefined;
this.guildhtml = new Map();
const members = {};
for (const thing of ready.d.merged_members) {
@ -86,15 +87,11 @@ class Localuser {
this.guildids.get(thing.guild_id).notisetting(thing);
}
for (const thing of ready.d.read_state.entries) {
const channel = this.resolveChannelFromID(thing.id);
const channel = this.channelids.get(thing.channel_id);
if (!channel) {
continue;
}
const guild = channel.guild;
if (guild === undefined) {
continue;
}
guild.channelids[thing.channel_id].readStateInfo(thing);
channel.readStateInfo(thing);
}
for (const thing of ready.d.relationships) {
const user = new User(thing.user, this);
@ -112,8 +109,8 @@ class Localuser {
if (this.channelfocus) {
this.channelfocus.infinite.delete();
}
this.lookingguild = null;
this.channelfocus = null;
this.lookingguild = undefined;
this.channelfocus = undefined;
}
unload() {
this.initialized = false;
@ -307,10 +304,7 @@ class Localuser {
case "MESSAGE_DELETE":
{
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];
const channel = this.channelids.get(temp.d.channel_id);
if (!channel)
break;
const message = channel.messages.get(temp.d.id);
@ -325,10 +319,7 @@ class Localuser {
case "MESSAGE_UPDATE":
{
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];
const channel = this.channelids.get(temp.d.channel_id);
if (!channel)
break;
const message = channel.messages.get(temp.d.id);
@ -389,7 +380,7 @@ class Localuser {
const guild = this.guildids.get(temp.d.guild_id);
if (!guild)
break;
const channel = guild.channelids[temp.d.channel_id];
const channel = this.channelids.get(temp.d.channel_id);
if (!channel)
break;
const message = channel.messages.get(temp.d.message_id);
@ -408,10 +399,7 @@ class Localuser {
case "MESSAGE_REACTION_REMOVE":
{
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];
const channel = this.channelids.get(temp.d.channel_id);
if (!channel)
break;
const message = channel.messages.get(temp.d.message_id);
@ -423,10 +411,7 @@ class Localuser {
case "MESSAGE_REACTION_REMOVE_ALL":
{
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];
const channel = this.channelids.get(temp.d.channel_id);
if (!channel)
break;
const message = channel.messages.get(temp.d.message_id);
@ -438,10 +423,7 @@ class Localuser {
case "MESSAGE_REACTION_REMOVE_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];
const channel = this.channelids.get(temp.d.channel_id);
if (!channel)
break;
const message = channel.messages.get(temp.d.message_id);
@ -473,13 +455,6 @@ class Localuser {
}
}
heartbeat_interval;
resolveChannelFromID(ID) {
const resolve = this.guilds.find(guild => guild.channelids[ID]);
if (resolve) {
return resolve.channelids[ID];
}
return undefined;
}
updateChannel(json) {
const guild = this.guildids.get(json.guild_id);
if (guild) {
@ -506,13 +481,9 @@ class Localuser {
}
gotoid;
async goToChannel(id) {
let guild;
for (const thing of this.guilds) {
if (thing.channelids[id]) {
guild = thing;
}
}
if (guild) {
const channel = this.channelids.get(id);
if (channel) {
const guild = channel.guild;
guild.loadGuild();
guild.loadChannel(id);
}
@ -540,7 +511,7 @@ class Localuser {
return;
}
guild.loadChannel(location[5]);
this.channelfocus = guild.channelids[location[5]];
this.channelfocus = this.channelids.get(location[5]);
}
}
loaduser() {
@ -770,11 +741,11 @@ class Localuser {
}
messageCreate(messagep) {
messagep.d.guild_id ??= "@me";
const guild = this.guildids.get(messagep.d.guild_id);
if (!guild)
return;
guild.channelids[messagep.d.channel_id].messageCreate(messagep);
this.unreads();
const channel = this.channelids.get(messagep.d.channel_id);
if (channel) {
channel.messageCreate(messagep);
this.unreads();
}
}
unreads() {
for (const thing of this.guilds) {
@ -787,10 +758,7 @@ class Localuser {
}
async typingStart(typing) {
//
const guild = this.guildids.get(typing.d.guild_id);
if (!guild)
return;
const channel = guild.channelids[typing.d.channel_id];
const channel = this.channelids.get(typing.d.channel_id);
if (!channel)
return;
channel.typingStart(typing);

View file

@ -1,6 +1,8 @@
import { Channel } from "./channel.js";
import { Dialog } from "./dialog.js";
import { Emoji } from "./emoji.js";
import { Localuser } from "./localuser.js";
import { Member } from "./member.js";
class MarkDown {
txt;
keep;
@ -22,6 +24,14 @@ class MarkDown {
this.owner = owner;
this.stdsize = stdsize;
}
get localuser() {
if (this.owner instanceof Localuser) {
return this.owner;
}
else {
return this.owner.localuser;
}
}
get rawString() {
return this.txt.join("");
}
@ -415,6 +425,68 @@ class MarkDown {
continue;
}
}
if (txt[i] === "<" && (txt[i + 1] === "@" || txt[i + 1] === "#")) {
let id = "";
let j = i + 2;
const numbers = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
for (; txt[j] !== undefined; j++) {
const char = txt[j];
if (!numbers.has(char)) {
break;
}
id += char;
}
if (txt[j] === ">") {
appendcurrent();
const mention = document.createElement("span");
mention.classList.add("mentionMD");
mention.contentEditable = "false";
const char = txt[i + 1];
i = j;
switch (char) {
case "@":
const user = this.localuser.userMap.get(id);
if (user) {
mention.textContent = `@${user.name}`;
let guild = null;
if (this.owner instanceof Channel) {
guild = this.owner.guild;
}
if (!keep) {
user.bind(mention, guild);
}
if (guild) {
Member.resolveMember(user, guild).then(member => {
if (member) {
mention.textContent = `@${member.name}`;
}
});
}
}
else {
mention.textContent = `@unknown`;
}
break;
case "#":
const channel = this.localuser.channelids.get(id);
if (channel) {
mention.textContent = `#${channel.name}`;
if (!keep) {
mention.onclick = _ => {
this.localuser.goToChannel(id);
};
}
}
else {
mention.textContent = `#unknown`;
}
break;
}
span.appendChild(mention);
mention.setAttribute("real", `<${char}${id}>`);
continue;
}
}
if (txt[i] === "<" && txt[i + 1] === "t" && txt[i + 2] === ":") {
let found = false;
const build = ["<", "t", ":"];
@ -571,6 +643,9 @@ class MarkDown {
if (element.tagName.toLowerCase() === "br") {
return "\n";
}
if (element.hasAttribute("real")) {
return element.getAttribute("real");
}
let build = "";
for (const thing of element.childNodes) {
if (thing instanceof Text) {
@ -633,15 +708,29 @@ class MarkDown {
throw Error(url + " is not a valid URL");
}
}
static replace(base, newelm) {
const basechildren = base.children;
const newchildren = newelm.children;
for (const thing of newchildren) {
}
}
}
//solution from https://stackoverflow.com/questions/4576694/saving-and-restoring-caret-position-for-contenteditable-div
let text = "";
function saveCaretPosition(context) {
const selection = window.getSelection();
if (!selection)
return;
const range = selection.getRangeAt(0);
range.setStart(context, 0);
const len = range.toString().length;
text = selection.toString();
let len = text.length + 1;
for (const str in text.split("\n")) {
if (str.length !== 0) {
len--;
}
}
len += +(text[text.length - 1] === "\n");
return function restore() {
if (!selection)
return;

View file

@ -273,6 +273,11 @@ class Message extends SnowFlake {
premessage = this.channel.messages.get(this.channel.idToPrev.get(this.id));
}
const div = this.div;
for (const user of this.mentions) {
if (user === this.localuser.user) {
div.classList.add("mentioned");
}
}
if (this === this.channel.replyingto) {
div.classList.add("replying");
}

View file

@ -138,6 +138,9 @@ class User extends SnowFlake {
get localuser() {
return this.owner;
}
get name() {
return this.username;
}
constructor(userjson, owner, dontclone = false) {
super(userjson.id);
this.owner = owner;