Source: schemas/channel/media/playlistMediaSchema.js

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.*/

//NPM Imports
const {mongoose} = require('mongoose');

//Local Imports
const mediaSchema = require('./mediaSchema');
const media = require('../../../app/channel/media/media');

/**
 * DB Schema for documents represnting a piece of media held in a playlist
 */
const playlistMediaProperties = new mongoose.Schema({
    uuid: {
        type: mongoose.SchemaTypes.UUID,
        required:true,
        default: crypto.randomUUID()
    }
},
{
    discriminatorKey: 'status'
});

//Schema Middleware
/**
 * Pre-save function for playlist meda, ensures unique UUID
 */
playlistMediaProperties.pre('save', async function (next){
    //If the UUID was modified in anyway
    if(this.isModified("uuid")){
        //Throw that shit out and make a new one since it's probably either null or a leftover from some channel queue
        this.uuid = crypto.randomUUID();
    }

    //Keep it moving
    next();
});

//methods
/**
 * Rehydrate to a full phat media object
 * @returns {media} A full phat media object, re-hydrated from the DB
 */
playlistMediaProperties.methods.rehydrate = function(){
    //Return item as a full phat, standard media object
    return new media(
        this.title,
        this.fileName,
        this.url,
        this.id,
        this.type,
        this.duration
    );
}

/**
 * Dehydrate to minified flat network-friendly object
 * @returns {Object} Network-Friendly Browser-Digestable object representing media from a playlist
 */
playlistMediaProperties.methods.dehydrate = function(){
    return {
        title: this.title,
        url: this.url,
        duration: this.duration,
        uuid: this.uuid.toString()
    };
}

module.exports = mediaSchema.discriminator('saved', playlistMediaProperties);