From 1168511f13076652f956fee41878c71cfa578f36 Mon Sep 17 00:00:00 2001 From: Ashley Date: Sun, 8 Oct 2023 06:43:10 +0000 Subject: [PATCH] add account API --- src/libpoketube/init/pages-account.js | 113 ++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/libpoketube/init/pages-account.js diff --git a/src/libpoketube/init/pages-account.js b/src/libpoketube/init/pages-account.js new file mode 100644 index 00000000..9cb65b4e --- /dev/null +++ b/src/libpoketube/init/pages-account.js @@ -0,0 +1,113 @@ +const { + fetcher, + core, + wiki, + musicInfo, + modules, + version, + initlog, + init, +} = require("../libpoketube-initsys.js"); +const { + IsJsonString, + convert, + getFirstLine, + capitalizeFirstLetter, + turntomins, + getRandomInt, + getRandomArbitrary, +} = require("../ptutils/libpt-coreutils.js"); + +var http = require("https"); +var ping = require("ping"); + +const sha384 = modules.hash; + +function getJson(str) { + try { + return JSON.parse(str); + } catch { + return null; + } +} + +module.exports = function (app, config, renderTemplate) { +const db = require("quick.db"); + +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") + +}); + + 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 + 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`)) { + // 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.json("user subbed"); + } 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.json("user subbed"); + } else { + // If the user is already subscribed to this channel, send a message indicating so + res.json("user already subscribed"); + } +}); + + + +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 + var userSubs = db.get(`user.${userid}.subs`) + renderTemplate(res, req, "account-me.ejs", { userid, userSubs }); + +}); +};