Skip to content
Closed
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
36 changes: 27 additions & 9 deletions packages/common/src/adapters/accessConditionsFromSDK.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
import type { full } from '@audius/sdk'
import type { AccessGate, full } from '@audius/sdk'
import {
instanceOfExtendedPurchaseGate,
instanceOfFollowGate,
instanceOfPurchaseGate,
instanceOfTokenGate,
instanceOfNftGate
} from '@audius/sdk/src/sdk/api/generated/full'
instanceOfTokenGate
} from '@audius/sdk'

import { AccessConditions } from '~/models'

/** Accepts default API AccessGate or full API AccessGate (e.g. from playlists). */
export const accessConditionsFromSDK = (
input: full.AccessGate
input: AccessGate | full.AccessGate
): AccessConditions | null => {
if (instanceOfFollowGate(input)) {
return { follow_user_id: input.followUserId }
} else if (instanceOfPurchaseGate(input)) {
return { usdc_purchase: input.usdcPurchase }
} else if (instanceOfExtendedPurchaseGate(input)) {
const purchase = input.usdcPurchase
const splits = Array.isArray(purchase.splits)
? purchase.splits.map((s) => ({
user_id: s.userId,
percentage: s.percentage,
payout_wallet: s.payoutWallet,
amount: s.amount,
...(s.ethWallet != null && { eth_wallet: s.ethWallet })
}))
: []
const albumTrackPrice = (purchase as { albumTrackPrice?: number })
.albumTrackPrice
return {
usdc_purchase: {
price: purchase.price,
...(albumTrackPrice != null && { albumTrackPrice }),
splits
}
}
} else if (instanceOfTokenGate(input)) {
return {
token_gate: {
token_mint: input.tokenGate.tokenMint,
token_amount: input.tokenGate.tokenAmount
}
}
} else if (instanceOfNftGate(input)) {
} else if ('nftCollection' in input && input.nftCollection != null) {
return null
} else {
throw new Error(`Unsupported access gate type: ${JSON.stringify(input)}`)
Expand Down
42 changes: 37 additions & 5 deletions packages/common/src/adapters/accessConditionsToSDK.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import type { TrackMetadata } from '@audius/sdk'
import type { CreateAlbumMetadata, TrackMetadata } from '@audius/sdk'

import {
AccessConditions,
isContentFollowGated,
isContentTokenGated,
isContentUSDCPurchaseGated
isContentUSDCPurchaseGated,
type PaymentSplit,
type USDCPurchaseConditions
} from '~/models'

/** Maps common USDC conditions to SDK format. Use for albums (stream conditions are USDC-only). */
export const usdcPurchaseConditionsToSDK = (
input: USDCPurchaseConditions
): NonNullable<CreateAlbumMetadata['streamConditions']> => {
const splits = input.usdc_purchase.splits ?? []
return {
usdcPurchase: {
price: input.usdc_purchase.price,
...(input.usdc_purchase.albumTrackPrice != null && {
albumTrackPrice: input.usdc_purchase.albumTrackPrice
}),
splits: splits.map(
(
s: PaymentSplit & {
userId?: number
payoutWallet?: string
ethWallet?: string
}
) => ({
userId: s.user_id ?? s.userId,
percentage: s.percentage,
payoutWallet: s.payout_wallet ?? s.payoutWallet ?? '',
amount: s.amount,
...((s.eth_wallet ?? s.ethWallet) != null && {
ethWallet: s.eth_wallet ?? s.ethWallet
})
})
)
}
}
}

export const accessConditionsToSDK = (
input: AccessConditions
): TrackMetadata['downloadConditions'] => {
Expand All @@ -15,9 +49,7 @@ export const accessConditionsToSDK = (
followUserId: input.follow_user_id
}
} else if (isContentUSDCPurchaseGated(input)) {
return {
usdcPurchase: input.usdc_purchase
}
return usdcPurchaseConditionsToSDK(input)
} else if (isContentTokenGated(input)) {
return {
tokenGate: {
Expand Down
12 changes: 6 additions & 6 deletions packages/common/src/adapters/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import {
UserCollectionMetadata,
Variant
} from '~/models/Collection'
import { Copyright } from '~/models/Track'
import { Copyright, isContentUSDCPurchaseGated } from '~/models/Track'
import type { AlbumValues, PlaylistValues } from '~/schemas'

import { accessConditionsFromSDK } from './accessConditionsFromSDK'
import { usdcPurchaseConditionsToSDK } from './accessConditionsToSDK'
import { resourceContributorFromSDK } from './attribution'
import { favoriteFromSDK } from './favorite'
import { coverArtSizesCIDsFromSDK } from './imageSize'
Expand Down Expand Up @@ -135,7 +136,7 @@ export const userCollectionMetadataFromSDK = (
: null,
stream_conditions:
'streamConditions' in input && input.streamConditions
? accessConditionsFromSDK(input.streamConditions as full.AccessGate)
? accessConditionsFromSDK(input.streamConditions)
: null,
tracks: transformAndCleanList(
'tracks' in input ? (input.tracks ?? []) : [],
Expand Down Expand Up @@ -254,10 +255,9 @@ export const albumMetadataForCreateWithSDK = (
): CreateAlbumMetadata => {
return {
streamConditions:
input.stream_conditions && 'usdc_purchase' in input.stream_conditions
? {
usdcPurchase: input.stream_conditions.usdc_purchase
}
input.stream_conditions != null &&
isContentUSDCPurchaseGated(input.stream_conditions)
? usdcPurchaseConditionsToSDK(input.stream_conditions)
: null,
isStreamGated: input.is_stream_gated ?? false,
isScheduledRelease: input.is_scheduled_release ?? false,
Expand Down
10 changes: 4 additions & 6 deletions packages/common/src/adapters/track.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
type full,
type CrossPlatformFile,
type Genre,
type Mood,
type NativeFile,
type Track,
HashId,
Expand Down Expand Up @@ -177,11 +175,11 @@ export const userTrackMetadataFromSDK = (
permalink: input.permalink,
is_stream_gated: input.isStreamGated ?? false,
stream_conditions: input.streamConditions
? accessConditionsFromSDK(input.streamConditions as full.AccessGate)
? accessConditionsFromSDK(input.streamConditions)
: null,
is_download_gated: input.isDownloadGated ?? false,
download_conditions: input.downloadConditions
? accessConditionsFromSDK(input.downloadConditions as full.AccessGate)
? accessConditionsFromSDK(input.downloadConditions)
: null,
access: { stream: access.stream, download: access.download },
is_available: input.isAvailable ?? true,
Expand Down Expand Up @@ -380,9 +378,9 @@ export const trackMetadataForUploadToSdk = (input: TrackMetadataForUpload) => ({
trackId: OptionalId.parse(input.track_id),
title: input.title,
description: squashNewLines(input.description) ?? undefined,
mood: input.mood as Mood,
mood: input.mood,
tags: input.tags ?? undefined,
genre: (input.genre as Genre) || undefined,
genre: input.genre || undefined,
releaseDate: input.release_date ? new Date(input.release_date) : undefined,
previewStartSeconds: input.preview_start_seconds ?? undefined,
previewCid: input.preview_cid ?? '',
Expand Down
9 changes: 4 additions & 5 deletions packages/common/src/adapters/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {
HashId,
OptionalHashId,
OptionalId,
type full,
type User,
type UpdateProfileRequest
type UpdateUserRequest
} from '@audius/sdk'
import camelcaseKeys from 'camelcase-keys'
import { omit, pick } from 'lodash'
Expand Down Expand Up @@ -220,7 +219,7 @@ export const accountFromSDK = (

export const userMetadataToSdk = (
input: WriteableUserMetadata & Pick<AccountUserMetadata, 'playlist_library'>
): UpdateProfileRequest['metadata'] => ({
): UpdateUserRequest['metadata'] => ({
...camelcaseKeys(
pick(input, [
'name',
Expand All @@ -233,9 +232,9 @@ export const userMetadataToSdk = (
),
bio: input.bio ?? undefined,
website: input.website ?? undefined,
artistPickTrackId: OptionalId.parse(input.artist_pick_track_id ?? undefined),
artistPickTrackId: input.artist_pick_track_id ?? undefined,
events: {
referrer: OptionalId.parse(input.events?.referrer ?? undefined),
referrer: input.events?.referrer ?? undefined,
isMobileUser: input.events?.is_mobile_user ?? undefined
},
location: input.location ?? undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const useUpdateArtistCoin = () => {
const response = await sdk.coins.updateCoin({
mint,
userId: Id.parse(currentUserId),
updateCoinRequest: {
metadata: {
description: updateCoinRequest.description,
link1: updateCoinRequest.links?.[0] ?? '',
link2: updateCoinRequest.links?.[1] ?? '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ export const useUpdateCollection = () => {
collectionUpdate.stream_conditions = {
usdc_purchase: {
price: priceCents,
splits: {
[userBankStr]: priceWei
}
splits: [
{
payout_wallet: userBankStr,
percentage: 100,
amount: priceWei
}
]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Id } from '@audius/sdk'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { cloneDeep } from 'lodash'
import { useDispatch } from 'react-redux'
Expand Down Expand Up @@ -27,7 +28,10 @@ export const useDeleteComment = () => {
const dispatch = useDispatch()
return useMutation({
mutationFn: async ({ commentId, userId }: DeleteCommentArgs) => {
const commentData = { userId, entityId: commentId }
const commentData = {
userId: Id.parse(userId),
commentId: Id.parse(commentId)
}
const sdk = await audiusSdk()
return await sdk.comments.deleteComment(commentData)
},
Expand Down
24 changes: 12 additions & 12 deletions packages/common/src/api/tan-query/comments/useEditComment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityType, CommentMention } from '@audius/sdk'
import { EntityType, CommentMention, Id } from '@audius/sdk'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { useDispatch } from 'react-redux'

Expand Down Expand Up @@ -29,19 +29,19 @@ export const useEditComment = () => {
userId,
newMessage,
trackId,
mentions,
entityType = EntityType.TRACK
mentions
}: EditCommentArgs) => {
const commentData = {
body: newMessage,
userId,
entityId: commentId,
trackId,
entityType,
mentions: mentions?.map((mention) => mention.userId) ?? []
}
const sdk = await audiusSdk()
await sdk.comments.editComment(commentData)
await sdk.comments.updateComment({
userId: Id.parse(userId)!,
commentId: Id.parse(commentId)!,
metadata: {
body: newMessage,
entityId: trackId,
entityType: 'Track',
mentions: mentions?.map((mention) => mention.userId) ?? []
}
})
},
onMutate: ({ commentId, newMessage, mentions }) => {
const prevComment = queryClient.getQueryData(
Expand Down
26 changes: 20 additions & 6 deletions packages/common/src/api/tan-query/comments/usePinComment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Id } from '@audius/sdk'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { cloneDeep } from 'lodash'
import { useDispatch } from 'react-redux'
Expand Down Expand Up @@ -29,12 +30,25 @@ export const usePinComment = () => {
mutationFn: async (args: PinCommentArgs) => {
const { userId, commentId, isPinned, trackId } = args
const sdk = await audiusSdk()
return await sdk.comments.pinComment({
userId,
entityId: commentId,
trackId,
isPin: isPinned
})
if (isPinned) {
return await sdk.comments.pinComment({
userId: Id.parse(userId)!,
commentId: Id.parse(commentId)!,
metadata: {
entityId: trackId,
entityType: 'Track'
}
})
} else {
return await sdk.comments.unpinComment({
userId: Id.parse(userId)!,
commentId: Id.parse(commentId)!,
metadata: {
entityId: trackId,
entityType: 'Track'
}
})
}
},
onMutate: ({ commentId, isPinned, trackId, currentSort }) => {
if (isPinned) {
Expand Down
18 changes: 12 additions & 6 deletions packages/common/src/api/tan-query/comments/usePostComment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommentMention, EntityType } from '@audius/sdk'
import { CommentMention, EntityType, Id } from '@audius/sdk'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { cloneDeep } from 'lodash'

Expand Down Expand Up @@ -32,11 +32,17 @@ export const usePostComment = () => {
return useMutation({
mutationFn: async (args: PostCommentArgs) => {
const sdk = await audiusSdk()
return await sdk.comments.postComment({
...args,
mentions: args.mentions?.map((mention) => mention.userId) ?? [],
entityId: args.trackId,
commentId: args.newId
return await sdk.comments.createComment({
userId: Id.parse(args.userId)!,
metadata: {
commentId: args.newId,
entityId: args.trackId,
entityType: 'Track',
body: args.body,
trackTimestampS: args.trackTimestampS,
mentions: args.mentions?.map((mention) => mention.userId) ?? [],
parentId: args.parentCommentId
}
})
},
onMutate: async (args: PostCommentArgs) => {
Expand Down
15 changes: 14 additions & 1 deletion packages/common/src/api/tan-query/comments/useReactToComment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Id } from '@audius/sdk'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { useDispatch } from 'react-redux'

Expand Down Expand Up @@ -29,7 +30,19 @@ export const useReactToComment = () => {
trackId
}: ReactToCommentArgs) => {
const sdk = await audiusSdk()
await sdk.comments.reactComment({ userId, commentId, isLiked, trackId })
if (isLiked) {
await sdk.comments.reactToComment({
userId: Id.parse(userId)!,
commentId: Id.parse(commentId)!,
metadata: { entityId: trackId, entityType: 'Track' }
})
} else {
await sdk.comments.unreactToComment({
userId: Id.parse(userId)!,
commentId: Id.parse(commentId)!,
metadata: { entityId: trackId, entityType: 'Track' }
})
}
},
mutationKey: ['reactToComment'],
onMutate: async ({
Expand Down
Loading
Loading