mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 09:24:07 +00:00
opn: update user's playlists
This commit is contained in:
parent
6c497c9be1
commit
7074009fab
113
controllers/operations.js
Normal file
113
controllers/operations.js
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
const logger = require("../utils/logger")(module);
|
||||||
|
const { axiosInstance } = require("../utils/axios");
|
||||||
|
const { parseSpotifyUri } = require("../utils/spotifyUriTransformer");
|
||||||
|
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
/** @type {typedefs.Model} */
|
||||||
|
const userPlaylists = require("../models").userPlaylists;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store user's playlists
|
||||||
|
* @param {typedefs.Req} req
|
||||||
|
* @param {typedefs.Res} res
|
||||||
|
*/
|
||||||
|
const updateUser = async (req, res) => {
|
||||||
|
try {
|
||||||
|
let currentPlaylists = [];
|
||||||
|
const userURI = parseSpotifyUri(req.session.user.uri);
|
||||||
|
|
||||||
|
// get first 50
|
||||||
|
const response = await axiosInstance.get(
|
||||||
|
`/users/${userURI.id}/playlists`,
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
offset: 0,
|
||||||
|
limit: 50,
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
...req.authHeader
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.status >= 400 && response.status < 500)
|
||||||
|
return res.status(response.status).send(response.data);
|
||||||
|
|
||||||
|
currentPlaylists = response.data.items.map(playlist => parseSpotifyUri(playlist.uri).id);
|
||||||
|
nextURL = response.data.next;
|
||||||
|
|
||||||
|
// keep getting batches of 50 till exhausted
|
||||||
|
while (nextURL) {
|
||||||
|
const nextResponse = await axiosInstance.get(
|
||||||
|
nextURL, // absolute URL from previous response which has params
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
...req.authHeader
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (response.status >= 400 && response.status < 500)
|
||||||
|
return res.status(response.status).send(response.data);
|
||||||
|
|
||||||
|
currentPlaylists.push(
|
||||||
|
...nextResponse.data.items.map(playlist => parseSpotifyUri(playlist.uri).id)
|
||||||
|
);
|
||||||
|
|
||||||
|
nextURL = nextResponse.data.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let oldPlaylists = await userPlaylists.findAll({
|
||||||
|
attributes: ["playlistID"],
|
||||||
|
raw: true,
|
||||||
|
where: {
|
||||||
|
userID: userURI.id
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
let toRemove, toAdd;
|
||||||
|
if (oldPlaylists.length) {
|
||||||
|
// existing user
|
||||||
|
oldPlaylists = oldPlaylists.map(pl => pl.playlistID);
|
||||||
|
const currentSet = new Set(currentPlaylists);
|
||||||
|
const oldSet = new Set(oldPlaylists);
|
||||||
|
|
||||||
|
toAdd = currentPlaylists.filter(current => !oldSet.has(current));
|
||||||
|
toRemove = oldPlaylists.filter(old => !currentSet.has(old));
|
||||||
|
} else {
|
||||||
|
// new user
|
||||||
|
toAdd = currentPlaylists;
|
||||||
|
toRemove = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toRemove.length) {
|
||||||
|
const cleanedUser = await userPlaylists.destroy({
|
||||||
|
where: { playlistID: toRemove }
|
||||||
|
});
|
||||||
|
if (cleanedUser !== toRemove.length) {
|
||||||
|
logger.error("Could not remove old playlists", { error: new Error("model.destroy failed?") });
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toAdd.length) {
|
||||||
|
const updatedUser = await userPlaylists.bulkCreate(
|
||||||
|
toAdd.map((pl) => { return { playlistID: pl, userID: userURI.id } }),
|
||||||
|
{ validate: true }
|
||||||
|
);
|
||||||
|
if (updatedUser.length !== toAdd.length) {
|
||||||
|
logger.error("Could not add new playlists", { error: new Error("model.bulkCreate failed?") });
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.sendStatus(200);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('updateUser', { error });
|
||||||
|
return res.sendStatus(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
updateUser
|
||||||
|
};
|
||||||
@ -130,9 +130,11 @@ const getPlaylistDetails = async (req, res) => {
|
|||||||
|
|
||||||
// previous fields get carried over to the next URL, but most of these fields are not present in the new endpoint
|
// 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...
|
// API shouldn't be returning such URLs, the problem's in the API ig...
|
||||||
playlist.next = new URL(response.data.tracks.next);
|
if (response.data.tracks.next) {
|
||||||
playlist.next.searchParams.set("fields", mainFields.join());
|
playlist.next = new URL(response.data.tracks.next);
|
||||||
playlist.next = playlist.next.href;
|
playlist.next.searchParams.set("fields", mainFields.join());
|
||||||
|
playlist.next = playlist.next.href;
|
||||||
|
}
|
||||||
playlist.tracks = response.data.tracks.items.map((playlist_item) => {
|
playlist.tracks = response.data.tracks.items.map((playlist_item) => {
|
||||||
return {
|
return {
|
||||||
is_local: playlist_item.is_local,
|
is_local: playlist_item.is_local,
|
||||||
|
|||||||
3
index.js
3
index.js
@ -9,6 +9,7 @@ const cookieParser = require('cookie-parser');
|
|||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
|
|
||||||
const SQLiteStore = require("connect-sqlite3")(session);
|
const SQLiteStore = require("connect-sqlite3")(session);
|
||||||
|
const db = require("./models");
|
||||||
|
|
||||||
const logger = require("./utils/logger")(module);
|
const logger = require("./utils/logger")(module);
|
||||||
|
|
||||||
@ -51,6 +52,7 @@ app.use(express.static(__dirname + '/static'));
|
|||||||
// Routes
|
// Routes
|
||||||
app.use("/api/auth/", require("./routes/auth"));
|
app.use("/api/auth/", require("./routes/auth"));
|
||||||
app.use("/api/playlists", require("./routes/playlists"));
|
app.use("/api/playlists", require("./routes/playlists"));
|
||||||
|
app.use("/api/operations", require("./routes/operations"));
|
||||||
|
|
||||||
// Fallbacks
|
// Fallbacks
|
||||||
app.use((_req, res) => {
|
app.use((_req, res) => {
|
||||||
@ -67,6 +69,7 @@ const server = app.listen(port, () => {
|
|||||||
|
|
||||||
const cleanupFunc = (signal) => {
|
const cleanupFunc = (signal) => {
|
||||||
Promise.allSettled([
|
Promise.allSettled([
|
||||||
|
db.sequelize.close(),
|
||||||
util.promisify(server.close),
|
util.promisify(server.close),
|
||||||
]).then(() => {
|
]).then(() => {
|
||||||
if (signal)
|
if (signal)
|
||||||
|
|||||||
31
migrations/20240727162141-create-user-playlists.js
Normal file
31
migrations/20240727162141-create-user-playlists.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
'use strict';
|
||||||
|
/** @type {import('sequelize-cli').Migration} */
|
||||||
|
module.exports = {
|
||||||
|
async up(queryInterface, Sequelize) {
|
||||||
|
await queryInterface.createTable('userPlaylists', {
|
||||||
|
id: {
|
||||||
|
allowNull: false,
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
type: Sequelize.INTEGER
|
||||||
|
},
|
||||||
|
playlistID: {
|
||||||
|
type: Sequelize.STRING
|
||||||
|
},
|
||||||
|
userID: {
|
||||||
|
type: Sequelize.STRING
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
allowNull: false,
|
||||||
|
type: Sequelize.DATE
|
||||||
|
},
|
||||||
|
updatedAt: {
|
||||||
|
allowNull: false,
|
||||||
|
type: Sequelize.DATE
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async down(queryInterface, Sequelize) {
|
||||||
|
await queryInterface.dropTable('userPlaylists');
|
||||||
|
}
|
||||||
|
};
|
||||||
55
models/index.js
Normal file
55
models/index.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
"use strict";
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
const Sequelize = require("sequelize");
|
||||||
|
const typedefs = require("../typedefs");
|
||||||
|
const logger = require("../utils/logger")(module);
|
||||||
|
const basename = path.basename(__filename);
|
||||||
|
const env = process.env.NODE_ENV || "development";
|
||||||
|
const config = require(__dirname + "/../config/sequelize.js")[env];
|
||||||
|
const db = {};
|
||||||
|
|
||||||
|
/** @type {typedefs.Sequelize} */
|
||||||
|
let sequelize;
|
||||||
|
if (config.use_env_variable) {
|
||||||
|
sequelize = new Sequelize(process.env[config.use_env_variable], config, {
|
||||||
|
logging: (msg) => logger.debug(msg)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sequelize = new Sequelize(config.database, config.username, config.password, config, {
|
||||||
|
logging: (msg) => logger.debug(msg)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
await sequelize.authenticate();
|
||||||
|
logger.info("Sequelize auth success");
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Sequelize auth error", { err });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
// Read model definitions from folder
|
||||||
|
fs
|
||||||
|
.readdirSync(__dirname)
|
||||||
|
.filter(file => {
|
||||||
|
return (file.indexOf(".") !== 0) && (file !== basename) && (file.slice(-3) === ".js");
|
||||||
|
})
|
||||||
|
.forEach(file => {
|
||||||
|
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
|
||||||
|
db[model.name] = model;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup defined associations
|
||||||
|
Object.keys(db).forEach(modelName => {
|
||||||
|
if (db[modelName].associate) {
|
||||||
|
db[modelName].associate(db);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
db.sequelize = sequelize;
|
||||||
|
db.Sequelize = Sequelize;
|
||||||
|
|
||||||
|
module.exports = db;
|
||||||
24
models/userplaylists.js
Normal file
24
models/userplaylists.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
'use strict';
|
||||||
|
const {
|
||||||
|
Model
|
||||||
|
} = require('sequelize');
|
||||||
|
module.exports = (sequelize, DataTypes) => {
|
||||||
|
class userPlaylists extends Model {
|
||||||
|
/**
|
||||||
|
* Helper method for defining associations.
|
||||||
|
* This method is not a part of Sequelize lifecycle.
|
||||||
|
* The `models/index` file will call this method automatically.
|
||||||
|
*/
|
||||||
|
static associate(models) {
|
||||||
|
// define association here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
userPlaylists.init({
|
||||||
|
playlistID: DataTypes.STRING,
|
||||||
|
userID: DataTypes.STRING
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'userPlaylists',
|
||||||
|
});
|
||||||
|
return userPlaylists;
|
||||||
|
};
|
||||||
12
routes/operations.js
Normal file
12
routes/operations.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
const router = require('express').Router();
|
||||||
|
|
||||||
|
const { updateUser } = require('../controllers/operations');
|
||||||
|
const { isAuthenticated } = require('../middleware/authCheck');
|
||||||
|
|
||||||
|
router.post(
|
||||||
|
"/update",
|
||||||
|
isAuthenticated,
|
||||||
|
updateUser
|
||||||
|
);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@ -56,8 +56,16 @@ const logger = (callingModule) => {
|
|||||||
),
|
),
|
||||||
transports: [
|
transports: [
|
||||||
new transports.Console({ level: 'debug' }),
|
new transports.Console({ level: 'debug' }),
|
||||||
new transports.File({ filename: __dirname + '/../logs/debug.log', level: 'debug' }),
|
new transports.File({
|
||||||
new transports.File({ filename: __dirname + '/../logs/error.log', level: 'error' }),
|
filename: __dirname + '/../logs/debug.log',
|
||||||
|
level: 'debug',
|
||||||
|
maxsize: 10485760,
|
||||||
|
}),
|
||||||
|
new transports.File({
|
||||||
|
filename: __dirname + '/../logs/error.log',
|
||||||
|
level: 'error',
|
||||||
|
maxsize: 10485760,
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user