Skip to content

Commit

Permalink
fix: wrong videos count/data due to wrong API response
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Nov 5, 2022
1 parent 1ede6dc commit 5e67fdc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ export function getDateBefore(before: number = 0): Date {

// -------------------------------------------------------------------

/**
* Convert the given date to ISO format
*
* @param date {Date}
*/
export function date2ISO(date: Date): string {
// const year = date.getFullYear();
// const month = date.getMonth() + 1;
// const day = String(date.getDate()).padStart(2, '0');
// return `${year}-${month}-${day}T00:00:00Z`;
return date.toISOString();
}

// -------------------------------------------------------------------

/**
* Check if the given timestamp is in today's date
* Stolen from: https://stackoverflow.com/a/40628566
Expand Down
51 changes: 40 additions & 11 deletions src/store/services/youtube/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,21 @@ const queries = {
maxResults,
},
}),
transformResponse: (response: Response): GetChannelActivitiesResponse => ({
items: response.items
transformResponse: (response: Response): GetChannelActivitiesResponse => {
const items = response.items
.filter((item) => item.snippet.type === 'upload')
.map((item) => ({
videoId: item.contentDetails.upload.videoId,
})),
count: response.items.length,
total: response.pageInfo.totalResults,
}),
}));
const count = items.length;
const total =
response.pageInfo.totalResults - (response.items.length - count);
return {
items,
count,
total,
};
},
},
// Videos informations query
getVideosById: {
Expand Down Expand Up @@ -245,12 +251,17 @@ export const extendedApi = youtubeApi.injectEndpoints({
}
return { error: activities.error as FetchBaseQueryError };
}
const { items, count, total } =
queries.getChannelActivities.transformResponse(
activities.data as Response,
);
const activitiesData = queries.getChannelActivities.transformResponse(
activities.data as Response,
);
let count = activitiesData.items.length;
let total = activitiesData.total;
// Fix: wrong total count (ex: maxResults = 6, total = 2, count = 1)
if (maxResults && maxResults >= total && count < total) {
total = count;
}
// Fetch channel videos
const ids = items.map(({ videoId }) => videoId);
const ids = activitiesData.items.map(({ videoId }) => videoId);
if (ids.length === 0) {
return {
data: {
Expand All @@ -272,13 +283,31 @@ export const extendedApi = youtubeApi.injectEndpoints({
const videosData = queries.getVideosById.transformResponse(
result.data as Response,
);
// Fix: force filter by publish date (since we may receive wrong activities data from the youtube API sometimes)
if (publishedAfter) {
videosData.items = videosData.items.filter(
(video) => video.publishedAt >= new Date(publishedAfter).getTime(),
);
const newCount = videosData.items.length;
if (newCount < count && maxResults && newCount < maxResults) {
total = newCount;
} else {
total = total - (count - newCount);
}
count = newCount;
}
// Apply custom user filters
if (channel.filters && channel.filters.length > 0) {
videosData.items = videosData.items.filter((video) =>
channel.filters!.some((filter) => {
const videoField = parseVideoField(video, filter.field);
return evaluateField(videoField, filter.operator, filter.value);
}),
);
// Recalculate total & count
const newCount = videosData.items.length;
total = total - (count - newCount);
count = newCount;
}
return {
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
selectVideosSeniority,
selectViewFilters,
} from 'store/selectors/settings';
import { getDateBefore } from 'helpers/utils';
import { date2ISO, getDateBefore } from 'helpers/utils';
import DefaultRenderer, { DefaultRendererProps } from './DefaultRenderer';
import { selectClassifiedChannelVideos } from 'store/selectors/videos';
import { HomeView, Video, VideosSeniority } from 'types';
Expand All @@ -31,7 +31,7 @@ function AllViewRenderer(props: AllViewRendererProps) {
() =>
videosSeniority === VideosSeniority.Any
? undefined
: getDateBefore(videosSeniority).toISOString(),
: date2ISO(getDateBefore(videosSeniority)),
[videosSeniority],
);

Expand Down
4 changes: 2 additions & 2 deletions src/ui/components/webext/Background/ChannelChecker.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react';
import { Channel, Video } from 'types';
import { useGetChannelVideosQuery } from 'store/services/youtube';
import { getDateBefore } from 'helpers/utils';
import { date2ISO, getDateBefore } from 'helpers/utils';
import { useAppSelector } from 'store';
import { selectChannelVideos } from 'store/selectors/videos';
import { useInterval, useStateRef } from 'hooks';
Expand All @@ -27,7 +27,7 @@ export default function ChannelChecker(props: ChannelCheckerProps) {
const [ready, setReady] = useState(false);
const cachedVideos = useAppSelector(selectChannelVideos(channel));
const cachedVideosRef = useStateRef(cachedVideos);
const publishedAfter = getDateBefore(config.videosSeniority).toISOString();
const publishedAfter = date2ISO(getDateBefore(config.videosSeniority));
const pollingInterval = config.checkInterval * 60000; // convert minutes to milliseconds
const { data, isFetching, refetch } = useGetChannelVideosQuery(
{
Expand Down

0 comments on commit 5e67fdc

Please sign in to comment.