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

⚠️ [C-4416, C-4418, C-4419 C-4322] Fast SSR #8603

Merged
merged 12 commits into from
May 29, 2024
Merged
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"homepage": "https://github.com/AudiusProject/audius-protocol/packages/common#readme",
"license": "",
"exports": {
"./adapters": "./src/adapters/index.ts",
"./api": "./src/api/index.ts",
"./assets": "./src/assets/index.ts",
"./audius-query": "./src/audius-query/index.ts",
Expand Down
15 changes: 15 additions & 0 deletions packages/common/src/adapters/grant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { full } from '@audius/sdk'

import { Grant } from '~/models/Grant'
import { decodeHashId } from '~/utils/hashIds'

export const grantFromSDK = (input: full.Grant): Grant => {
return {
grantee_address: input.granteeAddress,
user_id: decodeHashId(input.userId) ?? null,
is_revoked: input.isRevoked,
is_approved: input.isApproved,
created_at: input.createdAt,
updated_at: input.updatedAt
}
}
30 changes: 30 additions & 0 deletions packages/common/src/adapters/imageSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { full } from '@audius/sdk'

import {
CoverPhotoSizesCids,
ProfilePictureSizesCids,
SquareSizes,
WidthSizes
} from '~/models/ImageSizes'

export const coverPhotoSizesCIDsFromSDK = (
input: full.CoverPhoto
): CoverPhotoSizesCids => {
return [WidthSizes.SIZE_640, WidthSizes.SIZE_2000].reduce((out, size) => {
out[size] = input[size] ?? null
return out
}, {})
}

export const profilePictureSizesCIDsFromSDK = (
input: full.ProfilePicture
): ProfilePictureSizesCids => {
return [
SquareSizes.SIZE_1000_BY_1000,
SquareSizes.SIZE_150_BY_150,
SquareSizes.SIZE_480_BY_480
].reduce((out, size) => {
out[size] = input[size] ?? null
return out
}, {})
}
4 changes: 4 additions & 0 deletions packages/common/src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './grant'
export * from './imageSize'
export * from './playlistLibrary'
export * from './user'
12 changes: 12 additions & 0 deletions packages/common/src/adapters/playlistLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { full } from '@audius/sdk'

import { PlaylistLibrary, PlaylistLibraryItem } from '~/models/PlaylistLibrary'

export const playlistLibraryFromSDK = (
input?: full.PlaylistLibrary
): PlaylistLibrary | undefined => {
if (!input) return undefined
return {
contents: input.contents as PlaylistLibraryItem[]
}
}
105 changes: 105 additions & 0 deletions packages/common/src/adapters/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type { full } from '@audius/sdk'
import { omit } from 'lodash'
import snakecaseKeys from 'snakecase-keys'

import {
ManagedUserMetadata,
UserManagerMetadata,
UserMetadata
} from '~/models/User'
import { SolanaWalletAddress, StringWei } from '~/models/Wallet'
import { decodeHashId } from '~/utils/hashIds'
import { removeNullable } from '~/utils/typeUtils'

import { grantFromSDK } from './grant'
import {
coverPhotoSizesCIDsFromSDK,
profilePictureSizesCIDsFromSDK
} from './imageSize'
import { playlistLibraryFromSDK } from './playlistLibrary'

/** Converts a SDK `full.UserFull` response to a UserMetadata. Note: Will _not_ include the "current user" fields as those aren't returned by the Users API */
export const userMetadataFromSDK = (
input: full.UserFull
): UserMetadata | undefined => {
const user = snakecaseKeys(input)
const decodedUserId = decodeHashId(user.id)
if (!decodedUserId) {
return undefined
}

const newUser: UserMetadata = {
// Fields from API that are omitted in this model
...omit(user, ['id', 'cover_photo_legacy', 'profile_picture_legacy']),

// Conversions
artist_pick_track_id: user.artist_pick_track_id
? decodeHashId(user.artist_pick_track_id)
: null,

// Nested Types
playlist_library: playlistLibraryFromSDK(user.playlist_library) ?? null,
cover_photo_cids: user.cover_photo_cids
? coverPhotoSizesCIDsFromSDK(user.cover_photo_cids)
: null,
profile_picture_cids: user.profile_picture_cids
? profilePictureSizesCIDsFromSDK(user.profile_picture_cids)
: null,

// Re-types
balance: user.balance as StringWei,
associated_wallets_balance: user.associated_wallets_balance as StringWei,
total_balance: user.total_balance as StringWei,
user_id: decodedUserId,
spl_wallet: user.spl_wallet as SolanaWalletAddress,

// Legacy Overrides
cover_photo: user.cover_photo_legacy ?? null,
profile_picture: user.profile_picture_legacy ?? null,

// Required Nullable fields
bio: user.bio ?? null,
cover_photo_sizes: user.cover_photo_sizes ?? null,
creator_node_endpoint: user.creator_node_endpoint ?? null,
location: user.location ?? null,
metadata_multihash: user.metadata_multihash ?? null,
profile_picture_sizes: user.profile_picture_sizes ?? null
}

return newUser
}

export const userMetadataListFromSDK = (input?: full.UserFull[]) =>
input ? input.map((d) => userMetadataFromSDK(d)).filter(removeNullable) : []

export const managedUserFromSDK = (
input: full.ManagedUser
): ManagedUserMetadata | undefined => {
const user = userMetadataFromSDK(input.user)
if (!user) {
return undefined
}
return {
user,
grant: grantFromSDK(input.grant)
}
}

export const managedUserListFromSDK = (input?: full.ManagedUser[]) =>
input ? input.map((d) => managedUserFromSDK(d)).filter(removeNullable) : []

export const userManagerFromSDK = (
input: full.UserManager
): UserManagerMetadata | undefined => {
const manager = userMetadataFromSDK(input.manager)
if (!manager) {
return undefined
}
return {
manager,
grant: grantFromSDK(input.grant)
}
}

export const userManagerListFromSDK = (input?: full.UserManager[]) =>
input ? input.map((d) => userManagerFromSDK(d)).filter(removeNullable) : []
9 changes: 2 additions & 7 deletions packages/common/src/api/account.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import dayjs from 'dayjs'

import { managedUserListFromSDK, userManagerListFromSDK } from '~/adapters/user'
import { createApi } from '~/audius-query'
import {
ID,
User,
UserMetadata,
managedUserListFromSDK,
userManagerListFromSDK
} from '~/models'
import { ID, User, UserMetadata } from '~/models'

import { Id } from './utils'

Expand Down
Empty file.
2 changes: 1 addition & 1 deletion packages/common/src/api/topArtists.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { uniq } from 'lodash'

import { userMetadataListFromSDK } from '~/adapters/user'
import { createApi } from '~/audius-query'
import { userMetadataListFromSDK } from '~/models'
import { ID } from '~/models/Identifiers'
import { Kind } from '~/models/Kind'

Expand Down
9 changes: 2 additions & 7 deletions packages/common/src/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { full } from '@audius/sdk'

import { userMetadataListFromSDK } from '~/adapters/user'
import { createApi } from '~/audius-query'
import {
ID,
Kind,
OptionalId,
StringUSDC,
userMetadataListFromSDK
} from '~/models'
import { ID, Kind, OptionalId, StringUSDC } from '~/models'
import {
USDCTransactionDetails,
USDCTransactionMethod,
Expand Down
15 changes: 1 addition & 14 deletions packages/common/src/models/Grant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { full } from '@audius/sdk'

import { Nullable, decodeHashId } from '~/utils'
import { Nullable } from '~/utils'

import { ID } from './Identifiers'

Expand All @@ -12,14 +10,3 @@ export type Grant = {
created_at: string
updated_at: string
}

export const grantFromSDK = (input: full.Grant): Grant => {
return {
grantee_address: input.granteeAddress,
user_id: decodeHashId(input.userId) ?? null,
is_revoked: input.isRevoked,
is_approved: input.isApproved,
created_at: input.createdAt,
updated_at: input.updatedAt
}
}
24 changes: 0 additions & 24 deletions packages/common/src/models/ImageSizes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { full } from '@audius/sdk'

export enum DefaultSizes {
// Used as a catch-all fallback when no other size data is available.
OVERRIDE = 'OVERRIDE'
Expand Down Expand Up @@ -33,25 +31,3 @@ export type ProfilePictureSizesCids =
export type ProfilePictureSizes = ImageSizesObject<SquareSizes>
export type CoverPhotoSizesCids = ImageSizesObjectWithoutOverride<WidthSizes>
export type CoverPhotoSizes = ImageSizesObject<WidthSizes>

export const coverPhotoSizesCIDsFromSDK = (
input: full.CoverPhoto
): CoverPhotoSizesCids => {
return [WidthSizes.SIZE_640, WidthSizes.SIZE_2000].reduce((out, size) => {
out[size] = input[size] ?? null
return out
}, {})
}

export const profilePictureSizesCIDsFromSDK = (
input: full.ProfilePicture
): ProfilePictureSizesCids => {
return [
SquareSizes.SIZE_1000_BY_1000,
SquareSizes.SIZE_150_BY_150,
SquareSizes.SIZE_480_BY_480
].reduce((out, size) => {
out[size] = input[size] ?? null
return out
}, {})
}
11 changes: 0 additions & 11 deletions packages/common/src/models/PlaylistLibrary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { full } from '@audius/sdk'

import { SmartCollectionVariant } from '~/models/SmartCollectionVariant'

import { ID } from './Identifiers'
Expand Down Expand Up @@ -45,12 +43,3 @@ export type PlaylistLibraryItem =
export type PlaylistLibrary = {
contents: (PlaylistLibraryFolder | PlaylistLibraryIdentifier)[]
}

export const playlistLibraryFromSDK = (
input?: full.PlaylistLibrary
): PlaylistLibrary | undefined => {
if (!input) return undefined
return {
contents: input.contents as PlaylistLibraryItem[]
}
}
3 changes: 2 additions & 1 deletion packages/common/src/models/Tipping.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { full } from '@audius/sdk'

import { userMetadataFromSDK } from '~/adapters/user'
import { ID } from '~/models/Identifiers'
import { removeNullable } from '~/utils/typeUtils'

import { UserMetadata, userMetadataFromSDK } from './User'
import { UserMetadata } from './User'
import { StringWei } from './Wallet'

export type Supporter = {
Expand Down
Loading