Skip to content

Commit

Permalink
Merge pull request #49 from lens-protocol/T-17849/lenstube-bytes
Browse files Browse the repository at this point in the history
feat: parse lenstube-bytes videos as SHORT_VIDEO
  • Loading branch information
cesarenaldi authored Dec 6, 2023
2 parents 39005b4 + 312cdff commit 3bc031e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-poets-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lens-protocol/metadata': patch
---

**feat:** parse legacy lenstube-bytes videos as SHORT_VIDEO
68 changes: 46 additions & 22 deletions src/legacy/__tests__/PublicationMetadataSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,28 +767,6 @@ describe(`Given the PublicationMetadataSchema`, () => {
`);
});

// this should indeed fail
it.skip('then it should be resilient to `null` as `content` for TEXT_ONLY publications', () => {
PublicationMetadataSchema.parse({
version: '2.0.0',
metadata_id: '2NIqXaSSHePwi3rKX_4V4',
description: 'test collaborate',
content: null,
external_url: 'https://dataverse.art/',
image:
'https://bafybeif2xgqpawh3t6dj2ftfno7tprugcntdq4qoq7u2vxqvulvzlsiixe.ipfs.nftstorage.link/',
imageMimeType: null,
name: 'testCollaborate',
attributes: [],
media: [],
appId: 'dataverse-v0.1.0',
animation_url: null,
locale: 'en',
tags: [],
mainContentFocus: 'TEXT_ONLY',
});
});

it('then it should be resilient to `null` as `animation_url`', () => {
PublicationMetadataSchema.parse({
version: '2.0.0',
Expand Down Expand Up @@ -1479,4 +1457,50 @@ describe(`Given the PublicationMetadataSchema`, () => {
`);
});
});

describe(`when parsing a 'lenstube-bytes' videos`, () => {
it(`then should flag them as ${PublicationMainFocus.SHORT_VIDEO}`, () => {
expectResult(() =>
PublicationMetadataSchema.safeParse({
version: '2.0.0',
metadata_id: 'd4c19b76-736e-4d41-8d3c-4874b80fdd78',
locale: 'en-GB',
mainContentFocus: 'VIDEO',
name: 'GM 🧘🏼‍♂️🚲🍂',
attributes: [],
media: [
{
item: 'ipfs://bafybeihuephqb3rts3xiz5m3ooutgmz5yand5ayqi4sczkpsflfy33qwvm',
type: 'video/quicktime',
cover: 'ipfs://bafybeiessuuhjv6szov6rwejjpjr6pil7pqpbi4hgnkedh6mzv6iyteqka',
},
],
appId: 'lenstube-bytes',
}),
).toMatchObject({
mainContentFocus: PublicationMainFocus.SHORT_VIDEO,
});

expectResult(() =>
PublicationMetadataSchema.safeParse({
version: '2.0.0',
metadata_id: 'd4c19b76-736e-4d41-8d3c-4874b80fdd78',
locale: 'en-GB',
mainContentFocus: 'VIDEO',
name: 'GM 🧘🏼‍♂️🚲🍂',
attributes: [],
media: [
{
item: 'ipfs://bafybeihuephqb3rts3xiz5m3ooutgmz5yand5ayqi4sczkpsflfy33qwvm',
type: 'video/quicktime',
cover: 'ipfs://bafybeiessuuhjv6szov6rwejjpjr6pil7pqpbi4hgnkedh6mzv6iyteqka',
},
],
appId: 'other-app',
}),
).toMatchObject({
mainContentFocus: PublicationMainFocus.VIDEO,
});
});
});
});
49 changes: 27 additions & 22 deletions src/legacy/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum PublicationMainFocus {
EMBED = latest.PublicationMainFocus.EMBED,
IMAGE = latest.PublicationMainFocus.IMAGE,
LINK = latest.PublicationMainFocus.LINK,
SHORT_VIDEO = latest.PublicationMainFocus.SHORT_VIDEO,
TEXT_ONLY = latest.PublicationMainFocus.TEXT_ONLY,
VIDEO = latest.PublicationMainFocus.VIDEO,
}
Expand Down Expand Up @@ -540,15 +541,25 @@ export type PublicationMetadataV2Video = z.infer<typeof PublicationMetadataV2Vid
/**
* @internal
*/
export const PublicationMetadataV2Schema = z.discriminatedUnion('mainContentFocus', [
PublicationMetadataV2ArticleSchema,
PublicationMetadataV2AudioSchema,
PublicationMetadataV2EmbedSchema,
PublicationMetadataV2ImageSchema,
PublicationMetadataV2LinkSchema,
PublicationMetadataV2TextOnlySchema,
PublicationMetadataV2VideoSchema,
]);
export const PublicationMetadataV2Schema = z
.discriminatedUnion('mainContentFocus', [
PublicationMetadataV2ArticleSchema,
PublicationMetadataV2AudioSchema,
PublicationMetadataV2EmbedSchema,
PublicationMetadataV2ImageSchema,
PublicationMetadataV2LinkSchema,
PublicationMetadataV2TextOnlySchema,
PublicationMetadataV2VideoSchema,
])
.transform((data) => {
if (data.mainContentFocus === PublicationMainFocus.VIDEO && data.appId === 'lenstube-bytes') {
return {
...data,
mainContentFocus: PublicationMainFocus.SHORT_VIDEO,
};
}
return data;
});
export type PublicationMetadataV2 = z.infer<typeof PublicationMetadataV2Schema>;

export type PublicationMetadata = PublicationMetadataV1 | PublicationMetadataV2;
Expand All @@ -574,13 +585,13 @@ export type PublicationMetadata = PublicationMetadataV1 | PublicationMetadataV2;
* // => { success: false, error: ZodError }
* ```
*/
export const PublicationMetadataSchema = z
export const PublicationMetadataSchema: z.ZodType<PublicationMetadata, z.ZodTypeDef, object> = z
.object({
// although not optional it will allow the refine function to provide better error message
version: z.nativeEnum(PublicationMetadataVersion),
})
.passthrough()
.superRefine((data, ctx): data is PublicationMetadata => {
.transform((data, ctx) => {
switch (data.version) {
case PublicationMetadataVersion.V1:
const v1Result = PublicationMetadataV1Schema.safeParse(data);
Expand All @@ -589,8 +600,9 @@ export const PublicationMetadataSchema = z
v1Result.error.issues.forEach((issue) => {
ctx.addIssue(issue);
});
return z.NEVER;
}
break;
return v1Result.data;

case PublicationMetadataVersion.V2:
const v2Result = PublicationMetadataV2Schema.safeParse(data);
Expand All @@ -599,18 +611,11 @@ export const PublicationMetadataSchema = z
v2Result.error.issues.forEach((issue) => {
ctx.addIssue(issue);
});
return z.NEVER;
}
break;

return v2Result.data;
}

return z.NEVER;
})
.transform((data) => {
switch (data.version) {
case PublicationMetadataVersion.V1:
return PublicationMetadataV1Schema.parse(data);

case PublicationMetadataVersion.V2:
return PublicationMetadataV2Schema.parse(data);
}
});

0 comments on commit 3bc031e

Please sign in to comment.