Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Dual writes when setting artist pick (#2128)
Browse files Browse the repository at this point in the history
* Add write path using updateCreator

* Add artist_pick_track_id to userMetadataSchema used in AudiusBackend

* Add artist_pick_track_id to APIUser type

* Set artist_pick_track_id to identity service value when populating cache

* Pin keyv version

* Move to devDependencies

* Wait for user to be fetched when deleting track

* waitForValue, typecheck errors

* Typo
  • Loading branch information
michellebrier authored Oct 26, 2022
1 parent 25954c3 commit 7a8a207
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 12 deletions.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"stems": "cd packages/stems && npm run start"
},
"devDependencies": {
"@types/keyv": "4.2.0",
"@typescript-eslint/eslint-plugin": "5.40.0",
"@typescript-eslint/parser": "5.40.0",
"concurrently": "7.4.0",
Expand All @@ -55,7 +56,6 @@
"wait-on": "6.0.1"
},
"dependencies": {
"@escape.tech/mookme": "2.2.0",
"@types/keyv": "^4.2.0"
"@escape.tech/mookme": "2.2.0"
}
}
1 change: 1 addition & 0 deletions packages/common/src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Timestamped } from './Timestamped'

export type UserMetadata = {
album_count: number
artist_pick_track_id: number | null,
bio: string | null
cover_photo: Nullable<CID>
creator_node_endpoint: Nullable<string>
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ const userMetadataSchema = {
collectibles: null,
playlist_library: null,
events: null,
is_deactivated: false
is_deactivated: false,
artist_pick_track_id: null
}

export const newUserMetadata = (fields?: any, validate = false) => {
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/services/audius-api-client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type OpaqueID = string

export type APIUser = {
album_count: number
artist_pick_track_id: Nullable<number>
blocknumber: number
balance: string
associated_wallets_balance: string
Expand Down
14 changes: 13 additions & 1 deletion packages/common/src/services/audius-backend/AudiusBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2158,11 +2158,23 @@ export const audiusBackend = ({

/**
* Sets the artist pick for a user
* @param {User} userMetadata
* @param {number} userId
* @param {number?} trackId if null, unsets the artist pick
*/
async function setArtistPick(trackId: Nullable<ID> = null) {
async function setArtistPick(
userMetadata: User,
userId: ID,
trackId: Nullable<ID> = null
) {
await waitForLibsInit()
try {
// Dual write to the artist_pick_track_id field in the
// users table in the discovery DB. Part of the migration
// of the artist pick feature from the identity service
// to the entity manager in discovery.
updateCreator(userMetadata, userId)

const { data, signature } = await signData()
return await fetch(`${identityServiceUrl}/artist_pick`, {
method: 'POST',
Expand Down
8 changes: 6 additions & 2 deletions packages/web/src/common/store/cache/tracks/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,19 @@ function* deleteTrackAsync(action) {
handle
)
if (socials.pinnedTrackId === action.trackId) {
yield call(audiusBackendInstance.setArtistPick)
yield put(
cacheActions.update(Kind.USERS, [
{
id: userId,
metadata: { _artist_pick: null }
metadata: {
artist_pick_track_id: null,
_artist_pick: null
}
}
])
)
const user = yield call(waitForValue, getUser, { id: userId })
yield call(audiusBackendInstance.setArtistPick, user, userId)
}

const track = yield select(getTrack, { id: action.trackId })
Expand Down
3 changes: 2 additions & 1 deletion packages/web/src/common/store/cache/users/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ export function* fetchUserSocials({ handle }) {
tiktok_handle: socials.tikTokHandle || null,
website: socials.website || null,
donation: socials.donation || null,
_artist_pick: socials.pinnedTrackId || null
_artist_pick: socials.pinnedTrackId || null,
artist_pick_track_id: socials.pinnedTrackId || null
}
}
])
Expand Down
23 changes: 19 additions & 4 deletions packages/web/src/common/store/social/tracks/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,15 +540,25 @@ export function* watchSetArtistPick() {
function* (action: ReturnType<typeof socialActions.setArtistPick>) {
yield* waitForAccount()
const userId = yield* select(getUserId)
if (!userId) return
yield* put(
cacheActions.update(Kind.USERS, [
{
id: userId,
metadata: { _artist_pick: action.trackId }
metadata: {
artist_pick_track_id: action.trackId,
_artist_pick: action.trackId
}
}
])
)
yield* call(audiusBackendInstance.setArtistPick, action.trackId)
const user = yield* call(waitForValue, getUser, { id: userId })
yield* call(
audiusBackendInstance.setArtistPick,
user,
userId,
action.trackId
)

const event = make(Name.ARTIST_PICK_SELECT_TRACK, { id: action.trackId })
yield* put(event)
Expand All @@ -561,15 +571,20 @@ export function* watchUnsetArtistPick() {
yield* takeEvery(socialActions.UNSET_ARTIST_PICK, function* (action) {
yield* waitForAccount()
const userId = yield* select(getUserId)
if (!userId) return
yield* put(
cacheActions.update(Kind.USERS, [
{
id: userId,
metadata: { _artist_pick: null }
metadata: {
artist_pick_track_id: null,
_artist_pick: null
}
}
])
)
yield* call(audiusBackendInstance.setArtistPick)
const user = yield* call(waitForValue, getUser, { id: userId })
yield* call(audiusBackendInstance.setArtistPick, user, userId)

const event = make(Name.ARTIST_PICK_SELECT_TRACK, { id: 'none' })
yield* put(event)
Expand Down

0 comments on commit 7a8a207

Please sign in to comment.