mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 11:04:07 +00:00
added base graph logic for cycle detection
This commit is contained in:
parent
971698b318
commit
9bc0cb651d
@ -1,3 +1,9 @@
|
|||||||
# spotify-manager
|
# spotify-manager
|
||||||
|
|
||||||
Personal Spotify playlist manager. Features inbound!
|
Personal Spotify playlist manager. Features inbound!
|
||||||
|
|
||||||
|
## notes
|
||||||
|
|
||||||
|
- graphing
|
||||||
|
- stores links as from-to pairs
|
||||||
|
- fetches all playlists and links of the user into memory and then works with data. assumption is graphs won't be too big
|
||||||
|
|||||||
@ -2,6 +2,7 @@ const typedefs = require("../typedefs");
|
|||||||
const logger = require("../utils/logger")(module);
|
const logger = require("../utils/logger")(module);
|
||||||
|
|
||||||
const { axiosInstance } = require("../utils/axios");
|
const { axiosInstance } = require("../utils/axios");
|
||||||
|
const myGraph = require("../utils/graph");
|
||||||
const { parseSpotifyUri, parseSpotifyLink } = require("../utils/spotifyUriTransformer");
|
const { parseSpotifyUri, parseSpotifyLink } = require("../utils/spotifyUriTransformer");
|
||||||
|
|
||||||
|
|
||||||
@ -93,7 +94,6 @@ const updateUser = async (req, res) => {
|
|||||||
toRemove = [];
|
toRemove = [];
|
||||||
}
|
}
|
||||||
let toRemoveIDs = toRemove.map(pl => pl.playlistID);
|
let toRemoveIDs = toRemove.map(pl => pl.playlistID);
|
||||||
logger.debug("removeIDs", { toRemoveIDs });
|
|
||||||
let removedLinks = 0;
|
let removedLinks = 0;
|
||||||
|
|
||||||
if (toRemove.length) {
|
if (toRemove.length) {
|
||||||
@ -138,7 +138,7 @@ const updateUser = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch user's stored playlists
|
* Fetch user's stored playlists and links
|
||||||
* @param {typedefs.Req} req
|
* @param {typedefs.Req} req
|
||||||
* @param {typedefs.Res} res
|
* @param {typedefs.Res} res
|
||||||
*/
|
*/
|
||||||
@ -146,7 +146,7 @@ const fetchUser = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const userURI = parseSpotifyUri(req.session.user.uri);
|
const userURI = parseSpotifyUri(req.session.user.uri);
|
||||||
|
|
||||||
let currentPlaylists = await Playlists.findAll({
|
const currentPlaylists = await Playlists.findAll({
|
||||||
attributes: ["playlistID", "playlistName"],
|
attributes: ["playlistID", "playlistName"],
|
||||||
raw: true,
|
raw: true,
|
||||||
where: {
|
where: {
|
||||||
@ -154,7 +154,18 @@ const fetchUser = async (req, res) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).send(currentPlaylists);
|
const currentLinks = await Links.findAll({
|
||||||
|
attributes: ["from", "to"],
|
||||||
|
raw: true,
|
||||||
|
where: {
|
||||||
|
userID: userURI.id
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).send({
|
||||||
|
playlists: currentPlaylists,
|
||||||
|
links: currentLinks
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('fetchUser', { error });
|
logger.error('fetchUser', { error });
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
@ -175,19 +186,17 @@ const createLink = async (req, res) => {
|
|||||||
fromPl = parseSpotifyLink(req.body["from"]);
|
fromPl = parseSpotifyLink(req.body["from"]);
|
||||||
toPl = parseSpotifyLink(req.body["to"]);
|
toPl = parseSpotifyLink(req.body["to"]);
|
||||||
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
||||||
return res.sendStatus(400);
|
return res.status(400).send({ message: "Invalid Spotify playlist link" });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("parseSpotifyLink", { error });
|
logger.error("parseSpotifyLink", { error });
|
||||||
return res.sendStatus(400);
|
return res.status(400).send({ message: "Invalid Spotify playlist link" });
|
||||||
}
|
}
|
||||||
|
|
||||||
let playlists = await Playlists.findAll({
|
let playlists = await Playlists.findAll({
|
||||||
attributes: ["playlistID"],
|
attributes: ["playlistID"],
|
||||||
raw: true,
|
raw: true,
|
||||||
where: {
|
where: { userID: userURI.id }
|
||||||
userID: userURI.id
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
playlists = playlists.map(pl => pl.playlistID);
|
playlists = playlists.map(pl => pl.playlistID);
|
||||||
|
|
||||||
@ -212,6 +221,19 @@ const createLink = async (req, res) => {
|
|||||||
return res.sendStatus(409);
|
return res.sendStatus(409);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const allLinks = await Links.findAll({
|
||||||
|
attributes: ["from", "to"],
|
||||||
|
raw: true,
|
||||||
|
where: { userID: userURI.id }
|
||||||
|
});
|
||||||
|
|
||||||
|
const newGraph = new myGraph(playlists, [...allLinks, { from: fromPl.id, to: toPl.id }]);
|
||||||
|
|
||||||
|
if (newGraph.detectCycle()) {
|
||||||
|
logger.error("potential cycle detected");
|
||||||
|
return res.status(400).send({ message: "Proposed link cannot cause a cycle in the graph" });
|
||||||
|
}
|
||||||
|
|
||||||
const newLink = await Links.create({
|
const newLink = await Links.create({
|
||||||
userID: userURI.id,
|
userID: userURI.id,
|
||||||
from: fromPl.id,
|
from: fromPl.id,
|
||||||
@ -244,11 +266,11 @@ const removeLink = async (req, res) => {
|
|||||||
fromPl = parseSpotifyLink(req.body["from"]);
|
fromPl = parseSpotifyLink(req.body["from"]);
|
||||||
toPl = parseSpotifyLink(req.body["to"]);
|
toPl = parseSpotifyLink(req.body["to"]);
|
||||||
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
||||||
return res.sendStatus(400);
|
return res.status(400).send({ message: "Invalid Spotify playlist link" });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("parseSpotifyLink", { error });
|
logger.error("parseSpotifyLink", { error });
|
||||||
return res.sendStatus(400);
|
return res.status(400).send({ message: "Invalid Spotify playlist link" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if exists
|
// check if exists
|
||||||
|
|||||||
@ -98,11 +98,11 @@ const getPlaylistDetails = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
uri = parseSpotifyLink(req.query.playlist_link)
|
uri = parseSpotifyLink(req.query.playlist_link)
|
||||||
if (uri.type !== "playlist") {
|
if (uri.type !== "playlist") {
|
||||||
return res.status(400).send("Not a playlist link");
|
return res.status(400).send({ message: "Invalid Spotify playlist link" });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("parseSpotifyLink", { error });
|
logger.error("parseSpotifyLink", { error });
|
||||||
return res.sendStatus(400);
|
return res.status(400).send({ message: "Invalid Spotify playlist link" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axiosInstance.get(
|
const response = await axiosInstance.get(
|
||||||
|
|||||||
@ -23,7 +23,7 @@ router.post(
|
|||||||
createLinkValidator,
|
createLinkValidator,
|
||||||
validate,
|
validate,
|
||||||
createLink
|
createLink
|
||||||
)
|
);
|
||||||
|
|
||||||
router.delete(
|
router.delete(
|
||||||
"/link",
|
"/link",
|
||||||
@ -31,5 +31,6 @@ router.delete(
|
|||||||
removeLinkValidator,
|
removeLinkValidator,
|
||||||
validate,
|
validate,
|
||||||
removeLink
|
removeLink
|
||||||
)
|
);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
79
utils/graph.js
Normal file
79
utils/graph.js
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
const logger = require("./logger")(module);
|
||||||
|
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directed graph, may or may not be connected.
|
||||||
|
*
|
||||||
|
* NOTE: Assumes that nodes and edges are valid.
|
||||||
|
*/
|
||||||
|
class myGraph {
|
||||||
|
/**
|
||||||
|
* @param {string[]} nodes Graph nodes IDs
|
||||||
|
* @param {{ from: string, to: string }[]} edges Graph edges b/w nodes
|
||||||
|
*/
|
||||||
|
constructor(nodes, edges) {
|
||||||
|
this.nodes = [...nodes];
|
||||||
|
this.edges = structuredClone(edges);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {type} node
|
||||||
|
* @returns {string[]}
|
||||||
|
*/
|
||||||
|
getNeighbors(node) {
|
||||||
|
return this.edges.filter(edge => edge.from == node).map(edge => edge.to);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kahn's topological sort
|
||||||
|
* @returns {string[]}
|
||||||
|
*/
|
||||||
|
topoSort() {
|
||||||
|
let inDegree = {};
|
||||||
|
let zeroInDegreeQueue = [];
|
||||||
|
let topologicalOrder = [];
|
||||||
|
|
||||||
|
// Initialize inDegree of all nodes to 0
|
||||||
|
for (let node of this.nodes) {
|
||||||
|
inDegree[node] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate inDegree of each node
|
||||||
|
for (let edge of this.edges) {
|
||||||
|
inDegree[edge.to]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect nodes with 0 inDegree
|
||||||
|
for (let node of this.nodes) {
|
||||||
|
if (inDegree[node] === 0) {
|
||||||
|
zeroInDegreeQueue.push(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// process nodes with 0 inDegree
|
||||||
|
while (zeroInDegreeQueue.length > 0) {
|
||||||
|
let node = zeroInDegreeQueue.shift();
|
||||||
|
topologicalOrder.push(node);
|
||||||
|
|
||||||
|
for (let neighbor of this.getNeighbors(node)) {
|
||||||
|
inDegree[neighbor]--;
|
||||||
|
if (inDegree[neighbor] === 0) {
|
||||||
|
zeroInDegreeQueue.push(neighbor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return topologicalOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the graph contains a cycle
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
detectCycle() {
|
||||||
|
// If topological order includes all nodes, no cycle exists
|
||||||
|
return this.topoSort().length < this.nodes.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = myGraph;
|
||||||
Loading…
x
Reference in New Issue
Block a user