<commit_before>const express = require("express");
const Raven = require("raven");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");

const configuration = require("src/config/configuration").getInstance();

const reqTokenToUser = require("./middleware/reqTokenToUser");
const mustBeAuthenticated = require("./middleware/mustBeAuthenticated");
const mustBeAdmin = require("./middleware/mustBeAdmin");
const mustHaveAccountLinkedToPlex = require("./middleware/mustHaveAccountLinkedToPlex");

const listController = require("./controllers/list/listController");
const tautulli = require("./controllers/user/viewHistory.js");
const SettingsController = require("./controllers/user/settings");
const AuthenticatePlexAccountController = require("./controllers/user/authenticatePlexAccount");

// TODO: Have our raven router check if there is a value, if not don't enable raven.
Raven.config(configuration.get("raven", "DSN")).install();

const app = express(); // define our app using express
app.use(Raven.requestHandler());
app.use(bodyParser.json());
app.use(cookieParser());

const router = express.Router();
const allowedOrigins = configuration.get("webserver", "origins");

// TODO: All JSON handling in a single router
// router.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

/* Check header and cookie for authentication and set req.loggedInUser */
router.use(reqTokenToUser);

// TODO: Should have a separate middleware/router for handling headers.
router.use((req, res, next) => {
  // TODO add logging of all incoming
  const origin = req.headers.origin;
  if (allowedOrigins.indexOf(origin) > -1) {
    res.setHeader("Access-Control-Allow-Origin", origin);
  }
  res.header(
    "Access-Control-Allow-Headers",
    "Content-Type, Authorization, loggedinuser"
  );
  res.header("Access-Control-Allow-Methods", "POST, GET, PUT");

  next();
});

router.get("/", function mainHandler(req, res) {
  throw new Error("Broke!");
});

app.use(Raven.errorHandler());
app.use(function onError(err, req, res, next) {
  res.statusCode = 500;
  res.end(res.sentry + "\n");
});

/**
 * User
 */
router.post("/v1/user", require("./controllers/user/register.js"));
router.post("/v1/user/login", require("./controllers/user/login.js"));
router.post("/v1/user/logout", require("./controllers/user/logout.js"));
router.get(
  "/v1/user/settings",
  mustBeAuthenticated,
  SettingsController.getSettingsController
);
router.put(
  "/v1/user/settings",
  mustBeAuthenticated,
  SettingsController.updateSettingsController
);
router.get(
  "/v1/user/search_history",
  mustBeAuthenticated,
  require("./controllers/user/searchHistory.js")
);
router.get(
  "/v1/user/requests",
  mustBeAuthenticated,
  require("./controllers/user/requests.js")
);
router.post(
  "/v1/user/link_plex",
  mustBeAuthenticated,
  AuthenticatePlexAccountController.link
);
router.post(
  "/v1/user/unlink_plex",
  mustBeAuthenticated,
  AuthenticatePlexAccountController.unlink
);

router.get(
  "/v1/user/view_history",
  mustHaveAccountLinkedToPlex,
  tautulli.userViewHistoryController
);
router.get(
  "/v1/user/watch_time",
  mustHaveAccountLinkedToPlex,
  tautulli.watchTimeStatsController
);
router.get(
  "/v1/user/plays_by_day",
  mustHaveAccountLinkedToPlex,
  tautulli.getPlaysByDaysController
);
router.get(
  "/v1/user/plays_by_dayofweek",
  mustHaveAccountLinkedToPlex,
  tautulli.getPlaysByDayOfWeekController
);

/**
 * Seasoned
 */
router.get("/v1/seasoned/all", require("./controllers/seasoned/readStrays.js"));
router.get(
  "/v1/seasoned/:strayId",
  require("./controllers/seasoned/strayById.js")
);
router.post(
  "/v1/seasoned/verify/:strayId",
  require("./controllers/seasoned/verifyStray.js")
);

router.get("/v2/search/", require("./controllers/search/multiSearch.js"));
router.get("/v2/search/movie", require("./controllers/search/movieSearch.js"));
router.get("/v2/search/show", require("./controllers/search/showSearch.js"));
router.get(
  "/v2/search/person",
  require("./controllers/search/personSearch.js")
);

