Bug fixes, blocking and various other changes

This commit is contained in:
MathMan05 2024-08-21 11:37:26 -05:00
parent 42a438f6dc
commit dbee2f3628
22 changed files with 679 additions and 370 deletions

View file

@ -58,14 +58,14 @@ class Channel {
this.contextmenu.addbutton("Delete channel", function () {
console.log(this);
this.deleteChannel();
}, null, _ => { console.log(_); return _.isAdmin(); });
}, null, function () { return this.isAdmin(); });
this.contextmenu.addbutton("Edit channel", function () {
this.editChannel();
}, null, _ => { return _.isAdmin(); });
}, null, function () { return this.isAdmin(); });
this.contextmenu.addbutton("Make invite", function () {
this.createInvite();
}, null, (_) => {
return _.hasPermission("CREATE_INSTANT_INVITE") && _.type !== 4;
}, null, function () {
return this.hasPermission("CREATE_INSTANT_INVITE") && this.type !== 4;
});
/*
this.contextmenu.addbutton("Test button",function(){
@ -154,7 +154,7 @@ class Channel {
}
setUpInfiniteScroller() {
this.infinite = new InfiniteScroller(async function (id, offset) {
const snowflake = SnowFlake.getSnowFlakeFromID(id, Message);
const snowflake = this.messages.get(id).snowflake;
if (offset === 1) {
if (this.idToPrev.has(snowflake)) {
return this.idToPrev.get(snowflake)?.id;
@ -184,12 +184,15 @@ class Channel {
const html = messgage.buildhtml();
return html;
}
else {
console.error(id + " not found");
}
}
catch (e) {
console.error(e);
}
}.bind(this), async function (id) {
const message = SnowFlake.getSnowFlakeFromID(id, Message).getObject();
const message = this.messages.get(id);
try {
if (message) {
message.deleteDiv();
@ -374,7 +377,7 @@ class Channel {
caps.classList.add("capsflex");
decdiv.classList.add("channeleffects");
decdiv.classList.add("channel");
Channel.contextmenu.bind(decdiv, this);
Channel.contextmenu.bindContextmenu(decdiv, this, undefined);
decdiv["all"] = this;
for (const channel of this.children) {
childrendiv.appendChild(channel.createguildHTML(admin));
@ -400,7 +403,7 @@ class Channel {
if (this.hasunreads) {
div.classList.add("cunread");
}
Channel.contextmenu.bind(div, this);
Channel.contextmenu.bindContextmenu(div, this, undefined);
if (admin) {
this.coatDropDiv(div);
}
@ -815,12 +818,6 @@ class Channel {
async grabArround(id) {
throw new Error("please don't call this, no one has implemented it :P");
}
buildmessage(message, next) {
const built = message.buildhtml(next);
if (built) {
document.getElementById("messages").prepend(built);
}
}
async buildmessages() {
/*
if(((!this.lastmessage)||(!this.lastmessage.snowflake)||(!this.goBackIds(this.lastmessage.snowflake,50,false)))&&this.lastreadmessageid){

View file

@ -27,23 +27,23 @@ class Contextmenu {
this.buttons.push([text, onclick, img, shown, enabled, "submenu"]);
return {};
}
makemenu(x, y, addinfo, obj) {
makemenu(x, y, addinfo, other) {
const div = document.createElement("div");
div.classList.add("contextmenu", "flexttb");
for (const thing of this.buttons) {
if (!thing[3](addinfo)) {
if (!thing[3].bind(addinfo)(other)) {
continue;
}
const intext = document.createElement("button");
intext.disabled = !thing[4]();
intext.disabled = !thing[4].bind(addinfo)(other);
intext.classList.add("contextbutton");
intext.textContent = thing[0];
console.log(thing);
if (thing[5] === "button") {
intext.onclick = thing[1].bind(addinfo, obj);
intext.onclick = thing[1].bind(addinfo, other);
}
else if (thing[5] === "submenu") {
intext.onclick = thing[1].bind(addinfo);
intext.onclick = thing[1].bind(addinfo, other);
}
div.appendChild(intext);
}
@ -58,11 +58,11 @@ class Contextmenu {
Contextmenu.currentmenu = div;
return this.div;
}
bind(obj, addinfo = undefined) {
bindContextmenu(obj, addinfo, other) {
const func = (event) => {
event.preventDefault();
event.stopImmediatePropagation();
this.makemenu(event.clientX, event.clientY, addinfo, obj);
this.makemenu(event.clientX, event.clientY, addinfo, other);
};
obj.addEventListener("contextmenu", func);
return func;

View file

@ -42,10 +42,14 @@ class Guild {
});
Guild.contextmenu.addbutton("Leave guild", function () {
this.confirmleave();
}, null, function (_) { return _.properties.owner_id !== _.member.user.id; });
}, null, function (_) {
return this.properties.owner_id !== this.member.user.id;
});
Guild.contextmenu.addbutton("Delete guild", function () {
this.confirmDelete();
}, null, function (_) { return _.properties.owner_id === _.member.user.id; });
}, null, function (_) {
return this.properties.owner_id === this.member.user.id;
});
Guild.contextmenu.addbutton("Create invite", function () {
console.log(this);
}, null, _ => true, _ => false);
@ -271,7 +275,7 @@ class Guild {
this.loadGuild();
this.loadChannel();
};
Guild.contextmenu.bind(img, this);
Guild.contextmenu.bindContextmenu(img, this, undefined);
}
else {
const div = document.createElement("div");
@ -283,7 +287,7 @@ class Guild {
this.loadGuild();
this.loadChannel();
};
Guild.contextmenu.bind(div, this);
Guild.contextmenu.bindContextmenu(div, this, undefined);
}
return divy;
}

View file

@ -103,7 +103,7 @@ catch (e) {
thisuser = new Localuser(-1);
}
{
const menu = new Contextmenu("create rightclick");
const menu = new Contextmenu("create rightclick"); //Really should go into the localuser class, but that's a later thing
menu.addbutton("Create channel", function () {
if (thisuser.lookingguild) {
thisuser.lookingguild.createchannels();
@ -114,7 +114,7 @@ catch (e) {
thisuser.lookingguild.createcategory();
}
}, null, _ => { return thisuser.isAdmin(); });
menu.bind(document.getElementById("channels"));
menu.bindContextmenu(document.getElementById("channels"), 0, 0);
}
const pasteimage = document.getElementById("pasteimage");
let replyingto = null;

View file

@ -3,8 +3,9 @@ class InfiniteScroller {
getHTMLFromID;
destroyFromID;
reachesBottom;
minDist = 3000;
maxDist = 8000;
minDist = 2000;
fillDist = 3000;
maxDist = 6000;
HTMLElements = [];
div;
scroll;
@ -25,11 +26,21 @@ class InfiniteScroller {
this.div = div;
//this.interval=setInterval(this.updatestuff.bind(this,true),100);
this.scroll = scroll;
this.div.addEventListener("scroll", this.watchForChange.bind(this));
this.scroll.addEventListener("scroll", this.watchForChange.bind(this));
this.div.addEventListener("scroll", _ => {
if (this.scroll)
this.scrollTop = this.scroll.scrollTop;
this.watchForChange();
});
this.scroll.addEventListener("scroll", _ => {
if (null === this.timeout) {
this.timeout = setTimeout(this.updatestuff.bind(this), 300);
}
this.watchForChange();
});
{
let oldheight = 0;
new ResizeObserver(_ => {
this.updatestuff();
const change = oldheight - div.offsetHeight;
if (change > 0 && this.scroll) {
this.scroll.scrollTop += change;
@ -49,11 +60,16 @@ class InfiniteScroller {
scrollBottom;
scrollTop;
needsupdate = true;
averageheight = 60;
async updatestuff() {
this.timeout = null;
if (!this.scroll)
return;
this.timeout = null;
this.scrollBottom = this.scroll.scrollHeight - this.scroll.scrollTop - this.scroll.clientHeight;
this.averageheight = this.scroll.scrollHeight / this.HTMLElements.length;
if (this.averageheight < 10) {
this.averageheight = 60;
}
this.scrollTop = this.scroll.scrollTop;
if (!this.scrollBottom) {
if (!await this.watchForChange()) {
@ -84,120 +100,160 @@ class InfiniteScroller {
const scrollBottom = this.scrollBottom;
return () => {
if (this.scroll && scrollBottom < 30) {
this.scroll.scrollTop = this.scroll.scrollHeight;
this.scroll.scrollTop = this.scroll.scrollHeight + 20;
}
};
}
async watchForTop() {
async watchForTop(already = false, fragement = new DocumentFragment()) {
if (!this.scroll)
return false;
let again = false;
if (this.scrollTop === 0) {
this.scrollTop = 1;
this.scroll.scrollTop = 1;
}
if (this.scrollTop < this.minDist) {
let nextid;
const firstelm = this.HTMLElements.at(0);
if (firstelm) {
const previd = firstelm[1];
nextid = await this.getIDFromOffset(previd, 1);
}
if (!nextid) {
}
else {
again = true;
const html = await this.getHTMLFromID(nextid);
if (!html) {
this.destroyFromID(nextid);
console.error("html isn't defined");
throw Error("html isn't defined");
try {
let again = false;
if (this.scrollTop < (already ? this.fillDist : this.minDist)) {
let nextid;
const firstelm = this.HTMLElements.at(0);
if (firstelm) {
const previd = firstelm[1];
nextid = await this.getIDFromOffset(previd, 1);
}
this.scroll.prepend(html);
this.HTMLElements.unshift([html, nextid]);
this.scrollTop += 60;
if (!nextid) {
}
else {
const html = await this.getHTMLFromID(nextid);
if (!html) {
this.destroyFromID(nextid);
return false;
}
again = true;
fragement.prepend(html);
this.HTMLElements.unshift([html, nextid]);
this.scrollTop += this.averageheight;
}
;
}
;
if (this.scrollTop > this.maxDist) {
const html = this.HTMLElements.shift();
if (html) {
again = true;
await this.destroyFromID(html[1]);
this.scrollTop -= this.averageheight;
}
}
if (again) {
await this.watchForTop(true, fragement);
}
return again;
}
if (this.scrollTop > this.maxDist) {
const html = this.HTMLElements.shift();
if (html) {
again = true;
await this.destroyFromID(html[1]);
this.scrollTop -= 60;
finally {
if (!already) {
if (this.scroll.scrollTop === 0) {
this.scrollTop = 1;
this.scroll.scrollTop = 10;
}
this.scroll.prepend(fragement, fragement);
}
}
if (again) {
await this.watchForTop();
}
return again;
}
async watchForBottom() {
async watchForBottom(already = false, fragement = new DocumentFragment()) {
if (!this.scroll)
return false;
let again = false;
const scrollBottom = this.scrollBottom;
if (scrollBottom < this.minDist) {
let nextid;
const lastelm = this.HTMLElements.at(-1);
if (lastelm) {
const previd = lastelm[1];
nextid = await this.getIDFromOffset(previd, -1);
try {
let again = false;
const scrollBottom = this.scrollBottom;
if (scrollBottom < (already ? this.fillDist : this.minDist)) {
let nextid;
const lastelm = this.HTMLElements.at(-1);
if (lastelm) {
const previd = lastelm[1];
nextid = await this.getIDFromOffset(previd, -1);
}
if (!nextid) {
}
else {
again = true;
const html = await this.getHTMLFromID(nextid);
fragement.appendChild(html);
this.HTMLElements.push([html, nextid]);
this.scrollBottom += this.averageheight;
}
;
}
if (!nextid) {
if (scrollBottom > this.maxDist) {
const html = this.HTMLElements.pop();
if (html) {
await this.destroyFromID(html[1]);
this.scrollBottom -= this.averageheight;
again = true;
}
}
else {
again = true;
const html = await this.getHTMLFromID(nextid);
this.scroll.appendChild(html);
this.HTMLElements.push([html, nextid]);
this.scrollBottom += 60;
if (scrollBottom < 30) {
if (again) {
await this.watchForBottom(true, fragement);
}
return again;
}
finally {
if (!already) {
this.scroll.append(fragement);
if (this.scrollBottom < 30) {
this.scroll.scrollTop = this.scroll.scrollHeight;
}
}
;
}
if (scrollBottom > this.maxDist) {
const html = this.HTMLElements.pop();
if (html) {
await this.destroyFromID(html[1]);
this.scrollBottom -= 60;
again = true;
}
}
if (again) {
await this.watchForBottom();
}
return again;
}
watchtime = false;
changePromise;
async watchForChange() {
try {
if (this.currrunning) {
return false;
if (this.currrunning) {
this.watchtime = true;
if (this.changePromise) {
return await this.changePromise;
}
else {
this.currrunning = true;
return true;
}
if (!this.div) {
this.currrunning = false;
}
else {
this.watchtime = false;
this.currrunning = true;
}
this.changePromise = new Promise(async (res) => {
try {
try {
if (!this.div) {
res(false);
return false;
}
const out = await Promise.allSettled([this.watchForTop(), this.watchForBottom()]);
const changed = (out[0].value || out[1].value);
if (null === this.timeout && changed) {
this.timeout = setTimeout(this.updatestuff.bind(this), 300);
}
if (!this.currrunning) {
console.error("something really bad happened");
}
res(!!changed);
return !!changed;
}
catch (e) {
console.error(e);
}
res(false);
return false;
}
const out = await Promise.allSettled([this.watchForTop(), this.watchForBottom()]);
const changed = (out[0].value || out[1].value);
if (null === this.timeout && changed) {
this.timeout = setTimeout(this.updatestuff.bind(this), 300);
catch (e) {
throw e;
}
if (!this.currrunning) {
console.error("something really bad happened");
finally {
this.changePromise = undefined;
setTimeout(_ => {
this.currrunning = false;
if (this.watchtime) {
this.watchForChange();
}
}, 300);
}
this.currrunning = false;
return !!changed;
}
catch (e) {
console.error(e);
}
return false;
});
return await this.changePromise;
}
async focus(id, flash = true) {
let element;
@ -206,6 +262,7 @@ class InfiniteScroller {
element = thing[0];
}
}
console.log(id, element, this.HTMLElements.length, ":3");
if (element) {
if (flash) {
element.scrollIntoView({

View file

@ -89,6 +89,11 @@ class Localuser {
const guildid = guild.snowflake;
this.guildids.get(guildid.id).channelids[thing.channel_id].readStateInfo(thing);
}
for (const thing of ready.d.relationships) {
const user = new User(thing.user, this);
user.nickname = thing.nickname;
user.relationshipType = thing.type;
}
}
outoffocus() {
const servers = document.getElementById("servers");

View file

@ -22,7 +22,6 @@ function trimswitcher() {
const map = new Map();
for (const thing in json.users) {
const user = json.users[thing];
console.log(user, json.users);
let wellknown = user.serverurls.wellknown;
if (wellknown[wellknown.length - 1] !== "/") {
wellknown += "/";

View file

@ -1,6 +1,5 @@
import { User } from "./user.js";
import { Role } from "./role.js";
import { Contextmenu } from "./contextmenu.js";
import { SnowFlake } from "./snowflake.js";
class Member {
static already = {};
@ -9,18 +8,6 @@ class Member {
roles = [];
id;
nick;
static contextmenu = new Contextmenu("User Menu");
static setUpContextMenu() {
this.contextmenu.addbutton("Copy user id", function () {
navigator.clipboard.writeText(this.id);
});
this.contextmenu.addbutton("Message user", function () {
fetch(this.info.api + "/users/@me/channels", { method: "POST",
body: JSON.stringify({ "recipients": [this.id] }),
headers: this.localuser.headers
});
});
}
constructor(memberjson, owner) {
if (User.userids[memberjson.id]) {
this.user = User.userids[memberjson.id];
@ -169,12 +156,10 @@ class Member {
*/
html.style.color = this.getColor();
}
this.profileclick(html);
Member.contextmenu.bind(html);
//this.profileclick(html);
}
profileclick(html) {
//to be implemented
}
}
Member.setUpContextMenu();
export { Member };

