From d6f649f08c4a343b88a5fd6e64c5ff0a0f45b032 Mon Sep 17 00:00:00 2001 From: Michael Yogar Date: Sat, 24 Oct 2020 19:42:09 -0400 Subject: [PATCH] Created Account Library for frontend controller account.js for issue #228 --- controllers/frontend/account.js | 89 ++------------------------------- lib/account/helpers.js | 32 ++++++++++++ lib/account/order.js | 67 +++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 85 deletions(-) create mode 100644 lib/account/helpers.js create mode 100644 lib/account/order.js diff --git a/controllers/frontend/account.js b/controllers/frontend/account.js index 30082eae..09fd4f30 100644 --- a/controllers/frontend/account.js +++ b/controllers/frontend/account.js @@ -39,6 +39,8 @@ const { saveAndServeFilesDirectory } = require('../../lib/helpers/settings'); const { userCanUploadContentOfThisRating } = require('../../lib/uploading/helpers'); +const {getEnglishStringOrder, sortUploads} = require('../../lib/account/order'); + const validator = require('email-validator'); const { getUploadDuration } = require('../../lib/mediaBrowsing/helpers'); @@ -51,37 +53,6 @@ const timeAgoEnglish = new javascriptTimeAgo('en-US'); const secondsToFormattedTime = timeHelper.secondsToFormattedTime; -// TODO: pull this function out -async function addValuesIfNecessary(upload, channelUrl){ - if(upload.fileType == 'video' || upload.fileType == 'audio'){ - if(!upload.durationInSeconds || !upload.formattedDuration){ - - var server = uploadServer; - if(server.charAt(0) == '/') // the slash confuses the file reading, because host root directory is not the same as machine root directory - server = server.substr(1); - - const uploadLocation = `${server}/${channelUrl}/${upload.uniqueTag + upload.fileExtension}`; - - try { - const duration = await getUploadDuration(uploadLocation, upload.fileType); - console.log(duration); - - let uploadDocument = await Upload.findOne({uniqueTag: upload.uniqueTag}); - - uploadDocument.durationInSeconds = duration.seconds; - uploadDocument.formattedDuration = duration.formattedTime; - - await uploadDocument.save(); - - } catch(err){ - /** if the file has been deleted then it won't blow up **/ - // console.log(err); - } - // console.log('have to add'); - } - } -} - /** * GET /upload * Page to facilitate user uploads @@ -389,30 +360,7 @@ exports.getChannel = async(req, res) => { orderBy = req.query.orderBy; } - // console.log(`orderBy : ${orderBy}`) - - if(orderBy !== 'popular' && orderBy !== 'newToOld' && orderBy !== 'oldToNew' && orderBy !== 'alphabetical'){ - console.log('doesnt connect'); - orderBy = 'newToOld'; - } - - let orderByEnglishString; - - if(orderBy == 'alphabetical'){ - orderByEnglishString = 'Alphabetical'; - } - - if(orderBy == 'oldToNew'){ - orderByEnglishString = 'Old To New'; - } - - if(orderBy == 'newToOld'){ - orderByEnglishString = 'New To Old'; - } - - if(orderBy == 'popular'){ - orderByEnglishString = 'Popular'; - } + let orderByEnglishString = getEnglishStringOrder(orderBy); let alreadySubbed = false; @@ -445,30 +393,7 @@ exports.getChannel = async(req, res) => { const userUploadAmount = uploads.length; - if(orderBy == 'newToOld'){ - - // console.log('new to old'); - uploads = uploads.sort(function(a, b){ - return b.createdAt - a.createdAt; - }); - } - - if(orderBy == 'oldToNew'){ - - // console.log('old to new'); - uploads = uploads.sort(function(a, b){ - return a.createdAt - b.createdAt; - }); - } - - if(orderBy == 'alphabetical'){ - - // console.log('alphabetical'); - - uploads = uploads.sort(function(a, b){ - return a.title.localeCompare(b.title); - }); - } + uploads = sortUploads(orderBy, uploads); let filter = uploadFilters.getSensitivityFilter(req.user, req.siteVisitor); @@ -488,12 +413,6 @@ exports.getChannel = async(req, res) => { }) ); - if(orderBy == 'popular'){ - uploads = uploads.sort(function(a, b){ - return b.legitViewAmount - a.legitViewAmount; - }); - } - let totalViews = 0; for(upload of uploads){ totalViews = totalViews + upload.legitViewAmount; diff --git a/lib/account/helpers.js b/lib/account/helpers.js new file mode 100644 index 00000000..0d64c806 --- /dev/null +++ b/lib/account/helpers.js @@ -0,0 +1,32 @@ +async function addValuesIfNecessary(upload, channelUrl){ + if(upload.fileType == 'video' || upload.fileType == 'audio'){ + if(!upload.durationInSeconds || !upload.formattedDuration){ + + var server = uploadServer; + if(server.charAt(0) == '/') // the slash confuses the file reading, because host root directory is not the same as machine root directory + server = server.substr(1); + + const uploadLocation = `${server}/${channelUrl}/${upload.uniqueTag + upload.fileExtension}`; + + try { + const duration = await getUploadDuration(uploadLocation, upload.fileType); + console.log(duration); + + let uploadDocument = await Upload.findOne({uniqueTag: upload.uniqueTag}); + + uploadDocument.durationInSeconds = duration.seconds; + uploadDocument.formattedDuration = duration.formattedTime; + + await uploadDocument.save(); + + } catch(err){ + /** if the file has been deleted then it won't blow up **/ + // console.log(err); + } + // console.log('have to add'); + } + } +} + + +module.exports = addValuesIfNecessary; \ No newline at end of file diff --git a/lib/account/order.js b/lib/account/order.js new file mode 100644 index 00000000..22cb4302 --- /dev/null +++ b/lib/account/order.js @@ -0,0 +1,67 @@ +function getEnglishStringOrder(orderBy){ + + let orderByEnglishString; + + if(orderBy == 'alphabetical'){ + orderByEnglishString = 'Alphabetical'; + } + + if(orderBy == 'oldToNew'){ + orderByEnglishString = 'Old To New'; + } + + if(orderBy == 'newToOld'){ + orderByEnglishString = 'New To Old'; + } + + if(orderBy == 'popular'){ + orderByEnglishString = 'Popular'; + } + + return orderByEnglishString; +} + +function sortUploads(orderBy, unsortedUploads) { + + let uploads; + + if(orderBy == 'newToOld'){ + + // console.log('new to old'); + uploads = unsortedUploads.sort(function(a, b){ + return b.createdAt - a.createdAt; + }); + } + + if(orderBy == 'oldToNew'){ + + // console.log('old to new'); + uploads = unsortedUploads.sort(function(a, b){ + return a.createdAt - b.createdAt; + }); + } + + if(orderBy == 'alphabetical'){ + + // console.log('alphabetical'); + + uploads = unsortedUploads.sort(function(a, b){ + return a.title.localeCompare(b.title); + }); + } + + if(orderBy == 'popular'){ + uploads = unsortedUploads.sort(function(a, b){ + return b.legitViewAmount - a.legitViewAmount; + }); + } + + return uploads; + +} + + +module.exports = { + getEnglishStringOrder: getEnglishStringOrder, + sortUploads : sortUploads +} \ No newline at end of file