2026-04-14 17:41:39 +02:00

153 lines
5.3 KiB
JavaScript

/**
* @param {string} key
* @param {number} newMode
* @returns {string}
*/
app.use("/toggleDarkMode.lvorex", express.json())
app.post("/toggleDarkMode.lvorex", async (req, res) => {
const postBody = req.body
const { keyFound, userKey } = await controlKey(req, postBody.key)
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
const { newMode } = postBody
let result = await query(`
update \`madmin_accounts\`
set \`darkMode\` = ${newMode}
where \`username\` = '${userKey.userName.replaceAll("'", "\\'")}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Successfully toggled dark mode." })
})
/**
* @param {string} key
* @param {number} newMode
* @returns {string}
*/
app.use("/toggleSyncMode.lvorex", express.json())
app.post("/toggleSyncMode.lvorex", async (req, res) => {
const postBody = req.body
const { keyFound, userKey } = await controlKey(req, postBody.key)
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
const { newMode } = postBody
let newDarkMode = 0
if (newMode === 1) {
if (Number(moment(Date.now()).format("HH")) >= 18 || Number(moment(Date.now()).format("HH")) <= 3) {
newDarkMode = 1
}
}
let result = await query(`
update \`madmin_accounts\`
set \`syncedDarkMode\` = ${newMode},
\`darkMode\` = ${newDarkMode}
where \`username\` = '${userKey.userName.replaceAll("'", "\\'")}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Successfully toggled sync mode." })
})
// Change User Credentials Start
const ControlIfUsernameExists = async (username) => {
let result = await query(`
select * from \`madmin_accounts\`
where \`username\` = '${username.replaceAll("'", "\\'")}'
`)
if (result === false) return false
if (result.length === 0) return false
return true
}
/**
* @param {string} key
* @param {string} type
* @param {string} newValue
* @returns {string}
*/
app.use("/changeUserCredentials.lvorex", express.json())
app.post("/changeUserCredentials.lvorex", async (req, res) => {
const postBody = req.body
const { keyFound, userKey } = await controlKey(req, postBody.key)
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
if (postBody.type === "username") {
const IsUsernameExists = await ControlIfUsernameExists(postBody.newValue)
if (IsUsernameExists) return res.json({ code: 404, message: "This username is already taken by a different user." })
let result = await query(`
update \`madmin_accounts\`
set \`username\` = '${postBody.newValue.replaceAll("'", "\\'")}'
where \`username\` = '${userKey.userName.replaceAll("'", "\\'")}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
usersKeys.find(u => sha1(u.key) === postBody.key).userName = postBody.newValue
res.json({ code: 200, message: "Your username successfully updated." })
return
} else if (postBody.type === "password") {
let result = await query(`
update \`madmin_accounts\`
set \`password\` = '${sha1(postBody.newValue)}'
where \`username\` = '${userKey.userName.replaceAll("'", "\\'")}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Your password successfully updated." })
return
} else if (postBody.type === "picture") {
let result = await query(`
update \`madmin_accounts\`
set \`avatar\` = '${postBody.newValue}'
where \`username\` = '${userKey.userName.replaceAll("'", "\\'")}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Your avatar successfully updated." })
return
}
})
// Change User Credentials End
/**
* @param {string} key
* @returns {string}
*/
app.use("/useDiscordAvatar.lvorex", express.json())
app.post("/useDiscordAvatar.lvorex", async (req, res) => {
const postBody = req.body
const { keyFound, userKey } = await controlKey(req, postBody.key)
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
let result = await query(`
select * from \`madmin_accounts\`
where \`id\` = ${userKey.userId}
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
result = result [0]
if (result.discord_avatar === "") return res.json({ code: 404, message: "You're not using discord." })
const { discord_avatar: DiscordAvatar } = result
result = await query(`
update \`madmin_accounts\`
set \`avatar\` = '${DiscordAvatar}'
where \`id\` = ${userKey.userId}
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: DiscordAvatar })
})