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

This commit is contained in:
ashley 2025-09-02 00:49:27 +02:00
parent e2245ba14f
commit 9825676adc

View File

@ -1,103 +1,131 @@
const { modules } = require("../libpoketube-initsys.js"); const QuickDB = require("quick.db");
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 = require("quick.db"); const db = QuickDB;
app.get("/api/get-channel-subs", async function (req, res) {
var userid = req.query.ID
if(db.get(`user.${userid}`)) await res.json(db.get(`user.${userid}.subs`)) /**
if(!db.get(`user.${userid}`)) await res.json("no user found") * Get a user's subscriptions.
*
* @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 (!subs) return res.status(404).json({ ok: false, message: "no user found" });
app.get("/api/remove-channel-sub", async function (req, res) {
const userid = req.query.ID;
const channelToRemove = req.query.channelID;
// Check if the user has a 'subs' object in the database res.json({ ok: true, data: subs });
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");
}
});
app.get("/api/set-channel-subs", async function (req, res) {
var userid = req.query.ID;
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 /**
if (!db.get(`user.${userid}.subs`)) { * Get all subscriptions for a user.
// If not, create it and add the subscription *
db.set(`user.${userid}.subs.${channelToSub}`, { * @route GET /api/get-all-subs
channelName: channelToSubName, * @query {string} ID - User ID
avatar: avatar, // Store the avatar URL along with the subscription * @returns {object} JSON response with all subscriptions
}); */
res.redirect("/account-create") app.get("/api/get-all-subs", async (req, res) => {
} else if (!db.get(`user.${userid}.subs.${channelToSub}`)) { const userId = String(req.query.ID || "").trim();
// If the user has 'subs' but not this particular subscription, add it if (!userId) return res.status(400).json({ ok: false, message: "missing ID" });
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");
}
});
const subs = db.get(`user.${userId}.subs`) || {};
res.json({ ok: true, data: subs });
});
/**
app.get("/account-create", async function (req, res) { * Add a subscription for a user.
renderTemplate(res, req, "account-create.ejs", {db:db}); *
* @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" });
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 if (userId.length > 7) return res.status(400).json({ ok: false, message: "IDs can be 7 characters max :3" });
const userSubs = db.get(`user.${userid}.subs`); if (["__proto__", "prototype", "constructor"].includes(channelId))
return res.status(400).json({ ok: false, message: "invalid channel id" });
if (userSubs) { const path = `user.${userId}.subs.${channelId}`;
res.json(userSubs); // Return all subscriptions as JSON if (db.get(path)) return res.json({ ok: false, message: "already subscribed" });
} else {
res.json({}); // Return an empty object if the user has no subscriptions
}
});
app.get("/my-acc", async function (req, res) { if (!db.get(`user.${userId}.subs`)) db.set(`user.${userId}.subs`, {});
var userid = req.query.ID; db.set(path, { channelName, avatar });
// Check if userid is more than 7 characters res.redirect("/account-create");
if (userid.length > 7) { });
return res.status(400).json({ error: "IDs can be 7 characters max silly :3" });
/**
* 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") {
db.delete(`user.${userId}.subs`);
db.set(`user.${userId}.subs`, {});
return res.json({ ok: true, message: "all subscriptions removed", remaining: 0 });
} }
var userSubs = db.get(`user.${userid}.subs`); if (!db.get(`user.${userId}.subs.${channelId}`))
return res.status(404).json({ ok: false, message: "subscription not found" });
renderTemplate(res, req, "account-me.ejs", { userid, userSubs }); db.delete(`user.${userId}.subs.${channelId}`);
}); const remaining = Object.keys(db.get(`user.${userId}.subs`) || {}).length;
res.json({ ok: true, message: "subscription removed", remaining });
});
/**
* Render account creation page.
*
* @route GET /account-create
* @returns {HTML} Renders account-create.ejs
*/
app.get("/account-create", async (req, res) => {
renderTemplate(res, req, "account-create.ejs", { db });
});
/**
* 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 });
});
}; };