Browse Source

Added rough way to export your playlists

Kristian Vos 4 years ago
parent
commit
5ddb3703fc
2 changed files with 117 additions and 0 deletions
  1. 116 0
      backend/logic/api.js
  2. 1 0
      backend/logic/utils.js

+ 116 - 0
backend/logic/api.js

@@ -1,6 +1,7 @@
 const CoreClass = require("../core.js");
 
 const async = require("async");
+const config = require("config");
 const crypto = require("crypto");
 
 class APIModule extends CoreClass {
@@ -13,11 +14,112 @@ class APIModule extends CoreClass {
             this.app = this.moduleManager.modules["app"];
             this.stations = this.moduleManager.modules["stations"];
             this.db = this.moduleManager.modules["db"];
+            this.playlists = this.moduleManager.modules["playlists"];
+            this.utils = this.moduleManager.modules["utils"];
+            this.punishments = this.moduleManager.modules["punishments"];
             this.cache = this.moduleManager.modules["cache"];
             this.notifications = this.moduleManager.modules["notifications"];
 
+            const SIDname = config.get("cookie.SIDname");
+
             const actions = require("./actions");
 
+            const isLoggedIn = (req, res, next) => {
+                let SID;
+                async.waterfall(
+                    [
+                        next => {
+                            this.utils
+                                .runJob("PARSE_COOKIES", {
+                                    cookieString: req.headers.cookie,
+                                })
+                                .then(res => {
+                                    SID = res[SIDname];
+                                    next(null);
+                                }).catch(next);
+                        },
+
+                        next => {
+                            if (!SID) return next("No SID.");
+                            next();
+                        },
+
+                        (next) => {
+                            this.cache
+                                .runJob("HGET", { table: "sessions", key: SID })
+                                .then((session) => {
+                                    next(null, session);
+                                });
+                        },
+
+                        (session, next) => {
+                            if (!session) return next("No session found.");
+
+                            session.refreshDate = Date.now();
+
+                            req.session = session;
+                            this.cache
+                                .runJob("HSET", {
+                                    table: "sessions",
+                                    key: SID,
+                                    value: session,
+                                })
+                                .then((session) => {
+                                    next(null, session);
+                                });
+                        },
+
+                        (res, next) => {
+                            // check if a session's user / IP is banned
+                            this.punishments
+                                .runJob("GET_PUNISHMENTS", {})
+                                .then((punishments) => {
+                                    const isLoggedIn = !!(
+                                        req.session &&
+                                        req.session.refreshDate
+                                    );
+                                    const userId = isLoggedIn
+                                        ? req.session.userId
+                                        : null;
+
+                                    let banishment = { banned: false, ban: 0 };
+
+                                    punishments.forEach((punishment) => {
+                                        if (
+                                            punishment.expiresAt >
+                                            banishment.ban
+                                        )
+                                            banishment.ban = punishment;
+                                        if (
+                                            punishment.type === "banUserId" &&
+                                            isLoggedIn &&
+                                            punishment.value === userId
+                                        )
+                                            banishment.banned = true;
+                                        if (
+                                            punishment.type === "banUserIp" &&
+                                            punishment.value === req.ip
+                                        )
+                                            banishment.banned = true;
+                                    });
+
+                                    req.banishment = banishment;
+
+                                    next();
+                                })
+                                .catch(() => {
+                                    next();
+                                });
+                        },
+                    ],
+                    (err) => {
+                        if (err)
+                            return res.json({status: "error", message: "You are not logged in"});                        
+                        next();
+                    }
+                );
+            }
+
             this.app.runJob("GET_APP", {})
                 .then((response) => {
                     response.app.get("/", (req, res) => {
@@ -27,6 +129,20 @@ class APIModule extends CoreClass {
                         });
                     });
 
+                    response.app.get("/export/privatePlaylist/:playlistId", isLoggedIn, (req, res) => {
+                        const playlistId = req.params.playlistId;
+                        this.playlists.runJob("GET_PLAYLIST", { playlistId })
+                            .then(playlist => {
+                                if (playlist.createdBy === req.session.userId)
+                                    res.json({status: "success", playlist });
+                                else
+                                    res.json({status: "error", message: "You're not the owner."});
+                            })
+                            .catch(err => {
+                                res.json({status: "error", message: err.message});
+                            });
+                    });
+
                     // response.app.get("/debug_station", async (req, res) => {
                     //     const responseObject = {};
 

+ 1 - 0
backend/logic/utils.js

@@ -29,6 +29,7 @@ class UtilsModule extends CoreClass {
         //cookieString
         return new Promise((resolve, reject) => {
             let cookies = {};
+            if (typeof payload.cookieString !== "string") return reject("Cookie string is not a string");
             payload.cookieString.split("; ").map((cookie) => {
                 cookies[
                     cookie.substring(0, cookie.indexOf("="))