Fix: Linter warnings (#137)

* Automaticly fixable eslint issues, mostly 3 -> 2 space indentation

* fix: updated plex_userid to camelcase

* Linted and some consistency refactor on middleware

* eslint uses ecmaversion 2020 & allow empty catch rule

* Started linting source files

* Fixed eslint errors & improved a lot of error handling

* Set 2 eslint rules as warning temporarly
This commit is contained in:
2022-08-20 17:21:25 +02:00
committed by GitHub
parent cfbd4965db
commit 1815a429b0
83 changed files with 1625 additions and 1294 deletions

View File

@@ -1,9 +1,17 @@
const assert = require("assert");
const configuration = require("../config/configuration").getInstance();
const TMDB = require("../tmdb/tmdb");
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
// const configuration = require("../config/configuration").getInstance();
// const TMDB = require("../tmdb/tmdb");
const establishedDatabase = require("../database/database");
const utils = require("./utils");
// const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
// function mapToTmdbByType(rows) {
// return rows.map(row => {
// if (row.type === "movie") return tmdb.movieInfo(row.id);
// if (row.type === "show") return tmdb.showInfo(row.id);
// return null;
// });
// }
class RequestRepository {
constructor(database) {
@@ -18,85 +26,24 @@ class RequestRepository {
'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)",
// 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)",
// 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 ?",
// 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 ?"
};
}
sortAndFilterToDbQuery(by, direction, filter, query) {
let dbQuery = undefined;
if (query !== undefined) {
const dbParams = [query, query];
const dbquery = this.queries.fetchAllQuery;
dbQuery = dbquery
.split("")
.map(char => (char === "?" ? dbParams.shift() : char))
.join("");
} else if (by !== undefined && filter !== undefined) {
const paramToColumnAndValue = {
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) {
const dbParams = [by, direction];
const query = this.queries.fetchAllSort;
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"]
// downloading: [this.database.delugeStatus, 'downloading']
};
const dbParams = paramToColumnAndValue[filter];
const query = this.queries.fetchAllFilter;
dbQuery = query
.split("")
.map(char => (char === "?" ? dbParams.shift() : char))
.join("");
} else {
dbQuery = this.queries.fetchAll;
}
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);
});
}
/**
* Add tmdb movie|show to requests
* @param {tmdb} tmdb class of movie|show to add
* @returns {Promise}
*/
requestFromTmdb(tmdb, ip, user_agent, username) {
requestFromTmdb(tmdb, ip, userAgent, username) {
return Promise.resolve()
.then(() => this.database.get(this.queries.read, [tmdb.id, tmdb.type]))
.then(row =>
@@ -111,7 +58,7 @@ class RequestRepository {
tmdb.backdrop,
username,
ip,
user_agent,
userAgent,
tmdb.type
])
)
@@ -122,7 +69,6 @@ class RequestRepository {
) {
throw new Error("This id is already requested", error.message);
}
console.log("Error @ request.addTmdb:", error);
throw new Error("Could not add request");
});
}
@@ -152,20 +98,12 @@ class RequestRepository {
/**
* Fetch all requests with optional sort and filter params
* @param {String} what we are sorting by
* @param {String} direction that can be either 'asc' or 'desc', default 'asc'.
* @param {String} params to filter by
* @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, filter = null) {
// TODO implemented sort and filter
page = parseInt(page);
const page = parseInt(_page, 10);
let fetchQuery = this.queries.fetchAll;
let fetchTotalResults = this.queries.totalRequests;
let fetchParams = [page];
@@ -176,26 +114,24 @@ class RequestRepository {
filter === "downloaded" ||
filter === "requested")
) {
console.log("tes");
fetchQuery = this.queries.fetchAllFilteredStatus;
fetchTotalResults = this.queries.totalRequestsFilteredStatus;
fetchParams = [filter, page];
} else {
filter = undefined;
}
return Promise.resolve()
.then(dbQuery => this.database.all(fetchQuery, fetchParams))
return this.database
.all(fetchQuery, fetchParams)
.then(async rows => {
const sqliteResponse = await this.database.get(
fetchTotalResults,
filter ? filter : undefined
filter || null
);
const totalRequests = sqliteResponse["totalRequests"];
const { totalRequests } = sqliteResponse;
const totalPages = Math.ceil(totalRequests / 26);
return [
rows.map(item => {
rows.map(_item => {
const item = _item;
item.poster = item.poster_path;
delete item.poster_path;
item.backdrop = item.background_path;
@@ -205,18 +141,18 @@ class RequestRepository {
totalPages,
totalRequests
];
return Promise.all(this.mapToTmdbByType(rows));
// return mapToTmdbByType(rows);
})
.then(([result, totalPages, totalRequests]) =>
Promise.resolve({
results: result,
total_results: totalRequests,
page: page,
page,
total_pages: totalPages
})
)
.catch(error => {
console.log(error);
throw error;
});
}