This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tk:shared): defined metadata filters based on nature type
- Loading branch information
1 parent
cd83de4
commit 4a7776c
Showing
20 changed files
with
356 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { APIError, toAPIError } from '@shared/errors/APIError'; | ||
import { toValidationError } from '@shared/errors/ValidationError'; | ||
import { Metadata } from '@shared/models/Metadata'; | ||
import { string2Food } from '@shared/utils/food.utils'; | ||
import { | ||
FollowingVideoMetadata, | ||
ForYouMetadata, | ||
NativeMetadata, | ||
ProfileMetadata, | ||
SearchMetadata, | ||
TKMetadata, | ||
} from '@tktrex/shared/models/metadata'; | ||
import { | ||
CreatorType, | ||
FollowingType, | ||
ForYouType, | ||
NativeType, | ||
ProfileType, | ||
SearchType, | ||
} from '@tktrex/shared/models/Nature'; | ||
import * as E from 'fp-ts/Either'; | ||
import { pipe } from 'fp-ts/function'; | ||
import { | ||
FollowingVideoMetadataDB, | ||
ForYouVideoMetadataDB, | ||
NativeMetadataDB, | ||
ProfileMetadataDB, | ||
SearchMetadataDB, | ||
TKMetadataDB, | ||
} from '../models/metadata'; | ||
|
||
type SpecificM<M> = Omit<M, keyof Metadata | '_id' | 'publicKey'>; | ||
|
||
export const toTKNativeMetadata = ( | ||
{ author, ...m }: SpecificM<NativeMetadataDB>, | ||
meta: Metadata | ||
): NativeMetadata => { | ||
return { | ||
...m, | ||
...meta, | ||
author: author ?? undefined, | ||
}; | ||
}; | ||
|
||
export const toProfileMetadata = ( | ||
m: SpecificM<ProfileMetadataDB>, | ||
meta: Metadata | ||
): ProfileMetadata => { | ||
return { | ||
...m, | ||
...meta, | ||
}; | ||
}; | ||
|
||
export const toForYouMetadata = ( | ||
{ author, music, hashtags, ...m }: SpecificM<ForYouVideoMetadataDB>, | ||
meta: Metadata | ||
): ForYouMetadata => { | ||
return { | ||
...m, | ||
...meta, | ||
author: author ?? undefined, | ||
music: music ?? undefined, | ||
hashtags: hashtags ?? [], | ||
}; | ||
}; | ||
|
||
export const toSearchMetadata = ( | ||
m: SpecificM<SearchMetadataDB>, | ||
meta: Metadata | ||
): SearchMetadata => { | ||
return { ...m, ...meta }; | ||
}; | ||
|
||
export const toFollowingMetadata = ( | ||
m: SpecificM<FollowingVideoMetadataDB>, | ||
meta: Metadata | ||
): FollowingVideoMetadata => { | ||
return { ...m, ...meta }; | ||
}; | ||
|
||
export const toTKMetadata = ({ | ||
publicKey, | ||
_id, | ||
id, | ||
blang, | ||
href, | ||
savingTime, | ||
clientTime, | ||
experimentId, | ||
researchTag, | ||
...m | ||
}: TKMetadataDB): E.Either<APIError, TKMetadata> => { | ||
const meta: Metadata = { | ||
id: id.substring(0, 10), | ||
blang, | ||
href, | ||
supporter: string2Food(publicKey), | ||
savingTime, | ||
clientTime, | ||
researchTag, | ||
experimentId, | ||
}; | ||
|
||
return pipe( | ||
E.tryCatch(() => { | ||
const mm: any = m; | ||
switch (m.nature.type) { | ||
case SearchType.value: { | ||
return toSearchMetadata(mm, meta); | ||
} | ||
case ForYouType.value: { | ||
return toForYouMetadata(mm, meta); | ||
} | ||
case FollowingType.value: { | ||
return toFollowingMetadata(mm, meta); | ||
} | ||
case CreatorType.value: | ||
case ProfileType.value: { | ||
return toProfileMetadata(mm, meta); | ||
} | ||
case NativeType.value: { | ||
return toTKNativeMetadata(mm, meta); | ||
} | ||
} | ||
}, toAPIError), | ||
E.chain((e) => | ||
pipe( | ||
TKMetadata.decode(e), | ||
E.mapLeft((e) => toValidationError(TKMetadata.name, e)) | ||
) | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { ForYouVideoMetadata } from '@tktrex/shared/models/metadata/ForYouMetadata'; | ||
import { ForYouMetadata } from '@tktrex/shared/models/metadata/ForYouMetadata'; | ||
import * as t from 'io-ts'; | ||
|
||
const { supporter, ...metadataBaseProps } = ForYouVideoMetadata.types[0].props; | ||
export const ForYouVideoMetadataDB = t.strict( | ||
const { supporter, ...metadataBaseProps } = ForYouMetadata.types[0].props; | ||
export const ForYouMetadataDB = t.strict( | ||
{ | ||
...metadataBaseProps, | ||
...ForYouVideoMetadata.types[1].type.props, | ||
...ForYouVideoMetadata.types[2].props, | ||
...ForYouVideoMetadata.types[3].props, | ||
...ForYouMetadata.types[1].type.props, | ||
...ForYouMetadata.types[2].props, | ||
...ForYouMetadata.types[3].props, | ||
_id: t.any, | ||
publicKey: t.string, | ||
}, | ||
'ForYouVideoMetadataDB' | ||
'ForYouMetadataDB' | ||
); | ||
export type ForYouVideoMetadataDB = t.TypeOf<typeof ForYouVideoMetadataDB>; | ||
export type ForYouMetadataDB = t.TypeOf<typeof ForYouMetadataDB>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
"include": [ | ||
"./bin", | ||
"./lib", | ||
"./io", | ||
"./models", | ||
"./routes", | ||
"./test", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.