Skip to content

Commit

Permalink
fix: discovery endpoints where querying the user based on wrong infor…
Browse files Browse the repository at this point in the history
…mation
  • Loading branch information
ADRFranklin committed Mar 29, 2024
1 parent 8c267a2 commit a356aa6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
11 changes: 5 additions & 6 deletions pkg/api/src/api/routes/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Router from '@koa/router';
import { StatusCodes } from 'http-status-codes';
import { Context } from 'koa';

import { StatusNotFound } from "../http/request";
import logger from '@/loaders/logger';
import { UserModel } from '@/models/user';
import getDiscovery from '@/services/discovery/display';

const getMovies = async (ctx: Context) => {
const user = await UserModel.findOne({ id: ctx.state.user.id });

const user = await UserModel.findById(ctx.state.user.id);
if (!user) {
ctx.state = StatusCodes.NOT_FOUND;
StatusNotFound(ctx, "Could not get discovery by authed user");
return;
}

Expand All @@ -28,10 +28,9 @@ const getMovies = async (ctx: Context) => {
};

const getShows = async (ctx: Context) => {
const user = await UserModel.findOne({ id: ctx.state.user.id });

const user = await UserModel.findById(ctx.state.user.id);
if (!user) {
ctx.state = StatusCodes.NOT_FOUND;
StatusNotFound(ctx, "Could not get discovery by authed user");
return;
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/src/services/discovery/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
export default async (id, type = 'movie') => {
if (!id) return { error: 'No ID' };
try {
const discoveryPrefs: any = await Discovery.findOne({ id }).exec();
const discoveryPrefs: any = await Discovery.findOne({ altId: id }).exec();
const popular: any = [];
const [upcoming, popularData]: any = await Promise.all([
comingSoon(type),
Expand Down
29 changes: 20 additions & 9 deletions pkg/api/src/services/plex/top.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function getTopData(type) {
d.setMonth(d.getMonth() - 1);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
const timestamp = (d.getTime() / 1000) | 0;
const timestamp = (d.getTime() / 1000) || 0;

const url = MakePlexURL('/library/all/top', {
type,
Expand All @@ -43,21 +43,32 @@ async function parseTop(data, type) {
const top = data.MediaContainer.Metadata;
const output = {};
if (!top)
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw "No Plex Top Data, you're probably not a Plex Pass user. This is not an error";
for (let i = 0; i < top.length; i++) {
const item = top[i];
const {ratingKey} = item;
const promises = top.map(async (item: { ratingKey: any; }) => {
const { ratingKey } = item;
let plexData: any = false;
if (type === 2) {
plexData = await plexLookup(ratingKey, 'show');
} else {
plexData = await plexLookup(ratingKey, 'movie');
}
if (plexData.tmdb_id)
output[plexData.tmdb_id] =
type === 2
? await showLookup(plexData.tmdb_id, true)
: await movieLookup(plexData.tmdb_id, true);
if (plexData.tmdb_id) {
return type === 2
? showLookup(plexData.tmdb_id, true)
: movieLookup(plexData.tmdb_id, true);
}
return null;
});
const results = await Promise.all(promises);

for (let i = 0; i < top.length; i++) {
const item = top[i];
const { ratingKey } = item;
const result = results[i];
if (result) {
output[result.tmdb_id] = result;
}
}
return output;
}

0 comments on commit a356aa6

Please sign in to comment.