Skip to content

Commit

Permalink
[PAY-1884] Audio matching challenges for buyer and seller (#6175)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharit-tan authored Oct 3, 2023
1 parent 40325c8 commit 1362f0e
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 263 deletions.
59 changes: 59 additions & 0 deletions discovery-provider/src/challenges/audio_matching_challenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import Dict, Optional, Tuple

from sqlalchemy.orm.session import Session

from src.challenges.challenge import ChallengeManager, ChallengeUpdater
from src.models.users.user import User


def generate_audio_matching_specifier(user_id: int, extra: Dict) -> str:
return f"{user_id}=>{extra['track_id']}"


def does_user_exist_with_verification_status(
session, user_id: int, is_verified: bool
) -> bool:
user: Optional[Tuple[int]] = (
session.query(User.user_id)
.filter(
User.user_id == user_id,
bool(User.is_current),
User.is_verified == is_verified,
)
.one_or_none()
)
return bool(user)


class AudioMatchingBuyerChallengeUpdater(ChallengeUpdater):
def generate_specifier(self, user_id: int, extra: Dict) -> str:
return generate_audio_matching_specifier(user_id, extra)

def should_create_new_challenge(
self, session, event: str, user_id: int, extra: Dict
) -> bool:
return does_user_exist_with_verification_status(session, user_id, False)

def should_show_challenge_for_user(self, session: Session, user_id: int) -> bool:
return does_user_exist_with_verification_status(session, user_id, False)


class AudioMatchingSellerChallengeUpdater(ChallengeUpdater):
def generate_specifier(self, user_id: int, extra: Dict) -> str:
return generate_audio_matching_specifier(extra["sender_user_id"], extra)

def should_create_new_challenge(
self, session, event: str, user_id: int, extra: Dict
) -> bool:
return does_user_exist_with_verification_status(session, user_id, True)

def should_show_challenge_for_user(self, session: Session, user_id: int) -> bool:
return does_user_exist_with_verification_status(session, user_id, True)


audio_matching_buyer_challenge_manager = ChallengeManager(
"b", AudioMatchingBuyerChallengeUpdater()
)
audio_matching_seller_challenge_manager = ChallengeManager(
"s", AudioMatchingSellerChallengeUpdater()
)
2 changes: 2 additions & 0 deletions discovery-provider/src/challenges/challenge_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ class ChallengeEvent(str, enum.Enum):
trending_playlist = "trending_playlist"
send_tip = "send_tip" # Fired for sender
first_playlist = "first_playlist"
audio_matching_buyer = "audio_matching_buyer"
audio_matching_seller = "audio_matching_seller"
10 changes: 10 additions & 0 deletions discovery-provider/src/challenges/challenge_event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

from sqlalchemy.orm.session import Session

from src.challenges.audio_matching_challenge import (
audio_matching_buyer_challenge_manager,
audio_matching_seller_challenge_manager,
)
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
Expand Down Expand Up @@ -241,5 +245,11 @@ def setup_challenge_bus():
bus.register_listener(
ChallengeEvent.first_playlist, first_playlist_challenge_manager
)
bus.register_listener(
ChallengeEvent.audio_matching_buyer, audio_matching_buyer_challenge_manager
)
bus.register_listener(
ChallengeEvent.audio_matching_seller, audio_matching_seller_challenge_manager
)

return bus
146 changes: 81 additions & 65 deletions discovery-provider/src/challenges/challenges.dev.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,82 @@
[
{
"id": "listen-streak",
"active": true,
"starting_block": 0
},
{
"id": "track-upload",
"active": true,
"starting_block": 0
},
{
"id": "referrals",
"active": true,
"starting_block": 0
},
{
"id": "ref-v",
"active": true,
"starting_block": 0
},
{
"id": "referred",
"active": true,
"starting_block": 0
},
{
"id": "connect-verified",
"active": true,
"starting_block": 0
},
{
"id": "mobile-install",
"active": true,
"starting_block": 0
},
{
"id": "tt",
"active": true,
"starting_block": 0
},
{
"id": "tut",
"active": true,
"starting_block": 0
},
{
"id": "tp",
"active": true,
"starting_block": 0
},
{
"id": "send-first-tip",
"type": "boolean",
"amount": 2,
"active": true,
"starting_block": 0
},
{
"id": "first-playlist",
"type": "boolean",
"amount": 2,
"active": true,
"starting_block": 0
}
]
{
"id": "listen-streak",
"active": true,
"starting_block": 0
},
{
"id": "track-upload",
"active": true,
"starting_block": 0
},
{
"id": "referrals",
"active": true,
"starting_block": 0
},
{
"id": "ref-v",
"active": true,
"starting_block": 0
},
{
"id": "referred",
"active": true,
"starting_block": 0
},
{
"id": "connect-verified",
"active": true,
"starting_block": 0
},
{
"id": "mobile-install",
"active": true,
"starting_block": 0
},
{
"id": "tt",
"active": true,
"starting_block": 0
},
{
"id": "tut",
"active": true,
"starting_block": 0
},
{
"id": "tp",
"active": true,
"starting_block": 0
},
{
"id": "send-first-tip",
"type": "boolean",
"amount": 2,
"active": true,
"starting_block": 0
},
{
"id": "first-playlist",
"type": "boolean",
"amount": 2,
"active": true,
"starting_block": 0
},
{
"id": "b",
"type": "aggregate",
"amount": "1",
"active": true,
"step_count": 2147483647,
"starting_block": 0
},
{
"id": "s",
"type": "aggregate",
"amount": "1",
"active": true,
"step_count": 2147483647,
"starting_block": 0
}
]
Loading

0 comments on commit 1362f0e

Please sign in to comment.