Started work on implementing user playlists

This commit is contained in:
rainbow napkin 2025-04-06 00:24:05 -04:00
parent e629c63b2c
commit aefc2dc1bd
7 changed files with 228 additions and 33 deletions

View file

@ -22,6 +22,7 @@ const queuedMedia = require('./queuedMedia');
const loggerUtils = require('../../../utils/loggerUtils');
const yanker = require('../../../utils/media/yanker');
const channelModel = require('../../../schemas/channel/channelSchema');
const { userModel } = require('../../../schemas/user/userSchema');
module.exports = class{
constructor(server, chanDB, channel){
@ -32,6 +33,7 @@ module.exports = class{
}
defineListeners(socket){
//Channel Playlist Listeners
socket.on("getChannelPlaylists", () => {this.getChannelPlaylists(socket)});
socket.on("createChannelPlaylist", (data) => {this.createChannelPlaylist(socket, data)});
socket.on("deleteChannelPlaylist", (data) => {this.deleteChannelPlaylist(socket, data)});
@ -42,9 +44,14 @@ module.exports = class{
socket.on("queueFromChannelPlaylist", (data) => {this.queueFromChannelPlaylist(socket, data)});
socket.on("renameChannelPlaylist", (data) => {this.renameChannelPlaylist(socket, data)});
socket.on("changeDefaultTitlesChannelPlaylist", (data) => {this.changeDefaultTitlesChannelPlaylist(socket, data)});
//User Playlist Listeners
socket.on("getUserPlaylists", () => {this.getUserPlaylists(socket)});
socket.on("createUserPlaylist", (data) => {this.createUserPlaylist(socket, data)});
socket.on("deleteUserPlaylist", (data) => {this.deleteUserPlaylist(socket, data)});
}
//--- USER-FACING PLAYLIST FUNCTIONS ---
//Get playlist functions
async getChannelPlaylists(socket, chanDB){
try{
//if we wherent handed a channel document
@ -60,6 +67,52 @@ module.exports = class{
}
}
async getUserPlaylists(socket, userDB){
try{
//if we wherent handed a user document
if(userDB == null){
//Find the user in the Database
userDB = await userModel.findOne({user: socket.request.session.user.user});
}
//Return playlists
socket.emit('userPlaylists', userDB.getPlaylists());
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
//Create playlist functions
createPlaylistValidator(socket, data){
//Create empty array to hold titles
const safeTitles = [];
//If the title is too long
if(!validator.isLength(data.playlist, {min: 1, max:30})){
//Bitch, moan, complain...
loggerUtils.socketErrorHandler(socket, "Playlist name too long!", "validation");
//and ignore it!
return;
}
if(data.defaultTitles != null){
//For each default title passed by the data
for(let title of data.defaultTitles){
//If the title isn't too long
if(validator.isLength(title, {min:1, max:30})){
//Add it to the safe title list
safeTitles.push(validator.escape(validator.trim(title)))
}
}
}
//Escape/trim the playlist name
return {
playlist: validator.escape(validator.trim(data.playlist)),
defaultTitles: safeTitles
}
}
async createChannelPlaylist(socket, data, chanDB){
try{
//if we wherent handed a channel document
@ -69,41 +122,27 @@ module.exports = class{
}
if(await chanDB.permCheck(socket.user, 'editChannelPlaylists')){
//If the title is too long
if(!validator.isLength(data.playlist, {max:30})){
//Bitch, moan, complain...
loggerUtils.socketErrorHandler(socket, "Playlist name too long!", "validation");
//and ignore it!
//Validate Data
const validData = this.createPlaylistValidator(socket, data);
//If we got bad data
if(validData == null){
//Do nothing
return;
}
//Escape/trim the playlist name
const name = validator.escape(validator.trim(data.playlist));
//If the channel already exists
if(chanDB.getPlaylistByName(name) != null){
if(chanDB.getPlaylistByName(validData.playlist) != null){
//Bitch, moan, complain...
loggerUtils.socketErrorHandler(socket, `Playlist named '${name}' already exists!`, "validation");
loggerUtils.socketErrorHandler(socket, `Playlist named '${validData.playlist}' already exists!`, "validation");
//and ignore it!
return;
}
//Create empty array to hold titles
const safeTitles = [];
//For each default title passed by the data
for(let title of data.defaultTitles){
//If the title isn't too long
if(validator.isLength(title, {min:1, max:30})){
//Add it to the safe title list
safeTitles.push(validator.escape(validator.trim(title)))
}
}
}
//Add playlist to the channel doc
chanDB.media.playlists.push({
name,
defaultTitles: safeTitles
name: validData.playlist,
defaultTitles: validData.defaultTitles
});
//Save the channel doc
@ -117,6 +156,48 @@ module.exports = class{
}
}
async createUserPlaylist(socket, data, userDB){
try{
//if we wherent handed a user document
if(userDB == null){
//Find the user in the Database
userDB = await userModel.findOne({user: socket.request.session.user.user});
}
//Validate Data
const validData = this.createPlaylistValidator(socket, data);
//If we got bad data
if(validData == null){
//Do nothing
return;
}
//If the channel already exists
if(userDB.getPlaylistByName(validData.playlist) != null){
//Bitch, moan, complain...
loggerUtils.socketErrorHandler(socket, `Playlist named '${validData.playlist}' already exists!`, "validation");
//and ignore it!
return;
}
//Add playlist to the channel doc
userDB.playlists.push({
name: validData.playlist,
defaultTitles: validData.defaultTitles
});
//Save the channel doc
await userDB.save();
//Return playlists from channel doc
this.getUserPlaylists(socket, userDB);
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
//Delete playlist functions
async deleteChannelPlaylist(socket, data, chanDB){
try{
//if we wherent handed a channel document
@ -125,6 +206,14 @@ module.exports = class{
chanDB = await channelModel.findOne({name: this.channel.name});
}
//If the channel doesn't exist
if(chanDB.getPlaylistByName(data.playlist) == null){
//Bitch, moan, complain...
loggerUtils.socketErrorHandler(socket, `Playlist named '${data.playlist}' doesn't exist!`, "validation");
//and ignore it!
return;
}
if(await chanDB.permCheck(socket.user, 'editChannelPlaylists')){
//Delete playlist name
await chanDB.deletePlaylistByName(data.playlist);
@ -137,6 +226,33 @@ module.exports = class{
}
}
async deleteUserPlaylist(socket, data, userDB){
try{
//if we wherent handed a user document
if(userDB == null){
//Find the user in the Database
userDB = await userModel.findOne({user: socket.request.session.user.user});
}
//If the channel doesn't exist
if(userDB.getPlaylistByName(data.playlist) == null){
//Bitch, moan, complain...
loggerUtils.socketErrorHandler(socket, `Playlist named '${data.playlist}' doesn't exist!`, "validation");
//and ignore it!
return;
}
//Delete playlist name
await userDB.deletePlaylistByName(data.playlist);
//Return playlists from channel doc
this.getUserPlaylists(socket, userDB);
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
//Add Media Functions
async addToChannelPlaylist(socket, data, chanDB){
try{
//if we wherent handed a channel document
@ -198,6 +314,7 @@ module.exports = class{
}
}
//Queuing Functions
async queueChannelPlaylist(socket, data, chanDB){
try{
//if we wherent handed a channel document
@ -334,6 +451,7 @@ module.exports = class{
}
}
//Rename Channel Playlist
async renameChannelPlaylist(socket, data, chanDB){
try{
//if we wherent handed a channel document
@ -387,6 +505,7 @@ module.exports = class{
}
}
//Change Default Title Functions
async changeDefaultTitlesChannelPlaylist(socket, data, chanDB){
try{
//if we wherent handed a channel document
@ -433,6 +552,7 @@ module.exports = class{
}
}
//Delete Playlist Functions
async deleteChannelPlaylistMedia(socket, data, chanDB){
try{
//if we wherent handed a channel document

View file

@ -160,10 +160,6 @@ module.exports = class tokebot{
//we need to wait for this so we don't send used tokes pre-maturely
await userModel.tattooToke(tokers);
}
cooldown(){