Skip to content

Commit

Permalink
- Create single function for approval checks and another for limit ch…
Browse files Browse the repository at this point in the history
…ecks.

- Refactor movie request logic

Signed-off-by: Ricky Grassmuck <rigrassm@gmail.com>
  • Loading branch information
RickyGrassmuck committed May 24, 2018
1 parent 579883d commit b343437
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 107 deletions.
59 changes: 59 additions & 0 deletions server/methods/admin/approvalCheck.js
@@ -0,0 +1,59 @@
Meteor.methods({
// Pretty simple, pass a user and the request type and the function returns true if they need approval
// and false if they don't
'approvalCheck': function (user, requestType) {
var settings = Settings.find({}).fetch()
var movieApproval = settings.movieApproval
var tvApproval = settings.tvApproval

Label:if (requestType === 'movie') {
if (!movieApproval) {
{
return false
}
} else {
{
break Label
}
}
} else if (requestType === 'tv') {
if (!tvApproval) {
{
return false
}
} else {
{
break Label
}
}
}

if (settings.plexAuthenticationENABLED) {
return (Permissions.find({permUSER: user}).fetch()[0].permAPPROVAL)
} else{
return false
}
},

'limitCheck': function (user, requestType) {
var weeklyLimit = Settings.find({}).fetch()[0].movieWeeklyLimit
var userUnlimited = Permissions.find({permUSER: user}).fetch()[0].permLIMIT
if (Meteor.user() || userUnlimited || weeklyLimit) {
return false
}
if (requestType === 'movie') {
var userRequestTotal = Movies.find({
user: user,
createdAt: {'$gte': Date.now() - 6.048e8}
}).fetch().length
} else if (requestType === 'tv') {
userRequestTotal = TV.find({
user: user,
createdAt: {'$gte': Date.now() - 6.048e8}
}).fetch().length
} else {
userRequestTotal = 0
}
return (userRequestTotal >= weeklyLimit)
}
})
141 changes: 34 additions & 107 deletions server/methods/search/requestMovie.js
@@ -1,6 +1,5 @@
// TODO: Clean up the requestMovie method, much of the logic is duplicated throughout and can be simplified
// TODO: Define what exactly gets returned by each clients "add movie" method for uniform, predictable returns
// #td-done: Fix issue with not validating if movie exists in radarr