router.get("/v2/movie/now_playing", listController.nowPlayingMovies);
router.get("/v2/movie/popular", listController.popularMovies);
router.get("/v2/movie/top_rated", listController.topRatedMovies);
router.get("/v2/movie/upcoming", listController.upcomingMovies);

router.get("/v2/show/now_playing", listController.nowPlayingShows);
router.get("/v2/show/popular", listController.popularShows);
router.get("/v2/show/top_rated", listController.topRatedShows);

router.get("/v2/movie/:id/credits", require("./controllers/movie/credits.js"));
router.get(
  "/v2/movie/:id/release_dates",
  require("./controllers/movie/releaseDates.js")
);
router.get("/v2/show/:id/credits", require("./controllers/show/credits.js"));

router.get("/v2/movie/:id", require("./controllers/movie/info.js"));
router.get("/v2/show/:id", require("./controllers/show/info.js"));
router.get("/v2/person/:id", require("./controllers/person/info.js"));

/**
 * Plex
 */
router.get("/v2/plex/search", require("./controllers/plex/search"));

/**
 * List
 */
router.get("/v1/plex/search", require("./controllers/plex/searchMedia.js"));
router.get("/v1/plex/playing", require("./controllers/plex/plexPlaying.js"));
router.get("/v1/plex/request", require("./controllers/plex/searchRequest.js"));
router.get(
  "/v1/plex/request/:mediaId",
  require("./controllers/plex/readRequest.js")
);
router.post(
  "/v1/plex/request/:mediaId",
  require("./controllers/plex/submitRequest.js")
);
router.post("/v1/plex/hook", require("./controllers/plex/hookDump.js"));

