Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoPress: skip rating checking when pulling video data for block #28374

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

VideoPress: skip rating checking when pulling video data for the block
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ export function useSyncMedia(
options: UseSyncMediaOptionsProps
): UseSyncMediaProps {
const { id, guid } = attributes;
const { videoData, isRequestingVideoData } = useVideoData( { id, guid } );
const { videoData, isRequestingVideoData } = useVideoData( {
id,
guid,
skipRatingControl: true,
} );

const isSaving = useSelect( select => select( editorStore ).isSavingPost(), [] );
const wasSaving = usePrevious( isSaving );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { decodeEntities } from '../../../lib/url';
/**
* Types
*/
import { WPCOMRestAPIVideosGetEndpointResponseProps } from '../../../types';
import {
WPCOMRestAPIVideosGetEndpointRequestArguments,
WPCOMRestAPIVideosGetEndpointResponseProps,
} from '../../../types';
import { UseVideoDataProps, UseVideoDataArgumentsProps, VideoDataProps } from './types';

/**
Expand All @@ -23,6 +26,7 @@ import { UseVideoDataProps, UseVideoDataArgumentsProps, VideoDataProps } from '.
export default function useVideoData( {
id,
guid,
skipRatingControl = false,
}: UseVideoDataArgumentsProps ): UseVideoDataProps {
const [ videoData, setVideoData ] = useState< VideoDataProps >( {} );
const [ isRequestingVideoData, setIsRequestingVideoData ] = useState( false );
Expand All @@ -34,12 +38,26 @@ export default function useVideoData( {
async function fetchVideoItem() {
try {
const tokenData = await getMediaToken( 'playback', { id, guid } );
const params = tokenData?.token
? `?${ new URLSearchParams( { metadata_token: tokenData.token } ).toString() }`
const params: WPCOMRestAPIVideosGetEndpointRequestArguments = {};

// Add the token to the request if it exists.
if ( tokenData?.token ) {
params.metadata_token = tokenData.token;
}

// Add the birthdate to skip the rating check if it's required.
if ( skipRatingControl ) {
params.birth_day = '1';
params.birth_month = '1';
params.birth_year = '2000';
}

const requestArgs = Object.keys( params ).length
? `?${ new URLSearchParams( params ).toString() }`
: '';

const response: WPCOMRestAPIVideosGetEndpointResponseProps = await apiFetch( {
url: `https://public-api.wordpress.com/rest/v1.1/videos/${ guid }${ params }`,
url: `https://public-api.wordpress.com/rest/v1.1/videos/${ guid }${ requestArgs }`,
credentials: 'omit',
global: true,
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { VideoGUID, VideoId } from '../../blocks/video/types';
export type UseVideoDataArgumentsProps = {
id?: VideoId;
guid?: VideoGUID;
isPrivate?: boolean;
lhkowalski marked this conversation as resolved.
Show resolved Hide resolved
skipRatingControl: boolean;
};

export type VideoDataProps = {
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/videopress/src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ export type WPV2mediaGetEndpointResponseProps = {
/*
* https://public-api.wordpress.com/rest/v1.1/videos/${ guid }
*/
export type WPCOMRestAPIVideosGetEndpointRequestArguments = {
metadata_token?: string;
birth_day?: string;
birth_month?: string;
birth_year?: string;
};

export type WPCOMRestAPIVideosGetEndpointResponseProps = {
// source_url: string;

Expand Down