mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 06:04:05 +00:00
update playlist names
This commit is contained in:
@@ -8,6 +8,7 @@ const { randomBool, sleep } = require("../utils/flake");
|
|||||||
const myGraph = require("../utils/graph");
|
const myGraph = require("../utils/graph");
|
||||||
|
|
||||||
const { Op } = require("sequelize");
|
const { Op } = require("sequelize");
|
||||||
|
const { sequelize } = require("../models");
|
||||||
/** @type {typedefs.Model} */
|
/** @type {typedefs.Model} */
|
||||||
const Playlists = require("../models").playlists;
|
const Playlists = require("../models").playlists;
|
||||||
/** @type {typedefs.Model} */
|
/** @type {typedefs.Model} */
|
||||||
@@ -53,32 +54,52 @@ const updateUser = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let oldPlaylists = await Playlists.findAll({
|
let oldPlaylists = await Playlists.findAll({
|
||||||
attributes: ["playlistID"],
|
attributes: ["playlistID", "playlistName"],
|
||||||
raw: true,
|
raw: true,
|
||||||
where: {
|
where: {
|
||||||
userID: uID
|
userID: uID
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
let toRemovePls, toAddPls;
|
const deleted = [];
|
||||||
if (oldPlaylists.length) {
|
const added = [];
|
||||||
// existing user
|
const renamed = [];
|
||||||
const currentSet = new Set(currentPlaylists.map(pl => pl.playlistID));
|
|
||||||
const oldSet = new Set(oldPlaylists.map(pl => pl.playlistID));
|
|
||||||
|
|
||||||
// TODO: update playlist name
|
if (oldPlaylists.length) {
|
||||||
toAddPls = currentPlaylists.filter(current => !oldSet.has(current.playlistID));
|
const oldMap = new Map(oldPlaylists.map((p) => [p.playlistID, p]));
|
||||||
toRemovePls = oldPlaylists.filter(old => !currentSet.has(old.playlistID));
|
const currentMap = new Map(currentPlaylists.map((p) => [p.playlistID, p]));
|
||||||
|
|
||||||
|
// Check for added and renamed playlists
|
||||||
|
currentPlaylists.forEach((pl) => {
|
||||||
|
const oldPlaylist = oldMap.get(pl.playlistID);
|
||||||
|
|
||||||
|
if (!oldPlaylist) {
|
||||||
|
added.push(pl);
|
||||||
|
} else if (oldPlaylist.playlistName !== pl.playlistName) {
|
||||||
|
// Renamed playlists
|
||||||
|
renamed.push({
|
||||||
|
playlistID: pl.playlistID,
|
||||||
|
oldName: oldPlaylist.playlistName,
|
||||||
|
newName: pl.playlistName,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check for deleted playlists
|
||||||
|
oldPlaylists.forEach((pl) => {
|
||||||
|
if (!currentMap.has(pl.playlistID)) {
|
||||||
|
deleted.push(pl);
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// new user
|
// new user
|
||||||
toAddPls = currentPlaylists;
|
added.push(...currentPlaylists);
|
||||||
toRemovePls = [];
|
|
||||||
}
|
}
|
||||||
let toRemovePlIDs = toRemovePls.map(pl => pl.playlistID);
|
|
||||||
|
|
||||||
let removedLinks = 0, cleanedUser = 0, updatedUser = [];
|
let removedLinks = 0, delNum = 0, updateNum = 0, addPls = [];
|
||||||
|
|
||||||
if (toRemovePls.length) {
|
const deletedIDs = deleted.map(pl => pl.playlistID);
|
||||||
|
if (deleted.length) {
|
||||||
// clean up any links dependent on the playlists
|
// clean up any links dependent on the playlists
|
||||||
removedLinks = await Links.destroy({
|
removedLinks = await Links.destroy({
|
||||||
where: {
|
where: {
|
||||||
@@ -86,8 +107,8 @@ const updateUser = async (req, res) => {
|
|||||||
{ userID: uID },
|
{ userID: uID },
|
||||||
{
|
{
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
{ from: { [Op.in]: toRemovePlIDs } },
|
{ from: { [Op.in]: deletedIDs } },
|
||||||
{ to: { [Op.in]: toRemovePlIDs } },
|
{ to: { [Op.in]: deletedIDs } },
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -95,30 +116,54 @@ const updateUser = async (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// only then remove
|
// only then remove
|
||||||
cleanedUser = await Playlists.destroy({
|
delNum = await Playlists.destroy({
|
||||||
where: { playlistID: toRemovePlIDs }
|
where: { playlistID: deletedIDs, userID: uID }
|
||||||
});
|
});
|
||||||
if (cleanedUser !== toRemovePls.length) {
|
if (delNum !== deleted.length) {
|
||||||
res.status(500).send({ message: "Internal Server Error" });
|
res.status(500).send({ message: "Internal Server Error" });
|
||||||
logger.warn("Could not remove all old playlists", { error: new Error("Playlists.destroy failed?") });
|
logger.warn("Could not remove all old playlists", { error: new Error("Playlists.destroy failed?") });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (toAddPls.length) {
|
if (added.length) {
|
||||||
updatedUser = await Playlists.bulkCreate(
|
addPls = await Playlists.bulkCreate(
|
||||||
toAddPls.map(pl => { return { ...pl, userID: uID } }),
|
added.map(pl => { return { ...pl, userID: uID } }),
|
||||||
{ validate: true }
|
{ validate: true }
|
||||||
);
|
);
|
||||||
if (updatedUser.length !== toAddPls.length) {
|
if (addPls.length !== added.length) {
|
||||||
res.status(500).send({ message: "Internal Server Error" });
|
res.status(500).send({ message: "Internal Server Error" });
|
||||||
logger.error("Could not add all new playlists", { error: new Error("Playlists.bulkCreate failed?") });
|
logger.error("Could not add all new playlists", { error: new Error("Playlists.bulkCreate failed?") });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).send({ removedLinks: removedLinks > 0 });
|
const transaction = await sequelize.transaction();
|
||||||
logger.debug("Updated user data", { delLinks: removedLinks, delPls: cleanedUser, addPls: updatedUser.length });
|
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();
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).send({ message: "Internal Server Error" });
|
res.status(500).send({ message: "Internal Server Error" });
|
||||||
@@ -203,7 +248,7 @@ const createLink = async (req, res) => {
|
|||||||
|
|
||||||
// if playlists are unknown
|
// if playlists are unknown
|
||||||
if (![fromPl, toPl].every(pl => playlists.includes(pl.id))) {
|
if (![fromPl, toPl].every(pl => playlists.includes(pl.id))) {
|
||||||
res.status(404).send({ message: "Playlists out of sync " });
|
res.status(404).send({ message: "Playlists out of sync." });
|
||||||
logger.warn("unknown playlists, resync");
|
logger.warn("unknown playlists, resync");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user