View file

@ -49,7 +49,7 @@ class Message {
Message.contextmenu.addbutton("Copy raw text", function () {
navigator.clipboard.writeText(this.content.rawString);
});
Message.contextmenu.addbutton("Reply", function (div) {
Message.contextmenu.addbutton("Reply", function () {
this.channel.setReplying(this);
});
Message.contextmenu.addbutton("Copy message id", function () {
@ -65,10 +65,14 @@ class Message {
const markdown = document.getElementById("typebox")["markdown"];
markdown.txt = this.content.rawString.split('');
markdown.boxupdate(document.getElementById("typebox"));
}, null, _ => { return _.author.id === _.localuser.user.id; });
}, null, function () {
return this.author.id === this.localuser.user.id;
});
Message.contextmenu.addbutton("Delete message", function () {
this.delete();
}, null, _ => { return _.canDelete(); });
}, null, function () {
return this.canDelete();
});
}
constructor(messagejson, owner) {
this.owner = owner;
@ -169,7 +173,7 @@ class Message {
return this.owner.info;
}
messageevents(obj) {
const func = Message.contextmenu.bind(obj, this);
const func = Message.contextmenu.bindContextmenu(obj, this, undefined);
this.div = obj;
obj.classList.add("messagediv");
}
@ -245,7 +249,16 @@ class Message {
}
}
reactdiv;
generateMessage(premessage = undefined) {
blockedPropigate() {
const premessage = this.channel.idToPrev.get(this.snowflake)?.getObject();
if (premessage?.author === this.author) {
premessage.blockedPropigate();
}
else {
this.generateMessage();
}
}
generateMessage(premessage = undefined, ignoredblock = false) {
if (!this.div)
return;
if (!premessage) {
@ -257,7 +270,66 @@ class Message {
}
div.innerHTML = "";
const build = document.createElement('div');
build.classList.add("flexltr");
build.classList.add("flexltr", "message");
div.classList.remove("zeroheight");
if (this.author.relationshipType === 2) {
if (ignoredblock) {
if (premessage?.author !== this.author) {
const span = document.createElement("span");
span.textContent = `You have this user blocked, click to hide these messages.`;
div.append(span);
span.classList.add("blocked");
span.onclick = _ => {
const scroll = this.channel.infinite.scrollTop;
let next = this;
while (next?.author === this.author) {
next.generateMessage(undefined);
next = this.channel.idToNext.get(next.snowflake)?.getObject();
}
if (this.channel.infinite.scroll && scroll) {
this.channel.infinite.scroll.scrollTop = scroll;
}
};
}
}
else {
div.classList.remove("topMessage");
if (premessage?.author === this.author) {
div.classList.add("zeroheight");
premessage.blockedPropigate();
div.appendChild(build);
return div;
}
else {
build.classList.add("blocked", "topMessage");
const span = document.createElement("span");
let count = 1;
let next = this.channel.idToNext.get(this.snowflake)?.getObject();
while (next?.author === this.author) {
count++;
next = this.channel.idToNext.get(next.snowflake)?.getObject();
}
span.textContent = `You have this user blocked, click to see the ${count} blocked messages.`;
build.append(span);
span.onclick = _ => {
const scroll = this.channel.infinite.scrollTop;
const func = this.channel.infinite.snapBottom();
let next = this;
while (next?.author === this.author) {
next.generateMessage(undefined, true);
next = this.channel.idToNext.get(next.snowflake)?.getObject();
console.log("loopy");
}
if (this.channel.infinite.scroll && scroll) {
func();
this.channel.infinite.scroll.scrollTop = scroll;
}
};
div.appendChild(build);
return div;
}
}
}
if (this.message_reference) {
const replyline = document.createElement("div");
const line = document.createElement("hr");
@ -269,7 +341,6 @@ class Message {
replyline.appendChild(username);
const reply = document.createElement("div");
username.classList.add("username");
this.author.bind(username, this.guild);
reply.classList.add("replytext");
replyline.appendChild(reply);
const line2 = document.createElement("hr");
@ -278,6 +349,10 @@ class Message {
line.classList.add("startreply");
replyline.classList.add("replyflex");
this.channel.getmessage(this.message_reference.message_id).then(message => {
if (message.author.relationshipType === 2) {
username.textContent = "Blocked user";
return;
}
const author = message.author;
reply.appendChild(message.content.makeHTML({ stdsize: true }));
minipfp.src = author.getpfpsrc();
@ -290,7 +365,6 @@ class Message {
};
div.appendChild(replyline);
}
build.classList.add("message");
div.appendChild(build);
if ({ 0: true, 19: true }[this.type] || this.attachments.length !== 0) {
const pfpRow = document.createElement('div');
@ -491,21 +565,17 @@ class Message {
}
}
}
let now = new Date().toLocaleDateString();
const yesterday = new Date(now);
yesterday.setDate(new Date().getDate() - 1);
let yesterdayStr = yesterday.toLocaleDateString();
function formatTime(date) {
const now = new Date();
const sameDay = date.getDate() === now.getDate() &&
date.getMonth() === now.getMonth() &&
date.getFullYear() === now.getFullYear();
const yesterday = new Date(now);
yesterday.setDate(now.getDate() - 1);
const isYesterday = date.getDate() === yesterday.getDate() &&
date.getMonth() === yesterday.getMonth() &&
date.getFullYear() === yesterday.getFullYear();
const formatTime = date => date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
if (sameDay) {
const datestring = date.toLocaleDateString();
const formatTime = (date) => date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
if (datestring === now) {
return `Today at ${formatTime(date)}`;
}
else if (isYesterday) {
else if (datestring === yesterdayStr) {
return `Yesterday at ${formatTime(date)}`;
}
else {

View file

@ -10,6 +10,8 @@ class User {
snowflake;
avatar;
username;
nickname = null;
relationshipType = 0;
bio;
discriminator;
pronouns;
@ -75,6 +77,25 @@ class User {
headers: this.localuser.headers
});
});
this.contextmenu.addbutton("Block user", function () {
this.block();
}, null, function () {
return this.relationshipType !== 2;
});
this.contextmenu.addbutton("Unblock user", function () {
this.unblock();
}, null, function () {
return this.relationshipType === 2;
});
this.contextmenu.addbutton("Friend request", function () {
fetch(`${this.info.api}/users/@me/relationships/${this.id}`, {
method: "PUT",
headers: this.owner.headers,
body: JSON.stringify({
type: 1
})
});
});
}
static clear() {
this.userids = {};
@ -148,6 +169,7 @@ class User {
}
buildpfp() {
const pfp = document.createElement('img');
pfp.loading = "lazy";
pfp.src = this.getpfpsrc();
pfp.classList.add("pfp");
pfp.classList.add("userid:" + this.id);
@ -183,6 +205,7 @@ class User {
bind(html, guild = null, error = true) {
if (guild && guild.id !== "@me") {
Member.resolveMember(this, guild).then(_ => {
User.contextmenu.bindContextmenu(html, this, _);
if (_ === undefined && error) {
const error = document.createElement("span");
error.textContent = "!";
@ -203,7 +226,6 @@ class User {
else {
this.profileclick(html);
}
User.contextmenu.bind(html, this);
}
static async resolve(id, localuser) {
const json = await fetch(localuser.info.api.toString() + "/users/" + id + "/profile", { headers: localuser.headers }).then(_ => _.json());
@ -218,6 +240,35 @@ class User {
thing.src = src;
}
}
block() {
fetch(`${this.info.api}/users/@me/relationships/${this.id}`, {
method: "PUT",
headers: this.owner.headers,
body: JSON.stringify({
type: 2
})
});
this.relationshipType = 2;
const channel = this.localuser.channelfocus;
if (channel) {
for (const thing of channel.messages) {
thing[1].generateMessage();
}
}
}
unblock() {
fetch(`${this.info.api}/users/@me/relationships/${this.id}`, {
method: "DELETE",
headers: this.owner.headers,
});
this.relationshipType = 0;
const channel = this.localuser.channelfocus;
if (channel) {
for (const thing of channel.messages) {
thing[1].generateMessage();
}
}
}
getpfpsrc() {
if (this.hypotheticalpfp) {
return this.avatar;