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
69 changes: 1 addition & 68 deletions packages/common/src/api/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,6 @@ import { SDKRequest } from './types'
const trackApi = createApi({
reducerPath: 'trackApi',
endpoints: {
getTrackById: {
fetch: async (
{
id,
currentUserId
}: { id: ID | null | undefined; currentUserId?: Nullable<ID> },
{ audiusSdk }
) => {
if (!id || id === -1) return null
const sdk = await audiusSdk()
const { data } = await sdk.full.tracks.getTrack({
trackId: Id.parse(id),
userId: OptionalId.parse(currentUserId)
})
return data ? userTrackMetadataFromSDK(data) : null
},
fetchBatch: async (
{ ids, currentUserId }: { ids: ID[]; currentUserId?: Nullable<ID> },
{ audiusSdk }
) => {
const id = ids.filter((id) => id && id !== -1).map((id) => Id.parse(id))
if (id.length === 0) return []

const sdk = await audiusSdk()
const { data = [] } = await sdk.full.tracks.getBulkTracks({
id,
userId: OptionalId.parse(currentUserId)
})
return transformAndCleanList(data, userTrackMetadataFromSDK)
},
options: {
idArgKey: 'id',
kind: Kind.TRACKS,
schemaKey: 'track'
}
},
getTrackByPermalink: {
fetch: async (
{
permalink,
currentUserId
}: { permalink: Nullable<string>; currentUserId: Nullable<ID> },
{ audiusSdk }
) => {
if (!permalink) {
console.error('Attempting to get track but permalink is null...')
return
}
const sdk = await audiusSdk()
const { data } = await sdk.full.tracks.getBulkTracks({
permalink: [permalink],
userId: OptionalId.parse(currentUserId)
})
return data && data.length > 0
? userTrackMetadataFromSDK(data[0])
: null
},
options: {
permalinkArgKey: 'permalink',
kind: Kind.TRACKS,
schemaKey: 'track'
}
},
getTracksByIds: {
fetch: async (
{ ids, currentUserId }: { ids: ID[]; currentUserId: Nullable<ID> },
Expand Down Expand Up @@ -123,11 +60,7 @@ const trackApi = createApi({
}
})

export const {
useGetTrackByPermalink,
useGetTracksByIds,
useGetUserTracksByHandle
} = trackApi.hooks
export const { useGetTracksByIds, useGetUserTracksByHandle } = trackApi.hooks
export const trackApiFetch = trackApi.fetch
export const trackApiReducer = trackApi.reducer
export const trackApiActions = trackApi.actions
18 changes: 17 additions & 1 deletion packages/common/src/hooks/useGatedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,23 @@ export const useGatedCollectionAccess = (collectionId: ID) => {

// Returns whether user has access to given track.
export const useGatedContentAccess = (
content: Nullable<Partial<Track | Collection>> | undefined
content:
| Nullable<
| Pick<
Track,
| 'track_id'
| 'is_stream_gated'
| 'is_download_gated'
| 'access'
| 'stream_conditions'
| 'download_conditions'
>
| Pick<
Collection,
'playlist_id' | 'is_stream_gated' | 'access' | 'stream_conditions'
>
>
| undefined
Comment on lines +69 to +85
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wherever I find them I'm updating methods to be specific about entity fields they need - this way we can be good about query selectors

) => {
const nftAccessSignatureMap = useSelector(getNftAccessSignatureMap)
const hasAccount = useSelector(getHasAccount)
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/hooks/useTrackMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mood } from '@audius/sdk'
import { pick } from 'lodash'

import { useTrack } from '~/api/tan-query/useTrack'
import { useTrack } from '~/api'
import { ID } from '~/models'
import { parseMusicalKey } from '~/utils/musicalKeys'
import { searchPage } from '~/utils/route'
Expand Down
60 changes: 31 additions & 29 deletions packages/mobile/src/screens/chat-screen/ChatMessageTrack.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
import { useCallback, useEffect, useMemo } from 'react'

import { useGetTrackByPermalink } from '@audius/common/api'
import { useTrackByPermalink, useUser } from '@audius/common/api'
import { useGatedContentAccess, useToggleTrack } from '@audius/common/hooks'
import type { TrackPlayback } from '@audius/common/hooks'
import { Name, PlaybackSource, Kind } from '@audius/common/models'
import type { ID } from '@audius/common/models'
import { accountSelectors, QueueSource } from '@audius/common/store'
import { QueueSource } from '@audius/common/store'
import type { ChatMessageTileProps } from '@audius/common/store'
import { getPathFromTrackUrl, makeUid } from '@audius/common/utils'
import { useSelector } from 'react-redux'
import { pick } from 'lodash'

import { TrackTile } from 'app/components/lineup-tile'
import { LineupTileSource } from 'app/components/lineup-tile/types'
import { make, track as trackEvent } from 'app/services/analytics'

const { getUserId } = accountSelectors

export const ChatMessageTrack = ({
link,
onEmpty,
onSuccess,
styles
}: ChatMessageTileProps) => {
const currentUserId = useSelector(getUserId)

const permalink = getPathFromTrackUrl(link)
const { data: track } = useGetTrackByPermalink(
{
permalink,
currentUserId
},
{ disabled: !permalink }
)
const { hasStreamAccess } = useGatedContentAccess(track ?? null)
const isPreview =
!!track?.is_stream_gated && !!track?.preview_cid && !hasStreamAccess

const user = useMemo(() => (track ? { ...track.user } : null), [track])
const { data: partialTrack } = useTrackByPermalink(permalink, {
select: (track) =>
pick(track, [
'track_id',
'owner_id',
'preview_cid',
'access',
'is_download_gated',
'is_stream_gated',
'download_conditions',
'stream_conditions'
])
})
const trackExists = !!partialTrack
const { hasStreamAccess } = useGatedContentAccess(partialTrack)
const { track_id, is_stream_gated, preview_cid, owner_id } =
partialTrack ?? {}
const { data: user } = useUser(owner_id)
const isPreview = !!is_stream_gated && !!preview_cid && !hasStreamAccess

const trackId = track?.track_id
const uid = useMemo(() => {
return trackId ? makeUid(Kind.TRACKS, trackId) : null
}, [trackId])
return track_id ? makeUid(Kind.TRACKS, track_id) : null
}, [track_id])

const recordAnalytics = useCallback(
({ name, id }: { name: TrackPlayback; id: ID }) => {
if (!track) return
if (!trackExists) return
trackEvent(
make({
eventName: name,
Expand All @@ -54,19 +56,19 @@ export const ChatMessageTrack = ({
})
)
},
[track]
[trackExists]
)

const { togglePlay } = useToggleTrack({
id: track?.track_id,
id: track_id,
uid,
isPreview,
source: QueueSource.CHAT_TRACKS,
recordAnalytics
})

useEffect(() => {
if (track && user && uid) {
if (trackExists && user && uid) {
trackEvent(
make({
eventName: Name.MESSAGE_UNFURL_TRACK
Expand All @@ -76,11 +78,11 @@ export const ChatMessageTrack = ({
} else {
onEmpty?.()
}
}, [track, user, uid, onSuccess, onEmpty])
}, [trackExists, user, uid, onSuccess, onEmpty])

return track && user && uid ? (
return track_id && user && uid ? (
<TrackTile
id={track.track_id}
id={track_id}
index={0}
togglePlay={togglePlay}
uid={uid}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useEffect } from 'react'

import { useGetTrackByPermalink } from '@audius/common/api'
import { useTrackByPermalink } from '@audius/common/api'
import { useGatedContentAccess } from '@audius/common/hooks'
import { accountSelectors } from '@audius/common/store'
import { getPathFromTrackUrl } from '@audius/common/utils'
import { useField } from 'formik'
import { useSelector } from 'react-redux'
import { pick } from 'lodash'
import { useThrottle } from 'react-use'

import { Divider } from 'components/divider'
Expand All @@ -16,7 +15,6 @@ import { SwitchRowField } from '../SwitchRowField'
import styles from './RemixSettingsField.module.css'
import { TrackInfo } from './TrackInfo'
import { CAN_REMIX_PARENT, IS_REMIX, REMIX_LINK, SHOW_REMIXES } from './types'
const { getUserId } = accountSelectors

const messages = {
hideRemix: {
Expand All @@ -36,24 +34,29 @@ export const RemixSettingsMenuFields = () => {
const [{ value: trackUrl }] = useField(REMIX_LINK)
const [, , { setValue: setCanRemixParent }] = useField(CAN_REMIX_PARENT)
const permalink = useThrottle(getPathFromTrackUrl(trackUrl), 1000)
const currentUserId = useSelector(getUserId)

const { data: track } = useGetTrackByPermalink(
{ permalink, currentUserId },
{ disabled: !permalink }
)
const { data: partialTrack } = useTrackByPermalink(permalink, {
select: (track) =>
pick(track, [
'track_id',
'is_stream_gated',
'stream_conditions',
'is_download_gated',
'download_conditions',
'access'
])
})

const trackId = track?.track_id
const { hasStreamAccess: canRemixParent } = useGatedContentAccess(
track ?? null
)
const { track_id } = partialTrack ?? {}
const { hasStreamAccess: canRemixParent } =
useGatedContentAccess(partialTrack)

const [, , { setValue: setParentTrackId }] = useField('parentTrackId')

useEffect(() => {
setParentTrackId(trackId)
setParentTrackId(track_id)
setCanRemixParent(canRemixParent)
}, [trackId, setParentTrackId, canRemixParent, setCanRemixParent])
}, [track_id, setParentTrackId, canRemixParent, setCanRemixParent])

return (
<div className={styles.fields}>
Expand All @@ -63,7 +66,7 @@ export const RemixSettingsMenuFields = () => {
description={messages.remixOf.description}
>
<TextField name={REMIX_LINK} label={messages.remixOf.linkLabel} />
{track ? <TrackInfo trackId={track.track_id} /> : null}
{track_id ? <TrackInfo trackId={track_id} /> : null}
</SwitchRowField>

<Divider />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ const ActionsBar = ({
const { data: partialTrack } = useTrack(trackId, {
select: (track) =>
pick(track, [
'track_id',
'is_unlisted',
'owner_id',
'has_current_user_reposted',
'has_current_user_saved'
'has_current_user_saved',
'access',
'is_stream_gated',
'stream_conditions',
'is_download_gated',
'download_conditions'
])
})
const { data: currentUserId } = useCurrentUserId()
Expand All @@ -54,7 +60,7 @@ const ActionsBar = ({
is_unlisted: isUnlisted
} = partialTrack ?? {}
const isOwner = ownerId === currentUserId
const { hasStreamAccess } = useGatedContentAccess(partialTrack ?? {})
const { hasStreamAccess } = useGatedContentAccess(partialTrack)
const shouldShowActions = hasStreamAccess && !isUnlisted

if (!shouldShowActions) return null
Expand Down
6 changes: 5 additions & 1 deletion packages/web/src/components/track/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export const getTrackWithFallback = (track: Track | null | undefined) => {
ddex_app: null,
comment_count: 0,
comments_disabled: false,
album_backlink: undefined
album_backlink: undefined,
access: {
stream: false,
download: false
}
}
)
}
Expand Down
Loading