1- import type { Activity , Playlist , Track } from '@audius/sdk'
2- import { ActivityItemTypeEnum } from '@audius/sdk'
1+ import type {
2+ Activity ,
3+ Playlist ,
4+ PlaylistWithoutTracks ,
5+ Track
6+ } from '@audius/sdk'
7+ import {
8+ ActivityItemTypeEnum ,
9+ PlaylistWithoutTracksFromJSON ,
10+ TrackFromJSON
11+ } from '@audius/sdk'
312
413import { userCollectionMetadataFromSDK } from './collection'
514import { userTrackMetadataFromSDK } from './track'
615
16+ /**
17+ * Activity.item from getRepostsByHandle can be raw API JSON (snake_case) when
18+ * the response discriminator isn't used. Normalize to SDK shape (camelCase)
19+ * using the SDK's FromJSON before passing to our adapters.
20+ */
21+ function normalizeActivityItem (
22+ item : object ,
23+ itemType :
24+ | typeof ActivityItemTypeEnum . Track
25+ | typeof ActivityItemTypeEnum . Playlist
26+ ) : Track | PlaylistWithoutTracks {
27+ const isRaw = 'user_id' in item && ! ( 'userId' in item )
28+ if ( ! isRaw ) {
29+ return item as Track | PlaylistWithoutTracks
30+ }
31+ if ( itemType === ActivityItemTypeEnum . Track ) {
32+ return TrackFromJSON ( item as Parameters < typeof TrackFromJSON > [ 0 ] )
33+ }
34+ return PlaylistWithoutTracksFromJSON (
35+ item as Parameters < typeof PlaylistWithoutTracksFromJSON > [ 0 ]
36+ )
37+ }
38+
739export const activityFromSDK = ( input : Activity ) => {
840 const { timestamp, itemType : item_type , item } = input
941 if ( item_type === ActivityItemTypeEnum . Track ) {
42+ const normalized = normalizeActivityItem ( item as object , item_type )
1043 return {
1144 timestamp,
1245 item_type,
13- item : userTrackMetadataFromSDK ( item as Track )
46+ item : userTrackMetadataFromSDK ( normalized as Track )
1447 }
1548 } else if ( item_type === ActivityItemTypeEnum . Playlist ) {
49+ const normalized = normalizeActivityItem ( item as object , item_type )
1650 return {
1751 timestamp,
1852 item_type,
19- item : userCollectionMetadataFromSDK ( item as Playlist )
53+ item : userCollectionMetadataFromSDK ( normalized as Playlist )
2054 }
2155 }
2256 return undefined
@@ -25,10 +59,11 @@ export const activityFromSDK = (input: Activity) => {
2559export const trackActivityFromSDK = ( input : Activity ) => {
2660 const { timestamp, itemType : item_type , item } = input
2761 if ( item_type === ActivityItemTypeEnum . Track ) {
62+ const normalized = normalizeActivityItem ( item as object , item_type )
2863 return {
2964 timestamp,
3065 item_type,
31- item : userTrackMetadataFromSDK ( item as Track )
66+ item : userTrackMetadataFromSDK ( normalized as Track )
3267 }
3368 }
3469 return undefined
@@ -37,16 +72,28 @@ export const trackActivityFromSDK = (input: Activity) => {
3772export const repostActivityFromSDK = ( input : Activity ) => {
3873 const { timestamp, itemType : item_type , item } = input
3974 if ( item_type === ActivityItemTypeEnum . Track ) {
40- return {
41- timestamp,
42- item_type,
43- item : userTrackMetadataFromSDK ( item as Track )
75+ try {
76+ const normalized = normalizeActivityItem ( item as object , item_type )
77+ return {
78+ timestamp,
79+ item_type,
80+ item : userTrackMetadataFromSDK ( normalized as Track )
81+ }
82+ } catch ( error ) {
83+ console . error ( 'repostActivityFromSDK track error' , error )
84+ return undefined
4485 }
4586 } else if ( item_type === ActivityItemTypeEnum . Playlist ) {
46- return {
47- timestamp,
48- item_type,
49- item : userCollectionMetadataFromSDK ( item as Playlist )
87+ try {
88+ const normalized = normalizeActivityItem ( item as object , item_type )
89+ return {
90+ timestamp,
91+ item_type,
92+ item : userCollectionMetadataFromSDK ( normalized as Playlist )
93+ }
94+ } catch ( error ) {
95+ console . error ( 'repostActivityFromSDK playlist error' , error )
96+ return undefined
5097 }
5198 }
5299 return undefined
0 commit comments