Update src/libpoketube/init/pages-account.js
This commit is contained in:
parent
e2245ba14f
commit
9825676adc
@ -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
|
* 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" });
|
||||||
|
|
||||||
if(db.get(`user.${userid}`)) await res.json(db.get(`user.${userid}.subs`))
|
const subs = db.get(`user.${userId}.subs`);
|
||||||
if(!db.get(`user.${userid}`)) await res.json("no user found")
|
if (!subs) return res.status(404).json({ ok: false, message: "no user found" });
|
||||||
|
|
||||||
});
|
res.json({ ok: true, data: subs });
|
||||||
|
});
|
||||||
|
|
||||||
app.get("/api/remove-channel-sub", async function (req, res) {
|
/**
|
||||||
const userid = req.query.ID;
|
* Get all subscriptions for a user.
|
||||||
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" });
|
||||||
|
|
||||||
// Check if the user has a 'subs' object in the database
|
const subs = db.get(`user.${userId}.subs`) || {};
|
||||||
if (db.get(`user.${userid}.subs.${channelToRemove}`)) {
|
res.json({ ok: true, data: subs });
|
||||||
// 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;
|
* Add a subscription for a user.
|
||||||
var channelToSub = req.query.channelID;
|
*
|
||||||
var channelToSubName = req.query.channelName;
|
* @route GET /api/set-channel-subs
|
||||||
var avatar = req.query.avatar; // Add avatar query parameter
|
* @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();
|
||||||
|
|
||||||
// Check if the user has a 'subs' object in the database
|
if (!userId || !channelId || !channelName || !avatar)
|
||||||
if (!db.get(`user.${userid}.subs`)) {
|
return res.status(400).json({ ok: false, message: "missing fields" });
|
||||||
// If not, create it and add the subscription
|
|
||||||
db.set(`user.${userid}.subs.${channelToSub}`, {
|
|
||||||
channelName: channelToSubName,
|
|
||||||
avatar: avatar, // Store the avatar URL along with the subscription
|
|
||||||
});
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
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" });
|
||||||
|
|
||||||
app.get("/account-create", async function (req, res) {
|
if (!db.get(`user.${userId}.subs`)) db.set(`user.${userId}.subs`, {});
|
||||||
renderTemplate(res, req, "account-create.ejs", {db:db});
|
db.set(path, { channelName, avatar });
|
||||||
|
|
||||||
});
|
res.redirect("/account-create");
|
||||||
|
});
|
||||||
|
|
||||||
app.get("/api/get-all-subs", async function (req, res) {
|
/**
|
||||||
var userid = req.query.ID;
|
* 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();
|
||||||
|
|
||||||
// Check if the user has a 'subs' object in the database
|
if (!userId || !channelId) return res.status(400).json({ ok: false, message: "missing fields" });
|
||||||
const userSubs = db.get(`user.${userid}.subs`);
|
if (!db.get(`user.${userId}.subs`)) return res.status(404).json({ ok: false, message: "no user or subs" });
|
||||||
|
|
||||||
if (userSubs) {
|
if (channelId === "ALL") {
|
||||||
res.json(userSubs); // Return all subscriptions as JSON
|
db.delete(`user.${userId}.subs`);
|
||||||
} else {
|
db.set(`user.${userId}.subs`, {});
|
||||||
res.json({}); // Return an empty object if the user has no subscriptions
|
return res.json({ ok: true, message: "all subscriptions removed", remaining: 0 });
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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`);
|
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 });
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user