More supported attatchment types and cancel uploads

This commit is contained in:
MathMan05 2024-06-28 19:15:10 -05:00
parent 039491ca87
commit a05c74bb3c
13 changed files with 416 additions and 103 deletions

View file

@ -404,10 +404,24 @@ class Channel {
headers: this.headers
});
}
async getmessage(id) {
if (this.messageids[id]) {
return this.messageids[id];
}
else {
const gety = await fetch(this.info.api.toString() + "/v9/channels/" + this.id + "/messages?limit=1&around=" + id, { headers: this.headers });
const json = await gety.json();
return new Message(json[0], this);
}
}
async getHTML() {
if (this.guild !== this.localuser.lookingguild) {
this.guild.loadGuild();
}
if (this.localuser.channelfocus && this.localuser.channelfocus.myhtml) {
this.localuser.channelfocus.myhtml.classList.remove("viewChannel");
}
this.myhtml.classList.add("viewChannel");
this.guild.prevchannel = this;
this.localuser.channelfocus = this;
const prom = Message.wipeChanel();
@ -420,7 +434,7 @@ class Channel {
document.getElementById("typebox").disabled = !this.canMessage;
}
async putmessages() {
if (this.messages.length >= 100) {
if (this.messages.length >= 100 || this.allthewayup) {
return;
}
;
@ -429,6 +443,9 @@ class Channel {
headers: this.headers,
});
const responce = await j.json();
if (responce.length !== 100) {
this.allthewayup = true;
}
for (const thing of responce) {
const messager = new Message(thing, this);
if (this.messageids[messager.id] === undefined) {

127
.dist/file.js Normal file
View file

@ -0,0 +1,127 @@
import { Fullscreen } from "./fullscreen.js";
class File {
owner;
id;
filename;
content_type;
width;
height;
proxy_url;
url;
size;
constructor(fileJSON, owner) {
console.log(fileJSON);
this.owner = owner;
this.id = fileJSON.id;
this.filename = fileJSON.filename;
this.content_type = fileJSON.content_type;
this.width = fileJSON.width;
this.height = fileJSON.height;
this.url = fileJSON.url;
this.proxy_url = fileJSON.proxy_url;
this.content_type = fileJSON.content_type;
this.size = fileJSON.size;
}
getHTML(temp = false) {
const src = this.proxy_url || this.url;
if (this.content_type.startsWith('image/')) {
const img = document.createElement("img");
img.classList.add("messageimg");
img.onclick = function () {
const full = new Fullscreen(["img", img.src, ["fit"]]);
full.show();
};
img.src = src;
img.height = this.height;
img.width = this.width;
return img;
}
else if (this.content_type.startsWith('video/')) {
const video = document.createElement("video");
const source = document.createElement("source");
source.src = src;
video.append(source);
source.type = this.content_type;
video.controls = !temp;
return video;
}
else if (this.content_type.startsWith('audio/')) {
const audio = document.createElement("audio");
const source = document.createElement("source");
source.src = src;
audio.append(source);
source.type = this.content_type;
audio.controls = !temp;
return audio;
}
else {
return this.createunknown();
}
}
upHTML(files, file) {
const div = document.createElement("div");
const contained = this.getHTML(true);
div.classList.add("containedFile");
div.append(contained);
const controls = document.createElement("div");
const garbage = document.createElement("button");
garbage.textContent = "🗑";
garbage.onclick = _ => {
div.remove();
files.splice(files.indexOf(file), 1);
};
controls.classList.add("controls");
div.append(controls);
controls.append(garbage);
return div;
}
static initFromBlob(file) {
return new File({
filename: file.name,
size: file.size,
id: null,
content_type: file.type,
width: undefined,
height: undefined,
url: URL.createObjectURL(file),
proxy_url: undefined
}, null);
}
createunknown() {
console.log("🗎");
const src = this.proxy_url || this.url;
const div = document.createElement("table");
div.classList.add("unknownfile");
const nametr = document.createElement("tr");
div.append(nametr);
const fileicon = document.createElement("td");
nametr.append(fileicon);
fileicon.append("🗎");
fileicon.classList.add("fileicon");
fileicon.rowSpan = 2;
const nametd = document.createElement("td");
if (src) {
const a = document.createElement("a");
a.href = src;
a.textContent = this.filename;
nametd.append(a);
}
else {
nametd.textContent = this.filename;
}
nametd.classList.add("filename");
nametr.append(nametd);
const sizetr = document.createElement("tr");
const size = document.createElement("td");
sizetr.append(size);
size.textContent = "Size:" + File.filesizehuman(this.size);
size.classList.add("filesize");
div.appendChild(sizetr);
return div;
}
static filesizehuman(fsize) {
var i = fsize == 0 ? 0 : Math.floor(Math.log(fsize) / Math.log(1024));
return +((fsize / Math.pow(1024, i)).toFixed(2)) * 1 + ' ' + ['Bytes', 'Kilobytes', 'Megabytes', 'Gigabytes', 'Terabytes'][i];
}
}
export { File };

View file

@ -208,15 +208,15 @@ function filetohtml(file) {
return createunknownfile(file);
}
}
import { File } from "./file.js";
document.addEventListener('paste', async (e) => {
Array.from(e.clipboardData.files).forEach(async (file) => {
Array.from(e.clipboardData.files).forEach(async (f) => {
const file = File.initFromBlob(f);
e.preventDefault();
const html = filetohtml(file);
const html = file.upHTML(images, f);
pasteimage.appendChild(html);
const blob = URL.createObjectURL(file);
images.push(file);
images.push(f);
imageshtml.push(html);
console.log(file.type);
});
});
setTheme();

View file

@ -66,7 +66,11 @@ class Localuser {
this.guildids[thing.guild_id].notisetting(thing);
}
for (const thing of ready.d.read_state.entries) {
const guild = this.resolveChannelFromID(thing.id).guild;
const channel = this.resolveChannelFromID(thing.id);
if (!channel) {
continue;
}
const guild = channel.guild;
if (guild === undefined) {
continue;
}
@ -238,9 +242,11 @@ class Localuser {
return;
}
resolveChannelFromID(ID) {
let resolve = this.guilds.find(guild => guild.channelids[ID]).channelids[ID];
resolve ??= undefined;
return resolve;
let resolve = this.guilds.find(guild => guild.channelids[ID]);
if (resolve) {
return resolve.channelids[ID];
}
return undefined;
}
updateChannel(JSON) {
this.guildids[JSON.guild_id].updateChannel(JSON);
@ -264,12 +270,12 @@ class Localuser {
}
init() {
const location = window.location.href.split("/");
this.buildservers();
if (location[3] === "channels") {
const guild = this.loadGuild(location[4]);
guild.loadChannel(location[5]);
this.channelfocus = guild.channelids[location[5]];
}
this.buildservers();
}
loaduser() {
document.getElementById("username").textContent = this.user.username;
@ -284,6 +290,12 @@ class Localuser {
if (!guild) {
guild = this.guildids["@me"];
}
if (this.lookingguild) {
this.lookingguild.html.classList.remove("serveropen");
}
if (guild.html) {
guild.html.classList.add("serveropen");
}
this.lookingguild = guild;
document.getElementById("serverName").textContent = guild.properties.name;
//console.log(this.guildids,id)
@ -293,11 +305,18 @@ class Localuser {
}
buildservers() {
const serverlist = document.getElementById("servers"); //
const outdiv = document.createElement("div");
const div = document.createElement("div");
div.textContent = "⌂";
div.classList.add("home", "servericon");
div["all"] = this.guildids["@me"];
serverlist.appendChild(div);
this.guildids["@me"].html = outdiv;
const unread = document.createElement("div");
unread.classList.add("unread");
outdiv.append(unread);
outdiv.appendChild(div);
outdiv.classList.add("servernoti");
serverlist.append(outdiv);
div.onclick = function () {
this["all"].loadGuild();
this["all"].loadChannel();

View file

@ -3,7 +3,7 @@ import { User } from "./user.js";
import { Member } from "./member.js";
import { markdown } from "./markdown.js";
import { Embed } from "./embed.js";
import { Fullscreen } from "./fullscreen.js";
import { File } from "./file.js";
class Message {
static contextmenu = new Contextmenu("message menu");
owner;
@ -67,6 +67,13 @@ class Message {
this.owner = owner;
this.headers = this.owner.headers;
for (const thing of Object.keys(messagejson)) {
if (thing === "attachments") {
this.attachments = [];
for (const thing of messagejson.attachments) {
this.attachments.push(new File(thing, this));
}
continue;
}
this[thing] = messagejson[thing];
}
for (const thing in this.embeds) {
@ -195,9 +202,9 @@ class Message {
line2.classList.add("reply");
line.classList.add("startreply");
replyline.classList.add("replyflex");
fetch(this.info.api.toString() + "/v9/channels/" + this.message_reference.channel_id + "/messages?limit=1&around=" + this.message_reference.message_id, { headers: this.headers }).then(responce => responce.json()).then(responce => {
const author = new User(responce[0].author, this.localuser);
reply.appendChild(markdown(responce[0].content, { stdsize: true }));
this.channel.getmessage(this.message_reference.message_id).then(message => {
const author = message.author;
reply.appendChild(markdown(message.content, { stdsize: true }));
minipfp.src = author.getpfpsrc();
author.profileclick(minipfp);
username.textContent = author.username;
@ -277,24 +284,7 @@ class Message {
console.log(this.attachments);
const attatch = document.createElement("tr");
for (const thing of this.attachments) {
const array = thing.url.split("/");
array.shift();
array.shift();
array.shift();
const src = this.info.cdn.toString() + array.join("/");
if (thing.content_type.startsWith('image/')) {
const img = document.createElement("img");
img.classList.add("messageimg");
img.onclick = function () {
const full = new Fullscreen(["img", img.src, ["fit"]]);
full.show();
};
img.src = src;
attatch.appendChild(img);
}
else {
attatch.appendChild(this.createunknown(thing.filename, thing.size, src));
}
attatch.appendChild(thing.getHTML());
}
messagedwrap.appendChild(attatch);
}