Meteor.methods({
'requestMovie': function(request) {
Expand All @@ -10,15 +9,15 @@ Meteor.methods({

request['notification_type'] = 'request'
request['media_type'] = 'Movie'
// Check user request limit
var date = Date.now() - 6.048e8
var weeklyLimit = Settings.find({}).fetch()[0].movieWeeklyLimit
var userRequestTotal = Movies.find({user:request.user, createdAt: {'$gte': date} }).fetch().length

// One function to rule them all!!
// Or just to simplify adding movies to the DB
function insertMovie(request, imdbid, dlStatus, approvalStatus, poster) {
function insertMovie(request, imdbid, dlStatus, approvalStatus, poster, service) {

try {
if (service === 'couchpotato'){
CouchPotato.movieAdd(imdbid)
} else if (service === 'radarr'){
Radarr.radarrMovieAdd(request, settings)
}
Movies.insert({
title: request.title,
id: request.id,
Expand All @@ -30,37 +29,30 @@ Meteor.methods({
approval_status: approvalStatus,
poster_path: poster
})
Meteor.call('sendNotifications', request)
return true
} catch (error) {
logger.error(error.message)
Plexrequests.log('error', error)
return false
}
return true
}

if (weeklyLimit !== 0
&& (userRequestTotal >= weeklyLimit)
&& !(Meteor.user())
//Check if user has override permission
&& (!settings.plexAuthenticationENABLED || !Permissions.find({permUSER: request.user}).fetch()[0].permLIMIT)) {

return 'limit'
}

// Movie Request only requires IMDB_ID
// Get IMDB ID
if (Meteor.call('limitCheck', request.user, 'movie')) {return 'limit'}

try {
var imdb = TMDBSearch.externalIds(request.id, 'movie')
if (imdb.indexOf('tt') === -1) {
logger.error(('Error getting IMDB ID, none found!'))
Plexrequests.log('error', 'Error getting IMDB ID, none found!')
return false
} else {
request['imdb'] = imdb
}
} catch (error) {
logger.error('Error getting IMDB ID:', error.message)
Plexrequests.log('error', 'Error getting IMDB ID:' + error)
return false
}

// Check if it already exists in CouchPotato
// If it exists, insert into our collection
if (settings.couchPotatoENABLED) {
try {
var checkCP = CouchPotato.mediaGet(imdb)
Expand All @@ -77,131 +69,66 @@ Meteor.methods({
}
}
} catch (error) {
logger.error('Error checking Couch Potato:', error.message)
Plexrequests.log('error', 'Error checking Couch Potato:' + error)
return false
}
}

// Check if it exists in Radarr
// Same deal here, check and bail on fail else continue on
if (settings.radarrENABLED) {
try {
// MovieGet returns false if not found or the data from radarr as a JSON object if it exists
// So here, just check if it's false and if so we can continue on
var checkRadarr = Radarr.radarrMovieGet(request.id)
logger.log('debug', 'Radarr Movie info: \n' + JSON.stringify(checkRadarr))
if (checkRadarr !== false) {
logger.log('info', 'Movie already present in Radarr')
Radarr.log('info', 'Movie already present in Radarr')
insertMovie(request, imdb, checkRadarr.downloaded, 1, poster)
// Using these values to set client states
// TODO: Look into alternate method for this
return 'exists'
}
} catch (error) {
logger.error('Error checking Radarr:', error.message)
Radarr.log('error', 'Error checking Radarr:' + error)
return false
}
}

/*
| Making it here means the media did not exist in either client so we proceed to handling the request
| based on the users permissions.
*/

// If approval needed and user does not have override permission
if (settings.movieApproval

//Check if user has override permission
&& (!settings.plexAuthenticationENABLED || !Permissions.find({permUSER: request.user}).fetch()[0].permAPPROVAL)) {
if (Meteor.call('approvalCheck', request.user, 'movie')){

// Approval required
// Add to DB but do NOT send to client
try {

var result = insertMovie(request, imdb, false, 0, poster)

} catch (error) {

logger.error(error.message)
return false

}

if (result) {
Meteor.call('sendNotifications', request)
insertMovie(request, imdb, false, 0, poster, null)
return true
}
else {
logger.error('Error sending notification')
} catch (error) {
Plexrequests.log('error', error)
return false
}

} else {
request['imdb'] = imdb
// No approval required
if (settings.couchPotatoENABLED) {

// Were sending the request to CP here
try {
var add = CouchPotato.movieAdd(imdb)
} catch (error) {
logger.error('Error adding to Couch Potato:', error.message)
return false
}

// If the request was successful, insert the movie into our collection
try {
if (add) {
result = insertMovie(request, imdb, false, 1, poster)
}
} catch (error) {

logger.error(error.message)
return false

}
// If we added to our collection successfully, were ready to tell the world!
if (result) {
Meteor.call('sendNotifications', request)
insertMovie(request, imdb, false, 1, poster, 'couchpotato')
return true
}
else {
logger.error('Error sending notification')
} catch (error) {
Plexrequests.log('error', 'Error adding to Couch Potato: ' + error)
return false
}

// Radarr's turn now
} else if (settings.radarrENABLED) {
// So, standard practice here, send the request to client then insert then notify.
try {
add = Radarr.radarrMovieAdd(request, settings)
} catch (error) {
logger.error('Error adding to Radarr:', error.message)
return false
}
if (add) {
try {
insertMovie(request, imdb, false, 1, poster)
} catch (error) {
logger.error(error.message)
return false
}
Meteor.call('sendNotifications', request)
insertMovie(request, imdb, false, 1, poster, 'radarr')
return true
} else {
} catch (error) {
Plexrequests.log('error', 'Error adding to Radarr: ' + error)
return false
}
} else {
// Nothing enabled but still add to DB

} else {
try {
insertMovie(request, imdb, false, 1, poster)
insertMovie(request, imdb, false, 1, poster, null)
return true
} catch (error) {
logger.error(error.message)
Plexrequests.log('error', error)
return false
}
Meteor.call('sendNotifications', request)
return true

}
}
}
Expand Down

0 comments on commit b343437

Please sign in to comment.