diff --git a/src/app/channel/media/queue.js b/src/app/channel/media/queue.js index e257bc8..6ad6697 100644 --- a/src/app/channel/media/queue.js +++ b/src/app/channel/media/queue.js @@ -16,6 +16,7 @@ along with this program. If not, see .*/ //NPM imports const validator = require('validator'); +const mongoose = require('mongoose'); //Local imports const config = require('../../../../config.json'); @@ -425,7 +426,7 @@ class queue{ //Pull the channel DB doc const chanDB = await channelModel.findOne({name:this.channel.name}); //Pull archived media by channel name - const archived = await archivedMediaModel.find({channel:this.channel.name}); + const archived = await this.getArchive(); //Cook and emit a new object from all of the data socket.emit("dumpQueue", { @@ -787,12 +788,13 @@ class queue{ } //Keep a copy of the archive that hasn't been changed - const preArchive = chanDB.media.archived; + const preArchive = await this.getArchive(); + //This logic wont work since we cant just set the archive as an array anymore, maybe using mongoose.delete finding by channel and uuid? //Filter out the requested item from the archive - chanDB.media.archived = chanDB.media.archived.filter((record)=>{ + /*chanDB.media.archived = chanDB.media.archived.filter((record)=>{ return record.uuid.toString() != uuid; - }) + })*/ //If nothing changed in the archive if(preArchive.length == chanDB.media.archived.length){ @@ -1741,30 +1743,28 @@ class queue{ //Create an empty array to hold our schedule let schedule = []; - //Get yestedays epoch - const yesterday = new Date().setDate(new Date().getDate() - 1); + //Get todays epoch + const yesterday = new Date(); + + //Make it yesterdays + yesterday.setDate(yesterday.getDate() - 1); + + //Get archive since yesterday + const archive = await this.getArchive(yesterday); //Iterate through the channel archive backwards to save time - for(let mediaIndex = chanDB.media.archived.length - 1; mediaIndex >= 0; mediaIndex--){ + for(let mediaIndex = archive.length - 1; mediaIndex >= 0; mediaIndex--){ //Grab the current media record - let media = chanDB.media.archived[mediaIndex].rehydrate(); + let media = archive[mediaIndex].rehydrate(); - //If the media started within the last 24 hours - if(media.startTime > yesterday){ - //If we're sending out the live remainder during a live stream - if(this.liveRemainder != null && media.uuid.toString() == this.liveRemainder.uuid.toString()){ - //Throw out the early end before sending it off, so it looks like it hasn't been cut off yet (smoke n mirrors :P) - media.earlyEnd = null; - } - - //Add it to the temporary schedule array as if it where part of the actual schedule map - schedule.unshift([media.startTime, media]); - //Otherwise if it's older - }else{ - //Then we should be done as archived items are added as they are played/end. - //No newer items should be beyond this point! - break; + //If we're sending out the live remainder during a live stream + if(this.liveRemainder != null && media.uuid.toString() == this.liveRemainder.uuid.toString()){ + //Throw out the early end before sending it off, so it looks like it hasn't been cut off yet (smoke n mirrors :P) + media.earlyEnd = null; } + + //Add it to the temporary schedule array as if it where part of the actual schedule map + schedule.unshift([media.startTime, media]); } //Concatonate the actual schedule to the items we pulled out of the archive return it @@ -1907,6 +1907,25 @@ class queue{ //Save archivedMedia object to site-wide media archive (gross but performant) let archivedDoc = await archivedMediaModel.create(archived); } + + /** + * Get Channel Archive + */ + async getArchive(startDate){ + //If we provided a start date + if(startDate != null){ + //Return archived media after start date + return await archivedMediaModel.find({ + channel:this.channel.name, + //Prevent mongoose from attempting to sanatize and invalidate our query object, plus I think we can trust an epoch that came from server-side :) + startTime: mongoose.trusted({$gt: startDate.getTime()}) + }); + //Otherwise + }else{ + //return all archived media + return await archivedMediaModel.find({channel:this.channel.name}); + } + } } module.exports = queue; \ No newline at end of file