mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
MASSIVE commit
- moved to typescript - axios rate limitmodule is busted, removed for now, do something else for that - sequelize-typescript - dotenv, not dotenv-flow - removed playlist details route types for API ton of minor fixes and improvements
This commit is contained in:
@@ -1,48 +1,51 @@
|
||||
import { authInstance } from "../api/axios.js";
|
||||
import { authInstance } from "../api/axios.ts";
|
||||
import { getCurrentUsersProfile } from "../api/spotify.ts";
|
||||
|
||||
import * as typedefs from "../typedefs.js";
|
||||
import { scopes, stateKey, accountsAPIURL, sessionName } from "../constants.js";
|
||||
import {
|
||||
requiredScopes,
|
||||
stateKey,
|
||||
accountsAPIURL,
|
||||
sessionName,
|
||||
} from "../constants.ts";
|
||||
import type { RequestHandler } from "express";
|
||||
|
||||
import generateRandString from "../utils/generateRandString.js";
|
||||
import { getUserProfile } from "../api/spotify.js";
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
import { generateRandString } from "../utils/generateRandString.ts";
|
||||
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
/**
|
||||
* Stateful redirect to Spotify login with credentials
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const login = (_req, res) => {
|
||||
const login: RequestHandler = async (_req, res) => {
|
||||
try {
|
||||
const state = generateRandString(16);
|
||||
res.cookie(stateKey, state);
|
||||
|
||||
const scope = Object.values(scopes).join(" ");
|
||||
const scope = Object.values(requiredScopes).join(" ");
|
||||
|
||||
res.redirect(
|
||||
`${accountsAPIURL}/authorize?` +
|
||||
new URLSearchParams({
|
||||
response_type: "code",
|
||||
client_id: process.env.CLIENT_ID,
|
||||
scope: scope,
|
||||
redirect_uri: process.env.REDIRECT_URI,
|
||||
state: state
|
||||
}).toString()
|
||||
new URLSearchParams({
|
||||
response_type: "code",
|
||||
client_id: process.env["CLIENT_ID"],
|
||||
scope: scope,
|
||||
redirect_uri: process.env["REDIRECT_URI"],
|
||||
state: state,
|
||||
} as Record<string, string>).toString()
|
||||
);
|
||||
return;
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("login", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Exchange authorization code for refresh and access tokens
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const callback = async (req, res) => {
|
||||
const callback: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const { code, state, error } = req.query;
|
||||
const storedState = req.cookies ? req.cookies[stateKey] : null;
|
||||
@@ -51,22 +54,22 @@ export const callback = async (req, res) => {
|
||||
if (state === null || state !== storedState) {
|
||||
res.status(409).send({ message: "Invalid state" });
|
||||
logger.warn("state mismatch");
|
||||
return;
|
||||
return null;
|
||||
} else if (error) {
|
||||
res.status(401).send({ message: "Auth callback error" });
|
||||
logger.error("callback error", { error });
|
||||
return;
|
||||
return null;
|
||||
} else {
|
||||
// get auth tokens
|
||||
res.clearCookie(stateKey);
|
||||
|
||||
const authForm = {
|
||||
code: code,
|
||||
redirect_uri: process.env.REDIRECT_URI,
|
||||
grant_type: "authorization_code"
|
||||
}
|
||||
redirect_uri: process.env["REDIRECT_URI"],
|
||||
grant_type: "authorization_code",
|
||||
} as Record<string, string>;
|
||||
|
||||
const authPayload = (new URLSearchParams(authForm)).toString();
|
||||
const authPayload = new URLSearchParams(authForm).toString();
|
||||
|
||||
const tokenResponse = await authInstance.post("/api/token", authPayload);
|
||||
|
||||
@@ -76,88 +79,96 @@ export const callback = async (req, res) => {
|
||||
req.session.refreshToken = tokenResponse.data.refresh_token;
|
||||
} else {
|
||||
logger.error("login failed", { statusCode: tokenResponse.status });
|
||||
res.status(tokenResponse.status).send({ message: "Error: Login failed" });
|
||||
res
|
||||
.status(tokenResponse.status)
|
||||
.send({ message: "Error: Login failed" });
|
||||
}
|
||||
|
||||
const userData = await getUserProfile(req, res);
|
||||
if (res.headersSent) return;
|
||||
const userData = await getCurrentUsersProfile({ req, res });
|
||||
if (!userData) return null;
|
||||
|
||||
/** @type {typedefs.User} */
|
||||
req.session.user = {
|
||||
username: userData.display_name,
|
||||
username: userData.display_name ?? "",
|
||||
id: userData.id,
|
||||
};
|
||||
|
||||
// res.status(200).send({ message: "OK" });
|
||||
res.redirect(process.env.APP_URI + "?login=success");
|
||||
res.redirect(process.env["APP_URI"] + "?login=success");
|
||||
logger.debug("New login.", { username: userData.display_name });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("callback", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Request new access token using refresh token
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const refresh = async (req, res) => {
|
||||
const refresh: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const authForm = {
|
||||
refresh_token: req.session.refreshToken,
|
||||
refresh_token: req.session.refreshToken ?? "",
|
||||
grant_type: "refresh_token",
|
||||
}
|
||||
};
|
||||
|
||||
const authPayload = (new URLSearchParams(authForm)).toString();
|
||||
const authPayload = new URLSearchParams(authForm).toString();
|
||||
|
||||
// TODO: types for this and other auth endpoints... but is it necessary?
|
||||
const response = await authInstance.post("/api/token", authPayload);
|
||||
|
||||
if (response.status === 200) {
|
||||
req.session.accessToken = response.data.access_token;
|
||||
req.session.refreshToken = response.data.refresh_token ?? req.session.refreshToken; // refresh token rotation
|
||||
req.session.refreshToken =
|
||||
response.data.refresh_token ?? req.session.refreshToken; // refresh token rotation
|
||||
|
||||
res.status(200).send({ message: "OK" });
|
||||
logger.debug(`Access token refreshed${(response.data.refresh_token !== null) ? " and refresh token updated" : ""}.`);
|
||||
return;
|
||||
logger.debug(
|
||||
`Access token refreshed${
|
||||
response.data.refresh_token !== null
|
||||
? " and refresh token updated"
|
||||
: ""
|
||||
}.`
|
||||
);
|
||||
return null;
|
||||
} else {
|
||||
res.status(response.status).send({ message: "Error: Refresh token flow failed." });
|
||||
res
|
||||
.status(response.status)
|
||||
.send({ message: "Error: Refresh token flow failed." });
|
||||
logger.error("refresh failed", { statusCode: response.status });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("refresh", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Clear session
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const logout = async (req, res) => {
|
||||
const logout: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
const delSession = req.session.destroy((error) => {
|
||||
if (Object.keys(error).length) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Error while logging out", { error });
|
||||
return;
|
||||
} else {
|
||||
res.clearCookie(sessionName);
|
||||
// res.status(200).send({ message: "OK" });
|
||||
res.redirect(process.env.APP_URI + "?logout=success");
|
||||
res.redirect(process.env["APP_URI"] + "?logout=success");
|
||||
logger.debug("Logged out.", { sessionID: delSession.id });
|
||||
return;
|
||||
}
|
||||
})
|
||||
});
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("logout", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export { login, callback, refresh, logout };
|
||||
@@ -1,52 +1,73 @@
|
||||
import * as typedefs from "../typedefs.js";
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
|
||||
import { getUserPlaylistsFirstPage, getUserPlaylistsNextPage, getPlaylistDetailsFirstPage, getPlaylistDetailsNextPage, addItemsToPlaylist, removeItemsFromPlaylist, checkPlaylistEditable } from "../api/spotify.js";
|
||||
|
||||
import { parseSpotifyLink } from "../utils/spotifyURITransformer.js";
|
||||
import { randomBool, sleep } from "../utils/flake.js";
|
||||
import myGraph from "../utils/graph.js";
|
||||
|
||||
import { Op } from "sequelize";
|
||||
|
||||
import models, { sequelize } from "../models/index.js";
|
||||
const Playlists = models.playlists;
|
||||
const Links = models.links;
|
||||
import {
|
||||
getCurrentUsersPlaylistsFirstPage,
|
||||
getCurrentUsersPlaylistsNextPage,
|
||||
getPlaylistDetailsFirstPage,
|
||||
getPlaylistDetailsNextPage,
|
||||
addItemsToPlaylist,
|
||||
removePlaylistItems,
|
||||
checkPlaylistEditable,
|
||||
} from "../api/spotify.ts";
|
||||
|
||||
import type { RequestHandler } from "express";
|
||||
import type {
|
||||
EndpointHandlerBaseArgs,
|
||||
LinkModel_Edge,
|
||||
PlaylistModel_Pl,
|
||||
URIObject,
|
||||
} from "spotify_manager/index.d.ts";
|
||||
|
||||
import seqConn from "../models/index.ts";
|
||||
|
||||
import myGraph from "../utils/graph.ts";
|
||||
import { parseSpotifyLink } from "../utils/spotifyUriTransformer.ts";
|
||||
// import { randomBool, sleep } from "../utils/flake.ts";
|
||||
|
||||
// load db models
|
||||
import Playlists from "../models/playlists.ts";
|
||||
import Links from "../models/links.ts";
|
||||
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
/**
|
||||
* Sync user's Spotify data
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const updateUser = async (req, res) => {
|
||||
const updateUser: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
let currentPlaylists = [];
|
||||
let currentPlaylists: PlaylistModel_Pl[] = [];
|
||||
if (!req.session.user)
|
||||
throw new ReferenceError("sessionData does not have user object");
|
||||
const uID = req.session.user.id;
|
||||
|
||||
// get first 50
|
||||
const respData = await getUserPlaylistsFirstPage(req, res);
|
||||
if (res.headersSent) return;
|
||||
const respData = await getCurrentUsersPlaylistsFirstPage({ req, res });
|
||||
if (!respData) return null;
|
||||
|
||||
currentPlaylists = respData.items.map(playlist => {
|
||||
currentPlaylists = respData.items.map((playlist) => {
|
||||
return {
|
||||
playlistID: playlist.id,
|
||||
playlistName: playlist.name
|
||||
}
|
||||
playlistName: playlist.name,
|
||||
};
|
||||
});
|
||||
let nextURL = respData.next;
|
||||
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (nextURL) {
|
||||
const nextData = await getUserPlaylistsNextPage(req, res, nextURL);
|
||||
if (res.headersSent) return;
|
||||
const nextData = await getCurrentUsersPlaylistsNextPage({
|
||||
req,
|
||||
res,
|
||||
nextURL,
|
||||
});
|
||||
if (!nextData) return null;
|
||||
|
||||
currentPlaylists.push(
|
||||
...nextData.items.map(playlist => {
|
||||
...nextData.items.map((playlist) => {
|
||||
return {
|
||||
playlistID: playlist.id,
|
||||
playlistName: playlist.name
|
||||
}
|
||||
playlistName: playlist.name,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
@@ -57,17 +78,20 @@ export const updateUser = async (req, res) => {
|
||||
attributes: ["playlistID", "playlistName"],
|
||||
raw: true,
|
||||
where: {
|
||||
userID: uID
|
||||
userID: uID,
|
||||
},
|
||||
});
|
||||
|
||||
const deleted = [];
|
||||
const added = [];
|
||||
const renamed = [];
|
||||
const deleted: PlaylistModel_Pl[] = [];
|
||||
const added: PlaylistModel_Pl[] = [];
|
||||
const renamed: { playlistID: string; oldName: string; newName: string }[] =
|
||||
[];
|
||||
|
||||
if (oldPlaylists.length) {
|
||||
const oldMap = new Map(oldPlaylists.map((p) => [p.playlistID, p]));
|
||||
const currentMap = new Map(currentPlaylists.map((p) => [p.playlistID, p]));
|
||||
const currentMap = new Map(
|
||||
currentPlaylists.map((p) => [p.playlistID, p])
|
||||
);
|
||||
|
||||
// Check for added and renamed playlists
|
||||
currentPlaylists.forEach((pl) => {
|
||||
@@ -96,9 +120,12 @@ export const updateUser = async (req, res) => {
|
||||
added.push(...currentPlaylists);
|
||||
}
|
||||
|
||||
let removedLinks = 0, delNum = 0, updateNum = 0, addPls = [];
|
||||
let removedLinks = 0,
|
||||
delNum = 0,
|
||||
updateNum = 0,
|
||||
addPls = [];
|
||||
|
||||
const deletedIDs = deleted.map(pl => pl.playlistID);
|
||||
const deletedIDs = deleted.map((pl) => pl.playlistID);
|
||||
if (deleted.length) {
|
||||
// clean up any links dependent on the playlists
|
||||
removedLinks = await Links.destroy({
|
||||
@@ -109,87 +136,94 @@ export const updateUser = async (req, res) => {
|
||||
[Op.or]: [
|
||||
{ from: { [Op.in]: deletedIDs } },
|
||||
{ to: { [Op.in]: deletedIDs } },
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
// only then remove
|
||||
delNum = await Playlists.destroy({
|
||||
where: { playlistID: deletedIDs, userID: uID }
|
||||
where: { playlistID: deletedIDs, userID: uID },
|
||||
});
|
||||
if (delNum !== deleted.length) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Could not remove all old playlists", { error: new Error("Playlists.destroy failed?") });
|
||||
return;
|
||||
logger.error("Could not remove all old playlists", {
|
||||
error: new Error("Playlists.destroy failed?"),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (added.length) {
|
||||
addPls = await Playlists.bulkCreate(
|
||||
added.map(pl => { return { ...pl, userID: uID } }),
|
||||
added.map((pl) => {
|
||||
return { ...pl, userID: uID };
|
||||
}),
|
||||
{ validate: true }
|
||||
);
|
||||
if (addPls.length !== added.length) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Could not add all new playlists", { error: new Error("Playlists.bulkCreate failed?") });
|
||||
return;
|
||||
logger.error("Could not add all new playlists", {
|
||||
error: new Error("Playlists.bulkCreate failed?"),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const transaction = await sequelize.transaction();
|
||||
try {
|
||||
for (const { playlistID, newName } of renamed) {
|
||||
const updateRes = await Playlists.update(
|
||||
{ playlistName: newName },
|
||||
{ where: { playlistID, userID: uID } },
|
||||
{ transaction }
|
||||
);
|
||||
updateNum += Number(updateRes[0]);
|
||||
}
|
||||
|
||||
await transaction.commit();
|
||||
await seqConn.transaction(async (transaction) => {
|
||||
for (const { playlistID, newName } of renamed) {
|
||||
const updateRes = await Playlists.update(
|
||||
{ playlistName: newName },
|
||||
{ where: { playlistID, userID: uID }, transaction }
|
||||
);
|
||||
updateNum += Number(updateRes[0]);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Could not update playlist names", { error: new Error("Playlists.update failed?") });
|
||||
return;
|
||||
logger.error("Could not update playlist names", {
|
||||
error: new Error("Playlists.update failed?"),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
res.status(200).send({ message: "Updated user data.", removedLinks: removedLinks > 0 });
|
||||
res
|
||||
.status(200)
|
||||
.send({ message: "Updated user data.", removedLinks: removedLinks > 0 });
|
||||
logger.debug("Updated user data", {
|
||||
delLinks: removedLinks,
|
||||
delPls: delNum,
|
||||
addPls: addPls.length,
|
||||
updatedPls: updateNum
|
||||
updatedPls: updateNum,
|
||||
});
|
||||
return;
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("updateUser", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetch user's stored playlists and links
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const fetchUser = async (req, res) => {
|
||||
const fetchUser: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
// if (randomBool(0.5)) {
|
||||
// res.status(404).send({ message: "Not Found" });
|
||||
// return;
|
||||
// return null;
|
||||
// }
|
||||
if (!req.session.user)
|
||||
throw new ReferenceError("sessionData does not have user object");
|
||||
const uID = req.session.user.id;
|
||||
|
||||
const currentPlaylists = await Playlists.findAll({
|
||||
attributes: ["playlistID", "playlistName"],
|
||||
raw: true,
|
||||
where: {
|
||||
userID: uID
|
||||
userID: uID,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -197,31 +231,34 @@ export const fetchUser = async (req, res) => {
|
||||
attributes: ["from", "to"],
|
||||
raw: true,
|
||||
where: {
|
||||
userID: uID
|
||||
userID: uID,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).send({
|
||||
playlists: currentPlaylists,
|
||||
links: currentLinks
|
||||
links: currentLinks,
|
||||
});
|
||||
logger.debug("Fetched user data", { pls: currentPlaylists.length, links: currentLinks.length });
|
||||
return;
|
||||
logger.debug("Fetched user data", {
|
||||
pls: currentPlaylists.length,
|
||||
links: currentLinks.length,
|
||||
});
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("fetchUser", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Create link between playlists!
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const createLink = async (req, res) => {
|
||||
const createLink: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
// await sleep(1000);
|
||||
if (!req.session.user)
|
||||
throw new ReferenceError("sessionData does not have user object");
|
||||
const uID = req.session.user.id;
|
||||
|
||||
let fromPl, toPl;
|
||||
@@ -231,87 +268,89 @@ export const createLink = async (req, res) => {
|
||||
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
||||
res.status(400).send({ message: "Link is not a playlist" });
|
||||
logger.info("non-playlist link provided", { from: fromPl, to: toPl });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send({ message: "Could not parse link" });
|
||||
logger.warn("parseSpotifyLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
let playlists = await Playlists.findAll({
|
||||
const playlists = (await Playlists.findAll({
|
||||
attributes: ["playlistID"],
|
||||
raw: true,
|
||||
where: { userID: uID }
|
||||
});
|
||||
playlists = playlists.map(pl => pl.playlistID);
|
||||
where: { userID: uID },
|
||||
})) as unknown as PlaylistModel_Pl[];
|
||||
const playlistIDs = playlists.map((pl) => pl.playlistID);
|
||||
|
||||
// if playlists are unknown
|
||||
if (![fromPl, toPl].every(pl => playlists.includes(pl.id))) {
|
||||
if (![fromPl, toPl].every((pl) => playlistIDs.includes(pl.id))) {
|
||||
res.status(404).send({ message: "Playlists out of sync." });
|
||||
logger.warn("unknown playlists, resync");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
// check if exists
|
||||
const existingLink = await Links.findOne({
|
||||
where: {
|
||||
[Op.and]: [
|
||||
{ userID: uID },
|
||||
{ from: fromPl.id },
|
||||
{ to: toPl.id }
|
||||
]
|
||||
}
|
||||
[Op.and]: [{ userID: uID }, { from: fromPl.id }, { to: toPl.id }],
|
||||
},
|
||||
});
|
||||
if (existingLink) {
|
||||
res.status(409).send({ message: "Link already exists!" });
|
||||
logger.info("link already exists");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
const allLinks = await Links.findAll({
|
||||
const allLinks = (await Links.findAll({
|
||||
attributes: ["from", "to"],
|
||||
raw: true,
|
||||
where: { userID: uID }
|
||||
});
|
||||
where: { userID: uID },
|
||||
})) as unknown as LinkModel_Edge[];
|
||||
|
||||
const newGraph = new myGraph(playlists, [...allLinks, { from: fromPl.id, to: toPl.id }]);
|
||||
const newGraph = new myGraph(playlistIDs, [
|
||||
...allLinks,
|
||||
{ from: fromPl.id, to: toPl.id },
|
||||
]);
|
||||
|
||||
if (newGraph.detectCycle()) {
|
||||
res.status(400).send({ message: "Proposed link cannot cause a cycle in the graph" });
|
||||
res
|
||||
.status(400)
|
||||
.send({ message: "Proposed link cannot cause a cycle in the graph" });
|
||||
logger.warn("potential cycle detected");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
const newLink = await Links.create({
|
||||
userID: uID,
|
||||
from: fromPl.id,
|
||||
to: toPl.id
|
||||
to: toPl.id,
|
||||
});
|
||||
if (!newLink) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Could not create link", { error: new Error("Links.create failed?") });
|
||||
return;
|
||||
logger.error("Could not create link", {
|
||||
error: new Error("Links.create failed?"),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
res.status(201).send({ message: "Created link." });
|
||||
logger.debug("Created link");
|
||||
return;
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("createLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove link between playlists
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const removeLink = async (req, res) => {
|
||||
*/
|
||||
const removeLink: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
if (!req.session.user)
|
||||
throw new Error("sessionData does not have user object");
|
||||
const uID = req.session.user.id;
|
||||
|
||||
let fromPl, toPl;
|
||||
@@ -321,103 +360,122 @@ export const removeLink = async (req, res) => {
|
||||
if (fromPl.type !== "playlist" || toPl.type !== "playlist") {
|
||||
res.status(400).send({ message: "Link is not a playlist" });
|
||||
logger.info("non-playlist link provided", { from: fromPl, to: toPl });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send({ message: "Could not parse link" });
|
||||
logger.warn("parseSpotifyLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
// check if exists
|
||||
const existingLink = await Links.findOne({
|
||||
where: {
|
||||
[Op.and]: [
|
||||
{ userID: uID },
|
||||
{ from: fromPl.id },
|
||||
{ to: toPl.id }
|
||||
]
|
||||
}
|
||||
[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");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
const removedLink = await Links.destroy({
|
||||
where: {
|
||||
[Op.and]: [
|
||||
{ userID: uID },
|
||||
{ from: fromPl.id },
|
||||
{ to: toPl.id }
|
||||
]
|
||||
}
|
||||
[Op.and]: [{ userID: uID }, { from: fromPl.id }, { to: toPl.id }],
|
||||
},
|
||||
});
|
||||
if (!removedLink) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("Could not remove link", { error: new Error("Links.destroy failed?") });
|
||||
return;
|
||||
logger.error("Could not remove link", {
|
||||
error: new Error("Links.destroy failed?"),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
res.status(200).send({ message: "Deleted link." });
|
||||
logger.debug("Deleted link");
|
||||
return;
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("removeLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {string} playlistID
|
||||
*/
|
||||
const _getPlaylistTracks = async (req, res, playlistID) => {
|
||||
interface _GetPlaylistTracksArgs extends EndpointHandlerBaseArgs {
|
||||
playlistID: string;
|
||||
}
|
||||
interface _GetPlaylistTracks {
|
||||
tracks: {
|
||||
is_local: boolean;
|
||||
uri: string;
|
||||
}[];
|
||||
snapshot_id: string;
|
||||
}
|
||||
const _getPlaylistTracks: (
|
||||
opts: _GetPlaylistTracksArgs
|
||||
) => Promise<_GetPlaylistTracks | null> = async ({ req, res, playlistID }) => {
|
||||
let initialFields = ["tracks(next,items(is_local,track(uri)))"];
|
||||
let mainFields = ["next", "items(is_local,track(uri))"];
|
||||
|
||||
const respData = await getPlaylistDetailsFirstPage(req, res, initialFields.join(), playlistID);
|
||||
if (res.headersSent) return;
|
||||
const respData = await getPlaylistDetailsFirstPage({
|
||||
req,
|
||||
res,
|
||||
initialFields: initialFields.join(),
|
||||
playlistID,
|
||||
});
|
||||
if (!respData) return null;
|
||||
|
||||
const pl: _GetPlaylistTracks = {
|
||||
tracks: [],
|
||||
snapshot_id: respData.snapshot_id,
|
||||
};
|
||||
let nextURL;
|
||||
|
||||
let pl = {};
|
||||
// varying fields again smh
|
||||
if (respData.tracks.next) {
|
||||
pl.next = new URL(respData.tracks.next);
|
||||
pl.next.searchParams.set("fields", mainFields.join());
|
||||
pl.next = pl.next.href;
|
||||
nextURL = new URL(respData.tracks.next);
|
||||
nextURL.searchParams.set("fields", mainFields.join());
|
||||
nextURL = nextURL.href;
|
||||
}
|
||||
pl.tracks = respData.tracks.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
uri: playlist_item.track.uri,
|
||||
};
|
||||
});
|
||||
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (pl.next) {
|
||||
const nextData = await getPlaylistDetailsNextPage(req, res, pl.next);
|
||||
if (res.headersSent) return;
|
||||
while (nextURL) {
|
||||
const nextData = await getPlaylistDetailsNextPage({
|
||||
req,
|
||||
res,
|
||||
nextURL,
|
||||
});
|
||||
if (!nextData) return null;
|
||||
|
||||
pl.tracks.push(
|
||||
...nextData.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
uri: playlist_item.track.uri,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
pl.next = nextData.next;
|
||||
nextURL = nextData.next;
|
||||
}
|
||||
|
||||
delete pl.next;
|
||||
return pl;
|
||||
}
|
||||
};
|
||||
|
||||
interface _PopulateSingleLinkCoreArgs extends EndpointHandlerBaseArgs {
|
||||
link: {
|
||||
from: URIObject;
|
||||
to: URIObject;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Add tracks to the link-head playlist,
|
||||
* that are present in the link-tail playlist but not in the link-head playlist,
|
||||
@@ -434,49 +492,63 @@ const _getPlaylistTracks = async (req, res, playlistID) => {
|
||||
* after populateMissingInLink, pl_a will have tracks: a, b, c, e, d
|
||||
*
|
||||
* CANNOT populate local files; Spotify API does not support it yet.
|
||||
*
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {{from: typedefs.URIObject, to: typedefs.URIObject}} link
|
||||
* @returns {Promise<{toAddNum: number, localNum: number} | undefined>}
|
||||
*/
|
||||
const _populateSingleLinkCore = async (req, res, link) => {
|
||||
const _populateSingleLinkCore: (
|
||||
opts: _PopulateSingleLinkCoreArgs
|
||||
) => Promise<{ toAddNum: number; localNum: number } | null> = async ({
|
||||
req,
|
||||
res,
|
||||
link,
|
||||
}) => {
|
||||
try {
|
||||
const fromPl = link.from, toPl = link.to;
|
||||
const fromPl = link.from,
|
||||
toPl = link.to;
|
||||
|
||||
const fromPlaylist = await _getPlaylistTracks(req, res, fromPl.id);
|
||||
const toPlaylist = await _getPlaylistTracks(req, res, toPl.id);
|
||||
const fromPlaylist = await _getPlaylistTracks({
|
||||
req,
|
||||
res,
|
||||
playlistID: fromPl.id,
|
||||
});
|
||||
const toPlaylist = await _getPlaylistTracks({
|
||||
req,
|
||||
res,
|
||||
playlistID: toPl.id,
|
||||
});
|
||||
|
||||
const fromTrackURIs = fromPlaylist.tracks.map(track => track.uri);
|
||||
let toTrackURIs = toPlaylist.tracks.
|
||||
filter(track => !track.is_local). // API doesn't support adding local files to playlists yet
|
||||
filter(track => !fromTrackURIs.includes(track.uri)). // only ones missing from the 'from' playlist
|
||||
map(track => track.uri);
|
||||
if (!fromPlaylist || !toPlaylist) return null;
|
||||
const fromTrackURIs = fromPlaylist.tracks.map((track) => track.uri);
|
||||
let toTrackURIs = toPlaylist.tracks
|
||||
.filter((track) => !track.is_local) // API doesn't support adding local files to playlists yet
|
||||
.filter((track) => !fromTrackURIs.includes(track.uri)) // only ones missing from the 'from' playlist
|
||||
.map((track) => track.uri);
|
||||
|
||||
const toAddNum = toTrackURIs.length;
|
||||
const localNum = toPlaylist.tracks.filter(track => track.is_local).length;
|
||||
const localNum = toPlaylist.tracks.filter((track) => track.is_local).length;
|
||||
|
||||
// append to end in batches of 100
|
||||
while (toTrackURIs.length > 0) {
|
||||
const nextBatch = toTrackURIs.splice(0, 100);
|
||||
const addData = await addItemsToPlaylist(req, res, nextBatch, fromPl.id);
|
||||
if (res.headersSent) return;
|
||||
const addData = await addItemsToPlaylist({
|
||||
req,
|
||||
res,
|
||||
nextBatch,
|
||||
playlistID: fromPl.id,
|
||||
});
|
||||
if (!addData) return null;
|
||||
}
|
||||
|
||||
return { toAddNum, localNum };
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("_populateSingleLinkCore", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const populateSingleLink = async (req, res) => {
|
||||
const populateSingleLink: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
if (!req.session.user)
|
||||
throw new Error("sessionData does not have user object");
|
||||
const uID = req.session.user.id;
|
||||
const link = { from: req.body.from, to: req.body.to };
|
||||
let fromPl, toPl;
|
||||
@@ -487,51 +559,63 @@ export const populateSingleLink = async (req, res) => {
|
||||
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;
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send({ message: "Could not parse link" });
|
||||
logger.warn("parseSpotifyLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
// check if exists
|
||||
const existingLink = await Links.findOne({
|
||||
where: {
|
||||
[Op.and]: [
|
||||
{ userID: uID },
|
||||
{ from: fromPl.id },
|
||||
{ to: toPl.id }
|
||||
]
|
||||
}
|
||||
[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;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!await checkPlaylistEditable(req, res, fromPl.id, uID))
|
||||
return;
|
||||
if (
|
||||
!(await checkPlaylistEditable({
|
||||
req,
|
||||
res,
|
||||
playlistID: fromPl.id,
|
||||
userID: uID,
|
||||
}))
|
||||
)
|
||||
return null;
|
||||
|
||||
const result = await _populateSingleLinkCore(req, res, { from: fromPl, to: toPl });
|
||||
const result = await _populateSingleLinkCore({
|
||||
req,
|
||||
res,
|
||||
link: { from: fromPl, to: toPl },
|
||||
});
|
||||
if (result) {
|
||||
const { toAddNum, localNum } = result;
|
||||
let logMsg;
|
||||
logMsg = toAddNum > 0 ? "Added " + toAddNum + " tracks" : "No tracks to add";
|
||||
logMsg += localNum > 0 ? "; could not process " + localNum + " local files" : ".";
|
||||
logMsg =
|
||||
toAddNum > 0 ? "Added " + toAddNum + " tracks" : "No tracks to add";
|
||||
logMsg +=
|
||||
localNum > 0 ? "; could not process " + localNum + " local files" : ".";
|
||||
|
||||
res.status(200).send({ message: logMsg });
|
||||
logger.debug(logMsg, { toAddNum, localNum });
|
||||
}
|
||||
return;
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("populateSingleLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
interface _PruneSingleLinkCoreArgs extends EndpointHandlerBaseArgs {
|
||||
link: { from: URIObject; to: URIObject };
|
||||
}
|
||||
/**
|
||||
* Remove tracks from the link-tail playlist,
|
||||
* that are present in the link-tail playlist but not in the link-head playlist.
|
||||
@@ -546,53 +630,64 @@ export const populateSingleLink = async (req, res) => {
|
||||
*
|
||||
* after pruneSingleLink, pl_b will have tracks: b, c
|
||||
*
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {{from: typedefs.URIObject, to: typedefs.URIObject}} link
|
||||
* @returns {Promise<{toDelNum: number} | undefined>}
|
||||
*/
|
||||
const _pruneSingleLinkCore = async (req, res, link) => {
|
||||
const _pruneSingleLinkCore: (
|
||||
opts: _PruneSingleLinkCoreArgs
|
||||
) => Promise<{ toDelNum: number } | null> = async ({ req, res, link }) => {
|
||||
try {
|
||||
const fromPl = link.from, toPl = link.to;
|
||||
const fromPl = link.from,
|
||||
toPl = link.to;
|
||||
|
||||
const fromPlaylist = await _getPlaylistTracks(req, res, fromPl.id);
|
||||
const toPlaylist = await _getPlaylistTracks(req, res, toPl.id);
|
||||
|
||||
const fromTrackURIs = fromPlaylist.tracks.map(track => track.uri);
|
||||
let indexedToTrackURIs = toPlaylist.tracks;
|
||||
|
||||
indexedToTrackURIs.forEach((track, index) => {
|
||||
track.position = index;
|
||||
const fromPlaylist = await _getPlaylistTracks({
|
||||
req,
|
||||
res,
|
||||
playlistID: fromPl.id,
|
||||
});
|
||||
const toPlaylist = await _getPlaylistTracks({
|
||||
req,
|
||||
res,
|
||||
playlistID: toPl.id,
|
||||
});
|
||||
|
||||
let indexes = indexedToTrackURIs.filter(track => !fromTrackURIs.includes(track.uri)); // only those missing from the 'from' playlist
|
||||
indexes = indexes.map(track => track.position); // get track positions
|
||||
if (!fromPlaylist || !toPlaylist) return null;
|
||||
const fromTrackURIs = fromPlaylist.tracks.map((track) => track.uri);
|
||||
const indexedToTrackURIs = toPlaylist.tracks.map((track, index) => {
|
||||
return { ...track, position: index };
|
||||
});
|
||||
|
||||
let indexes = indexedToTrackURIs
|
||||
.filter((track) => !fromTrackURIs.includes(track.uri)) // only those missing from the 'from' playlist
|
||||
.map((track) => track.position); // get track positions
|
||||
|
||||
const toDelNum = indexes.length;
|
||||
|
||||
// remove in batches of 100 (from reverse, to preserve positions while modifying)
|
||||
let currentSnapshot = toPlaylist.snapshot_id;
|
||||
while (indexes.length) {
|
||||
while (indexes.length > 0) {
|
||||
const nextBatch = indexes.splice(Math.max(indexes.length - 100, 0), 100);
|
||||
const delResponse = await removeItemsFromPlaylist(req, res, nextBatch, toPl.id, currentSnapshot);
|
||||
if (res.headersSent) return;
|
||||
const delResponse = await removePlaylistItems({
|
||||
req,
|
||||
res,
|
||||
nextBatch,
|
||||
playlistID: toPl.id,
|
||||
snapshotID: currentSnapshot,
|
||||
});
|
||||
if (!delResponse) return null;
|
||||
currentSnapshot = delResponse.snapshot_id;
|
||||
}
|
||||
|
||||
return { toDelNum };
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("_pruneSingleLinkCore", { error })
|
||||
return;
|
||||
logger.error("_pruneSingleLinkCore", { error });
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const pruneSingleLink = async (req, res) => {
|
||||
const pruneSingleLink: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
if (!req.session.user)
|
||||
throw new Error("sessionData does not have user object");
|
||||
const uID = req.session.user.id;
|
||||
const link = { from: req.body.from, to: req.body.to };
|
||||
|
||||
@@ -603,43 +698,62 @@ export const pruneSingleLink = async (req, res) => {
|
||||
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;
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
res.status(400).send({ message: error.message });
|
||||
logger.warn("parseSpotifyLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
// check if exists
|
||||
const existingLink = await Links.findOne({
|
||||
where: {
|
||||
[Op.and]: [
|
||||
{ userID: uID },
|
||||
{ from: fromPl.id },
|
||||
{ to: toPl.id }
|
||||
]
|
||||
}
|
||||
[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;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!await checkPlaylistEditable(req, res, toPl.id, uID))
|
||||
return;
|
||||
if (
|
||||
!(await checkPlaylistEditable({
|
||||
req,
|
||||
res,
|
||||
playlistID: toPl.id,
|
||||
userID: uID,
|
||||
}))
|
||||
)
|
||||
return null;
|
||||
|
||||
const result = await _pruneSingleLinkCore(req, res, { from: fromPl, to: toPl });
|
||||
const result = await _pruneSingleLinkCore({
|
||||
req,
|
||||
res,
|
||||
link: {
|
||||
from: fromPl,
|
||||
to: toPl,
|
||||
},
|
||||
});
|
||||
if (result) {
|
||||
const { toDelNum } = result;
|
||||
res.status(200).send({ message: `Removed ${toDelNum} tracks.` });
|
||||
logger.debug(`Pruned ${toDelNum} tracks`, { toDelNum });
|
||||
}
|
||||
return;
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("pruneSingleLink", { error });
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
updateUser,
|
||||
fetchUser,
|
||||
createLink,
|
||||
removeLink,
|
||||
populateSingleLink,
|
||||
pruneSingleLink,
|
||||
};
|
||||
@@ -1,155 +0,0 @@
|
||||
import curriedLogger from "../utils/logger.js";
|
||||
const logger = curriedLogger(import.meta);
|
||||
|
||||
import * as typedefs from "../typedefs.js";
|
||||
import { getUserPlaylistsFirstPage, getUserPlaylistsNextPage, getPlaylistDetailsFirstPage, getPlaylistDetailsNextPage } from "../api/spotify.js";
|
||||
import { parseSpotifyLink } from "../utils/spotifyURITransformer.js";
|
||||
|
||||
/**
|
||||
* Retrieve list of all of user's playlists
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const fetchUserPlaylists = async (req, res) => {
|
||||
try {
|
||||
let userPlaylists = {};
|
||||
|
||||
// get first 50
|
||||
const respData = await getUserPlaylistsFirstPage(req, res);
|
||||
if (res.headersSent) return;
|
||||
|
||||
userPlaylists.total = respData.total;
|
||||
|
||||
userPlaylists.items = respData.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
});
|
||||
|
||||
userPlaylists.next = respData.next;
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (userPlaylists.next) {
|
||||
const nextData = await getUserPlaylistsNextPage(req, res, userPlaylists.next);
|
||||
if (res.headersSent) return;
|
||||
|
||||
userPlaylists.items.push(
|
||||
...nextData.items.map((playlist) => {
|
||||
return {
|
||||
uri: playlist.uri,
|
||||
images: playlist.images,
|
||||
name: playlist.name,
|
||||
total: playlist.tracks.total
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
userPlaylists.next = nextData.next;
|
||||
}
|
||||
|
||||
delete userPlaylists.next;
|
||||
|
||||
res.status(200).send(userPlaylists);
|
||||
logger.debug("Fetched user playlists", { num: userPlaylists.total });
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("fetchUserPlaylists", { error });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an entire playlist
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
*/
|
||||
export const fetchPlaylistDetails = async (req, res) => {
|
||||
try {
|
||||
let playlist = {};
|
||||
/** @type {typedefs.URIObject} */
|
||||
let uri;
|
||||
let initialFields = ["collaborative", "description", "images", "name", "owner(uri,display_name)", "public",
|
||||
"snapshot_id", "tracks(next,total,items(is_local,track(name,uri)))"];
|
||||
let mainFields = ["next,items(is_local,track(name,uri))"];
|
||||
|
||||
try {
|
||||
uri = parseSpotifyLink(req.query.playlist_link)
|
||||
if (uri.type !== "playlist") {
|
||||
res.status(400).send({ message: "Link is not a playlist" });
|
||||
logger.warn("non-playlist link provided", { uri });
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(400).send({ message: error.message });
|
||||
logger.warn("parseSpotifyLink", { error });
|
||||
return;
|
||||
}
|
||||
|
||||
const respData = await getPlaylistDetailsFirstPage(req, res, initialFields.join(), uri.id);
|
||||
if (res.headersSent) return;
|
||||
|
||||
// TODO: this whole section needs to be DRYer
|
||||
// look into serializr
|
||||
playlist.name = respData.name;
|
||||
playlist.description = respData.description;
|
||||
playlist.collaborative = respData.collaborative;
|
||||
playlist.public = respData.public;
|
||||
playlist.images = [...respData.images];
|
||||
playlist.owner = { ...respData.owner };
|
||||
playlist.snapshot_id = respData.snapshot_id;
|
||||
playlist.total = respData.tracks.total;
|
||||
|
||||
// previous fields get carried over to the next URL, but most of these fields are not present in the new endpoint
|
||||
// API shouldn't be returning such URLs, the problem's in the API ig...
|
||||
if (respData.tracks.next) {
|
||||
playlist.next = new URL(respData.tracks.next);
|
||||
playlist.next.searchParams.set("fields", mainFields.join());
|
||||
playlist.next = playlist.next.href;
|
||||
}
|
||||
playlist.tracks = respData.tracks.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (playlist.next) {
|
||||
const nextData = await getPlaylistDetailsNextPage(req, res, playlist.next);
|
||||
if (res.headersSent) return;
|
||||
|
||||
playlist.tracks.push(
|
||||
...nextData.items.map((playlist_item) => {
|
||||
return {
|
||||
is_local: playlist_item.is_local,
|
||||
track: {
|
||||
name: playlist_item.track.name,
|
||||
type: playlist_item.track.type,
|
||||
uri: playlist_item.track.uri
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
playlist.next = nextData.next;
|
||||
}
|
||||
|
||||
delete playlist.next;
|
||||
|
||||
res.status(200).send(playlist);
|
||||
logger.debug("Fetched playlist tracks", { num: playlist.tracks.length });
|
||||
return;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("getPlaylistDetails", { error });
|
||||
return;
|
||||
}
|
||||
}
|
||||
55
controllers/playlists.ts
Normal file
55
controllers/playlists.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
getCurrentUsersPlaylistsFirstPage,
|
||||
getCurrentUsersPlaylistsNextPage,
|
||||
} from "../api/spotify.ts";
|
||||
|
||||
import type { RequestHandler } from "express";
|
||||
import type {
|
||||
Pagination,
|
||||
SimplifiedPlaylistObject,
|
||||
} from "spotify_manager/index.d.ts";
|
||||
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
/**
|
||||
* Get user's playlists
|
||||
*/
|
||||
const fetchUserPlaylists: RequestHandler = async (req, res) => {
|
||||
try {
|
||||
// get first 50
|
||||
const respData = await getCurrentUsersPlaylistsFirstPage({ req, res });
|
||||
if (!respData) return null;
|
||||
|
||||
let tmpData = structuredClone(respData);
|
||||
const userPlaylists: Pick<
|
||||
Pagination<SimplifiedPlaylistObject>,
|
||||
"items" | "total"
|
||||
> = {
|
||||
items: [...tmpData.items],
|
||||
total: tmpData.total,
|
||||
};
|
||||
let nextURL = respData.next;
|
||||
// keep getting batches of 50 till exhausted
|
||||
while (nextURL) {
|
||||
const nextData = await getCurrentUsersPlaylistsNextPage({
|
||||
req,
|
||||
res,
|
||||
nextURL,
|
||||
});
|
||||
if (!nextData) return null;
|
||||
|
||||
userPlaylists.items.push(...nextData.items);
|
||||
nextURL = nextData.next;
|
||||
}
|
||||
|
||||
res.status(200).send(userPlaylists);
|
||||
logger.debug("Fetched user playlists", { num: userPlaylists.total });
|
||||
return null;
|
||||
} catch (error) {
|
||||
res.status(500).send({ message: "Internal Server Error" });
|
||||
logger.error("fetchUserPlaylists", { error });
|
||||
return null;
|
||||
}
|
||||
};
|
||||
export { fetchUserPlaylists };
|
||||
Reference in New Issue
Block a user