dashboard-core/theme/dbd-soft-ui/pages/admin/feed.js

197 lines
5.1 KiB
JavaScript
Raw Normal View History

2023-06-22 19:36:52 +05:00
const db = require("quick.db");
const { icons, otherIcons } = require("../../icons");
2023-06-19 14:21:58 +05:00
module.exports = {
2023-06-22 19:36:52 +05:00
page: "/feed",
// eslint-disable-next-line no-unused-vars
execute: async (req, res, app, config, themeConfig, info) => {
if (req.query.action === "delete") {
const deleteFeed = req.query.feed;
if (!deleteFeed) return res.redirect("/admin?error=invalidFeed");
if (!/^\d+$/.test(deleteFeed)) return res.redirect("/admin?error=invalidFeed");
if (deleteFeed !== "1" && deleteFeed !== "2" && deleteFeed !== "3") return res.redirect("/admin?error=invalidFeed");
if (deleteFeed === "1") {
if (!db.get("feeds.one")) return res.redirect("/admin?error=invalidFeed");
if (db.get("feeds.two")) {
const f = await db.get("feeds.two");
await db.set("feeds.one", {
color: f.color,
description: f.description,
published: f.published,
icon: f.icon,
diff: f.diff,
});
} else await db.delete("feeds.one");
if (db.get("feeds.three")) {
const f = await db.get("feeds.three");
await db.set("feeds.two", {
color: f.color,
description: f.description,
published: f.published,
icon: f.icon,
diff: f.diff,
});
await db.delete("feeds.three");
}
} else if (deleteFeed === "2") {
if (!db.get("feeds.two")) return res.redirect("/admin?error=invalidFeed");
if (db.get("feeds.one")) {
const f = await db.get("feeds.one");
await db.set("feeds.two", {
color: f.color,
description: f.description,
published: f.published,
icon: f.icon,
diff: f.diff,
});
await db.delete("feeds.one");
} else {
await db.delete("feeds.two");
}
} else if (deleteFeed === "3") {
if (!db.get("feeds.three")) return res.redirect("/admin?error=invalidFeed");
await db.delete("feeds.three");
if (db.get("feeds.two")) {
const f = await db.get("feeds.two");
await db.set("feeds.three", {
color: f.color,
description: f.description,
published: f.published,
icon: f.icon,
diff: f.diff,
});
}
if (db.get("feeds.one")) {
const f = await db.get("feeds.one");
await db.set("feeds.two", {
color: f.color,
description: f.description,
published: f.published,
icon: f.icon,
diff: f.diff,
});
}
}
return res.redirect("/admin");
} else if (req.query.action === "create") {
const { color, description, icon } = req.query;
if (!color || !description || !icon) return res.redirect("/admin?error=missingData");
if (color !== "red" && color !== "orange" && color !== "pink" && color !== "gray" && color !== "green" && color !== "blue" && color !== "dark") return res.redirect("/admin?error=invalidData");
if (description.length < 3 || description.length > 128) return res.redirect("/admin?error=invalidData");
if (!icons.includes(icon) && !otherIcons.includes(icon)) return res.redirect("/admin?error=invalidData");
let diff;
let col;
if (otherIcons.includes(icon)) diff = true;
if (color === "red") col = "danger";
if (color === "orange") col = "warning";
if (color === "pink") col = "primary";
if (color === "gray") col = "secondary";
if (color === "green") col = "success";
if (color === "blue") col = "info";
if (color === "dark") col = "dark";
if (db.get("feeds.three") && db.get("feeds.two") && db.get("feeds.one")) {
await db.delete("feeds.one");
const f3 = db.get("feeds.three");
const f2 = db.get("feeds.two");
await db.set("feeds.two", {
color: f3.color,
description: f3.description,
published: f3.published,
icon: f3.icon,
diff: f3.diff,
});
await db.set("feeds.one", {
color: f2.color,
description: f2.description,
published: f2.published,
icon: f2.icon,
diff: f2.diff,
});
await db.set("feeds.three", {
color: col,
2023-07-03 19:57:52 +05:00
description,
2023-06-22 19:36:52 +05:00
published: Date.now(),
2023-07-03 19:57:52 +05:00
icon,
diff,
2023-06-22 19:36:52 +05:00
});
} else {
if (!db.get("feeds.three")) {
await db.set("feeds.three", {
color: col,
2023-07-03 19:57:52 +05:00
description,
2023-06-22 19:36:52 +05:00
published: Date.now(),
2023-07-03 19:57:52 +05:00
icon,
diff,
2023-06-22 19:36:52 +05:00
});
} else if (!db.get("feeds.two")) {
const f3 = db.get("feeds.three");
await db.set("feeds.two", {
color: f3.color,
description: f3.description,
published: f3.published,
icon: f3.icon,
diff: f3.diff,
});
await db.set("feeds.three", {
color: col,
2023-07-03 19:57:52 +05:00
description,
2023-06-22 19:36:52 +05:00
published: Date.now(),
2023-07-03 19:57:52 +05:00
icon,
diff,
2023-06-22 19:36:52 +05:00
});
} else {
const f3 = db.get("feeds.three");
const f2 = db.get("feeds.two");
await db.set("feeds.one", {
color: f2.color,
description: f2.description,
published: f2.published,
icon: f2.icon,
diff: f2.diff,
});
await db.set("feeds.two", {
color: f3.color,
description: f3.description,
published: f3.published,
icon: f3.icon,
diff: f3.diff,
});
await db.set("feeds.three", {
color: col,
2023-07-03 19:57:52 +05:00
description,
2023-06-22 19:36:52 +05:00
published: Date.now(),
2023-07-03 19:57:52 +05:00
icon,
diff,
2023-06-22 19:36:52 +05:00
});
}
}
return res.redirect("/admin");
}
},
};