From 7094aa2bb516fb8ab2481315ffdf16115622643f Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Tue, 16 Aug 2022 01:15:36 +0200 Subject: [PATCH] Updated all imports to be relative to itself --- seasoned_api/src/config/configuration.js | 70 +++--- seasoned_api/src/database/database.js | 9 +- seasoned_api/src/database/schemas/setup.sql | 2 +- seasoned_api/src/database/sqliteDatabase.js | 147 ++++++------ seasoned_api/src/media_classes/plex.js | 37 +-- seasoned_api/src/media_classes/tmdb.js | 59 +++-- seasoned_api/src/notifications/sms.js | 50 ++-- seasoned_api/src/pirate/pirateRepository.js | 4 +- seasoned_api/src/plex/convertPlexToEpisode.js | 10 +- seasoned_api/src/plex/convertPlexToMovie.js | 4 +- .../src/plex/convertPlexToSeasoned.js | 40 ++-- seasoned_api/src/plex/convertPlexToShow.js | 2 +- seasoned_api/src/plex/convertPlexToStream.js | 24 +- .../src/plex/convertStreamToMediaInfo.js | 28 +-- seasoned_api/src/plex/hookDump.js | 4 +- seasoned_api/src/plex/plex.js | 10 +- seasoned_api/src/plex/plexRepository.js | 182 ++++++++------- seasoned_api/src/plex/requestRepository.js | 8 +- .../src/plex/stream/convertStreamToPlayer.js | 14 +- .../src/plex/stream/convertStreamToUser.js | 4 +- seasoned_api/src/request/request.js | 218 +++++++++++------- .../src/searchHistory/searchHistory.js | 75 +++--- seasoned_api/src/seasoned/strayRepository.js | 112 ++++----- seasoned_api/src/tmdb/cache.js | 78 ++++--- seasoned_api/src/tmdb/tmdb.js | 28 +-- seasoned_api/src/user/token.js | 2 +- seasoned_api/src/user/userRepository.js | 2 +- seasoned_api/src/user/userSecurity.js | 2 +- seasoned_api/src/webserver/app.js | 2 +- .../src/webserver/controllers/git/dumpHook.js | 17 +- .../controllers/list/listController.js | 75 +++--- .../webserver/controllers/movie/credits.js | 24 +- .../src/webserver/controllers/movie/info.js | 6 +- .../controllers/movie/releaseDates.js | 24 +- .../webserver/controllers/person/credits.js | 4 +- .../src/webserver/controllers/person/info.js | 4 +- .../webserver/controllers/pirate/addMagnet.js | 33 ++- .../controllers/pirate/searchTheBay.js | 2 +- .../controllers/plex/fetchRequested.js | 23 +- .../webserver/controllers/plex/plexPlaying.js | 21 +- .../webserver/controllers/plex/readRequest.js | 20 +- .../src/webserver/controllers/plex/search.js | 35 +-- .../webserver/controllers/plex/searchMedia.js | 36 +-- .../controllers/plex/searchRequest.js | 6 +- .../controllers/plex/submitRequest.js | 16 +- .../controllers/plex/updateRequested.js | 23 +- .../controllers/plex/watchDirectLink.js | 20 +- .../controllers/request/fetchAllRequests.js | 16 +- .../controllers/request/getRequest.js | 5 +- .../controllers/request/requestTmdbId.js | 6 +- .../controllers/search/movieSearch.js | 6 +- .../controllers/search/multiSearch.js | 14 +- .../controllers/search/personSearch.js | 6 +- .../controllers/search/showSearch.js | 6 +- .../controllers/seasoned/readStrays.js | 20 +- .../controllers/seasoned/strayById.js | 19 +- .../controllers/seasoned/verifyStray.js | 19 +- .../src/webserver/controllers/show/credits.js | 23 +- .../src/webserver/controllers/show/info.js | 49 ++-- .../user/authenticatePlexAccount.js | 2 +- .../src/webserver/controllers/user/login.js | 10 +- .../webserver/controllers/user/register.js | 10 +- .../webserver/controllers/user/requests.js | 2 +- .../controllers/user/searchHistory.js | 2 +- .../webserver/controllers/user/settings.js | 2 +- .../webserver/controllers/user/viewHistory.js | 4 +- .../src/webserver/middleware/mustBeAdmin.js | 42 ++-- .../middleware/mustHaveAccountLinkedToPlex.js | 50 ++-- .../webserver/middleware/reqTokenToUser.js | 4 +- seasoned_api/src/webserver/server.js | 18 +- 70 files changed, 1066 insertions(+), 885 deletions(-) diff --git a/seasoned_api/src/config/configuration.js b/seasoned_api/src/config/configuration.js index 0e8988c..0682081 100644 --- a/seasoned_api/src/config/configuration.js +++ b/seasoned_api/src/config/configuration.js @@ -1,43 +1,49 @@ -const path = require('path'); -const Field = require('./field.js'); +const path = require("path"); +const Field = require("./field.js"); let instance = null; class Config { - constructor() { - this.location = Config.determineLocation(); - this.fields = require(`${this.location}`); - } + constructor() { + this.location = Config.determineLocation(); + this.fields = require(`${this.location}`); + } - static getInstance() { - if (instance == null) { - instance = new Config(); + static getInstance() { + if (instance == null) { + instance = new Config(); + } + return instance; + } + + static determineLocation() { + return path.join(__dirname, "..", "..", process.env.SEASONED_CONFIG); + } + + get(section, option) { + if ( + this.fields[section] === undefined || + this.fields[section][option] === undefined + ) { + throw new Error(`Field "${section} => ${option}" does not exist.`); + } + + const field = new Field(this.fields[section][option]); + + if (field.value === "") { + const envField = + process.env[[section.toUpperCase(), option.toUpperCase()].join("_")]; + if (envField !== undefined && envField.length !== 0) { + return envField; } - return instance; - } + } - static determineLocation() { - return path.join(__dirname, '..', '..', process.env.SEASONED_CONFIG); - } + if (field.value === undefined) { + throw new Error(`${section} => ${option} is empty.`); + } - get(section, option) { - if (this.fields[section] === undefined || this.fields[section][option] === undefined) { - throw new Error(`Field "${section} => ${option}" does not exist.`); - } - - const field = new Field(this.fields[section][option]); - - if (field.value === '') { - const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')]; - if (envField !== undefined && envField.length !== 0) { return envField; } - } - - if (field.value === undefined) { - throw new Error(`${section} => ${option} is empty.`); - } - - return field.value; - } + return field.value; + } } module.exports = Config; diff --git a/seasoned_api/src/database/database.js b/seasoned_api/src/database/database.js index a696e7f..e00bb70 100644 --- a/seasoned_api/src/database/database.js +++ b/seasoned_api/src/database/database.js @@ -1,14 +1,13 @@ -const configuration = require('src/config/configuration').getInstance(); -const SqliteDatabase = require('src/database/sqliteDatabase'); +const configuration = require("../config/configuration").getInstance(); +const SqliteDatabase = require("./sqliteDatabase"); -const database = new SqliteDatabase(configuration.get('database', 'host')); +const database = new SqliteDatabase(configuration.get("database", "host")); /** * This module establishes a connection to the database * specified in the confgiuration file. It tries to setup * the required tables after successfully connecting. * If the tables already exists, it simply proceeds. */ -Promise.resolve() - .then(() => database.setUp()); +Promise.resolve().then(() => database.setUp()); module.exports = database; diff --git a/seasoned_api/src/database/schemas/setup.sql b/seasoned_api/src/database/schemas/setup.sql index 7099b1c..7ae8b9b 100644 --- a/seasoned_api/src/database/schemas/setup.sql +++ b/seasoned_api/src/database/schemas/setup.sql @@ -30,7 +30,7 @@ CREATE TABLE IF NOT EXISTS search_history ( foreign key(user_name) REFERENCES user(user_name) ON DELETE CASCADE ); -CREATE TABLE IF NOT EXISTS requests( +CREATE TABLE IF NOT EXISTS requests ( id NUMBER, title TEXT, year NUMBER, diff --git a/seasoned_api/src/database/sqliteDatabase.js b/seasoned_api/src/database/sqliteDatabase.js index fc5459e..a330487 100644 --- a/seasoned_api/src/database/sqliteDatabase.js +++ b/seasoned_api/src/database/sqliteDatabase.js @@ -1,120 +1,119 @@ -const fs = require('fs'); -const path = require('path'); -const sqlite3 = require('sqlite3').verbose(); +const fs = require("fs"); +const path = require("path"); +const sqlite3 = require("sqlite3").verbose(); class SqliteDatabase { - constructor(host) { - this.host = host; - this.connection = new sqlite3.Database(this.host); - this.execute('pragma foreign_keys = on;'); - this.schemaDirectory = path.join(__dirname, 'schemas'); - } + constructor(host) { + this.host = host; + this.connection = new sqlite3.Database(this.host); + this.execute("pragma foreign_keys = on;"); + this.schemaDirectory = path.join(__dirname, "schemas"); + } - /** + /** * Connect to the database. * @returns {Promise} succeeds if connection was established */ - // connect() { - // let database = ; - // this.connection = database; - // return database; - // } + // connect() { + // let database = ; + // this.connection = database; + // return database; + // } - /** + /** * Run a SQL query against the database. * @param {String} sql SQL query * @param {Array} parameters in the SQL query * @returns {Promise} */ - run(sql, parameters) { - return new Promise((resolve, reject) => { - this.connection.run(sql, parameters, (error, result) => { - if (error) - reject(error); - resolve(result) - }); + run(sql, parameters) { + return new Promise((resolve, reject) => { + this.connection.run(sql, parameters, (error, result) => { + if (error) reject(error); + resolve(result); }); - } + }); + } - /** + /** * Run a SQL query against the database and retrieve all the rows. * @param {String} sql SQL query * @param {Array} parameters in the SQL query * @returns {Promise} */ - all(sql, parameters) { - return new Promise((resolve, reject) => { - this.connection.all(sql, parameters, (err, rows) => { - if (err) { - reject(err); - } - resolve(rows); - }) - }) - } + all(sql, parameters) { + return new Promise((resolve, reject) => { + this.connection.all(sql, parameters, (err, rows) => { + if (err) { + reject(err); + } + resolve(rows); + }); + }); + } - /** + /** * Run a SQL query against the database and retrieve one row. * @param {String} sql SQL query * @param {Array} parameters in the SQL query * @returns {Promise} */ - get(sql, parameters) { - return new Promise((resolve, reject) => { - this.connection.get(sql, parameters, (err, rows) => { - if (err) { - reject(err); - } - resolve(rows); - }) - }) - } + get(sql, parameters) { + return new Promise((resolve, reject) => { + this.connection.get(sql, parameters, (err, rows) => { + if (err) { + reject(err); + } + resolve(rows); + }); + }); + } - /** + /** * Run a SQL query against the database and retrieve the status. * @param {String} sql SQL query * @param {Array} parameters in the SQL query * @returns {Promise} */ - execute(sql) { - return new Promise(resolve => { - this.connection.exec(sql, (err, database) => { - if (err) { - console.log('ERROR: ', err); - reject(err); - } - resolve(); - }) - }) - } + execute(sql) { + return new Promise(resolve => { + this.connection.exec(sql, (err, database) => { + if (err) { + console.log("ERROR: ", err); + reject(err); + } + resolve(); + }); + }); + } - /** + /** * Setup the database by running setup.sql file in schemas/. * @returns {Promise} */ - setUp() { - const setupSchema = this.readSqlFile('setup.sql'); - return Promise.resolve(this.execute(setupSchema)); - } + setUp() { + const setupSchema = this.readSqlFile("setup.sql"); + return Promise.resolve(this.execute(setupSchema)); + } - /** + /** * Tears down the database by running tearDown.sql file in schemas/. * @returns {Promise} */ - tearDown() { - const tearDownSchema = this.readSqlFile('teardown.sql'); - return Promise.resolve(this.execute(tearDownSchema)); - } + tearDown() { + const tearDownSchema = this.readSqlFile("teardown.sql"); + return Promise.resolve(this.execute(tearDownSchema)); + } - /** + /** * Returns the file contents of a SQL file in schemas/. * @returns {String} */ - readSqlFile(filename) { - const schemaPath = path.join(this.schemaDirectory, filename); - const schema = fs.readFileSync(schemaPath).toString('utf-8'); - return schema; - } + readSqlFile(filename) { + const schemaPath = path.join(this.schemaDirectory, filename); + const schema = fs.readFileSync(schemaPath).toString("utf-8"); + return schema; + } } module.exports = SqliteDatabase; diff --git a/seasoned_api/src/media_classes/plex.js b/seasoned_api/src/media_classes/plex.js index 57528b1..2df0f77 100644 --- a/seasoned_api/src/media_classes/plex.js +++ b/seasoned_api/src/media_classes/plex.js @@ -1,22 +1,31 @@ - -const Media = require('src/media_classes/media'); +const Media = require("./media"); class Plex extends Media { - constructor(title, year, type, summary, poster_path, background_path, added, seasons, episodes) { - super(title, year, type); + constructor( + title, + year, + type, + summary, + poster_path, + background_path, + added, + seasons, + episodes + ) { + super(title, year, type); - this.summary = summary; - this.poster_path = poster_path; - this.background_path = background_path; - this.added = added; + this.summary = summary; + this.poster_path = poster_path; + this.background_path = background_path; + this.added = added; - this.seasons = seasons; - this.episodes = episodes; - } + this.seasons = seasons; + this.episodes = episodes; + } - print() { - super.print(); - } + print() { + super.print(); + } } module.exports = Plex; diff --git a/seasoned_api/src/media_classes/tmdb.js b/seasoned_api/src/media_classes/tmdb.js index c1a7dc6..724ac86 100644 --- a/seasoned_api/src/media_classes/tmdb.js +++ b/seasoned_api/src/media_classes/tmdb.js @@ -1,33 +1,46 @@ - -const Media = require('src/media_classes/media'); +const Media = require("./media"); class TMDB extends Media { - // constructor(...args) { - constructor(title, year, type, id, summary, poster_path, background_path, popularity, score, release_status, tagline, seasons, episodes) { - super(title, year, type); + // constructor(...args) { + constructor( + title, + year, + type, + id, + summary, + poster_path, + background_path, + popularity, + score, + release_status, + tagline, + seasons, + episodes + ) { + super(title, year, type); - this.id = id; - this.summary = summary; - this.poster_path = poster_path; - this.background_path = background_path; - this.popularity = popularity; - this.score = score; + this.id = id; + this.summary = summary; + this.poster_path = poster_path; + this.background_path = background_path; + this.popularity = popularity; + this.score = score; - this.release_status = release_status; - this.tagline = tagline; + this.release_status = release_status; + this.tagline = tagline; - this.seasons = seasons; - this.episodes = episodes; - } + this.seasons = seasons; + this.episodes = episodes; + } - toString() { - return `${super.toString()} | ID: ${this.id}`; - } + toString() { + return `${super.toString()} | ID: ${this.id}`; + } - print() { - /* eslint-disable no-console */ - console.log(this.toString()); - } + print() { + /* eslint-disable no-console */ + console.log(this.toString()); + } } module.exports = TMDB; diff --git a/seasoned_api/src/notifications/sms.js b/seasoned_api/src/notifications/sms.js index fe81c42..7e78b05 100644 --- a/seasoned_api/src/notifications/sms.js +++ b/seasoned_api/src/notifications/sms.js @@ -1,35 +1,35 @@ const request = require("request"); -const configuration = require('src/config/configuration').getInstance(); +const configuration = require("../config/configuration").getInstance(); -const sendSMS = (message) => { - const apiKey = configuration.get('sms', 'apikey') +const sendSMS = message => { + const apiKey = configuration.get("sms", "apikey"); if (!apiKey) { - console.warning("api key for sms not set, cannot send sms.") - return null + console.warning("api key for sms not set, cannot send sms."); + return null; } - const sender = configuration.get('sms', 'sender') - const recipient = configuration.get('sms', 'recipient') + const sender = configuration.get("sms", "sender"); + const recipient = configuration.get("sms", "recipient"); return new Promise((resolve, reject) => { - request.post( - { - url: `https://gatewayapi.com/rest/mtsms?token=${apiKey}`, - json: true, - body: { - sender, - message, - recipients: [{ msisdn: `47${recipient}` }] + request.post( + { + url: `https://gatewayapi.com/rest/mtsms?token=${apiKey}`, + json: true, + body: { + sender, + message, + recipients: [{ msisdn: `47${recipient}` }] + } + }, + function (err, r, body) { + console.log(err ? err : body); + console.log("sms provider response:", body); + resolve(); } - }, - function(err, r, body) { - console.log(err ? err : body); - console.log("sms provider response:", body) - resolve() - } - ); - }) -} + ); + }); +}; -module.exports = { sendSMS } +module.exports = { sendSMS }; diff --git a/seasoned_api/src/pirate/pirateRepository.js b/seasoned_api/src/pirate/pirateRepository.js index 9827179..c2561b1 100644 --- a/seasoned_api/src/pirate/pirateRepository.js +++ b/seasoned_api/src/pirate/pirateRepository.js @@ -3,9 +3,9 @@ const http = require("http"); const { URL } = require("url"); const PythonShell = require("python-shell"); -const establishedDatabase = require("src/database/database"); +const establishedDatabase = require("../database/database"); -const RedisCache = require("src/cache/redis"); +const RedisCache = require("../cache/redis"); const cache = new RedisCache(); function getMagnetFromURL(url) { diff --git a/seasoned_api/src/plex/convertPlexToEpisode.js b/seasoned_api/src/plex/convertPlexToEpisode.js index a85b5d2..398724f 100644 --- a/seasoned_api/src/plex/convertPlexToEpisode.js +++ b/seasoned_api/src/plex/convertPlexToEpisode.js @@ -1,7 +1,11 @@ -const Episode = require('src/plex/types/episode'); +const Episode = require("./types/episode"); function convertPlexToEpisode(plexEpisode) { - const episode = new Episode(plexEpisode.title, plexEpisode.grandparentTitle, plexEpisode.year); + const episode = new Episode( + plexEpisode.title, + plexEpisode.grandparentTitle, + plexEpisode.year + ); episode.season = plexEpisode.parentIndex; episode.episode = plexEpisode.index; episode.summary = plexEpisode.summary; @@ -12,7 +16,7 @@ function convertPlexToEpisode(plexEpisode) { } if (plexEpisode.originallyAvailableAt !== undefined) { - episode.airdate = new Date(plexEpisode.originallyAvailableAt) + episode.airdate = new Date(plexEpisode.originallyAvailableAt); } return episode; diff --git a/seasoned_api/src/plex/convertPlexToMovie.js b/seasoned_api/src/plex/convertPlexToMovie.js index b41a887..96c6882 100644 --- a/seasoned_api/src/plex/convertPlexToMovie.js +++ b/seasoned_api/src/plex/convertPlexToMovie.js @@ -1,10 +1,10 @@ -const Movie = require('src/plex/types/movie'); +const Movie = require("./types/movie"); function convertPlexToMovie(plexMovie) { const movie = new Movie(plexMovie.title, plexMovie.year); movie.rating = plexMovie.rating; movie.tagline = plexMovie.tagline; - + if (plexMovie.summary !== undefined) { movie.summary = plexMovie.summary; } diff --git a/seasoned_api/src/plex/convertPlexToSeasoned.js b/seasoned_api/src/plex/convertPlexToSeasoned.js index 023c6f4..148fe7c 100644 --- a/seasoned_api/src/plex/convertPlexToSeasoned.js +++ b/seasoned_api/src/plex/convertPlexToSeasoned.js @@ -1,24 +1,34 @@ -const Plex = require('src/media_classes/plex'); +const Plex = require("../media_classes/plex"); function translateAdded(date_string) { - return new Date(date_string * 1000); + return new Date(date_string * 1000); } function convertPlexToSeasoned(plex) { - const title = plex.title; - const year = plex.year; - const type = plex.type; - const summary = plex.summary; - const poster_path = plex.thumb; - const background_path = plex.art; - const added = translateAdded(plex.addedAt); - // const genre = plex.genre; - const seasons = plex.childCount; - const episodes = plex.leafCount; + const title = plex.title; + const year = plex.year; + const type = plex.type; + const summary = plex.summary; + const poster_path = plex.thumb; + const background_path = plex.art; + const added = translateAdded(plex.addedAt); + // const genre = plex.genre; + const seasons = plex.childCount; + const episodes = plex.leafCount; - const seasoned = new Plex(title, year, type, summary, poster_path, background_path, added, seasons, episodes); - // seasoned.print(); - return seasoned; + const seasoned = new Plex( + title, + year, + type, + summary, + poster_path, + background_path, + added, + seasons, + episodes + ); + // seasoned.print(); + return seasoned; } module.exports = convertPlexToSeasoned; diff --git a/seasoned_api/src/plex/convertPlexToShow.js b/seasoned_api/src/plex/convertPlexToShow.js index 04e337a..dc51474 100644 --- a/seasoned_api/src/plex/convertPlexToShow.js +++ b/seasoned_api/src/plex/convertPlexToShow.js @@ -1,4 +1,4 @@ -const Show = require('src/plex/types/show'); +const Show = require("./types/show"); function convertPlexToShow(plexShow) { const show = new Show(plexShow.title, plexShow.year); diff --git a/seasoned_api/src/plex/convertPlexToStream.js b/seasoned_api/src/plex/convertPlexToStream.js index f6ba3ef..3255cc7 100644 --- a/seasoned_api/src/plex/convertPlexToStream.js +++ b/seasoned_api/src/plex/convertPlexToStream.js @@ -1,19 +1,19 @@ -const convertPlexToSeasoned = require('src/plex/convertPlexToSeasoned'); -const convertStreamToMediaInfo = require('src/plex/convertStreamToMediaInfo'); -const convertStreamToPlayer = require('src/plex/stream/convertStreamToPlayer'); -const convertStreamToUser = require('src/plex/stream/convertStreamToUser'); -const ConvertStreamToPlayback = require('src/plex/stream/convertStreamToPlayback'); +const convertPlexToSeasoned = require("./convertPlexToSeasoned"); +const convertStreamToMediaInfo = require("./convertStreamToMediaInfo"); +const convertStreamToPlayer = require("./stream/convertStreamToPlayer"); +const convertStreamToUser = require("./stream/convertStreamToUser"); +const ConvertStreamToPlayback = require("./stream/convertStreamToPlayback"); function convertPlexToStream(plexStream) { - const stream = convertPlexToSeasoned(plexStream); - const plexStreamMedia = plexStream.Media[0]; - stream.mediaInfo = convertStreamToMediaInfo(plexStreamMedia); - stream.player = convertStreamToPlayer(plexStream.Player); + const stream = convertPlexToSeasoned(plexStream); + const plexStreamMedia = plexStream.Media[0]; + stream.mediaInfo = convertStreamToMediaInfo(plexStreamMedia); + stream.player = convertStreamToPlayer(plexStream.Player); - stream.user = convertStreamToUser(plexStream.User); - stream.playback = new ConvertStreamToPlayback(plexStreamMedia.Part[0]); + stream.user = convertStreamToUser(plexStream.User); + stream.playback = new ConvertStreamToPlayback(plexStreamMedia.Part[0]); - return stream; + return stream; } module.exports = convertPlexToStream; diff --git a/seasoned_api/src/plex/convertStreamToMediaInfo.js b/seasoned_api/src/plex/convertStreamToMediaInfo.js index ca75776..e68f9f8 100644 --- a/seasoned_api/src/plex/convertStreamToMediaInfo.js +++ b/seasoned_api/src/plex/convertStreamToMediaInfo.js @@ -1,22 +1,22 @@ -const MediaInfo = require('src/media_classes/mediaInfo'); +const MediaInfo = require("../media_classes/mediaInfo"); function convertStreamToMediaInfo(plexStream) { - const mediaInfo = new MediaInfo(); + const mediaInfo = new MediaInfo(); - mediaInfo.duration = plexStream.duration; - mediaInfo.height = plexStream.height; - mediaInfo.width = plexStream.width; + mediaInfo.duration = plexStream.duration; + mediaInfo.height = plexStream.height; + mediaInfo.width = plexStream.width; - if (plexStream.bitrate) { - mediaInfo.bitrate = plexStream.bitrate; - } - mediaInfo.resolution = plexStream.videoResolution; - mediaInfo.framerate = plexStream.videoFrameRate; - mediaInfo.protocol = plexStream.protocol; - mediaInfo.container = plexStream.container; - mediaInfo.audioCodec = plexStream.audioCodec; + if (plexStream.bitrate) { + mediaInfo.bitrate = plexStream.bitrate; + } + mediaInfo.resolution = plexStream.videoResolution; + mediaInfo.framerate = plexStream.videoFrameRate; + mediaInfo.protocol = plexStream.protocol; + mediaInfo.container = plexStream.container; + mediaInfo.audioCodec = plexStream.audioCodec; - return mediaInfo; + return mediaInfo; } module.exports = convertStreamToMediaInfo; diff --git a/seasoned_api/src/plex/hookDump.js b/seasoned_api/src/plex/hookDump.js index 23cecf4..5a34b2e 100644 --- a/seasoned_api/src/plex/hookDump.js +++ b/seasoned_api/src/plex/hookDump.js @@ -1,7 +1,7 @@ -const configuration = require('src/config/configuration').getInstance(); +const configuration = require("../config/configuration").getInstance(); function hookDumpController(req, res) { - console.log(req); + console.log(req); } module.exports = hookDumpController; diff --git a/seasoned_api/src/plex/plex.js b/seasoned_api/src/plex/plex.js index 9a1af74..c42af9a 100644 --- a/seasoned_api/src/plex/plex.js +++ b/seasoned_api/src/plex/plex.js @@ -1,11 +1,11 @@ const fetch = require("node-fetch"); -const convertPlexToMovie = require("src/plex/convertPlexToMovie"); -const convertPlexToShow = require("src/plex/convertPlexToShow"); -const convertPlexToEpisode = require("src/plex/convertPlexToEpisode"); +const convertPlexToMovie = require("./convertPlexToMovie"); +const convertPlexToShow = require("./convertPlexToShow"); +const convertPlexToEpisode = require("./convertPlexToEpisode"); -const { Movie, Show, Person } = require("src/tmdb/types"); +const { Movie, Show, Person } = require("../tmdb/types"); -const RedisCache = require("src/cache/redis"); +const RedisCache = require("../cache/redis"); const redisCache = new RedisCache(); const sanitize = string => string.toLowerCase().replace(/[^\w]/gi, ""); diff --git a/seasoned_api/src/plex/plexRepository.js b/seasoned_api/src/plex/plexRepository.js index 8f56695..4433fa2 100644 --- a/seasoned_api/src/plex/plexRepository.js +++ b/seasoned_api/src/plex/plexRepository.js @@ -1,102 +1,110 @@ -const convertPlexToSeasoned = require('src/plex/convertPlexToSeasoned'); -const convertPlexToStream = require('src/plex/convertPlexToStream'); -const rp = require('request-promise'); +const convertPlexToSeasoned = require("./convertPlexToSeasoned"); +const convertPlexToStream = require("./convertPlexToStream"); +const rp = require("request-promise"); class PlexRepository { - constructor(plexIP) { + constructor(plexIP) { this.plexIP = plexIP; - } + } - inPlex(tmdbResult) { - return Promise.resolve() - .then(() => this.search(tmdbResult.title)) - .then(plexResult => this.compareTmdbToPlex(tmdbResult, plexResult)) - .catch((error) => { - console.log(error) - tmdbResult.matchedInPlex = false; - return tmdbResult; - }); - } + inPlex(tmdbResult) { + return Promise.resolve() + .then(() => this.search(tmdbResult.title)) + .then(plexResult => this.compareTmdbToPlex(tmdbResult, plexResult)) + .catch(error => { + console.log(error); + tmdbResult.matchedInPlex = false; + return tmdbResult; + }); + } - search(query) { - const queryUri = encodeURIComponent(query) - const uri = encodeURI(`http://${this.plexIP}:32400/search?query=${queryUri}`) - const options = { - uri: uri, - headers: { - Accept: 'application/json', - }, - json: true, - }; + search(query) { + const queryUri = encodeURIComponent(query); + const uri = encodeURI( + `http://${this.plexIP}:32400/search?query=${queryUri}` + ); + const options = { + uri: uri, + headers: { + Accept: "application/json" + }, + json: true + }; - return rp(options) - .catch((error) => { - console.log(error) - throw new Error('Unable to search plex.') - }) - .then(result => this.mapResults(result)) - .then(([mappedResults, resultCount]) => ({ results: mappedResults, total_results: resultCount })); - } + return rp(options) + .catch(error => { + console.log(error); + throw new Error("Unable to search plex."); + }) + .then(result => this.mapResults(result)) + .then(([mappedResults, resultCount]) => ({ + results: mappedResults, + total_results: resultCount + })); + } - compareTmdbToPlex(tmdb, plexResult) { - return Promise.resolve() - .then(() => { - if (plexResult.results.length === 0) { - tmdb.matchedInPlex = false - } - else { - // console.log('plex and tmdb:', plexResult, '\n', tmdb) - plexResult.results.map((plexItem) => { - if (tmdb.title === plexItem.title && tmdb.year === plexItem.year) - tmdb.matchedInPlex = true; - return tmdb; - }); - } - return tmdb; - }); - } + compareTmdbToPlex(tmdb, plexResult) { + return Promise.resolve().then(() => { + if (plexResult.results.length === 0) { + tmdb.matchedInPlex = false; + } else { + // console.log('plex and tmdb:', plexResult, '\n', tmdb) + plexResult.results.map(plexItem => { + if (tmdb.title === plexItem.title && tmdb.year === plexItem.year) + tmdb.matchedInPlex = true; + return tmdb; + }); + } + return tmdb; + }); + } - mapResults(response) { - return Promise.resolve() - .then(() => { - if (!response.MediaContainer.hasOwnProperty('Metadata')) return [[], 0]; + mapResults(response) { + return Promise.resolve() + .then(() => { + if (!response.MediaContainer.hasOwnProperty("Metadata")) return [[], 0]; - const mappedResults = response.MediaContainer.Metadata.filter((element) => { - return (element.type === 'movie' || element.type === 'show'); - }).map((element) => convertPlexToSeasoned(element)); - return [mappedResults, mappedResults.length]; - }) - .catch((error) => { throw new Error(error); }); - } + const mappedResults = response.MediaContainer.Metadata.filter( + element => { + return element.type === "movie" || element.type === "show"; + } + ).map(element => convertPlexToSeasoned(element)); + return [mappedResults, mappedResults.length]; + }) + .catch(error => { + throw new Error(error); + }); + } - nowPlaying() { - const options = { - uri: `http://${this.plexIP}:32400/status/sessions`, - headers: { - Accept: 'application/json', - }, - json: true, - }; + nowPlaying() { + const options = { + uri: `http://${this.plexIP}:32400/status/sessions`, + headers: { + Accept: "application/json" + }, + json: true + }; - return rp(options) - .then((result) => { - if (result.MediaContainer.size > 0) { - const playing = result.MediaContainer.Metadata.map(convertPlexToStream); - return { size: Object.keys(playing).length, video: playing }; - } - return { size: 0, video: [] }; - }) - .catch((err) => { - throw new Error(`Error handling plex playing. Error: ${err}`); - }); - } + return rp(options) + .then(result => { + if (result.MediaContainer.size > 0) { + const playing = + result.MediaContainer.Metadata.map(convertPlexToStream); + return { size: Object.keys(playing).length, video: playing }; + } + return { size: 0, video: [] }; + }) + .catch(err => { + throw new Error(`Error handling plex playing. Error: ${err}`); + }); + } - // multipleInPlex(tmdbResults) { - // const results = tmdbResults.results.map(async (tmdb) => { - // return this.inPlex(tmdb) - // }) - // return Promise.all(results) - // } + // multipleInPlex(tmdbResults) { + // const results = tmdbResults.results.map(async (tmdb) => { + // return this.inPlex(tmdb) + // }) + // return Promise.all(results) + // } } module.exports = PlexRepository; diff --git a/seasoned_api/src/plex/requestRepository.js b/seasoned_api/src/plex/requestRepository.js index 9c8d1e0..357ad40 100644 --- a/seasoned_api/src/plex/requestRepository.js +++ b/seasoned_api/src/plex/requestRepository.js @@ -1,7 +1,7 @@ -const PlexRepository = require("src/plex/plexRepository"); -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); -const establishedDatabase = require("src/database/database"); +const PlexRepository = require("./plexRepository"); +const configuration = require("../config/configuration").getInstance(); +const TMDB = require("../tmdb/tmdb"); +const establishedDatabase = require("../database/database"); const plexRepository = new PlexRepository(configuration.get("plex", "ip")); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); diff --git a/seasoned_api/src/plex/stream/convertStreamToPlayer.js b/seasoned_api/src/plex/stream/convertStreamToPlayer.js index 8d4abc5..0e07b1c 100644 --- a/seasoned_api/src/plex/stream/convertStreamToPlayer.js +++ b/seasoned_api/src/plex/stream/convertStreamToPlayer.js @@ -1,13 +1,13 @@ -const Player = require('src/media_classes/player'); +const Player = require("../../media_classes/player"); function convertStreamToPlayer(plexStream) { - const player = new Player(plexStream.device, plexStream.address); - player.platform = plexStream.platform; - player.product = plexStream.product; - player.title = plexStream.title; - player.state = plexStream.state; + const player = new Player(plexStream.device, plexStream.address); + player.platform = plexStream.platform; + player.product = plexStream.product; + player.title = plexStream.title; + player.state = plexStream.state; - return player; + return player; } module.exports = convertStreamToPlayer; diff --git a/seasoned_api/src/plex/stream/convertStreamToUser.js b/seasoned_api/src/plex/stream/convertStreamToUser.js index 6b356d2..ee1d261 100644 --- a/seasoned_api/src/plex/stream/convertStreamToUser.js +++ b/seasoned_api/src/plex/stream/convertStreamToUser.js @@ -1,7 +1,7 @@ -const User = require('src/media_classes/user'); +const User = require("../../media_classes/user"); function convertStreamToUser(plexStream) { - return new User(plexStream.id, plexStream.title); + return new User(plexStream.id, plexStream.title); } module.exports = convertStreamToUser; diff --git a/seasoned_api/src/request/request.js b/seasoned_api/src/request/request.js index 79a15ba..97afb05 100644 --- a/seasoned_api/src/request/request.js +++ b/seasoned_api/src/request/request.js @@ -1,28 +1,34 @@ -const assert = require('assert') -const configuration = require('src/config/configuration').getInstance(); -const TMDB = require('src/tmdb/tmdb'); -const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); -const establishedDatabase = require('src/database/database'); -const utils = require('./utils'); +const assert = require("assert"); +const configuration = require("../config/configuration").getInstance(); +const TMDB = require("../tmdb/tmdb"); +const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); +const establishedDatabase = require("../database/database"); +const utils = require("./utils"); class RequestRepository { constructor(database) { this.database = database || establishedDatabase; this.queries = { - add: 'insert into requests (id,title,year,poster_path,background_path,requested_by,ip,user_agent,type) values(?,?,?,?,?,?,?,?,?)', - fetchAll: 'select * from requests where status != "downloaded" order by date desc LIMIT 25 OFFSET ?*25-25', - fetchAllFilteredStatus: 'select * from requests where status = ? order by date desc LIMIT 25 offset ?*25-25', - totalRequests: 'select count(*) as totalRequests from requests where status != "downloaded"', - totalRequestsFilteredStatus: 'select count(*) as totalRequests from requests where status = ?', + add: "insert into requests (id,title,year,poster_path,background_path,requested_by,ip,user_agent,type) values(?,?,?,?,?,?,?,?,?)", + fetchAll: + 'select * from requests where status != "downloaded" order by date desc LIMIT 25 OFFSET ?*25-25', + fetchAllFilteredStatus: + "select * from requests where status = ? order by date desc LIMIT 25 offset ?*25-25", + totalRequests: + 'select count(*) as totalRequests from requests where status != "downloaded"', + totalRequestsFilteredStatus: + "select count(*) as totalRequests from requests where status = ?", fetchAllSort: `select id, type from request order by ? ?`, fetchAllFilter: `select id, type from request where ? is "?"`, fetchAllQuery: `select id, type from request where title like "%?%" or year like "%?%"`, fetchAllFilterAndSort: `select id, type from request where ? is "?" order by ? ?`, - downloaded: '(select status from requests where id is request.id and type is request.type limit 1)', + downloaded: + "(select status from requests where id is request.id and type is request.type limit 1)", // deluge: '(select status from deluge_torrent where id is request.id and type is request.type limit 1)', // fetchAllFilterStatus: 'select * from request where ' - readWithoutUserData: 'select id, title, year, type, status, date from requests where id is ? and type is ?', - read: 'select id, title, year, type, status, requested_by, ip, date, user_agent from requests where id is ? and type is ?' + readWithoutUserData: + "select id, title, year, type, status, date from requests where id is ? and type is ?", + read: "select id, title, year, type, status, requested_by, ip, date, user_agent from requests where id is ? and type is ?" }; } @@ -31,52 +37,58 @@ class RequestRepository { if (query !== undefined) { const dbParams = [query, query]; - const dbquery = this.queries.fetchAllQuery + const dbquery = this.queries.fetchAllQuery; - dbQuery = dbquery.split('').map((char) => char === '?' ? dbParams.shift() : char).join('') - } - else if (by !== undefined && filter !== undefined) { + dbQuery = dbquery + .split("") + .map(char => (char === "?" ? dbParams.shift() : char)) + .join(""); + } else if (by !== undefined && filter !== undefined) { const paramToColumnAndValue = { - movie: ['type', 'movie'], - show: ['type', 'show'] - } + movie: ["type", "movie"], + show: ["type", "show"] + }; const dbParams = paramToColumnAndValue[filter].concat([by, direction]); const query = this.queries.fetchAllFilterAndSort; - dbQuery = query.split('').map((char) => char === '?' ? dbParams.shift() : char).join('') - } - else if (by !== undefined) { + dbQuery = query + .split("") + .map(char => (char === "?" ? dbParams.shift() : char)) + .join(""); + } else if (by !== undefined) { const dbParams = [by, direction]; const query = this.queries.fetchAllSort; - dbQuery = query.split('').map((char) => char === '?' ? dbParams.shift() : char).join('') - } - else if (filter !== undefined) { + dbQuery = query + .split("") + .map(char => (char === "?" ? dbParams.shift() : char)) + .join(""); + } else if (filter !== undefined) { const paramToColumnAndValue = { - movie: ['type', 'movie'], - show: ['type', 'show'], - downloaded: [this.queries.downloaded, 'downloaded'] + movie: ["type", "movie"], + show: ["type", "show"], + downloaded: [this.queries.downloaded, "downloaded"] // downloading: [this.database.delugeStatus, 'downloading'] - } - const dbParams = paramToColumnAndValue[filter] + }; + const dbParams = paramToColumnAndValue[filter]; const query = this.queries.fetchAllFilter; - dbQuery = query.split('').map((char) => char === '?' ? dbParams.shift() : char).join('') - } - else { + dbQuery = query + .split("") + .map(char => (char === "?" ? dbParams.shift() : char)) + .join(""); + } else { dbQuery = this.queries.fetchAll; } - return dbQuery + return dbQuery; } mapToTmdbByType(rows) { - return rows.map((row) => { - if (row.type === 'movie') - return tmdb.movieInfo(row.id) - else if (row.type === 'show') - return tmdb.showInfo(row.id) - }) + return rows.map(row => { + if (row.type === "movie") return tmdb.movieInfo(row.id); + else if (row.type === "show") return tmdb.showInfo(row.id); + }); } /** @@ -87,14 +99,31 @@ class RequestRepository { requestFromTmdb(tmdb, ip, user_agent, username) { return Promise.resolve() .then(() => this.database.get(this.queries.read, [tmdb.id, tmdb.type])) - .then(row => assert.equal(row, undefined, 'Id has already been requested')) - .then(() => this.database.run(this.queries.add, [tmdb.id, tmdb.title, tmdb.year, tmdb.poster, tmdb.backdrop, username, ip, user_agent, tmdb.type])) - .catch((error) => { - if (error.name === 'AssertionError' || error.message.endsWith('been requested')) { - throw new Error('This id is already requested', error.message); + .then(row => + assert.equal(row, undefined, "Id has already been requested") + ) + .then(() => + this.database.run(this.queries.add, [ + tmdb.id, + tmdb.title, + tmdb.year, + tmdb.poster, + tmdb.backdrop, + username, + ip, + user_agent, + tmdb.type + ]) + ) + .catch(error => { + if ( + error.name === "AssertionError" || + error.message.endsWith("been requested") + ) { + throw new Error("This id is already requested", error.message); } - console.log('Error @ request.addTmdb:', error); - throw new Error('Could not add request'); + console.log("Error @ request.addTmdb:", error); + throw new Error("Could not add request"); }); } @@ -105,9 +134,10 @@ class RequestRepository { * @returns {Promise} */ getRequestByIdAndType(id, type) { - return this.database.get(this.queries.readWithoutUserData, [id, type]) + return this.database + .get(this.queries.readWithoutUserData, [id, type]) .then(row => { - assert(row, 'Could not find request item with that id and type') + assert(row, "Could not find request item with that id and type"); return { id: row.id, title: row.title, @@ -115,8 +145,8 @@ class RequestRepository { type: row.type, status: row.status, requested_date: new Date(row.date) - } - }) + }; + }); } /** @@ -127,40 +157,68 @@ class RequestRepository { * @param {String} query param to filter result on. Filters on title and year * @returns {Promise} */ - fetchAll(page=1, sort_by=undefined, sort_direction='asc', filter=undefined, query=undefined) { + fetchAll( + page = 1, + sort_by = undefined, + sort_direction = "asc", + filter = undefined, + query = undefined + ) { // TODO implemented sort and filter - page = parseInt(page) - let fetchQuery = this.queries.fetchAll - let fetchTotalResults = this.queries.totalRequests - let fetchParams = [page] + page = parseInt(page); + let fetchQuery = this.queries.fetchAll; + let fetchTotalResults = this.queries.totalRequests; + let fetchParams = [page]; - if (filter && (filter === 'downloading' || filter === 'downloaded' || filter === 'requested')) { - console.log('tes') - fetchQuery = this.queries.fetchAllFilteredStatus - fetchTotalResults = this.queries.totalRequestsFilteredStatus - fetchParams = [filter, page] + if ( + filter && + (filter === "downloading" || + filter === "downloaded" || + filter === "requested") + ) { + console.log("tes"); + fetchQuery = this.queries.fetchAllFilteredStatus; + fetchTotalResults = this.queries.totalRequestsFilteredStatus; + fetchParams = [filter, page]; } else { - filter = undefined + filter = undefined; } return Promise.resolve() - .then((dbQuery) => this.database.all(fetchQuery, fetchParams)) - .then(async (rows) => { - const sqliteResponse = await this.database.get(fetchTotalResults, filter ? filter : undefined) - const totalRequests = sqliteResponse['totalRequests'] - const totalPages = Math.ceil(totalRequests / 26) + .then(dbQuery => this.database.all(fetchQuery, fetchParams)) + .then(async rows => { + const sqliteResponse = await this.database.get( + fetchTotalResults, + filter ? filter : undefined + ); + const totalRequests = sqliteResponse["totalRequests"]; + const totalPages = Math.ceil(totalRequests / 26); - return [ rows.map(item => { - item.poster = item.poster_path; delete item.poster_path; - item.backdrop = item.background_path; delete item.background_path; - return item - }), totalPages, totalRequests ] - return Promise.all(this.mapToTmdbByType(rows)) -}) - .then(([result, totalPages, totalRequests]) => Promise.resolve({ - results: result, total_results: totalRequests, page: page, total_pages: totalPages - })) - .catch(error => { console.log(error);throw error }) + return [ + rows.map(item => { + item.poster = item.poster_path; + delete item.poster_path; + item.backdrop = item.background_path; + delete item.background_path; + return item; + }), + totalPages, + totalRequests + ]; + return Promise.all(this.mapToTmdbByType(rows)); + }) + .then(([result, totalPages, totalRequests]) => + Promise.resolve({ + results: result, + total_results: totalRequests, + page: page, + total_pages: totalPages + }) + ) + .catch(error => { + console.log(error); + throw error; + }); } } diff --git a/seasoned_api/src/searchHistory/searchHistory.js b/seasoned_api/src/searchHistory/searchHistory.js index c2d5a67..15fdcb1 100644 --- a/seasoned_api/src/searchHistory/searchHistory.js +++ b/seasoned_api/src/searchHistory/searchHistory.js @@ -1,52 +1,57 @@ -const establishedDatabase = require('src/database/database'); +const establishedDatabase = require("../database/database"); class SearchHistory { - constructor(database) { - this.database = database || establishedDatabase; - this.queries = { - 'create': 'insert into search_history (search_query, user_name) values (?, ?)', - 'read': 'select search_query from search_history where user_name = ? order by id desc', - }; - } + constructor(database) { + this.database = database || establishedDatabase; + this.queries = { + create: + "insert into search_history (search_query, user_name) values (?, ?)", + read: "select search_query from search_history where user_name = ? order by id desc" + }; + } - /** + /** * Retrive a search queries for a user from the database. * @param {User} user existing user * @returns {Promise} */ - read(user) { - return new Promise((resolve, reject) => this.database.all(this.queries.read, user) - .then((result, error) => { - if (error) throw new Error(error); - resolve(result.map(row => row.search_query)); - }) - .catch((error) => { - console.log('Error when fetching history from database:', error) - reject('Unable to get history.'); - })); - } + read(user) { + return new Promise((resolve, reject) => + this.database + .all(this.queries.read, user) + .then((result, error) => { + if (error) throw new Error(error); + resolve(result.map(row => row.search_query)); + }) + .catch(error => { + console.log("Error when fetching history from database:", error); + reject("Unable to get history."); + }) + ); + } - /** + /** * Creates a new search entry in the database. * @param {String} username logged in user doing the search * @param {String} searchQuery the query the user searched for * @returns {Promise} */ - create(username, searchQuery) { - return this.database.run(this.queries.create, [searchQuery, username]) - .catch(error => { - if (error.message.includes('FOREIGN')) { - throw new Error('Could not create search history.'); - } + create(username, searchQuery) { + return this.database + .run(this.queries.create, [searchQuery, username]) + .catch(error => { + if (error.message.includes("FOREIGN")) { + throw new Error("Could not create search history."); + } - throw { - success: false, - status: 500, - message: 'An unexpected error occured', - source: 'database' - } - }); - } + throw { + success: false, + status: 500, + message: "An unexpected error occured", + source: "database" + }; + }); + } } module.exports = SearchHistory; diff --git a/seasoned_api/src/seasoned/strayRepository.js b/seasoned_api/src/seasoned/strayRepository.js index 19ad8bb..8474295 100644 --- a/seasoned_api/src/seasoned/strayRepository.js +++ b/seasoned_api/src/seasoned/strayRepository.js @@ -1,62 +1,68 @@ -const assert = require('assert'); -const Stray = require('src/seasoned/stray'); -const establishedDatabase = require('src/database/database'); -const pythonShell = require('python-shell'); +const assert = require("assert"); +const Stray = require("./stray"); +const establishedDatabase = require("../database/database"); +const pythonShell = require("python-shell"); class StrayRepository { - constructor(database) { - this.database = database || establishedDatabase; - this.queries = { - read: 'SELECT * FROM stray_eps WHERE id = ?', - readAll: 'SELECT id, name, season, episode, verified FROM stray_eps', - readAllFiltered: 'SELECT id, name, season, episode, verified FROM stray_eps WHERE verified = ', - checkVerified: 'SELECT id FROM stray_eps WHERE verified = 0 AND id = ?', - verify: 'UPDATE stray_eps SET verified = 1 WHERE id = ?', + constructor(database) { + this.database = database || establishedDatabase; + this.queries = { + read: "SELECT * FROM stray_eps WHERE id = ?", + readAll: "SELECT id, name, season, episode, verified FROM stray_eps", + readAllFiltered: + "SELECT id, name, season, episode, verified FROM stray_eps WHERE verified = ", + checkVerified: "SELECT id FROM stray_eps WHERE verified = 0 AND id = ?", + verify: "UPDATE stray_eps SET verified = 1 WHERE id = ?" + }; + } + + read(strayId) { + return this.database.get(this.queries.read, strayId).then(row => { + assert.notEqual( + row, + undefined, + `Could not find list with id ${strayId}.` + ); + return row; + }); + } + + readAll(verified = null) { + let dbSearchQuery = this.queries.readAll; + if (verified != null) { + dbSearchQuery = this.queries.readAllFiltered + verified.toString(); + } + return this.database.all(dbSearchQuery).then(rows => + rows.map(row => { + const stray = new Stray(row.id); + stray.name = row.name; + stray.season = row.season; + stray.episode = row.episode; + stray.verified = row.verified; + return stray; + }) + ); + } + + verifyStray(strayId) { + return this.database.get(this.queries.checkVerified, strayId).then(row => { + assert.notEqual(row, undefined, `Stray '${strayId}' already verified.`); + + const options = { + pythonPath: "../app/env/bin/python3", + args: [strayId] }; - } - read(strayId) { - return this.database.get(this.queries.read, strayId).then((row) => { - assert.notEqual(row, undefined, `Could not find list with id ${strayId}.`); - return row; + pythonShell.run("../app/moveSeasoned.py", options, (err, results) => { + if (err) throw err; + // TODO Add error handling!! StrayRepository.ERROR + // results is an array consisting of messages collected during execution + console.log("results: %j", results); }); - } - readAll(verified = null) { - let dbSearchQuery = this.queries.readAll; - if (verified != null) { - dbSearchQuery = this.queries.readAllFiltered + verified.toString(); - } - return this.database.all(dbSearchQuery).then(rows => - rows.map((row) => { - const stray = new Stray(row.id); - stray.name = row.name; - stray.season = row.season; - stray.episode = row.episode; - stray.verified = row.verified; - return stray; - })); - } - - verifyStray(strayId) { - return this.database.get(this.queries.checkVerified, strayId).then((row) => { - assert.notEqual(row, undefined, `Stray '${strayId}' already verified.`); - - const options = { - pythonPath: '../app/env/bin/python3', - args: [strayId], - }; - - pythonShell.run('../app/moveSeasoned.py', options, (err, results) => { - if (err) throw err; - // TODO Add error handling!! StrayRepository.ERROR - // results is an array consisting of messages collected during execution - console.log('results: %j', results); - }); - - return this.database.run(this.queries.verify, strayId); - }); - } + return this.database.run(this.queries.verify, strayId); + }); + } } module.exports = StrayRepository; diff --git a/seasoned_api/src/tmdb/cache.js b/seasoned_api/src/tmdb/cache.js index e85494a..0806e8c 100644 --- a/seasoned_api/src/tmdb/cache.js +++ b/seasoned_api/src/tmdb/cache.js @@ -1,44 +1,48 @@ -const assert = require('assert'); -const establishedDatabase = require('src/database/database'); +const assert = require("assert"); +const establishedDatabase = require("../database/database"); class Cache { - constructor(database) { - this.database = database || establishedDatabase; - this.queries = { - read: 'SELECT value, time_to_live, created_at, DATETIME("now", "localtime") as now, ' + - 'DATETIME(created_at, "+" || time_to_live || " seconds") as expires ' + - 'FROM cache WHERE key = ? AND now < expires', - create: 'INSERT OR REPLACE INTO cache (key, value, time_to_live) VALUES (?, ?, ?)', - }; - } + constructor(database) { + this.database = database || establishedDatabase; + this.queries = { + read: + 'SELECT value, time_to_live, created_at, DATETIME("now", "localtime") as now, ' + + 'DATETIME(created_at, "+" || time_to_live || " seconds") as expires ' + + "FROM cache WHERE key = ? AND now < expires", + create: + "INSERT OR REPLACE INTO cache (key, value, time_to_live) VALUES (?, ?, ?)" + }; + } - /** - * Retrieve an unexpired cache entry by key. - * @param {String} key of the cache entry - * @returns {Object} - */ - get(key) { - return Promise.resolve() - .then(() => this.database.get(this.queries.read, [key])) - .then(row => { - assert(row, 'Could not find cache entry with that key.'); - return JSON.parse(row.value); - }) - } + /** + * Retrieve an unexpired cache entry by key. + * @param {String} key of the cache entry + * @returns {Object} + */ + get(key) { + return Promise.resolve() + .then(() => this.database.get(this.queries.read, [key])) + .then(row => { + assert(row, "Could not find cache entry with that key."); + return JSON.parse(row.value); + }); + } - /** - * Insert cache entry with key and value. - * @param {String} key of the cache entry - * @param {String} value of the cache entry - * @param {Number} timeToLive the number of seconds before entry expires - * @returns {Object} - */ - set(key, value, timeToLive = 10800) { - const json = JSON.stringify(value); - return Promise.resolve() - .then(() => this.database.run(this.queries.create, [key, json, timeToLive])) - .then(() => value); - } + /** + * Insert cache entry with key and value. + * @param {String} key of the cache entry + * @param {String} value of the cache entry + * @param {Number} timeToLive the number of seconds before entry expires + * @returns {Object} + */ + set(key, value, timeToLive = 10800) { + const json = JSON.stringify(value); + return Promise.resolve() + .then(() => + this.database.run(this.queries.create, [key, json, timeToLive]) + ) + .then(() => value); + } } module.exports = Cache; diff --git a/seasoned_api/src/tmdb/tmdb.js b/seasoned_api/src/tmdb/tmdb.js index eae14d4..6b9164d 100644 --- a/seasoned_api/src/tmdb/tmdb.js +++ b/seasoned_api/src/tmdb/tmdb.js @@ -1,14 +1,8 @@ const moviedb = require("km-moviedb"); -const RedisCache = require("src/cache/redis"); +const RedisCache = require("../cache/redis"); const redisCache = new RedisCache(); -const { - Movie, - Show, - Person, - Credits, - ReleaseDates -} = require("src/tmdb/types"); +const { Movie, Show, Person, Credits, ReleaseDates } = require("./types"); const tmdbErrorResponse = (error, typeString = undefined) => { if (error.status === 404) { @@ -107,14 +101,20 @@ class TMDB { * @returns {Promise} movie release dates object */ movieReleaseDates(identifier) { - const query = { id: identifier } - const cacheKey = `tmdb/${this.cacheTags.movieReleaseDates}:${identifier}` + const query = { id: identifier }; + const cacheKey = `tmdb/${this.cacheTags.movieReleaseDates}:${identifier}`; - return this.getFromCacheOrFetchFromTmdb(cacheKey, 'movieReleaseDates', query) - .then(releaseDates => this.cache.set(cacheKey, releaseDates, this.defaultTTL)) - .then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates)) + return this.getFromCacheOrFetchFromTmdb( + cacheKey, + "movieReleaseDates", + query + ) + .then(releaseDates => + this.cache.set(cacheKey, releaseDates, this.defaultTTL) + ) + .then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates)); } - + /** * Retrieve a specific show by id from TMDB. * @param {Number} identifier of the show you want to retrieve diff --git a/seasoned_api/src/user/token.js b/seasoned_api/src/user/token.js index 2f65c03..d99baac 100644 --- a/seasoned_api/src/user/token.js +++ b/seasoned_api/src/user/token.js @@ -1,4 +1,4 @@ -const User = require("src/user/user"); +const User = require("./user"); const jwt = require("jsonwebtoken"); class Token { diff --git a/seasoned_api/src/user/userRepository.js b/seasoned_api/src/user/userRepository.js index 745fce7..4f9683c 100644 --- a/seasoned_api/src/user/userRepository.js +++ b/seasoned_api/src/user/userRepository.js @@ -1,5 +1,5 @@ const assert = require("assert"); -const establishedDatabase = require("src/database/database"); +const establishedDatabase = require("../database/database"); class UserRepository { constructor(database) { diff --git a/seasoned_api/src/user/userSecurity.js b/seasoned_api/src/user/userSecurity.js index e05f469..ea07bb8 100644 --- a/seasoned_api/src/user/userSecurity.js +++ b/seasoned_api/src/user/userSecurity.js @@ -1,5 +1,5 @@ const bcrypt = require("bcrypt"); -const UserRepository = require("src/user/userRepository"); +const UserRepository = require("./userRepository"); class UserSecurity { constructor(database) { diff --git a/seasoned_api/src/webserver/app.js b/seasoned_api/src/webserver/app.js index beeeb61..270c9ff 100644 --- a/seasoned_api/src/webserver/app.js +++ b/seasoned_api/src/webserver/app.js @@ -3,7 +3,7 @@ const Raven = require("raven"); const cookieParser = require("cookie-parser"); const bodyParser = require("body-parser"); -const configuration = require("src/config/configuration").getInstance(); +const configuration = require("../config/configuration").getInstance(); const reqTokenToUser = require("./middleware/reqTokenToUser"); const mustBeAuthenticated = require("./middleware/mustBeAuthenticated"); diff --git a/seasoned_api/src/webserver/controllers/git/dumpHook.js b/seasoned_api/src/webserver/controllers/git/dumpHook.js index cfc1637..bed04f5 100644 --- a/seasoned_api/src/webserver/controllers/git/dumpHook.js +++ b/seasoned_api/src/webserver/controllers/git/dumpHook.js @@ -1,15 +1,16 @@ -const GitRepository = require('src/git/gitRepository'); +const GitRepository = require("../../../git/gitRepository"); const gitRepository = new GitRepository(); function dumpHookController(req, res) { - gitRepository.dumpHook(req.body) - .then(() => { - res.status(200); - }) - .catch((error) => { - res.status(500); - }); + gitRepository + .dumpHook(req.body) + .then(() => { + res.status(200); + }) + .catch(error => { + res.status(500); + }); } module.exports = dumpHookController; diff --git a/seasoned_api/src/webserver/controllers/list/listController.js b/seasoned_api/src/webserver/controllers/list/listController.js index db71722..9ec287e 100644 --- a/seasoned_api/src/webserver/controllers/list/listController.js +++ b/seasoned_api/src/webserver/controllers/list/listController.js @@ -1,15 +1,15 @@ -const configuration = require('src/config/configuration').getInstance(); -const TMDB = require('src/tmdb/tmdb'); -const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); -// there should be a translate function from query params to -// tmdb list that is valid. Should it be a helper function or does it -// belong in tmdb. +// there should be a translate function from query params to +// tmdb list that is valid. Should it be a helper function or does it +// belong in tmdb. // + could also have default value that are sent to the client. // * have the same class create a getListNames() and a fetchList() // * dicover list might be overkill_https://tinyurl.com/y7f8ragw -// + trending! https://tinyurl.com/ydywrqox -// by all, mediatype, or person. Can also define time periode to +// + trending! https://tinyurl.com/ydywrqox +// by all, mediatype, or person. Can also define time periode to // get more trending view of what people are checking out. // + newly created (tv/latest). // + movie/latest @@ -18,44 +18,57 @@ function handleError(error, res) { const { status, message } = error; if (status && message) { - res.status(status).send({ success: false, message }) + res.status(status).send({ success: false, message }); } else { - console.log('caught list controller error', error) - res.status(500).send({ message: 'An unexpected error occured while requesting list'}) + console.log("caught list controller error", error); + res + .status(500) + .send({ message: "An unexpected error occured while requesting list" }); } } function handleListResponse(response, res) { - return res.send(response) - .catch(error => handleError(error, res)) + return res.send(response).catch(error => handleError(error, res)); } function fetchTmdbList(req, res, listname, type) { const { page } = req.query; - if (type === 'movie') { - return tmdb.movieList(listname, page) + if (type === "movie") { + return tmdb + .movieList(listname, page) .then(listResponse => res.send(listResponse)) - .catch(error => handleError(error, res)) - } else if (type === 'show') { - return tmdb.showList(listname, page) + .catch(error => handleError(error, res)); + } else if (type === "show") { + return tmdb + .showList(listname, page) .then(listResponse => res.send(listResponse)) - .catch(error => handleError(error, res)) + .catch(error => handleError(error, res)); } - handleError({ - status: 400, - message: `'${type}' is not a valid list type.` - }, res) + handleError( + { + status: 400, + message: `'${type}' is not a valid list type.` + }, + res + ); } -const nowPlayingMovies = (req, res) => fetchTmdbList(req, res, 'miscNowPlayingMovies', 'movie') -const popularMovies = (req, res) => fetchTmdbList(req, res, 'miscPopularMovies', 'movie') -const topRatedMovies = (req, res) => fetchTmdbList(req, res, 'miscTopRatedMovies', 'movie') -const upcomingMovies = (req, res) => fetchTmdbList(req, res, 'miscUpcomingMovies', 'movie') -const nowPlayingShows = (req, res) => fetchTmdbList(req, res, 'tvOnTheAir', 'show') -const popularShows = (req, res) => fetchTmdbList(req, res, 'miscPopularTvs', 'show') -const topRatedShows = (req, res) => fetchTmdbList(req, res, 'miscTopRatedTvs', 'show') +const nowPlayingMovies = (req, res) => + fetchTmdbList(req, res, "miscNowPlayingMovies", "movie"); +const popularMovies = (req, res) => + fetchTmdbList(req, res, "miscPopularMovies", "movie"); +const topRatedMovies = (req, res) => + fetchTmdbList(req, res, "miscTopRatedMovies", "movie"); +const upcomingMovies = (req, res) => + fetchTmdbList(req, res, "miscUpcomingMovies", "movie"); +const nowPlayingShows = (req, res) => + fetchTmdbList(req, res, "tvOnTheAir", "show"); +const popularShows = (req, res) => + fetchTmdbList(req, res, "miscPopularTvs", "show"); +const topRatedShows = (req, res) => + fetchTmdbList(req, res, "miscTopRatedTvs", "show"); module.exports = { nowPlayingMovies, @@ -65,4 +78,4 @@ module.exports = { nowPlayingShows, popularShows, topRatedShows -} +}; diff --git a/seasoned_api/src/webserver/controllers/movie/credits.js b/seasoned_api/src/webserver/controllers/movie/credits.js index dbf5ff2..a33b2fa 100644 --- a/seasoned_api/src/webserver/controllers/movie/credits.js +++ b/seasoned_api/src/webserver/controllers/movie/credits.js @@ -1,24 +1,30 @@ -const configuration = require('src/config/configuration').getInstance(); -const TMDB = require('src/tmdb/tmdb'); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); -const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); +const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const movieCreditsController = (req, res) => { const movieId = req.params.id; - tmdb.movieCredits(movieId) + tmdb + .movieCredits(movieId) .then(credits => res.send(credits.createJsonResponse())) .catch(error => { const { status, message } = error; if (status && message) { - res.status(status).send({ success: false, message }) + res.status(status).send({ success: false, message }); } else { // TODO log unhandled errors - console.log('caugth movie credits controller error', error) - res.status(500).send({ message: 'An unexpected error occured while requesting movie credits' }) + console.log("caugth movie credits controller error", error); + res + .status(500) + .send({ + message: + "An unexpected error occured while requesting movie credits" + }); } - }) -} + }); +}; module.exports = movieCreditsController; diff --git a/seasoned_api/src/webserver/controllers/movie/info.js b/seasoned_api/src/webserver/controllers/movie/info.js index e32f284..dc13ea3 100644 --- a/seasoned_api/src/webserver/controllers/movie/info.js +++ b/seasoned_api/src/webserver/controllers/movie/info.js @@ -1,6 +1,6 @@ -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); -const Plex = require("src/plex/plex"); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const Plex = require("../../../plex/plex"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const plex = new Plex(configuration.get("plex", "ip")); diff --git a/seasoned_api/src/webserver/controllers/movie/releaseDates.js b/seasoned_api/src/webserver/controllers/movie/releaseDates.js index 4a87c6a..d8eb2e0 100644 --- a/seasoned_api/src/webserver/controllers/movie/releaseDates.js +++ b/seasoned_api/src/webserver/controllers/movie/releaseDates.js @@ -1,24 +1,30 @@ -const configuration = require('src/config/configuration').getInstance(); -const TMDB = require('src/tmdb/tmdb'); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); -const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); +const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const movieReleaseDatesController = (req, res) => { const movieId = req.params.id; - tmdb.movieReleaseDates(movieId) + tmdb + .movieReleaseDates(movieId) .then(releaseDates => res.send(releaseDates.createJsonResponse())) .catch(error => { const { status, message } = error; if (status && message) { - res.status(status).send({ success: false, message }) + res.status(status).send({ success: false, message }); } else { // TODO log unhandled errors : here our at tmdbReleaseError ? - console.log('caugth release dates controller error', error) - res.status(500).send({ message: 'An unexpected error occured while requesting movie credits' }) + console.log("caugth release dates controller error", error); + res + .status(500) + .send({ + message: + "An unexpected error occured while requesting movie credits" + }); } - }) -} + }); +}; module.exports = movieReleaseDatesController; diff --git a/seasoned_api/src/webserver/controllers/person/credits.js b/seasoned_api/src/webserver/controllers/person/credits.js index 45da62e..91a79e5 100644 --- a/seasoned_api/src/webserver/controllers/person/credits.js +++ b/seasoned_api/src/webserver/controllers/person/credits.js @@ -1,5 +1,5 @@ -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const personCreditsController = (req, res) => { diff --git a/seasoned_api/src/webserver/controllers/person/info.js b/seasoned_api/src/webserver/controllers/person/info.js index 72d8a61..be1dad0 100644 --- a/seasoned_api/src/webserver/controllers/person/info.js +++ b/seasoned_api/src/webserver/controllers/person/info.js @@ -1,5 +1,5 @@ -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); function handleError(error, res) { diff --git a/seasoned_api/src/webserver/controllers/pirate/addMagnet.js b/seasoned_api/src/webserver/controllers/pirate/addMagnet.js index b11af5f..0ce38d0 100644 --- a/seasoned_api/src/webserver/controllers/pirate/addMagnet.js +++ b/seasoned_api/src/webserver/controllers/pirate/addMagnet.js @@ -1,25 +1,24 @@ - /* -* @Author: KevinMidboe -* @Date: 2017-10-21 09:54:31 -* @Last Modified by: KevinMidboe -* @Last Modified time: 2017-10-21 15:32:43 -*/ + * @Author: KevinMidboe + * @Date: 2017-10-21 09:54:31 + * @Last Modified by: KevinMidboe + * @Last Modified time: 2017-10-21 15:32:43 + */ -const PirateRepository = require('src/pirate/pirateRepository'); +const PirateRepository = require("../../../pirate/pirateRepository"); function addMagnet(req, res) { - const magnet = req.body.magnet; - const name = req.body.name; - const tmdb_id = req.body.tmdb_id; + const magnet = req.body.magnet; + const name = req.body.name; + const tmdb_id = req.body.tmdb_id; - PirateRepository.AddMagnet(magnet, name, tmdb_id) - .then((result) => { - res.send(result); - }) - .catch(error => { - res.status(500).send({ success: false, message: error.message }); - }); + PirateRepository.AddMagnet(magnet, name, tmdb_id) + .then(result => { + res.send(result); + }) + .catch(error => { + res.status(500).send({ success: false, message: error.message }); + }); } module.exports = addMagnet; diff --git a/seasoned_api/src/webserver/controllers/pirate/searchTheBay.js b/seasoned_api/src/webserver/controllers/pirate/searchTheBay.js index 5d5fc10..84516d4 100644 --- a/seasoned_api/src/webserver/controllers/pirate/searchTheBay.js +++ b/seasoned_api/src/webserver/controllers/pirate/searchTheBay.js @@ -5,7 +5,7 @@ * @Last Modified time: 2018-02-26 19:56:32 */ -const PirateRepository = require("src/pirate/pirateRepository"); +const PirateRepository = require("../../../pirate/pirateRepository"); // const pirateRepository = new PirateRepository(); /** diff --git a/seasoned_api/src/webserver/controllers/plex/fetchRequested.js b/seasoned_api/src/webserver/controllers/plex/fetchRequested.js index 149fe73..56eeaaa 100644 --- a/seasoned_api/src/webserver/controllers/plex/fetchRequested.js +++ b/seasoned_api/src/webserver/controllers/plex/fetchRequested.js @@ -1,4 +1,4 @@ -const RequestRepository = require('src/plex/requestRepository.js'); +const RequestRepository = require("../../../plex/requestRepository.js"); const requestRepository = new RequestRepository(); @@ -9,16 +9,21 @@ const requestRepository = new RequestRepository(); * @returns {Callback} */ function fetchRequestedController(req, res) { - // const user = req.loggedInUser; - const { status, page } = req.query; + // const user = req.loggedInUser; + const { status, page } = req.query; - requestRepository.fetchRequested(status, page) - .then((requestedItems) => { - res.send({ success: true, results: requestedItems, total_results: requestedItems.length }); - }) - .catch((error) => { - res.status(401).send({ success: false, message: error.message }); + requestRepository + .fetchRequested(status, page) + .then(requestedItems => { + res.send({ + success: true, + results: requestedItems, + total_results: requestedItems.length }); + }) + .catch(error => { + res.status(401).send({ success: false, message: error.message }); + }); } module.exports = fetchRequestedController; diff --git a/seasoned_api/src/webserver/controllers/plex/plexPlaying.js b/seasoned_api/src/webserver/controllers/plex/plexPlaying.js index c26f677..aa153b6 100644 --- a/seasoned_api/src/webserver/controllers/plex/plexPlaying.js +++ b/seasoned_api/src/webserver/controllers/plex/plexPlaying.js @@ -1,16 +1,17 @@ -const PlexRepository = require('src/plex/plexRepository'); -const configuration = require('src/config/configuration').getInstance(); +const PlexRepository = require("../../../plex/plexRepository"); +const configuration = require("../../../config/configuration").getInstance(); -const plexRepository = new PlexRepository(configuration.get('plex', 'ip')); +const plexRepository = new PlexRepository(configuration.get("plex", "ip")); function playingController(req, res) { - plexRepository.nowPlaying() - .then(movies => { - res.send(movies); - }) - .catch(error => { - res.status(500).send({ success: false, message: error.message }); - }); + plexRepository + .nowPlaying() + .then(movies => { + res.send(movies); + }) + .catch(error => { + res.status(500).send({ success: false, message: error.message }); + }); } module.exports = playingController; diff --git a/seasoned_api/src/webserver/controllers/plex/readRequest.js b/seasoned_api/src/webserver/controllers/plex/readRequest.js index 224f682..db29647 100644 --- a/seasoned_api/src/webserver/controllers/plex/readRequest.js +++ b/seasoned_api/src/webserver/controllers/plex/readRequest.js @@ -1,4 +1,4 @@ -const RequestRepository = require('src/plex/requestRepository'); +const RequestRepository = require("../../../plex/requestRepository"); const requestRepository = new RequestRepository(); @@ -9,14 +9,16 @@ const requestRepository = new RequestRepository(); * @returns {Callback} */ function readRequestController(req, res) { - const mediaId = req.params.mediaId; - const { type } = req.query; - requestRepository.lookup(mediaId, type) - .then(movies => { - res.send(movies); - }).catch(error => { - res.status(404).send({ success: false, message: error.message }); - }); + const mediaId = req.params.mediaId; + const { type } = req.query; + requestRepository + .lookup(mediaId, type) + .then(movies => { + res.send(movies); + }) + .catch(error => { + res.status(404).send({ success: false, message: error.message }); + }); } module.exports = readRequestController; diff --git a/seasoned_api/src/webserver/controllers/plex/search.js b/seasoned_api/src/webserver/controllers/plex/search.js index bf932d3..0d7b577 100644 --- a/seasoned_api/src/webserver/controllers/plex/search.js +++ b/seasoned_api/src/webserver/controllers/plex/search.js @@ -1,6 +1,6 @@ -const configuration = require('src/config/configuration').getInstance(); -const Plex = require('src/plex/plex'); -const plex = new Plex(configuration.get('plex', 'ip')); +const configuration = require("../../../config/configuration").getInstance(); +const Plex = require("../../../plex/plex"); +const plex = new Plex(configuration.get("plex", "ip")); /** * Controller: Search plex for movies, shows and episodes by query @@ -9,17 +9,24 @@ const plex = new Plex(configuration.get('plex', 'ip')); * @returns {Callback} */ function searchPlexController(req, res) { - const { query, type } = req.query; - plex.search(query, type) - .then(movies => { - if (movies.length > 0) { - res.send(movies); - } else { - res.status(404).send({ success: false, message: 'Search query did not give any results from plex.'}) - } - }).catch(error => { - res.status(500).send({ success: false, message: error.message }); - }); + const { query, type } = req.query; + plex + .search(query, type) + .then(movies => { + if (movies.length > 0) { + res.send(movies); + } else { + res + .status(404) + .send({ + success: false, + message: "Search query did not give any results from plex." + }); + } + }) + .catch(error => { + res.status(500).send({ success: false, message: error.message }); + }); } module.exports = searchPlexController; diff --git a/seasoned_api/src/webserver/controllers/plex/searchMedia.js b/seasoned_api/src/webserver/controllers/plex/searchMedia.js index 514312f..c4d9698 100644 --- a/seasoned_api/src/webserver/controllers/plex/searchMedia.js +++ b/seasoned_api/src/webserver/controllers/plex/searchMedia.js @@ -1,7 +1,7 @@ -const PlexRepository = require('src/plex/plexRepository'); -const configuration = require('src/config/configuration').getInstance(); +const PlexRepository = require("../../../plex/plexRepository"); +const configuration = require("../../../config/configuration").getInstance(); -const plexRepository = new PlexRepository(configuration.get('plex', 'ip')); +const plexRepository = new PlexRepository(configuration.get("plex", "ip")); /** * Controller: Search for media and check existence @@ -11,19 +11,25 @@ const plexRepository = new PlexRepository(configuration.get('plex', 'ip')); * @returns {Callback} */ function searchMediaController(req, res) { - const { query } = req.query; + const { query } = req.query; - plexRepository.search(query) - .then(media => { - if (media !== undefined || media.length > 0) { - res.send(media); - } else { - res.status(404).send({ success: false, message: 'Search query did not return any results.' }); - } - }) - .catch(error => { - res.status(500).send({ success: false, message: error.message }); - }); + plexRepository + .search(query) + .then(media => { + if (media !== undefined || media.length > 0) { + res.send(media); + } else { + res + .status(404) + .send({ + success: false, + message: "Search query did not return any results." + }); + } + }) + .catch(error => { + res.status(500).send({ success: false, message: error.message }); + }); } module.exports = searchMediaController; diff --git a/seasoned_api/src/webserver/controllers/plex/searchRequest.js b/seasoned_api/src/webserver/controllers/plex/searchRequest.js index 9631c4c..c040f48 100644 --- a/seasoned_api/src/webserver/controllers/plex/searchRequest.js +++ b/seasoned_api/src/webserver/controllers/plex/searchRequest.js @@ -1,6 +1,6 @@ -const SearchHistory = require("src/searchHistory/searchHistory"); -const Cache = require("src/tmdb/cache"); -const RequestRepository = require("src/plex/requestRepository.js"); +const SearchHistory = require("../../../searchHistory/searchHistory"); +const Cache = require("../../../tmdb/cache"); +const RequestRepository = require("../../../plex/requestRepository.js"); const cache = new Cache(); const requestRepository = new RequestRepository(cache); diff --git a/seasoned_api/src/webserver/controllers/plex/submitRequest.js b/seasoned_api/src/webserver/controllers/plex/submitRequest.js index d77dd80..2d78018 100644 --- a/seasoned_api/src/webserver/controllers/plex/submitRequest.js +++ b/seasoned_api/src/webserver/controllers/plex/submitRequest.js @@ -1,6 +1,6 @@ -const configuration = require("src/config/configuration").getInstance(); -const RequestRepository = require("src/request/request"); -const TMDB = require("src/tmdb/tmdb"); +const configuration = require("../../../config/configuration").getInstance(); +const RequestRepository = require("../../../request/request"); +const TMDB = require("../../../tmdb/tmdb"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const request = new RequestRepository(); @@ -35,12 +35,10 @@ function submitRequestController(req, res) { console.log("show"); mediaFunction = tmdbShowInfo; } else { - res - .status(422) - .send({ - success: false, - message: 'Incorrect type. Allowed types: "movie" or "show"' - }); + res.status(422).send({ + success: false, + message: 'Incorrect type. Allowed types: "movie" or "show"' + }); } if (mediaFunction === undefined) { diff --git a/seasoned_api/src/webserver/controllers/plex/updateRequested.js b/seasoned_api/src/webserver/controllers/plex/updateRequested.js index fdb2a72..3194a03 100644 --- a/seasoned_api/src/webserver/controllers/plex/updateRequested.js +++ b/seasoned_api/src/webserver/controllers/plex/updateRequested.js @@ -1,4 +1,4 @@ -const RequestRepository = require('src/plex/requestRepository'); +const RequestRepository = require("../../../plex/requestRepository"); const requestRepository = new RequestRepository(); @@ -9,17 +9,18 @@ const requestRepository = new RequestRepository(); * @returns {Callback} */ function updateRequested(req, res) { - const id = req.params.requestId; - const type = req.body.type; - const status = req.body.status; + const id = req.params.requestId; + const type = req.body.type; + const status = req.body.status; - requestRepository.updateRequestedById(id, type, status) - .then(() => { - res.send({ success: true }); - }) - .catch((error) => { - res.status(401).send({ success: false, message: error.message }); - }); + requestRepository + .updateRequestedById(id, type, status) + .then(() => { + res.send({ success: true }); + }) + .catch(error => { + res.status(401).send({ success: false, message: error.message }); + }); } module.exports = updateRequested; diff --git a/seasoned_api/src/webserver/controllers/plex/watchDirectLink.js b/seasoned_api/src/webserver/controllers/plex/watchDirectLink.js index 0020ba7..a56f85d 100644 --- a/seasoned_api/src/webserver/controllers/plex/watchDirectLink.js +++ b/seasoned_api/src/webserver/controllers/plex/watchDirectLink.js @@ -1,6 +1,6 @@ -const configuration = require('src/config/configuration').getInstance(); -const Plex = require('src/plex/plex'); -const plex = new Plex(configuration.get('plex', 'ip')); +const configuration = require("../../../config/configuration").getInstance(); +const Plex = require("../../../plex/plex"); +const plex = new Plex(configuration.get("plex", "ip")); /** * Controller: Search plex for movies, shows and episodes by query @@ -9,18 +9,18 @@ const plex = new Plex(configuration.get('plex', 'ip')); * @returns {Callback} */ -function watchDirectLink (req, res) { - const { title, year } = req.query; +function watchDirectLink(req, res) { + const { title, year } = req.query; - plex.getDirectLinkByTitleAndYear(title, year) + plex + .getDirectLinkByTitleAndYear(title, year) .then(plexDirectLink => { if (plexDirectLink == false) - res.status(404).send({ success: true, link: null }) - else - res.status(200).send({ success: true, link: plexDirectLink }) + res.status(404).send({ success: true, link: null }); + else res.status(200).send({ success: true, link: plexDirectLink }); }) .catch(error => { - res.status(500).send({ success: false, message: error.message }); + res.status(500).send({ success: false, message: error.message }); }); } diff --git a/seasoned_api/src/webserver/controllers/request/fetchAllRequests.js b/seasoned_api/src/webserver/controllers/request/fetchAllRequests.js index 030deb3..7679169 100644 --- a/seasoned_api/src/webserver/controllers/request/fetchAllRequests.js +++ b/seasoned_api/src/webserver/controllers/request/fetchAllRequests.js @@ -1,4 +1,4 @@ -const RequestRepository = require('src/request/request'); +const RequestRepository = require("../../../request/request"); const request = new RequestRepository(); /** @@ -12,16 +12,16 @@ function fetchAllRequests(req, res) { let sort_by = sort; let sort_direction = undefined; - if (sort !== undefined && sort.includes(':')) { - [sort_by, sort_direction] = sort.split(':') + if (sort !== undefined && sort.includes(":")) { + [sort_by, sort_direction] = sort.split(":"); } Promise.resolve() - .then(() => request.fetchAll(page, sort_by, sort_direction, filter, query)) - .then(result => res.send(result)) - .catch(error => { - res.status(404).send({ success: false, message: error.message }); - }); + .then(() => request.fetchAll(page, sort_by, sort_direction, filter, query)) + .then(result => res.send(result)) + .catch(error => { + res.status(404).send({ success: false, message: error.message }); + }); } module.exports = fetchAllRequests; diff --git a/seasoned_api/src/webserver/controllers/request/getRequest.js b/seasoned_api/src/webserver/controllers/request/getRequest.js index 856f320..9167e95 100644 --- a/seasoned_api/src/webserver/controllers/request/getRequest.js +++ b/seasoned_api/src/webserver/controllers/request/getRequest.js @@ -1,4 +1,4 @@ -const RequestRepository = require('src/request/request'); +const RequestRepository = require("../../../request/request"); const request = new RequestRepository(); /** @@ -11,7 +11,8 @@ function fetchAllRequests(req, res) { const id = req.params.id; const { type } = req.query; - request.getRequestByIdAndType(id, type) + request + .getRequestByIdAndType(id, type) .then(result => res.send(result)) .catch(error => { res.status(404).send({ success: false, message: error.message }); diff --git a/seasoned_api/src/webserver/controllers/request/requestTmdbId.js b/seasoned_api/src/webserver/controllers/request/requestTmdbId.js index e442eb3..a4e6185 100644 --- a/seasoned_api/src/webserver/controllers/request/requestTmdbId.js +++ b/seasoned_api/src/webserver/controllers/request/requestTmdbId.js @@ -1,6 +1,6 @@ -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); -const RequestRepository = require("src/request/request"); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const RequestRepository = require("../../../request/request"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const request = new RequestRepository(); // const { sendSMS } = require("src/notifications/sms"); diff --git a/seasoned_api/src/webserver/controllers/search/movieSearch.js b/seasoned_api/src/webserver/controllers/search/movieSearch.js index 8cad277..bfee6d1 100644 --- a/seasoned_api/src/webserver/controllers/search/movieSearch.js +++ b/seasoned_api/src/webserver/controllers/search/movieSearch.js @@ -1,6 +1,6 @@ -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); -const SearchHistory = require("src/searchHistory/searchHistory"); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const SearchHistory = require("../../../searchHistory/searchHistory"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const searchHistory = new SearchHistory(); diff --git a/seasoned_api/src/webserver/controllers/search/multiSearch.js b/seasoned_api/src/webserver/controllers/search/multiSearch.js index e030f11..db977c7 100644 --- a/seasoned_api/src/webserver/controllers/search/multiSearch.js +++ b/seasoned_api/src/webserver/controllers/search/multiSearch.js @@ -1,6 +1,6 @@ -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); -const SearchHistory = require("src/searchHistory/searchHistory"); +const configuration = require("../../..//config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const SearchHistory = require("../../../searchHistory/searchHistory"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const searchHistory = new SearchHistory(); @@ -36,11 +36,9 @@ function multiSearchController(req, res) { } else { // TODO log unhandled errors console.log("caugth multi search controller error", error); - res - .status(500) - .send({ - message: `An unexpected error occured while searching with query: ${query}` - }); + res.status(500).send({ + message: `An unexpected error occured while searching with query: ${query}` + }); } }); } diff --git a/seasoned_api/src/webserver/controllers/search/personSearch.js b/seasoned_api/src/webserver/controllers/search/personSearch.js index 45d5cfc..dbd11c1 100644 --- a/seasoned_api/src/webserver/controllers/search/personSearch.js +++ b/seasoned_api/src/webserver/controllers/search/personSearch.js @@ -1,6 +1,6 @@ -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); -const SearchHistory = require("src/searchHistory/searchHistory"); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const SearchHistory = require("../../../searchHistory/searchHistory"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const searchHistory = new SearchHistory(); diff --git a/seasoned_api/src/webserver/controllers/search/showSearch.js b/seasoned_api/src/webserver/controllers/search/showSearch.js index 20b3c05..cd420c4 100644 --- a/seasoned_api/src/webserver/controllers/search/showSearch.js +++ b/seasoned_api/src/webserver/controllers/search/showSearch.js @@ -1,6 +1,6 @@ -const SearchHistory = require("src/searchHistory/searchHistory"); -const configuration = require("src/config/configuration").getInstance(); -const TMDB = require("src/tmdb/tmdb"); +const SearchHistory = require("../../../searchHistory/searchHistory"); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const searchHistory = new SearchHistory(); diff --git a/seasoned_api/src/webserver/controllers/seasoned/readStrays.js b/seasoned_api/src/webserver/controllers/seasoned/readStrays.js index ca71d46..c07e7a7 100644 --- a/seasoned_api/src/webserver/controllers/seasoned/readStrays.js +++ b/seasoned_api/src/webserver/controllers/seasoned/readStrays.js @@ -1,17 +1,17 @@ -const StrayRepository = require('src/seasoned/strayRepository'); +const StrayRepository = require("../../../seasoned/strayRepository"); const strayRepository = new StrayRepository(); - function readStraysController(req, res) { - const { verified, page } = req.query; - strayRepository.readAll(verified, page) - .then((strays) => { - res.send(strays); - }) - .catch((error) => { - res.status(500).send({ success: false, message: error.message }); - }); + const { verified, page } = req.query; + strayRepository + .readAll(verified, page) + .then(strays => { + res.send(strays); + }) + .catch(error => { + res.status(500).send({ success: false, message: error.message }); + }); } module.exports = readStraysController; diff --git a/seasoned_api/src/webserver/controllers/seasoned/strayById.js b/seasoned_api/src/webserver/controllers/seasoned/strayById.js index 2810097..387bce8 100644 --- a/seasoned_api/src/webserver/controllers/seasoned/strayById.js +++ b/seasoned_api/src/webserver/controllers/seasoned/strayById.js @@ -1,17 +1,18 @@ -const StrayRepository = require('src/seasoned/strayRepository'); +const StrayRepository = require("../../../seasoned/strayRepository"); const strayRepository = new StrayRepository(); function strayByIdController(req, res) { - const id = req.params.strayId; + const id = req.params.strayId; - strayRepository.read(id) - .then((stray) => { - res.send(stray); - }) - .catch((error) => { - res.status(500).send({ success: false, message: error.message }); - }); + strayRepository + .read(id) + .then(stray => { + res.send(stray); + }) + .catch(error => { + res.status(500).send({ success: false, message: error.message }); + }); } module.exports = strayByIdController; diff --git a/seasoned_api/src/webserver/controllers/seasoned/verifyStray.js b/seasoned_api/src/webserver/controllers/seasoned/verifyStray.js index 84abc76..cb7cf6d 100644 --- a/seasoned_api/src/webserver/controllers/seasoned/verifyStray.js +++ b/seasoned_api/src/webserver/controllers/seasoned/verifyStray.js @@ -1,17 +1,18 @@ -const StrayRepository = require('src/seasoned/strayRepository'); +const StrayRepository = require("../../../seasoned/strayRepository"); const strayRepository = new StrayRepository(); function verifyStrayController(req, res) { - const id = req.params.strayId; + const id = req.params.strayId; - strayRepository.verifyStray(id) - .then(() => { - res.send({ success: true, message: 'Episode verified' }); - }) - .catch((error) => { - res.status(500).send({ success: false, message: error.message }); - }); + strayRepository + .verifyStray(id) + .then(() => { + res.send({ success: true, message: "Episode verified" }); + }) + .catch(error => { + res.status(500).send({ success: false, message: error.message }); + }); } module.exports = verifyStrayController; diff --git a/seasoned_api/src/webserver/controllers/show/credits.js b/seasoned_api/src/webserver/controllers/show/credits.js index f146391..b7014a5 100644 --- a/seasoned_api/src/webserver/controllers/show/credits.js +++ b/seasoned_api/src/webserver/controllers/show/credits.js @@ -1,23 +1,28 @@ -const configuration = require('src/config/configuration').getInstance(); -const TMDB = require('src/tmdb/tmdb'); -const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); const showCreditsController = (req, res) => { const showId = req.params.id; - tmdb.showCredits(showId) + tmdb + .showCredits(showId) .then(credits => res.send(credits.createJsonResponse())) .catch(error => { const { status, message } = error; if (status && message) { - res.status(status).send({ success: false, message }) + res.status(status).send({ success: false, message }); } else { // TODO log unhandled errors - console.log('caugth show credits controller error', error) - res.status(500).send({ message: 'An unexpected error occured while requesting show credits' }) + console.log("caugth show credits controller error", error); + res + .status(500) + .send({ + message: "An unexpected error occured while requesting show credits" + }); } - }) -} + }); +}; module.exports = showCreditsController; diff --git a/seasoned_api/src/webserver/controllers/show/info.js b/seasoned_api/src/webserver/controllers/show/info.js index 340dc8b..322c381 100644 --- a/seasoned_api/src/webserver/controllers/show/info.js +++ b/seasoned_api/src/webserver/controllers/show/info.js @@ -1,24 +1,24 @@ -const configuration = require('src/config/configuration').getInstance(); -const TMDB = require('src/tmdb/tmdb'); -const Plex = require('src/plex/plex'); -const tmdb = new TMDB(configuration.get('tmdb', 'apiKey')); -const plex = new Plex(configuration.get('plex', 'ip')); +const configuration = require("../../../config/configuration").getInstance(); +const TMDB = require("../../../tmdb/tmdb"); +const Plex = require("../../../plex/plex"); +const tmdb = new TMDB(configuration.get("tmdb", "apiKey")); +const plex = new Plex(configuration.get("plex", "ip")); function handleError(error, res) { const { status, message } = error; if (status && message) { - res.status(status).send({ success: false, message }) + res.status(status).send({ success: false, message }); } else { - console.log('caught showinfo controller error', error) + console.log("caught showinfo controller error", error); res.status(500).send({ - message: 'An unexpected error occured while requesting show info.' - }) + message: "An unexpected error occured while requesting show info." + }); } } /** - * Controller: Retrieve information for a show + * Controller: Retrieve information for a show * @param {Request} req http request variable * @param {Response} res * @returns {Callback} @@ -28,26 +28,27 @@ async function showInfoController(req, res) { const showId = req.params.id; let { credits, check_existance } = req.query; - credits && credits.toLowerCase() === 'true' ? credits = true : credits = false - check_existance && check_existance.toLowerCase() === 'true' ? check_existance = true : check_existance = false + credits && credits.toLowerCase() === "true" + ? (credits = true) + : (credits = false); + check_existance && check_existance.toLowerCase() === "true" + ? (check_existance = true) + : (check_existance = false); - let tmdbQueue = [tmdb.showInfo(showId)] - if (credits) - tmdbQueue.push(tmdb.showCredits(showId)) + let tmdbQueue = [tmdb.showInfo(showId)]; + if (credits) tmdbQueue.push(tmdb.showCredits(showId)); try { - const [Show, Credits] = await Promise.all(tmdbQueue) + const [Show, Credits] = await Promise.all(tmdbQueue); - const show = Show.createJsonResponse() - if (credits) - show.credits = Credits.createJsonResponse() + const show = Show.createJsonResponse(); + if (credits) show.credits = Credits.createJsonResponse(); - if (check_existance) - show.exists_in_plex = await plex.existsInPlex(show) + if (check_existance) show.exists_in_plex = await plex.existsInPlex(show); - res.send(show) - } catch(error) { - handleError(error, res) + res.send(show); + } catch (error) { + handleError(error, res); } } diff --git a/seasoned_api/src/webserver/controllers/user/authenticatePlexAccount.js b/seasoned_api/src/webserver/controllers/user/authenticatePlexAccount.js index 4bd7466..19a2749 100644 --- a/seasoned_api/src/webserver/controllers/user/authenticatePlexAccount.js +++ b/seasoned_api/src/webserver/controllers/user/authenticatePlexAccount.js @@ -1,4 +1,4 @@ -const UserRepository = require("src/user/userRepository"); +const UserRepository = require("../../../user/userRepository"); const userRepository = new UserRepository(); const fetch = require("node-fetch"); const FormData = require("form-data"); diff --git a/seasoned_api/src/webserver/controllers/user/login.js b/seasoned_api/src/webserver/controllers/user/login.js index 46fc895..c0098ca 100644 --- a/seasoned_api/src/webserver/controllers/user/login.js +++ b/seasoned_api/src/webserver/controllers/user/login.js @@ -1,8 +1,8 @@ -const User = require("src/user/user"); -const Token = require("src/user/token"); -const UserSecurity = require("src/user/userSecurity"); -const UserRepository = require("src/user/userRepository"); -const configuration = require("src/config/configuration").getInstance(); +const User = require("../../../user/user"); +const Token = require("../../../user/token"); +const UserSecurity = require("../../../user/userSecurity"); +const UserRepository = require("../../../user/userRepository"); +const configuration = require("../../../config/configuration").getInstance(); const secret = configuration.get("authentication", "secret"); const userSecurity = new UserSecurity(); diff --git a/seasoned_api/src/webserver/controllers/user/register.js b/seasoned_api/src/webserver/controllers/user/register.js index c08569a..e3a5d71 100644 --- a/seasoned_api/src/webserver/controllers/user/register.js +++ b/seasoned_api/src/webserver/controllers/user/register.js @@ -1,8 +1,8 @@ -const User = require("src/user/user"); -const Token = require("src/user/token"); -const UserSecurity = require("src/user/userSecurity"); -const UserRepository = require("src/user/userRepository"); -const configuration = require("src/config/configuration").getInstance(); +const User = require("../../../user/user"); +const Token = require("../../../user/token"); +const UserSecurity = require("../../../user/userSecurity"); +const UserRepository = require("../../../user/userRepository"); +const configuration = require("../../../config/configuration").getInstance(); const secret = configuration.get("authentication", "secret"); const userSecurity = new UserSecurity(); diff --git a/seasoned_api/src/webserver/controllers/user/requests.js b/seasoned_api/src/webserver/controllers/user/requests.js index d6d70b5..e412c64 100644 --- a/seasoned_api/src/webserver/controllers/user/requests.js +++ b/seasoned_api/src/webserver/controllers/user/requests.js @@ -1,4 +1,4 @@ -const RequestRepository = require("src/plex/requestRepository.js"); +const RequestRepository = require("../../../plex/requestRepository.js"); const requestRepository = new RequestRepository(); diff --git a/seasoned_api/src/webserver/controllers/user/searchHistory.js b/seasoned_api/src/webserver/controllers/user/searchHistory.js index 6e453d8..2d6b70a 100644 --- a/seasoned_api/src/webserver/controllers/user/searchHistory.js +++ b/seasoned_api/src/webserver/controllers/user/searchHistory.js @@ -1,4 +1,4 @@ -const SearchHistory = require("src/searchHistory/searchHistory"); +const SearchHistory = require("../../../searchHistory/searchHistory"); const searchHistory = new SearchHistory(); diff --git a/seasoned_api/src/webserver/controllers/user/settings.js b/seasoned_api/src/webserver/controllers/user/settings.js index 883a04d..b69b322 100644 --- a/seasoned_api/src/webserver/controllers/user/settings.js +++ b/seasoned_api/src/webserver/controllers/user/settings.js @@ -1,4 +1,4 @@ -const UserRepository = require("src/user/userRepository"); +const UserRepository = require("../../../user/userRepository"); const userRepository = new UserRepository(); /** * Controller: Retrieves settings of a logged in user diff --git a/seasoned_api/src/webserver/controllers/user/viewHistory.js b/seasoned_api/src/webserver/controllers/user/viewHistory.js index 9f49fc9..8a5334e 100644 --- a/seasoned_api/src/webserver/controllers/user/viewHistory.js +++ b/seasoned_api/src/webserver/controllers/user/viewHistory.js @@ -1,5 +1,5 @@ -const configuration = require("src/config/configuration").getInstance(); -const Tautulli = require("src/tautulli/tautulli"); +const configuration = require("../../../config/configuration").getInstance(); +const Tautulli = require("../../../tautulli/tautulli"); const apiKey = configuration.get("tautulli", "apiKey"); const ip = configuration.get("tautulli", "ip"); const port = configuration.get("tautulli", "port"); diff --git a/seasoned_api/src/webserver/middleware/mustBeAdmin.js b/seasoned_api/src/webserver/middleware/mustBeAdmin.js index 3fe9c50..be516c6 100644 --- a/seasoned_api/src/webserver/middleware/mustBeAdmin.js +++ b/seasoned_api/src/webserver/middleware/mustBeAdmin.js @@ -1,27 +1,31 @@ -const establishedDatabase = require('src/database/database'); +const establishedDatabase = require("../../database/database"); const mustBeAdmin = (req, res, next) => { - let database = establishedDatabase; + let database = establishedDatabase; - if (req.loggedInUser === undefined) { - return res.status(401).send({ - success: false, - message: 'You must be logged in.', + if (req.loggedInUser === undefined) { + return res.status(401).send({ + success: false, + message: "You must be logged in." + }); + } else { + database + .get( + `SELECT admin FROM user WHERE user_name IS ?`, + req.loggedInUser.username + ) + .then(isAdmin => { + console.log(isAdmin, req.loggedInUser); + if (isAdmin.admin == 0) { + return res.status(401).send({ + success: false, + message: "You must be logged in as a admin." + }); + } }); - } else { - database.get(`SELECT admin FROM user WHERE user_name IS ?`, req.loggedInUser.username) - .then((isAdmin) => { - console.log(isAdmin, req.loggedInUser) - if (isAdmin.admin == 0) { - return res.status(401).send({ - success: false, - message: 'You must be logged in as a admin.' - }) - } - }) - } + } - return next(); + return next(); }; module.exports = mustBeAdmin; diff --git a/seasoned_api/src/webserver/middleware/mustHaveAccountLinkedToPlex.js b/seasoned_api/src/webserver/middleware/mustHaveAccountLinkedToPlex.js index 67f6944..d3413d0 100644 --- a/seasoned_api/src/webserver/middleware/mustHaveAccountLinkedToPlex.js +++ b/seasoned_api/src/webserver/middleware/mustHaveAccountLinkedToPlex.js @@ -1,31 +1,35 @@ -const establishedDatabase = require('src/database/database'); +const establishedDatabase = require("../../database/database"); const mustHaveAccountLinkedToPlex = (req, res, next) => { - let database = establishedDatabase; - const loggedInUser = req.loggedInUser; + let database = establishedDatabase; + const loggedInUser = req.loggedInUser; - if (loggedInUser === undefined) { - return res.status(401).send({ - success: false, - message: 'You must have your account linked to a plex account.', - }); - } else { - database.get(`SELECT plex_userid FROM settings WHERE user_name IS ?`, loggedInUser.username) + if (loggedInUser === undefined) { + return res.status(401).send({ + success: false, + message: "You must have your account linked to a plex account." + }); + } else { + database + .get( + `SELECT plex_userid FROM settings WHERE user_name IS ?`, + loggedInUser.username + ) .then(row => { - const plex_userid = row.plex_userid; - - if (plex_userid === null || plex_userid === undefined) { - return res.status(403).send({ - success: false, - message: 'No plex account user id found for your user. Please authenticate your plex account at /user/authenticate.' - }) - } else { - req.loggedInUser.plex_userid = plex_userid; - return next(); - } - }) - } + const plex_userid = row.plex_userid; + if (plex_userid === null || plex_userid === undefined) { + return res.status(403).send({ + success: false, + message: + "No plex account user id found for your user. Please authenticate your plex account at /user/authenticate." + }); + } else { + req.loggedInUser.plex_userid = plex_userid; + return next(); + } + }); + } }; module.exports = mustHaveAccountLinkedToPlex; diff --git a/seasoned_api/src/webserver/middleware/reqTokenToUser.js b/seasoned_api/src/webserver/middleware/reqTokenToUser.js index e3cea6a..0b92cef 100644 --- a/seasoned_api/src/webserver/middleware/reqTokenToUser.js +++ b/seasoned_api/src/webserver/middleware/reqTokenToUser.js @@ -1,6 +1,6 @@ /* eslint-disable no-param-reassign */ -const configuration = require("src/config/configuration").getInstance(); -const Token = require("src/user/token"); +const configuration = require("../../config/configuration").getInstance(); +const Token = require("../../user/token"); const secret = configuration.get("authentication", "secret"); diff --git a/seasoned_api/src/webserver/server.js b/seasoned_api/src/webserver/server.js index a3f91b4..95dc9c7 100644 --- a/seasoned_api/src/webserver/server.js +++ b/seasoned_api/src/webserver/server.js @@ -1,11 +1,11 @@ -const config = require('src/config/configuration').getInstance(); -const app = require('./app'); +const config = require("../config/configuration").getInstance(); +const app = require("./app"); -module.exports = app.listen(config.get('webserver', 'port'), () => { - /* eslint-disable no-console */ - console.log('seasonedAPI'); - /* eslint-disable no-console */ - console.log(`Database is located at ${config.get('database', 'host')}`); - /* eslint-disable no-console */ - console.log(`Webserver is listening on ${config.get('webserver', 'port')}`); +module.exports = app.listen(config.get("webserver", "port"), () => { + /* eslint-disable no-console */ + console.log("seasonedAPI"); + /* eslint-disable no-console */ + console.log(`Database is located at ${config.get("database", "host")}`); + /* eslint-disable no-console */ + console.log(`Webserver is listening on ${config.get("webserver", "port")}`); });