Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.
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
21 changes: 16 additions & 5 deletions packages/web/src/pages/smart-collection/store/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { takeEvery, put, call, select } from 'typed-redux-saga/macro'
import { SmartCollection } from 'common/models/Collection'
import { SmartCollectionVariant } from 'common/models/SmartCollectionVariant'
import Status from 'common/models/Status'
import { Track, UserTrack } from 'common/models/Track'
import { Track, UserTrack, UserTrackMetadata } from 'common/models/Track'
import { getAccountStatus, getUserId } from 'common/store/account/selectors'
import { processAndCacheTracks } from 'common/store/cache/tracks/utils'
import { fetchUsers as retrieveUsers } from 'common/store/cache/users/sagas'
Expand Down Expand Up @@ -55,7 +55,15 @@ function* fetchHeavyRotation() {
}

function* fetchBestNewReleases() {
const tracks = yield* call(Explore.getTopFolloweeTracksFromWindow, 'month')
const currentUserId = yield* select(getUserId)
if (currentUserId == null) {
return
}
const tracks = yield* call(
Explore.getTopFolloweeTracksFromWindow,
currentUserId,
'month'
)

const trackIds = tracks
.filter((track) => !track.user.is_deactivated)
Expand Down Expand Up @@ -96,11 +104,14 @@ function* fetchUnderTheRadar() {
}

function* fetchMostLoved() {
const tracks = yield* call(Explore.getTopFolloweeSaves)

const currentUserId = yield* select(getUserId)
if (currentUserId == null) {
return
}
const tracks = yield* call(Explore.getMostLovedTracks, currentUserId)
const trackIds = tracks
.filter((track) => !track.user.is_deactivated)
.map((track: Track) => ({
.map((track: UserTrackMetadata) => ({

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.

does const tracks = yield* call(...) not return UserTrackMetadata[] ? or are the types not defined in libs or something?

time: track.created_at,
track: track.track_id
}))
Expand Down
65 changes: 45 additions & 20 deletions packages/web/src/services/audius-backend/Explore.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ID } from '@audius/common'

import { Collection } from 'common/models/Collection'
import { Collection, UserCollectionMetadata } from 'common/models/Collection'
import FeedFilter from 'common/models/FeedFilter'
import { Track, UserTrack } from 'common/models/Track'
import { removeNullable } from 'common/utils/typeUtils'
import AudiusBackend, {
IDENTITY_SERVICE,
AuthHeaders
} from 'services/AudiusBackend'
import apiClient from 'services/audius-api-client/AudiusAPIClient'
import * as adapter from 'services/audius-api-client/ResponseAdapter'
import { APIPlaylist, APITrack } from 'services/audius-api-client/types'
import { encodeHashId } from 'utils/route/hashIds'

type CollectionWithScore = Collection & { score: number }
type CollectionWithScore = APIPlaylist & { score: number }

// @ts-ignore
const libs = () => window.audiusLibs
Expand Down Expand Up @@ -64,17 +68,19 @@ class Explore {
}

static async getTopFolloweeTracksFromWindow(
userId: ID,
window: string,
limit = 25
): Promise<UserTrack[]> {
try {
const tracks = await libs().discoveryProvider.getTopFolloweeWindowed(
'track',
const encodedUserId = encodeHashId(userId)
const tracks = await libs().discoveryProvider.getBestNewReleases(
encodedUserId,
window,
limit,
true
)
return tracks
return tracks.map(adapter.makeTrack).filter(removeNullable)
} catch (e) {
console.error(e)
return []
Expand Down Expand Up @@ -130,6 +136,22 @@ class Explore {
}
}

static async getMostLovedTracks(userId: ID, limit = 25) {
try {
const encodedUserId = encodeHashId(userId)
const tracks: APITrack[] =
await libs().discoveryProvider.getMostLovedTracks(
encodedUserId,
limit,
true
)
return tracks.map(adapter.makeTrack).filter(removeNullable)
} catch (e) {
console.error(e)
return []

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.

just wondering what the error case is here, i guess seems like the pattern in the file?

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.

Retuning [] causes the page to show zero tracks in this playlist - I'm not sure what the intended error behavior is.

}
}

static async getLatestTrackID(): Promise<number> {
try {
const latestTrackID = await libs().discoveryProvider.getLatest('track')
Expand All @@ -147,14 +169,15 @@ class Explore {
limit = 20
): Promise<Collection[]> {
try {
const playlists = await libs().discoveryProvider.getTopPlaylists(
const playlists = await libs().discoveryProvider.getTopFullPlaylists({
type,
limit,
undefined,
followeesOnly ? 'followees' : undefined,
true
)
return playlists
mood: undefined,
filter: followeesOnly ? 'followees' : undefined,
withUsers: true
})
const adapted = playlists.map(adapter.makePlaylist)
return adapted
} catch (e) {
console.error(e)
return []
Expand All @@ -164,24 +187,26 @@ class Explore {
static async getTopPlaylistsForMood(
moods: string[],
limit = 16
): Promise<Collection[]> {
): Promise<UserCollectionMetadata[]> {
try {
const requests = moods.map((mood) => {
return libs().discoveryProvider.getTopPlaylists(
'playlist',
return libs().discoveryProvider.getTopFullPlaylists({
type: 'playlist',
limit,
mood,
undefined,
true
)
filter: undefined,
withUsers: true
})
})
const playlistsByMood = await Promise.all(requests)

const playlistsByMood: CollectionWithScore[] = await Promise.all(requests)
let allPlaylists: CollectionWithScore[] = []
playlistsByMood.forEach((playlists) => {
allPlaylists = allPlaylists.concat(playlists)
})
return allPlaylists.sort(scoreComparator).slice(0, 20)
const playlists: APIPlaylist[] = allPlaylists
.sort(scoreComparator)
.slice(0, 20)
return playlists.map(adapter.makePlaylist).filter(removeNullable)
} catch (e) {
console.error(e)
return []
Expand Down