Skip to content

Commit 885479a

Browse files
schottrasliptype
andauthored
Update users endpoints: round 1 (#9166)
Co-authored-by: Sebastian Klingler <[email protected]>
1 parent 8618e3c commit 885479a

File tree

21 files changed

+509
-242
lines changed

21 files changed

+509
-242
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
full,
3+
instanceOfFollowGate,
4+
instanceOfNftGate,
5+
instanceOfTipGate
6+
} from '@audius/sdk'
7+
8+
import {
9+
AccessConditions,
10+
AccessConditionsEthNFTCollection,
11+
AccessConditionsSolNFTCollection
12+
} from '~/models'
13+
14+
export const accessConditionsFromSDK = (
15+
input: full.AccessGate
16+
): AccessConditions => {
17+
if (instanceOfFollowGate(input)) {
18+
return { follow_user_id: input.followUserId }
19+
} else if (instanceOfNftGate(input)) {
20+
return input.nftCollection.chain === full.NftCollectionChainEnum.Eth
21+
? {
22+
nft_collection:
23+
input.nftCollection as AccessConditionsEthNFTCollection
24+
}
25+
: {
26+
nft_collection:
27+
input.nftCollection as AccessConditionsSolNFTCollection
28+
}
29+
} else if (full.instanceOfPurchaseGate(input)) {
30+
return { usdc_purchase: input.usdcPurchase }
31+
} else if (instanceOfTipGate(input)) {
32+
return { tip_user_id: input.tipUserId }
33+
} else {
34+
throw new Error(`Unsupported access gate type: ${input}`)
35+
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { full } from '@audius/sdk'
2+
3+
import { userCollectionMetadataFromSDK } from './collection'
4+
import { userTrackMetadataFromSDK } from './track'
5+
6+
export const repostActivityFromSDK = (input: full.ActivityFull) => {
7+
const { timestamp, itemType: item_type, item } = input
8+
if (item_type === full.ActivityFullItemTypeEnum.Track) {
9+
return {
10+
timestamp,
11+
item_type,
12+
item: userTrackMetadataFromSDK(full.TrackFullFromJSON(item))
13+
}
14+
} else if (item_type === full.ActivityFullItemTypeEnum.Playlist) {
15+
return {
16+
timestamp,
17+
item_type,
18+
item: userCollectionMetadataFromSDK(full.PlaylistFullFromJSON(item))
19+
}
20+
}
21+
return undefined
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import snakecaseKeys from 'snakecase-keys'
2+
3+
import { ResourceContributor } from '~/models/Track'
4+
5+
// Type from API is Raw, so we'll just convert the keys
6+
export const resourceContributorFromSDK = (
7+
input: object
8+
): ResourceContributor => {
9+
return snakecaseKeys(input) as ResourceContributor
10+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { full } from '@audius/sdk'
2+
import { omit } from 'lodash'
3+
import snakecaseKeys from 'snakecase-keys'
4+
5+
import {
6+
PlaylistTrackId,
7+
UserCollectionMetadata,
8+
Variant
9+
} from '~/models/Collection'
10+
import { Copyright } from '~/models/Track'
11+
import { decodeHashId } from '~/utils/hashIds'
12+
13+
import { accessConditionsFromSDK } from './access'
14+
import { resourceContributorFromSDK } from './attribution'
15+
import { favoriteFromSDK } from './favorite'
16+
import { coverArtSizesCIDsFromSDK } from './imageSize'
17+
import { repostFromSDK } from './repost'
18+
import { userTrackMetadataFromSDK } from './track'
19+
import { userMetadataFromSDK } from './user'
20+
import { transformAndCleanList } from './utils'
21+
22+
const addedTimestampToPlaylistTrackId = ({
23+
timestamp,
24+
trackId,
25+
metadataTimestamp
26+
}: full.PlaylistAddedTimestamp): PlaylistTrackId | null => {
27+
const decoded = decodeHashId(trackId)
28+
if (decoded) {
29+
return {
30+
track: decoded,
31+
time: timestamp,
32+
metadata_time: metadataTimestamp
33+
}
34+
}
35+
return null
36+
}
37+
38+
export const userCollectionMetadataFromSDK = (
39+
input: full.PlaylistFullWithoutTracks
40+
): UserCollectionMetadata | undefined => {
41+
const decodedPlaylistId = decodeHashId(input.id)
42+
const decodedOwnerId = decodeHashId(input.userId)
43+
const user = userMetadataFromSDK(input.user)
44+
if (!decodedPlaylistId || !decodedOwnerId || !user) {
45+
return undefined
46+
}
47+
48+
const newCollection: UserCollectionMetadata = {
49+
// Fields from API that are omitted in this model
50+
...omit(snakecaseKeys(input), [
51+
'id',
52+
'user_id',
53+
'followee_favorites',
54+
'artwork',
55+
'favorite_count',
56+
'added_timestamps'
57+
]),
58+
59+
variant: Variant.USER_GENERATED,
60+
61+
// Nested Transformed Fields
62+
artists: input.artists
63+
? transformAndCleanList(input.artists, resourceContributorFromSDK)
64+
: null,
65+
copyright_line: input.copyrightLine
66+
? (snakecaseKeys(input.copyrightLine) as Copyright)
67+
: null,
68+
cover_art_cids: input.coverArtCids
69+
? coverArtSizesCIDsFromSDK(input.coverArtCids)
70+
: null,
71+
followee_reposts: transformAndCleanList(
72+
input.followeeReposts,
73+
repostFromSDK
74+
),
75+
followee_saves: transformAndCleanList(
76+
input.followeeFavorites,
77+
favoriteFromSDK
78+
),
79+
// TODO: Use playlistContents
80+
playlist_contents: {
81+
track_ids: transformAndCleanList(
82+
input.addedTimestamps,
83+
addedTimestampToPlaylistTrackId
84+
)
85+
},
86+
playlist_id: decodedPlaylistId,
87+
playlist_owner_id: decodedOwnerId,
88+
producer_copyright_line: input.producerCopyrightLine
89+
? (snakecaseKeys(input.producerCopyrightLine) as Copyright)
90+
: null,
91+
stream_conditions: input.streamConditions
92+
? accessConditionsFromSDK(input.streamConditions)
93+
: null,
94+
tracks: transformAndCleanList(input.tracks, userTrackMetadataFromSDK),
95+
user,
96+
97+
// Retypes / Renames
98+
save_count: input.favoriteCount,
99+
100+
// Nullable fields
101+
cover_art: input.coverArt ?? null,
102+
cover_art_sizes: input.coverArtSizes ?? null,
103+
description: input.description ?? null,
104+
release_date: input.releaseDate ?? null
105+
}
106+
107+
return newCollection
108+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { full } from '@audius/sdk'
2+
import { omit } from 'lodash'
3+
import snakecaseKeys from 'snakecase-keys'
4+
5+
import { Favorite, FavoriteType } from '~/models/Favorite'
6+
import { decodeHashId } from '~/utils'
7+
8+
export const favoriteFromSDK = (input: full.Favorite): Favorite | undefined => {
9+
const decodedSaveItemId = decodeHashId(input.favoriteItemId)
10+
const decodedUserId = decodeHashId(input.userId)
11+
if (!decodedSaveItemId || !decodedUserId) {
12+
return undefined
13+
}
14+
15+
return {
16+
// 'save' is renamed to 'favorite' in the model
17+
...omit(snakecaseKeys(input), ['favorite_item_id', 'favorite_type']),
18+
save_item_id: decodedSaveItemId,
19+
save_type: input.favoriteType as FavoriteType,
20+
user_id: decodedUserId
21+
}
22+
}

packages/common/src/adapters/imageSize.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { full } from '@audius/sdk'
22

33
import {
4+
CoverArtSizesCids,
45
CoverPhotoSizesCids,
56
ProfilePictureSizesCids,
67
SquareSizes,
@@ -16,6 +17,19 @@ export const coverPhotoSizesCIDsFromSDK = (
1617
}, {})
1718
}
1819

20+
export const coverArtSizesCIDsFromSDK = (
21+
input: full.CoverArt
22+
): CoverArtSizesCids => {
23+
return [
24+
SquareSizes.SIZE_1000_BY_1000,
25+
SquareSizes.SIZE_150_BY_150,
26+
SquareSizes.SIZE_480_BY_480
27+
].reduce((out, size) => {
28+
out[size] = input[size] ?? null
29+
return out
30+
}, {})
31+
}
32+
1933
export const profilePictureSizesCIDsFromSDK = (
2034
input: full.ProfilePicture
2135
): ProfilePictureSizesCids => {

packages/common/src/adapters/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './activity'
12
export * from './grant'
23
export * from './imageSize'
34
export * from './playlistLibrary'

packages/common/src/adapters/remix.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { full } from '@audius/sdk'
2+
import snakecaseKeys from 'snakecase-keys'
3+
4+
import { Remix } from '~/models/Track'
5+
import { decodeHashId } from '~/utils/hashIds'
6+
7+
import { userMetadataFromSDK } from './user'
8+
9+
export const remixFromSDK = (input: full.FullRemix): Remix | undefined => {
10+
const decodedTrackId = decodeHashId(input.parentTrackId)
11+
const user = userMetadataFromSDK(input.user)
12+
if (!decodedTrackId || !user) {
13+
return undefined
14+
}
15+
16+
return {
17+
...snakecaseKeys(input),
18+
parent_track_id: decodedTrackId,
19+
user
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { full } from '@audius/sdk'
2+
import snakecaseKeys from 'snakecase-keys'
3+
4+
import { Repost } from '~/models/Repost'
5+
import { decodeHashId } from '~/utils/hashIds'
6+
7+
export const repostFromSDK = (input: full.Repost): Repost | undefined => {
8+
const decodedRepostItemId = decodeHashId(input.repostItemId)
9+
const decodedUserId = decodeHashId(input.userId)
10+
if (!decodedRepostItemId || !decodedUserId) {
11+
return undefined
12+
}
13+
14+
return {
15+
...snakecaseKeys(input),
16+
repost_item_id: decodedRepostItemId,
17+
user_id: decodedUserId
18+
}
19+
}

0 commit comments

Comments
 (0)