formatting updates

This commit is contained in:
MathMan05 2024-12-20 19:28:08 -06:00
parent ffe21e6d6c
commit d2d0f45c81
50 changed files with 7783 additions and 7432 deletions

View file

@ -1,55 +1,50 @@
import { getBulkInfo, Specialuser } from "./utils/utils.js";
import { I18n } from "./i18n.js";
import { Dialog, FormError } from "./settings.js";
import { checkInstance } from "./utils/utils.js";
import {getBulkInfo, Specialuser} from "./utils/utils.js";
import {I18n} from "./i18n.js";
import {Dialog, FormError} from "./settings.js";
import {checkInstance} from "./utils/utils.js";
await I18n.done;
(async ()=>{
await I18n.done
const instanceField=document.getElementById("instanceField");
const emailField= document.getElementById("emailField");
const pwField= document.getElementById("pwField");
const loginButton=document.getElementById("loginButton");
const noAccount=document.getElementById("switch")
if(instanceField&&emailField&&pwField&&loginButton&&noAccount){
instanceField.textContent=I18n.getTranslation("htmlPages.instanceField");
emailField.textContent=I18n.getTranslation("htmlPages.emailField");
pwField.textContent=I18n.getTranslation("htmlPages.pwField");
loginButton.textContent=I18n.getTranslation("htmlPages.loginButton");
noAccount.textContent=I18n.getTranslation("htmlPages.noAccount");
(async () => {
await I18n.done;
const instanceField = document.getElementById("instanceField");
const emailField = document.getElementById("emailField");
const pwField = document.getElementById("pwField");
const loginButton = document.getElementById("loginButton");
const noAccount = document.getElementById("switch");
if (instanceField && emailField && pwField && loginButton && noAccount) {
instanceField.textContent = I18n.getTranslation("htmlPages.instanceField");
emailField.textContent = I18n.getTranslation("htmlPages.emailField");
pwField.textContent = I18n.getTranslation("htmlPages.pwField");
loginButton.textContent = I18n.getTranslation("htmlPages.loginButton");
noAccount.textContent = I18n.getTranslation("htmlPages.noAccount");
}
})()
})();
function trimswitcher(){
function trimswitcher() {
const json = getBulkInfo();
const map = new Map();
for(const thing in json.users){
for (const thing in json.users) {
const user = json.users[thing];
let wellknown = user.serverurls.wellknown;
if(wellknown.at(-1) !== "/"){
if (wellknown.at(-1) !== "/") {
wellknown += "/";
}
wellknown =(user.id||user.email)+"@"+wellknown;
if(map.has(wellknown)){
wellknown = (user.id || user.email) + "@" + wellknown;
if (map.has(wellknown)) {
const otheruser = map.get(wellknown);
if(otheruser[1].serverurls.wellknown.at(-1) === "/"){
if (otheruser[1].serverurls.wellknown.at(-1) === "/") {
delete json.users[otheruser[0]];
map.set(wellknown, [thing, user]);
}else{
} else {
delete json.users[thing];
}
}else{
} else {
map.set(wellknown, [thing, user]);
}
}
for(const thing in json.users){
if(thing.at(-1) === "/"){
for (const thing in json.users) {
if (thing.at(-1) === "/") {
const user = json.users[thing];
delete json.users[thing];
json.users[thing.slice(0, -1)] = user;
@ -59,9 +54,7 @@ function trimswitcher(){
console.log(json);
}
function adduser(user: typeof Specialuser.prototype.json){
function adduser(user: typeof Specialuser.prototype.json) {
user = new Specialuser(user);
const info = getBulkInfo();
info.users[user.uid] = user;
@ -73,32 +66,30 @@ const instancein = document.getElementById("instancein") as HTMLInputElement;
let timeout: ReturnType<typeof setTimeout> | string | number | undefined | null = null;
// let instanceinfo;
if(instancein){
if (instancein) {
console.log(instancein);
instancein.addEventListener("keydown", ()=>{
instancein.addEventListener("keydown", () => {
const verify = document.getElementById("verify");
verify!.textContent = I18n.getTranslation("login.waiting");
if(timeout !== null && typeof timeout !== "string"){
clearTimeout(timeout);
}
timeout = setTimeout(()=>checkInstance((instancein as HTMLInputElement).value), 1000);
verify!.textContent = I18n.getTranslation("login.waiting");
if (timeout !== null && typeof timeout !== "string") {
clearTimeout(timeout);
}
timeout = setTimeout(() => checkInstance((instancein as HTMLInputElement).value), 1000);
});
if(localStorage.getItem("instanceinfo")){
if (localStorage.getItem("instanceinfo")) {
const json = JSON.parse(localStorage.getItem("instanceinfo")!);
if(json.value){
if (json.value) {
(instancein as HTMLInputElement).value = json.value;
}else{
} else {
(instancein as HTMLInputElement).value = json.wellknown;
}
}else{
} else {
checkInstance("https://spacebar.chat/");
}
}
async function login(username: string, password: string, captcha: string){
if(captcha === ""){
async function login(username: string, password: string, captcha: string) {
if (captcha === "") {
captcha = "";
}
const options = {
@ -113,25 +104,25 @@ async function login(username: string, password: string, captcha: string){
"Content-type": "application/json; charset=UTF-8",
},
};
try{
try {
const info = JSON.parse(localStorage.getItem("instanceinfo")!);
const api = info.login + (info.login.startsWith("/") ? "/" : "");
return await fetch(api + "/auth/login", options)
.then(response=>response.json())
.then(response=>{
.then((response) => response.json())
.then((response) => {
console.log(response, response.message);
if(response.message === "Invalid Form Body"){
if (response.message === "Invalid Form Body") {
return response.errors.login._errors[0].message;
console.log("test");
}
//this.serverurls||!this.email||!this.token
console.log(response);
if(response.captcha_sitekey){
if (response.captcha_sitekey) {
const capt = document.getElementById("h-captcha");
if(capt!.children.length){
if (capt!.children.length) {
eval("hcaptcha.reset()");
}else{
} else {
const capty = document.createElement("div");
capty.classList.add("h-captcha");
@ -141,107 +132,100 @@ async function login(username: string, password: string, captcha: string){
capt!.append(script);
capt!.append(capty);
}
}else{
} else {
console.log(response);
if(response.ticket){
const better=new Dialog("");
const form=better.options.addForm("",(res:any)=>{
if(res.message){
throw new FormError(ti,res.message);
}else{
console.warn(res);
if(!res.token)return;
adduser({
serverurls: JSON.parse(localStorage.getItem("instanceinfo") as string),
email: username,
token: res.token,
}).username = username;
const redir = new URLSearchParams(
window.location.search
).get("goback");
if(redir){
window.location.href = redir;
}else{
window.location.href = "/channels/@me";
if (response.ticket) {
const better = new Dialog("");
const form = better.options.addForm(
"",
(res: any) => {
if (res.message) {
throw new FormError(ti, res.message);
} else {
console.warn(res);
if (!res.token) return;
adduser({
serverurls: JSON.parse(localStorage.getItem("instanceinfo") as string),
email: username,
token: res.token,
}).username = username;
const redir = new URLSearchParams(window.location.search).get("goback");
if (redir) {
window.location.href = redir;
} else {
window.location.href = "/channels/@me";
}
}
}
},{
fetchURL:api + "/auth/mfa/totp",
method:"POST",
headers:{
"Content-Type": "application/json",
}
});
},
{
fetchURL: api + "/auth/mfa/totp",
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
);
form.addTitle(I18n.getTranslation("2faCode"));
const ti=form.addTextInput("","code");
better.show()
}else{
const ti = form.addTextInput("", "code");
better.show();
} else {
console.warn(response);
if(!response.token)return;
if (!response.token) return;
adduser({
serverurls: JSON.parse(localStorage.getItem("instanceinfo")!),
email: username,
token: response.token,
}).username = username;
const redir = new URLSearchParams(window.location.search).get(
"goback"
);
if(redir){
const redir = new URLSearchParams(window.location.search).get("goback");
if (redir) {
window.location.href = redir;
}else{
} else {
window.location.href = "/channels/@me";
}
return"";
return "";
}
}
});
}catch(error){
} catch (error) {
console.error("Error:", error);
}
}
async function check(e: SubmitEvent){
async function check(e: SubmitEvent) {
e.preventDefault();
const target = e.target as HTMLFormElement;
const h = await login(
(target[1] as HTMLInputElement).value,
(target[2] as HTMLInputElement).value,
(target[3] as HTMLInputElement).value
(target[3] as HTMLInputElement).value,
);
const wrongElement = document.getElementById("wrong");
if(wrongElement){
if (wrongElement) {
wrongElement.textContent = h;
}
console.log(h);
}
if(document.getElementById("form")){
if (document.getElementById("form")) {
const form = document.getElementById("form");
if(form){
form.addEventListener("submit", (e: SubmitEvent)=>check(e));
if (form) {
form.addEventListener("submit", (e: SubmitEvent) => check(e));
}
}
//this currently does not work, and need to be implemented better at some time.
if(!localStorage.getItem("SWMode")){
localStorage.setItem("SWMode","true");
if (!localStorage.getItem("SWMode")) {
localStorage.setItem("SWMode", "true");
}
const switchurl = document.getElementById("switch") as HTMLAreaElement;
if(switchurl){
if (switchurl) {
switchurl.href += window.location.search;
const instance = new URLSearchParams(window.location.search).get("instance");
console.log(instance);
if(instance){
if (instance) {
instancein.value = instance;
checkInstance("");
}
}
trimswitcher();
export{
adduser,
};
export {adduser};