Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 134
[DVRL-11] Move client history requests to sdk#7679
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e5391a27a02be6eded04d1ee2249d2f96dbbf534fa104e054File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import { AudiusSdk } from '@audius/sdk' | ||
| import { | ||
| Collection, | ||
| FeedFilter, | ||
| @@ -8,6 +10,7 @@ import { | ||
| } from '../../models' | ||
| import { encodeHashId, removeNullable } from '../../utils' | ||
| import { | ||
| APIActivityV2, | ||
| APIPlaylist, | ||
| APITrack, | ||
| AudiusAPIClient, | ||
| @@ -28,15 +31,18 @@ type TopUserListen = { | ||
| type ExploreConfig = { | ||
| audiusBackendInstance: AudiusBackend | ||
| apiClient: AudiusAPIClient | ||
| audiusSdk: () => Promise<AudiusSdk> | ||
| } | ||
| export class Explore { | ||
| audiusBackendInstance: AudiusBackend | ||
| apiClient: AudiusAPIClient | ||
| audiusSdk: () => Promise<AudiusSdk> | ||
| constructor(config: ExploreConfig) { | ||
| this.audiusBackendInstance = config.audiusBackendInstance | ||
| this.apiClient = config.apiClient | ||
| this.audiusSdk = config.audiusSdk | ||
| } | ||
| /** TRACKS ENDPOINTS */ | ||
| @@ -83,6 +89,7 @@ export class Explore { | ||
| } | ||
| async getFeedNotListenedTo(currentUserId: ID, limit = 25) { | ||
| const sdk = await this.audiusSdk() | ||
| try { | ||
| const lineupItems = (await this.apiClient.getSocialFeed({ | ||
| offset: 0, | ||
| @@ -97,18 +104,27 @@ export class Explore { | ||
| const tracks = lineupItems.filter( | ||
| (lineupItem): lineupItem is UserTrack => 'track_id' in lineupItem | ||
| ) | ||
| const { data, signature } = | ||
| await this.audiusBackendInstance.signDiscoveryNodeRequest() | ||
| const history = await sdk.full.users.getUsersTrackHistory({ | ||
| id: encodeHashId(currentUserId), | ||
| encodedDataMessage: data, | ||
| encodedDataSignature: signature, | ||
| limit: 100 | ||
| }) | ||
| const activityData = history.data as APIActivityV2[] | ||
| const listenedToTracks = activityData | ||
| .map(responseAdapter.makeActivity) | ||
| .filter(removeNullable) as UserTrackMetadata[] | ||
| // Imperfect solution. Ideally we use an endpoint that gives us true/false | ||
| // if a user has listened to a passed in array of tracks. | ||
| const history = await this.apiClient.getUserTrackHistory({ | ||
| currentUserId, | ||
| userId: currentUserId, | ||
| limit: 50 | ||
| }) | ||
| const listens = history.map((item) => item.track?.track_id) | ||
| const listenendToTrackIds = listenedToTracks.map( | ||
| (track) => track.track_id | ||
| ) | ||
| const notListenedToTracks = tracks.filter( | ||
| (track) => !listens[track.track_id] | ||
| (track) => !listenendToTrackIds[track.track_id] | ||
raymondjacobson marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ) | ||
| return notListenedToTracks.slice(0, limit) | ||
| } catch (e) { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,15 @@ | ||
| import { LineupEntry, Track, UserTrackMetadata } from '@audius/common/models' | ||
| import { responseAdapter } from '@audius/common/services' | ||
| import { | ||
| accountSelectors, | ||
| getContext, | ||
| historyPageTracksLineupActions as tracksActions | ||
| } from '@audius/common/store' | ||
| import { removeNullable } from '@audius/common/utils' | ||
| import { | ||
| decodeHashId, | ||
| encodeHashId, | ||
| removeNullable | ||
| } from '@audius/common/utils' | ||
| import { keyBy } from 'lodash' | ||
| import { call, select } from 'typed-redux-saga' | ||
| @@ -17,28 +22,40 @@ const { prefix: PREFIX } = tracksActions | ||
| function* getHistoryTracks() { | ||
| yield* waitForRead() | ||
| const apiClient = yield* getContext('apiClient') | ||
| const audiusSdk = yield* getContext('audiusSdk') | ||
| const audiusBackendInstance = yield* getContext('audiusBackendInstance') | ||
| const sdk = yield* call(audiusSdk) | ||
| try { | ||
| const currentUserId = yield* select(getUserId) | ||
| if (!currentUserId) return [] | ||
| const activity = yield* call([apiClient, apiClient.getUserTrackHistory], { | ||
| currentUserId, | ||
| userId: currentUserId, | ||
| limit: 100 | ||
| }) | ||
| const { data, signature } = yield* call([ | ||
| audiusBackendInstance, | ||
| audiusBackendInstance.signDiscoveryNodeRequest | ||
| ]) | ||
| const activity = yield* call( | ||
| [sdk.full.users, sdk.full.users.getUsersTrackHistory], | ||
| { | ||
| id: encodeHashId(currentUserId), | ||
| encodedDataMessage: data, | ||
| encodedDataSignature: signature, | ||
| limit: 100 | ||
| } | ||
| ) | ||
| const activityData = activity.data | ||
| if (!activityData) return [] | ||
| const activityTracks: UserTrackMetadata[] = activity | ||
| .map((a) => a.track) | ||
| .filter(removeNullable) | ||
| const tracks = activityData | ||
| .map(responseAdapter.makeActivity) | ||
| .filter(removeNullable) as UserTrackMetadata[] | ||
| const processedTracks = yield* call(processAndCacheTracks, activityTracks) | ||
| const processedTracks = yield* call(processAndCacheTracks, tracks) | ||
| const processedTracksMap = keyBy(processedTracks, 'track_id') | ||
| const lineupTracks: Track[] = [] | ||
| activity.forEach((activity) => { | ||
| const trackMetadata = activity.track | ||
| ? processedTracksMap[activity.track.track_id] | ||
| activityData.forEach((activity) => { | ||
| const trackMetadata = activity.item | ||
| ? processedTracksMap[decodeHashId(activity.item.id)!] | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decodeHashId actually can return null, so this is right. It should never happen though, unless the provided input is a strange input like | ||
| : null | ||
| // Prevent history for invalid tracks from getting into the lineup. | ||
| if (trackMetadata) { | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JC, is the typing messed up here on sdk side?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i realize i'm failing typecheck -- i think this is actually good now, will remove.