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

validateAssetMeta merges any existing asset meta #1233

Merged
merged 2 commits into from
Sep 12, 2022

Conversation

Shih-Yu
Copy link
Collaborator

@Shih-Yu Shih-Yu commented Aug 26, 2022

What does this pull request do? Explain your changes. (required)

Addressing the issue #1229

Specific updates (required)

Change validateAssetMeta function to merge with any existing meta

  • I have read the CONTRIBUTING document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@Shih-Yu Shih-Yu added the bug Something isn't working label Aug 26, 2022
@Shih-Yu Shih-Yu requested a review from victorges August 26, 2022 20:14
@Shih-Yu Shih-Yu self-assigned this Aug 26, 2022
@Shih-Yu Shih-Yu requested a review from a team as a code owner August 26, 2022 20:14
@Shih-Yu Shih-Yu linked an issue Aug 26, 2022 that may be closed by this pull request
@vercel
Copy link

vercel bot commented Aug 26, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
livepeer-studio ✅ Ready (Inspect) Visit Preview Sep 6, 2022 at 9:50PM (UTC)

Copy link
Member

@victorges victorges left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add some unit tests for this feature as I believe you have not exercised the code to make sure it works (right now it does not).

The tests would go around here (as separate its): https://github.com/livepeer/livepeer-com/blob/845e074c9f755e876bb23db83b374901d4c68842/packages/api/src/controllers/asset.test.ts#L218

You should add some cases like:

  • Allows adding meta to an asset
  • Allows adding fields to an existing meta in an asset
  • Allows removing a filed from the meta by setting it to null
  • Disallows any update that would result in a meta greater than that META_MAX_SIZE (incl if it only hits max size after the merge)

@@ -47,7 +47,10 @@ const app = Router();

const META_MAX_SIZE = 1024;

function validateAssetMeta(meta: Record<string, string>) {
function validateAssetMeta( meta: Record<string, string>, asset?:WithID<Asset> ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently the prettier is still not working on your env. Run yarn prettier manually from the project root instead.

@@ -47,7 +47,10 @@ const app = Router();

const META_MAX_SIZE = 1024;

function validateAssetMeta(meta: Record<string, string>) {
function validateAssetMeta( meta: Record<string, string>, asset?:WithID<Asset> ) {
meta = _({ ...asset.meta, ...meta })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The asset is not always available so this needs to be ?.

Suggested change
meta = _({ ...asset.meta, ...meta })
meta = _({ ...asset?.meta, ...meta })

function validateAssetMeta(meta: Record<string, string>) {
function validateAssetMeta( meta: Record<string, string>, asset?:WithID<Asset> ) {
meta = _({ ...asset.meta, ...meta })
.omit(_.isNull)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not "compile". You need to fix your typescript env and pay attention to the errors it shows. The correct function to call here is omitBy (but you also need to test it)
Screen Shot 2022-08-26 at 17 38 33

if (!asset || (asset.userId !== req.user.id && !req.user.admin)) {
throw new NotFoundError(`asset not found`);
} else if (asset.status.phase !== "ready") {
throw new UnprocessableEntityError(`asset is not ready`);
}
validateAssetMeta(meta, asset);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of this function is being ignored. It is also not the only place where this is called. Anytime you change the behavior of a function you need to update everywhere it is used. So keep that in mind and look for the other usages in the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually... This function is already being called in this API handler. You should call it only once. No reason to call it again. Remove this line and edit the original call instead.

Copy link
Member

@victorges victorges left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Shih-Yu Shih-Yu merged commit 25b49e4 into master Sep 12, 2022
@Shih-Yu Shih-Yu deleted the sh/fix/vod/metadataoverride branch September 12, 2022 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Updating meta in VoD overrides existing fields
2 participants