Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAY-385] Added First Playlist Challenge #3375

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import logging
from datetime import datetime

import redis
from src.challenges.challenge_event_bus import ChallengeEvent, ChallengeEventBus
from src.challenges.first_playlist_challenge import first_playlist_challenge_manager
from src.models.indexing.block import Block
from src.models.rewards.challenge import Challenge
from src.models.users.user import User
from src.utils.config import shared_config
from src.utils.db_session import get_db

REDIS_URL = shared_config["redis"]["url"]
BLOCK_NUMBER = 10
logger = logging.getLogger(__name__)


def test_first_playlist_challenge(app):
redis_conn = redis.Redis.from_url(url=REDIS_URL)

with app.app_context():
db = get_db()

block = Block(blockhash="0x1", number=BLOCK_NUMBER)
user = User(
blockhash="0x1",
blocknumber=BLOCK_NUMBER,
txhash="xyz",
user_id=1,
is_current=True,
handle="TestHandle",
handle_lc="testhandle",
wallet="0x1",
is_creator=False,
is_verified=False,
name="test_name",
created_at=datetime.now(),
updated_at=datetime.now(),
)

with db.scoped_session() as session:
bus = ChallengeEventBus(redis_conn)
session.query(Challenge).filter(Challenge.id == "first-playlist").update(
{"active": True, "starting_block": BLOCK_NUMBER}
)

# Register events with the bus
bus.register_listener(
ChallengeEvent.first_playlist, first_playlist_challenge_manager
)

session.add(block)
session.flush()
session.add(user)
session.flush()

bus.dispatch(
ChallengeEvent.first_playlist,
BLOCK_NUMBER,
1, # user_id
dharit-tan marked this conversation as resolved.
Show resolved Hide resolved
{},
)

bus.flush()
bus.process_events(session)
session.flush()

state = first_playlist_challenge_manager.get_user_challenge_state(
session, ["1"]
)[0]

assert state.is_complete
1 change: 1 addition & 0 deletions discovery-provider/src/challenges/challenge_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ class ChallengeEvent(str, enum.Enum):
trending_underground = "trending_underground"
trending_playlist = "trending_playlist"
send_tip = "send_tip" # Fired for sender
first_playlist = "first_playlist"
4 changes: 4 additions & 0 deletions discovery-provider/src/challenges/challenge_event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from src.challenges.challenge import ChallengeManager, EventMetadata
from src.challenges.challenge_event import ChallengeEvent
from src.challenges.connect_verified_challenge import connect_verified_challenge_manager
from src.challenges.first_playlist_challenge import first_playlist_challenge_manager
from src.challenges.listen_streak_challenge import listen_streak_challenge_manager
from src.challenges.mobile_install_challenge import mobile_install_challenge_manager
from src.challenges.profile_challenge import profile_challenge_manager
Expand Down Expand Up @@ -235,5 +236,8 @@ def setup_challenge_bus():
ChallengeEvent.trending_playlist, trending_playlist_challenge_manager
)
bus.register_listener(ChallengeEvent.send_tip, send_first_tip_challenge_manager)
bus.register_listener(
ChallengeEvent.first_playlist, first_playlist_challenge_manager
)

return bus
7 changes: 7 additions & 0 deletions discovery-provider/src/challenges/challenges.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@
"amount": 2,
"active": true,
"starting_block": 0
},
{
"id": "first-playlist",
"type": "boolean",
"amount": 2,
"active": true,
"starting_block": 0
}
]
7 changes: 7 additions & 0 deletions discovery-provider/src/challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,12 @@
"amount": 2,
"active": true,
"starting_block": 0
},
{
"id": "first-playlist",
"type": "boolean",
"amount": 2,
"active": true,
"starting_block": 0
dharit-tan marked this conversation as resolved.
Show resolved Hide resolved
}
]
7 changes: 7 additions & 0 deletions discovery-provider/src/challenges/challenges.stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@
"amount": 2,
"active": true,
"starting_block": 0
},
{
"id": "first-playlist",
"type": "boolean",
"amount": 2,
"active": true,
"starting_block": 0
}
]
28 changes: 28 additions & 0 deletions discovery-provider/src/challenges/first_playlist_challenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List, Optional

from sqlalchemy.orm.session import Session
from src.challenges.challenge import (
ChallengeManager,
ChallengeUpdater,
FullEventMetadata,
)
from src.models.rewards.user_challenge import UserChallenge


class FirstPlaylistChallengeUpdater(ChallengeUpdater):
def update_user_challenges(
self,
session: Session,
event: str,
user_challenges: List[UserChallenge],
step_cout: Optional[int],
dharit-tan marked this conversation as resolved.
Show resolved Hide resolved
event_metadatas: List[FullEventMetadata],
starting_block: Optional[int],
):
for user_challenge in user_challenges:
user_challenge.is_complete = True


first_playlist_challenge_manager = ChallengeManager(
"first-playlist", FirstPlaylistChallengeUpdater()
)
12 changes: 12 additions & 0 deletions discovery-provider/src/tasks/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Dict, Set, Tuple

from sqlalchemy.orm.session import Session, make_transient
from src.challenges.challenge_event import ChallengeEvent
from src.database_task import DatabaseTask
from src.models.playlists.playlist import Playlist
from src.queries.skipped_transactions import add_node_level_skipped_transaction
Expand Down Expand Up @@ -36,6 +37,8 @@ def playlist_state_update(
# This stores the playlist_ids created or updated in the set of transactions
playlist_ids: Set[int] = set()

challenge_bus = update_task.challenge_event_bus

if not playlist_factory_txs:
return num_total_changes, playlist_ids

Expand Down Expand Up @@ -109,6 +112,15 @@ def playlist_state_update(
if value_obj["events"]:
invalidate_old_playlist(session, playlist_id)
session.add(value_obj["playlist"])
if (
playlist_event_types_lookup["playlist_track_added"]
in value_obj["events"]
):
challenge_bus.dispatch(
ChallengeEvent.first_playlist,
value_obj["playlist"].blocknumber,
value_obj["playlist"].playlist_owner_id,
)

return num_total_changes, playlist_ids

Expand Down