Update src/libpoketube/init/pages-account.js

This commit is contained in:
ashley 2025-09-02 00:53:01 +02:00
parent 9825676adc
commit e7d6776a1e

View File

@ -1,93 +1,33 @@
const QuickDB = require("quick.db"); const { modules } = require("../libpoketube-initsys.js");
var http = require("https");
var ping = require("ping");
const sha384 = modules.hash;
function getJson(str) {
try {
return JSON.parse(str);
} catch {
return null;
}
}
/**
* Initializes subscription-related API routes and account pages.
*
* @param {import("express").Express} app - Express application instance
* @param {object} config - Application configuration
* @param {Function} renderTemplate - Function to render templates
*/
module.exports = function (app, config, renderTemplate) { module.exports = function (app, config, renderTemplate) {
const db = QuickDB; const db = require("quick.db");
/** app.get("/api/get-channel-subs", async function (req, res) {
* Get a user's subscriptions. var userid = req.query.ID
*
* @route GET /api/get-channel-subs
* @query {string} ID - User ID
* @returns {object} JSON response with subscription data or error
*/
app.get("/api/get-channel-subs", async (req, res) => {
const userId = String(req.query.ID || "").trim();
if (!userId) return res.status(400).json({ ok: false, message: "missing ID" });
const subs = db.get(`user.${userId}.subs`); if(db.get(`user.${userid}`)) await res.json(db.get(`user.${userid}.subs`))
if (!subs) return res.status(404).json({ ok: false, message: "no user found" }); if(!db.get(`user.${userid}`)) await res.json("no user found")
res.json({ ok: true, data: subs }); });
});
/** app.get("/api/remove-channel-sub", async function (req, res) {
* Get all subscriptions for a user. const userid = req.query.ID;
* const channelToRemove = req.query.channelID;
* @route GET /api/get-all-subs
* @query {string} ID - User ID
* @returns {object} JSON response with all subscriptions
*/
app.get("/api/get-all-subs", async (req, res) => {
const userId = String(req.query.ID || "").trim();
if (!userId) return res.status(400).json({ ok: false, message: "missing ID" });
const subs = db.get(`user.${userId}.subs`) || {};
res.json({ ok: true, data: subs });
});
/**
* Add a subscription for a user.
*
* @route GET /api/set-channel-subs
* @query {string} ID - User ID
* @query {string} channelID - Channel ID
* @query {string} channelName - Channel name
* @query {string} avatar - Avatar URL
* @returns {object|Redirect} Redirects to /account-create or error JSON
*/
app.get("/api/set-channel-subs", async (req, res) => {
const userId = String(req.query.ID || "").trim();
const channelId = String(req.query.channelID || "").trim();
const channelName = String(req.query.channelName || "").trim();
const avatar = String(req.query.avatar || "").trim();
if (!userId || !channelId || !channelName || !avatar)
return res.status(400).json({ ok: false, message: "missing fields" });
if (userId.length > 7) return res.status(400).json({ ok: false, message: "IDs can be 7 characters max :3" });
if (["__proto__", "prototype", "constructor"].includes(channelId))
return res.status(400).json({ ok: false, message: "invalid channel id" });
const path = `user.${userId}.subs.${channelId}`;
if (db.get(path)) return res.json({ ok: false, message: "already subscribed" });
if (!db.get(`user.${userId}.subs`)) db.set(`user.${userId}.subs`, {});
db.set(path, { channelName, avatar });
res.redirect("/account-create");
});
/**
* Remove a subscription (or all subscriptions) for a user.
*
* @route GET /api/remove-channel-sub
* @query {string} ID - User ID
* @query {string} channelID - Channel ID or "ALL"
* @returns {object} JSON response with status and remaining subs count
*/
app.get("/api/remove-channel-sub", async (req, res) => {
const userId = String(req.query.ID || "").trim();
const channelId = String(req.query.channelID || "").trim();
if (!userId || !channelId) return res.status(400).json({ ok: false, message: "missing fields" });
if (!db.get(`user.${userId}.subs`)) return res.status(404).json({ ok: false, message: "no user or subs" });
if (channelId === "ALL") { if (channelId === "ALL") {
db.delete(`user.${userId}.subs`); db.delete(`user.${userId}.subs`);
@ -95,37 +35,75 @@ module.exports = function (app, config, renderTemplate) {
return res.json({ ok: true, message: "all subscriptions removed", remaining: 0 }); return res.json({ ok: true, message: "all subscriptions removed", remaining: 0 });
} }
if (!db.get(`user.${userId}.subs.${channelId}`)) // Check if the user has a 'subs' object in the database
return res.status(404).json({ ok: false, message: "subscription not found" }); if (db.get(`user.${userid}.subs.${channelToRemove}`)) {
// If the subscription exists, remove it from the database
db.delete(`user.${userid}.subs.${channelToRemove}`);
res.json("Subscription removed");
} else {
// If the subscription doesn't exist, send a message indicating so
res.json("Subscription not found");
}
});
db.delete(`user.${userId}.subs.${channelId}`); app.get("/api/set-channel-subs", async function (req, res) {
const remaining = Object.keys(db.get(`user.${userId}.subs`) || {}).length; var userid = req.query.ID;
res.json({ ok: true, message: "subscription removed", remaining }); var channelToSub = req.query.channelID;
}); var channelToSubName = req.query.channelName;
var avatar = req.query.avatar; // Add avatar query parameter
/** // Check if the user has a 'subs' object in the database
* Render account creation page. if (!db.get(`user.${userid}.subs`)) {
* // If not, create it and add the subscription
* @route GET /account-create db.set(`user.${userid}.subs.${channelToSub}`, {
* @returns {HTML} Renders account-create.ejs channelName: channelToSubName,
*/ avatar: avatar, // Store the avatar URL along with the subscription
app.get("/account-create", async (req, res) => { });
renderTemplate(res, req, "account-create.ejs", { db }); res.redirect("/account-create")
}); } else if (!db.get(`user.${userid}.subs.${channelToSub}`)) {
// If the user has 'subs' but not this particular subscription, add it
db.set(`user.${userid}.subs.${channelToSub}`, {
channelName: channelToSubName,
avatar: avatar, // Store the avatar URL along with the subscription
});
res.redirect("/account-create")
} else {
// If the user is already subscribed to this channel, send a message indicating so
res.json("ur already subscribed");
}
});
/**
* Render user's account page.
*
* @route GET /my-acc
* @query {string} ID - User ID
* @returns {HTML|object} Renders account-me.ejs or JSON error
*/
app.get("/my-acc", async (req, res) => {
const userId = String(req.query.ID || "").trim();
if (!userId) return res.status(400).json({ error: "missing ID" });
if (userId.length > 7) return res.status(400).json({ error: "IDs can be 7 characters max silly :3" });
const userSubs = db.get(`user.${userId}.subs`) || {};
renderTemplate(res, req, "account-me.ejs", { userid: userId, userSubs }); app.get("/account-create", async function (req, res) {
}); renderTemplate(res, req, "account-create.ejs", {db:db});
});
app.get("/api/get-all-subs", async function (req, res) {
var userid = req.query.ID;
// Check if the user has a 'subs' object in the database
const userSubs = db.get(`user.${userid}.subs`);
if (userSubs) {
res.json(userSubs); // Return all subscriptions as JSON
} else {
res.json({}); // Return an empty object if the user has no subscriptions
}
});
app.get("/my-acc", async function (req, res) {
var userid = req.query.ID;
// Check if userid is more than 7 characters
if (userid.length > 7) {
return res.status(400).json({ error: "IDs can be 7 characters max silly :3" });
}
var userSubs = db.get(`user.${userid}.subs`);
renderTemplate(res, req, "account-me.ejs", { userid, userSubs });
});
}; };