Skip to content

Commit

Permalink
Use sdk for metadata time on addTrackToPlaylist (#10801)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson authored Dec 19, 2024
1 parent 3b88339 commit 5678ece
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
11 changes: 3 additions & 8 deletions packages/common/src/hooks/useGeneratePlaylistArtwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,19 @@ export const useGeneratePlaylistArtwork = (collectionId: ID) => {
getCollectionTracks(state, { id: collectionId })
)

const { imageUtils, audiusBackend } = useAppContext()
const { imageUtils } = useAppContext()

return useCallback(async () => {
if (!collection || !collectionTracks) return null
const { artwork } = await updatePlaylistArtwork(
collection,
collectionTracks,
{ regenerate: true },
{ audiusBackend, generateImage: imageUtils.generatePlaylistArtwork }
{ generateImage: imageUtils.generatePlaylistArtwork }
)
if (!artwork) return null
const { url, file } = artwork
if (!url) return null
return { url, file }
}, [
collection,
collectionTracks,
audiusBackend,
imageUtils.generatePlaylistArtwork
])
}, [collection, collectionTracks, imageUtils.generatePlaylistArtwork])
}
2 changes: 0 additions & 2 deletions packages/common/src/utils/updatePlaylistArtwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isEqual } from 'lodash'
import { Collection } from '~/models/Collection'
import { SquareSizes } from '~/models/ImageSizes'
import { Track } from '~/models/Track'
import { AudiusBackend } from '~/services/audius-backend'

import { Nullable } from './typeUtils'

Expand All @@ -19,7 +18,6 @@ type ArtworkActions = {
}

type Context = {
audiusBackend: AudiusBackend
generateImage: (urls: string[]) => Promise<{ url: string; file: File }>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ function* addTrackToPlaylistAsync(action: AddTrackToPlaylistAction) {
yield* waitForWrite()
const userId = yield* call(ensureLoggedIn)
const isNative = yield* getContext('isNativeMobile')
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
const web3 = yield* call(audiusBackendInstance.getWeb3)
const sdk = yield* getSDK()
const { generatePlaylistArtwork } = yield* getContext('imageUtils')

let playlist = yield* select(getCollection, { id: playlistId })
Expand All @@ -84,18 +83,21 @@ function* addTrackToPlaylistAsync(action: AddTrackToPlaylistAction) {
yield* put(
cacheActions.subscribe(Kind.TRACKS, [{ uid: trackUid, id: action.trackId }])
)

const currentBlockNumber = yield* call([web3.eth, 'getBlockNumber'])
const currentBlock = (yield* call(
[web3.eth, 'getBlock'],
currentBlockNumber
)) as { timestamp: number }
const { timestamp: currentBlockTimestamp } = yield* call([
sdk.services.entityManager,
sdk.services.entityManager.getCurrentBlock
])

playlist.playlist_contents = {
track_ids: playlist.playlist_contents.track_ids.concat({
track: action.trackId,
metadata_time: currentBlock?.timestamp as number,
// Replaced in indexing with block timestamp
// Represents the server time seen when track was added to playlist
time: 0,
// Represents user-facing timestamp when the user added the track to the playlist.
// This is needed to disambiguate between tracks added at the same time/potentiall in
// the same block.
metadata_time: currentBlockTimestamp,
uid: trackUid
})
}
Expand All @@ -114,7 +116,6 @@ function* addTrackToPlaylistAsync(action: AddTrackToPlaylistAction) {
playlistTracks,
{ added: track },
{
audiusBackend: audiusBackendInstance,
generateImage: generatePlaylistArtwork
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ function* editPlaylistAsync(
yield* waitForWrite()

const isNative = yield* getContext('isNativeMobile')
const audiusBackend = yield* getContext('audiusBackendInstance')
const { generatePlaylistArtwork } = yield* getContext('imageUtils')

formFields.description = squashNewLines(formFields.description)
Expand Down Expand Up @@ -153,7 +152,7 @@ function* editPlaylistAsync(
playlist,
playlistTracks!,
{ updated: updatedTracks },
{ audiusBackend, generateImage: generatePlaylistArtwork }
{ generateImage: generatePlaylistArtwork }
)

// Optimistic update #2 to update the artwork
Expand Down Expand Up @@ -257,7 +256,6 @@ function* removeTrackFromPlaylistAsync(
const { playlistId, trackId, timestamp } = action
yield* waitForWrite()
const userId = yield* call(ensureLoggedIn)
const audiusBackend = yield* getContext('audiusBackendInstance')
const { generatePlaylistArtwork } = yield* getContext('imageUtils')

let playlist = yield* select(getCollection, { id: playlistId })
Expand All @@ -269,7 +267,7 @@ function* removeTrackFromPlaylistAsync(
playlist!,
playlistTracks!,
{ removed: removedTrack! },
{ audiusBackend, generateImage: generatePlaylistArtwork }
{ generateImage: generatePlaylistArtwork }
)

// Find the index of the track based on the track's id and timestamp
Expand Down Expand Up @@ -383,7 +381,6 @@ function* orderPlaylistAsync(
const { playlistId, trackIdsAndTimes } = action
yield* waitForWrite()
const userId = yield* call(ensureLoggedIn)
const audiusBackend = yield* getContext('audiusBackendInstance')
const { generatePlaylistArtwork } = yield* getContext('imageUtils')

const playlist = yield* select(getCollection, { id: playlistId })
Expand All @@ -400,7 +397,7 @@ function* orderPlaylistAsync(
playlist!,
tracks!,
{ reordered: orderedTracks },
{ audiusBackend, generateImage: generatePlaylistArtwork }
{ generateImage: generatePlaylistArtwork }
)

updatedPlaylist.playlist_contents.track_ids = trackIdsAndTimes.map(
Expand Down

0 comments on commit 5678ece

Please sign in to comment.