router.get(
  "/v1/plex/watch-link",
  mustBeAuthenticated,
  require("./controllers/plex/watchDirectLink.js")<commit_msg>Registered endpoint for person credits<commit_after>const express = require("express");
const Raven = require("raven");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");

const configuration = require("src/config/configuration").getInstance();

const reqTokenToUser = require("./middleware/reqTokenToUser");
const mustBeAuthenticated = require("./middleware/mustBeAuthenticated");
const mustBeAdmin = require("./middleware/mustBeAdmin");
const mustHaveAccountLinkedToPlex = require("./middleware/mustHaveAccountLinkedToPlex");

const listController = require("./controllers/list/listController");
const tautulli = require("./controllers/user/viewHistory.js");
const SettingsController = require("./controllers/user/settings");
const AuthenticatePlexAccountController = require("./controllers/user/authenticatePlexAccount");

// TODO: Have our raven router check if there is a value, if not don't enable raven.
Raven.config(configuration.get("raven", "DSN")).install();

const app = express(); // define our app using express
app.use(Raven.requestHandler());
app.use(bodyParser.json());
app.use(cookieParser());

const router = express.Router();
const allowedOrigins = configuration.get("webserver", "origins");

// TODO: All JSON handling in a single router
// router.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

/* Check header and cookie for authentication and set req.loggedInUser */
router.use(reqTokenToUser);

// TODO: Should have a separate middleware/router for handling headers.
router.use((req, res, next) => {
  // TODO add logging of all incoming
  const origin = req.headers.origin;
  if (allowedOrigins.indexOf(origin) > -1) {
    res.setHeader("Access-Control-Allow-Origin", origin);
  }
  res.header(
    "Access-Control-Allow-Headers",
    "Content-Type, Authorization, loggedinuser"
  );
  res.header("Access-Control-Allow-Methods", "POST, GET, PUT");

  next();
});

router.get("/", function mainHandler(req, res) {
  throw new Error("Broke!");
});

app.use(Raven.errorHandler());
app.use(function onError(err, req, res, next) {
  res.statusCode = 500;
  res.end(res.sentry + "\n");
});

/**
 * User
 */
router.post("/v1/user", require("./controllers/user/register.js"));
router.post("/v1/user/login", require("./controllers/user/login.js"));
router.post("/v1/user/logout", require("./controllers/user/logout.js"));
router.get(
  "/v1/user/settings",
  mustBeAuthenticated,
  SettingsController.getSettingsController
);
router.put(
  "/v1/user/settings",
  mustBeAuthenticated,
  SettingsController.updateSettingsController
);
router.get(
  "/v1/user/search_history",
  mustBeAuthenticated,
  require("./controllers/user/searchHistory.js")
);
router.get(
  "/v1/user/requests",
  mustBeAuthenticated,
  require("./controllers/user/requests.js")
);
router.post(
  "/v1/user/link_plex",
  mustBeAuthenticated,
  AuthenticatePlexAccountController.link
);
router.post(
  "/v1/user/unlink_plex",
  mustBeAuthenticated,
  AuthenticatePlexAccountController.unlink
);

router.get(
  "/v1/user/view_history",
  mustHaveAccountLinkedToPlex,
  tautulli.userViewHistoryController
);
router.get(
  "/v1/user/watch_time",
  mustHaveAccountLinkedToPlex,
  tautulli.watchTimeStatsController
);
router.get(
  "/v1/user/plays_by_day",
  mustHaveAccountLinkedToPlex,
  tautulli.getPlaysByDaysController
);
router.get(
  "/v1/user/plays_by_dayofweek",
  mustHaveAccountLinkedToPlex,
  tautulli.getPlaysByDayOfWeekController
);

/**
 * Seasoned
 */
router.get("/v1/seasoned/all", require("./controllers/seasoned/readStrays.js"));
router.get(
  "/v1/seasoned/:strayId",
  require("./controllers/seasoned/strayById.js")
);
router.post(
  "/v1/seasoned/verify/:strayId",
  require("./controllers/seasoned/verifyStray.js")
);

router.get("/v2/search/", require("./controllers/search/multiSearch.js"));
router.get("/v2/search/movie", require("./controllers/search/movieSearch.js"));
router.get("/v2/search/show", require("./controllers/search/showSearch.js"));
router.get(
  "/v2/search/person",
  require("./controllers/search/personSearch.js")
);

router.get("/v2/movie/now_playing", listController.nowPlayingMovies);
router.get("/v2/movie/popular", listController.popularMovies);
router.get("/v2/movie/top_rated", listController.topRatedMovies);
router.get("/v2/movie/upcoming", listController.upcomingMovies);

router.get("/v2/show/now_playing", listController.nowPlayingShows);
router.get("/v2/show/popular", listController.popularShows);
router.get("/v2/show/top_rated", listController.topRatedShows);

router.get("/v2/movie/:id/credits", require("./controllers/movie/credits.js"));
router.get(
  "/v2/movie/:id/release_dates",
  require("./controllers/movie/releaseDates.js")
);
router.get("/v2/show/:id/credits", require("./controllers/show/credits.js"));

router.get("/v2/movie/:id", require("./controllers/movie/info.js"));
router.get("/v2/show/:id", require("./controllers/show/info.js"));

router.get(
  "/v2/person/:id/credits",
  require("./controllers/person/credits.js")
);
router.get("/v2/person/:id", require("./controllers/person/info.js"));

/**
 * Plex
 */
router.get("/v2/plex/search", require("./controllers/plex/search"));

/**
 * List
 */
router.get("/v1/plex/search", require("./controllers/plex/searchMedia.js"));
router.get("/v1/plex/playing", require("./controllers/plex/plexPlaying.js"));
router.get("/v1/plex/request", require("./controllers/plex/searchRequest.js"));
router.get(
  "/v1/plex/request/:mediaId",
  require("./controllers/plex/readRequest.js")
);
router.post(
  "/v1/plex/request/:mediaId",
  require("./controllers/plex/submitRequest.js")
);
router.post("/v1/plex/hook", require("./controllers/plex/hookDump.js"));

router.get(
  "/v1/plex/watch-link",
  mustBeAuthenticated,
  require("./controllers/plex/watchDirectLink.js")
{'fraction_non_alphanumeric': 0.18174740484429067, 'fraction_numerical': 0.0078719723183391, 'mean_word_length': 5.331325301204819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '106847', 'n_tokens_mistral': 3906, 'n_tokens_neox': 3854, 'n_words': 534}