mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 12:54:07 +00:00
abstracting link operations for propagation
This commit is contained in:
parent
317c03b1a2
commit
d6070b4038
@ -389,45 +389,12 @@ const removeLink = async (req, res) => {
|
|||||||
*
|
*
|
||||||
* @param {typedefs.Req} req
|
* @param {typedefs.Req} req
|
||||||
* @param {typedefs.Res} res
|
* @param {typedefs.Res} res
|
||||||
|
* @param {{from: typedefs.URIObject, to: typedefs.URIObject}} link
|
||||||
|
* @returns {Promise<{toAddNum: number, localNum: number} | undefined>}
|
||||||
*/
|
*/
|
||||||
const populateSingleLink = async (req, res) => {
|
const _populateSingleLinkCore = async (req, res, link) => {
|
||||||
try {
|
try {
|
||||||
let fromPl, toPl;
|
const fromPl = link.from, toPl = link.to;
|
||||||
const link = { from: req.body.from, to: req.body.to };
|
|
||||||
const uID = req.session.user.id;
|
|
||||||
|
|
||||||
try {
|
|
||||||
fromPl = parseSpotifyLink(link.from);
|
|
||||||
toPl = parseSpotifyLink(link.to);
|
|
||||||
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
|
||||||
res.status(400).send({ message: "Link is not a playlist" });
|
|
||||||
logger.info("non-playlist link provided", link);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
res.status(400).send({ message: "Could not parse link" });
|
|
||||||
logger.warn("parseSpotifyLink", { error });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if exists
|
|
||||||
const existingLink = await Links.findOne({
|
|
||||||
where: {
|
|
||||||
[Op.and]: [
|
|
||||||
{ userID: uID },
|
|
||||||
{ from: fromPl.id },
|
|
||||||
{ to: toPl.id }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (!existingLink) {
|
|
||||||
res.status(409).send({ message: "Link does not exist!" });
|
|
||||||
logger.warn("link does not exist", { link });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!await checkPlaylistEditable(req, res, fromPl.id, uID))
|
|
||||||
return;
|
|
||||||
|
|
||||||
let initialFields = ["tracks(next,items(is_local,track(uri)))"];
|
let initialFields = ["tracks(next,items(is_local,track(uri)))"];
|
||||||
let mainFields = ["next", "items(is_local,track(uri))"];
|
let mainFields = ["next", "items(is_local,track(uri))"];
|
||||||
@ -449,7 +416,6 @@ const populateSingleLink = async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// keep getting batches of 50 till exhausted
|
// keep getting batches of 50 till exhausted
|
||||||
while (fromPlaylist.next) {
|
while (fromPlaylist.next) {
|
||||||
const nextData = await getPlaylistDetailsNextPage(req, res, fromPlaylist.next);
|
const nextData = await getPlaylistDetailsNextPage(req, res, fromPlaylist.next);
|
||||||
@ -521,13 +487,67 @@ const populateSingleLink = async (req, res) => {
|
|||||||
if (res.headersSent) return;
|
if (res.headersSent) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { toAddNum, localNum };
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).send({ message: "Internal Server Error" });
|
||||||
|
logger.error("_populateSingleLinkCore", { error });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {typedefs.Req} req
|
||||||
|
* @param {typedefs.Res} res
|
||||||
|
*/
|
||||||
|
const populateSingleLink = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const uID = req.session.user.id;
|
||||||
|
const link = { from: req.body.from, to: req.body.to };
|
||||||
|
let fromPl, toPl;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fromPl = parseSpotifyLink(link.from);
|
||||||
|
toPl = parseSpotifyLink(link.to);
|
||||||
|
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
||||||
|
res.status(400).send({ message: "Link is not a playlist" });
|
||||||
|
logger.info("non-playlist link provided", link);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send({ message: "Could not parse link" });
|
||||||
|
logger.warn("parseSpotifyLink", { error });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if exists
|
||||||
|
const existingLink = await Links.findOne({
|
||||||
|
where: {
|
||||||
|
[Op.and]: [
|
||||||
|
{ userID: uID },
|
||||||
|
{ from: fromPl.id },
|
||||||
|
{ to: toPl.id }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!existingLink) {
|
||||||
|
res.status(409).send({ message: "Link does not exist!" });
|
||||||
|
logger.warn("link does not exist", { link });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await checkPlaylistEditable(req, res, fromPl.id, uID))
|
||||||
|
return;
|
||||||
|
|
||||||
|
const result = await _populateSingleLinkCore(req, res, { from: fromPl, to: toPl });
|
||||||
|
if (result) {
|
||||||
let logMsg;
|
let logMsg;
|
||||||
logMsg = toAddNum > 0 ? "Added " + toAddNum + " tracks" : "No tracks to add";
|
logMsg = result.toAddNum > 0 ? "Added " + result.toAddNum + " tracks" : "No tracks to add";
|
||||||
logMsg += localNum > 0 ? ", but could not add " + localNum + " local files" : ".";
|
logMsg += result.localNum > 0 ? ", but could not add " + result.localNum + " local files" : ".";
|
||||||
|
|
||||||
res.status(200).send({ message: logMsg });
|
res.status(200).send({ message: logMsg });
|
||||||
logger.debug(logMsg);
|
logger.debug(logMsg);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).send({ message: "Internal Server Error" });
|
res.status(500).send({ message: "Internal Server Error" });
|
||||||
logger.error("populateSingleLink", { error });
|
logger.error("populateSingleLink", { error });
|
||||||
@ -705,5 +725,6 @@ module.exports = {
|
|||||||
createLink,
|
createLink,
|
||||||
removeLink,
|
removeLink,
|
||||||
populateSingleLink,
|
populateSingleLink,
|
||||||
|
populateLinksPropagate,
|
||||||
pruneSingleLink,
|
pruneSingleLink,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user