Skip to content

Commit

Permalink
Update playlist on EntityManager (#3417)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacsolo authored Jul 13, 2022
1 parent c2864d9 commit 6e0f058
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 29 deletions.
20 changes: 14 additions & 6 deletions discovery-provider/src/tasks/audius_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from datetime import datetime
from enum import Enum
from typing import Any, Dict, Set, Tuple

from sqlalchemy.orm.session import Session, make_transient
Expand All @@ -12,6 +13,16 @@
logger = logging.getLogger(__name__)


class Action(Enum):
CREATE = "Create"
UPDATE = "Update"
DELETE = "Delete"


class EntityType(Enum):
PLAYLIST = "Playlist"


def audius_data_state_update(
self,
update_task: DatabaseTask,
Expand Down Expand Up @@ -60,7 +71,7 @@ def audius_data_state_update(
)

# Handle playlist creation
if entity_type == "Playlist":
if entity_type == EntityType.PLAYLIST:
playlist_id = entity_id
# look up or populate existing record
if playlist_id in playlist_events_lookup:
Expand All @@ -77,8 +88,7 @@ def audius_data_state_update(
txhash,
)

if action == "Create":
playlist_id = entity_id
if action == Action.CREATE or Action.UPDATE:
playlist_record = parse_playlist_create_data_event(
update_task,
entry,
Expand All @@ -89,7 +99,7 @@ def audius_data_state_update(
session,
)

elif action == "Delete":
elif Action.DELETE:
existing_playlist_record.is_delete = True
playlist_record = existing_playlist_record

Expand All @@ -104,9 +114,7 @@ def audius_data_state_update(
"playlist"
] = playlist_record
playlist_events_lookup[playlist_id]["events"].append(event_type)

playlist_ids.add(playlist_id)

processed_entries += 1

num_total_changes += processed_entries
Expand Down
116 changes: 93 additions & 23 deletions libs/src/api/entityManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Base, BaseConstructorArgs, Services } from './base'

export enum Action {
CREATE = 'Create',
UPDATE = 'Update',
DELETE = 'Delete'
}

export enum EntityType {
PLAYLIST = 'Playlist'
}

/*
API surface for updated data contract interactions.
Provides simplified entity management in a generic fashion
Expand Down Expand Up @@ -50,18 +60,13 @@ export class EntityManager extends Base {
description: string
isAlbum: boolean
isPrivate: boolean
coverArt: any
logger: any
}): Promise<{ blockHash: any; blockNumber: any; playlistId: number }> {
let respValues = {
blockHash: null,
blockNumber: null,
playlistId: 0
}
coverArt: string
logger: Console
}): Promise<{ blockHash: string; blockNumber: number; playlistId: number }> {
try {
const ownerId: number = parseInt(this.userStateManager.getCurrentUserId())
const action = 'Create'
const entityType = 'Playlist'
const createAction = Action.CREATE
const entityType = EntityType.PLAYLIST
const entityId = await this.getValidPlaylistId()
this.REQUIRES(Services.CREATOR_NODE)
const updatedPlaylistImage = await this.creatorNode.uploadImage(
Expand All @@ -70,7 +75,7 @@ export class EntityManager extends Base {
)
const dirCID = updatedPlaylistImage.dirCID
const metadata = {
action,
createAction,
entity_type: entityType,
playlist_id: entityId,
playlist_contents: trackIds,
Expand All @@ -86,20 +91,20 @@ export class EntityManager extends Base {
userId: ownerId,
entityType,
entityId,
action,
createAction,
metadataMultihash
})
logger.info(`CreatePlaylistData - ${JSON.stringify(resp)}`)
const txReceipt = resp.txReceipt
respValues = {
return {
blockHash: txReceipt.blockHash,
blockNumber: txReceipt.blockNumber,
playlistId: entityId
}
} catch (e) {
logger.error(`Data create playlist: err ${e}`)
throw e
}
return respValues
}

/**
Expand All @@ -114,10 +119,6 @@ export class EntityManager extends Base {
userId: number
logger: any
}): Promise<{ blockHash: any; blockNumber: any }> {
let respValues = {
blockHash: null,
blockNumber: null
}
try {
const resp = await this.manageEntity({
userId,
Expand All @@ -128,14 +129,83 @@ export class EntityManager extends Base {
})
logger.info(`DeletePlaylistData - ${JSON.stringify(resp)}`)
const txReceipt = resp.txReceipt
respValues = {
return {
blockHash: txReceipt.blockHash,
blockNumber: txReceipt.blockNumber
}
} catch (e) {
logger.error(`Data delete playlist: err ${e}`)
throw e
}
}
/**
* Update a playlist using updated data contracts flow
**/
async updatePlaylist({
playlistId,
playlistName,
trackIds,
description,
isAlbum,
isPrivate,
coverArt,
logger = console
}: {
playlistId: number
playlistName: string
trackIds: number[]
description: string
isAlbum: boolean
isPrivate: boolean
coverArt: string
logger: Console
}): Promise<{ blockHash: string; blockNumber: number; playlistId: number }> {
try {
const userId: number = parseInt(this.userStateManager.getCurrentUserId())
const updateAction = Action.UPDATE
const entityType = 'Playlist'
this.REQUIRES(Services.CREATOR_NODE)
let dirCID;
if (coverArt) {
const updatedPlaylistImage = await this.creatorNode.uploadImage(
coverArt,
true // square
)
dirCID = updatedPlaylistImage.dirCID
}

const playlist = (await this.discoveryProvider.getPlaylists(1, 0, [playlistId]))[0]

const metadata = {
action: updateAction, // why include action here?
entityType,
playlist_id: playlistId,
playlist_contents: trackIds || playlist.playlist_contents,
playlist_name: playlistName || playlist.playlist_name,
playlist_image_sizes_multihash: dirCID || playlist.playlist_image_sizes_multihash,
description: description || playlist.description,
is_album: isAlbum || playlist.is_album,
is_private: isPrivate || playlist.is_private
}
const { metadataMultihash } = await this.creatorNode.uploadPlaylistMetadata(metadata)

const resp = await this.manageEntity({
userId,
entityType,
entityId: playlistId,
action: updateAction,
metadataMultihash
})
const txReceipt = resp.txReceipt
return {
blockHash: txReceipt.blockHash,
blockNumber: txReceipt.blockNumber,
playlistId
}
} catch (e) {
logger.error(`Data update playlist: err ${e}`)
throw e
}
return respValues
}

/**
Expand All @@ -150,11 +220,11 @@ export class EntityManager extends Base {
metadataMultihash
}: {
userId: number
entityType: string
entityType: EntityType
entityId: number
action: string
action: Action
metadataMultihash: string
}): Promise<{ txReceipt: {}; error: any }> {
}): Promise<{ txReceipt: {blockHash: string, blockNumber: number}; error: any }> {
let error: string = ''
let resp: any
try {
Expand Down

0 comments on commit 6e0f058

Please sign in to comment.