Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common/src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './grant'
export * from './imageSize'
export * from './playlistLibrary'
export * from './user'
export * from './utils'
26 changes: 20 additions & 6 deletions packages/common/src/api/relatedArtists.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { userMetadataFromSDK } from '~/adapters'
import { transformAndCleanList } from '~/adapters/utils'
import { createApi } from '~/audius-query'
import { ID } from '~/models/Identifiers'
import { ID, Id, OptionalId } from '~/models/Identifiers'
import { Nullable } from '~/utils'

const relatedArtistsApi = createApi({
reducerPath: 'relatedArtistsApi',
endpoints: {
getRelatedArtists: {
fetch: async ({ artistId }: { artistId: ID }, { apiClient }) =>
await apiClient.getRelatedArtists({
userId: artistId,
limit: 50
}),
fetch: async (
{
artistId,
currentUserId
}: { artistId: ID; currentUserId?: Nullable<ID> },
{ audiusSdk }
) => {
const sdk = await audiusSdk()

const { data } = await sdk.full.users.getRelatedUsers({
id: Id.parse(artistId),
limit: 50,
userId: OptionalId.parse(currentUserId)
})
return transformAndCleanList(data, userMetadataFromSDK)
},
options: {
schemaKey: 'users'
}
Expand Down
5 changes: 3 additions & 2 deletions packages/common/src/api/topArtists.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { uniq } from 'lodash'

import { userMetadataListFromSDK } from '~/adapters/user'
import { userMetadataFromSDK } from '~/adapters/user'
import { transformAndCleanList } from '~/adapters/utils'
import { createApi } from '~/audius-query'
import { ID } from '~/models/Identifiers'
import { Kind } from '~/models/Kind'
Expand All @@ -27,7 +28,7 @@ const topArtistsApi = createApi({
limit,
offset
})
return userMetadataListFromSDK(data)
return transformAndCleanList(data, userMetadataFromSDK)
},
options: { kind: Kind.USERS, schemaKey: 'users' }
},
Expand Down
52 changes: 0 additions & 52 deletions packages/common/src/services/audius-api-client/AudiusAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
APISearchAutocomplete,
APIStem,
APITrack,
APIUser,
GetNFTGatedTrackSignaturesResponse,
GetTipsResponse,
OpaqueID
Expand Down Expand Up @@ -76,13 +75,10 @@ const FULL_ENDPOINT_MAP = {
userTracksByHandle: (handle: OpaqueID) => `/users/handle/${handle}/tracks`,
userAiTracksByHandle: (handle: OpaqueID) =>
`/users/handle/${handle}/tracks/ai_attributed`,
userRepostsByHandle: (handle: OpaqueID) => `/users/handle/${handle}/reposts`,
getRelatedArtists: (userId: OpaqueID) => `/users/${userId}/related`,
getPlaylist: (playlistId: OpaqueID) => `/playlists/${playlistId}`,
getPlaylists: '/playlists',
getPlaylistByPermalink: (handle: string, slug: string) =>
`/playlists/by_permalink/${handle}/${slug}`,
topGenreUsers: '/users/genre/top',
getTrack: (trackId: OpaqueID) => `/tracks/${trackId}`,
getTrackStreamUrl: (trackId: OpaqueID) => `/tracks/${trackId}/stream`,
getTracks: () => `/tracks`,
Expand Down Expand Up @@ -149,11 +145,6 @@ type GetTrackByHandleAndSlugArgs = {
currentUserId: Nullable<ID>
}

type PaginationArgs = {
limit?: number
offset?: number
}

type GetTrendingArgs = {
timeRange?: TimeRange
offset?: number
Expand Down Expand Up @@ -208,21 +199,11 @@ type GetPremiumTracksArgs = {
limit?: number
}

type GetRelatedArtistsArgs = PaginationArgs & {
userId: ID
}

type GetFavoritesArgs = {
currentUserId: ID
limit?: number
}

type GetTopArtistGenresArgs = {
genres?: string[]
limit?: number
offset?: number
}

type GetCollectionMetadataArgs = {
collectionId: ID
currentUserId: ID
Expand Down Expand Up @@ -931,39 +912,6 @@ export class AudiusAPIClient {
return data.map(adapter.makeFavorite).filter(removeNullable)
}

async getRelatedArtists({ userId, offset, limit }: GetRelatedArtistsArgs) {
this._assertInitialized()
const encodedUserId = this._encodeOrThrow(userId)
const response = await this._getResponse<APIResponse<APIUser[]>>(
FULL_ENDPOINT_MAP.getRelatedArtists(encodedUserId),
{ offset, limit }
)
if (!response) return []
const adapted = response.data.map(adapter.makeUser).filter(removeNullable)
return adapted
}

async getTopArtistGenres({ genres, limit, offset }: GetTopArtistGenresArgs) {
this._assertInitialized()

const params = {
genre: genres,
limit,
offset
}

const favoritedTrackResponse = await this._getResponse<
APIResponse<APIUser[]>
>(FULL_ENDPOINT_MAP.topGenreUsers, params)

if (!favoritedTrackResponse) return []

const adapted = favoritedTrackResponse.data
.map(adapter.makeUser)
.filter(removeNullable)
return adapted
}

async getCollectionMetadata({
collectionId,
currentUserId,
Expand Down
19 changes: 12 additions & 7 deletions packages/common/src/store/ui/related-artists/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { PayloadAction } from '@reduxjs/toolkit'
import { shuffle } from 'lodash'
import { call, put, select, takeEvery } from 'typed-redux-saga'

import { userMetadataListFromSDK } from '~/adapters/user'
import { userMetadataFromSDK, userMetadataListFromSDK } from '~/adapters/user'
import { transformAndCleanList } from '~/adapters/utils'
import { Id } from '~/api'
import { ID, UserMetadata } from '~/models'
import { ID, OptionalId, UserMetadata } from '~/models'
import { DoubleKeys } from '~/services/remote-config'
import { accountSelectors } from '~/store/account'
import { processAndCacheUsers } from '~/store/cache'
Expand All @@ -19,19 +20,23 @@ const getUserId = accountSelectors.getUserId

export function* fetchRelatedArtists(action: PayloadAction<{ artistId: ID }>) {
yield* waitForRead()
const apiClient = yield* getContext('apiClient')
const sdk = yield* getSDK()
const remoteConfigInstance = yield* getContext('remoteConfigInstance')
const currentUserId = yield* select(getUserId)
if (relatedArtistsActions.fetchRelatedArtists.match(action)) {
const artistId = action.payload.artistId

const relatedArtists = yield* call(
[apiClient, apiClient.getRelatedArtists],
const { data } = yield* call(
[sdk.full.users, sdk.full.users.getRelatedUsers],
Comment on lines +29 to +30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we even need the context argument anymore or if you could just do

Suggested change
const { data } = yield* call(
[sdk.full.users, sdk.full.users.getRelatedUsers],
const { data } = yield* call(
sdk.full.users.getRelatedUser,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I think we do because each SDK api is an instance of a class and makes copious use of this.

{
userId: artistId,
limit: 50
id: Id.parse(artistId),
limit: 50,
userId: OptionalId.parse(currentUserId)
}
)

const relatedArtists = transformAndCleanList(data, userMetadataFromSDK)

let showingTopArtists = false
const filteredArtists = relatedArtists.filter(
(user) => !user.is_deactivated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { repostActivityFromSDK } from '@audius/common/adapters'
import {
repostActivityFromSDK,
transformAndCleanList
} from '@audius/common/adapters'
import {
ID,
Track,
UserTrackMetadata,
OptionalId,
UserCollectionMetadata
} from '@audius/common/models'
import { transformAndCleanList } from '@audius/common/src/adapters/utils'
import { getSDK } from '@audius/common/store'
import { full } from '@audius/sdk'
import { all } from 'redux-saga/effects'
Expand Down
23 changes: 16 additions & 7 deletions packages/web/src/common/store/pages/signon/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
userMetadataFromSDK,
transformAndCleanList
} from '@audius/common/adapters'
import {
Name,
FavoriteSource,
Expand Down Expand Up @@ -26,7 +30,8 @@ import {
toastActions,
getContext,
confirmerActions,
confirmTransaction
confirmTransaction,
getSDK
} from '@audius/common/store'
import {
Genre,
Expand Down Expand Up @@ -177,15 +182,19 @@ function* fetchAllFollowArtist() {
function* fetchFollowArtistGenre(
followArtistCategory: SelectableArtistCategory
) {
const apiClient = yield* getContext('apiClient')
const sdk = yield* getSDK()
const genres = followArtistCategoryGenreMappings[followArtistCategory]
const defaultFollowUserIds = yield* call(getDefautFollowUserIds)
try {
const users = yield* call([apiClient, apiClient.getTopArtistGenres], {
genres,
limit: 31,
offset: 0
})
const { data: sdkUsers } = yield* call(
[sdk.full.users, sdk.full.users.getTopUsersInGenre],
{
genre: genres,
limit: 31,
offset: 0
}
)
const users = transformAndCleanList(sdkUsers, userMetadataFromSDK)
const userOptions = users
.filter((user) => !defaultFollowUserIds.has(user.user_id))
.slice(0, 30)
Expand Down
29 changes: 20 additions & 9 deletions packages/web/src/common/store/user-list/related-artists/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ID } from '@audius/common/models'
import {
userMetadataFromSDK,
transformAndCleanList
} from '@audius/common/adapters'
import { ID, Id, OptionalId } from '@audius/common/models'
import {
accountSelectors,
UserListSagaFactory,
relatedArtistsUserListActions,
relatedArtistsUserListSelectors,
RELATED_ARTISTS_USER_LIST_TAG,
getContext
getSDK
} from '@audius/common/store'
import { call, put, select } from 'typed-redux-saga'

Expand All @@ -27,14 +32,20 @@ function* fetchRelatedArtists({
pageSize
}: FetchRelatedArtistsArgs) {
const offset = currentPage * pageSize
const apiClient = yield* getContext('apiClient')
const response = yield* call([apiClient, apiClient.getRelatedArtists], {
userId: artistId,
limit: MAX_RELATED_ARTISTS,
offset
})
const sdk = yield* getSDK()
const currentUserId = yield* select(accountSelectors.getUserId)

const { data } = yield* call(
[sdk.full.users, sdk.full.users.getRelatedUsers],
{
id: Id.parse(artistId),
limit: MAX_RELATED_ARTISTS,
offset,
userId: OptionalId.parse(currentUserId)
}
)
const users = transformAndCleanList(data, userMetadataFromSDK)

const users = response || []
const userIds = users.map((user) => user.user_id)
return {
userIds,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'

import { useGetRelatedArtists } from '@audius/common/api'
import { useGetCurrentUserId, useGetRelatedArtists } from '@audius/common/api'
import { User } from '@audius/common/models'
import { FeatureFlags } from '@audius/common/services'
import { profilePageSelectors } from '@audius/common/store'
Expand Down Expand Up @@ -30,14 +30,15 @@ const messages = {
export const RelatedArtists = () => {
const dispatch = useDispatch()
const profile = useSelector(getProfileUser)
const { data: currentUserId } = useGetCurrentUserId({})
const { isEnabled: isRelatedArtistsEnabled } = useFlag(
FeatureFlags.RELATED_ARTISTS_ON_PROFILE_ENABLED
)

const artistId = profile?.user_id

const { data: relatedArtists } = useGetRelatedArtists(
{ artistId: artistId! },
{ artistId: artistId!, currentUserId },
{ disabled: !artistId }
)

Expand Down