Skip to content

Commit

Permalink
Add follow premium condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Saliou Diallo committed Sep 26, 2022
1 parent e0dd229 commit 7b6e4ae
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions discovery-provider/src/api/v1/models/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"premium_conditions",
{
"nft-collection": fields.String,
"follow": fields.Integer,
},
)
premium_content_signature = ns.model(
Expand Down
16 changes: 15 additions & 1 deletion discovery-provider/src/premium_content/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

# from src.utils import db_session
from src.models.social.follow import Follow
from src.utils import db_session

logger = logging.getLogger(__name__)

Expand All @@ -11,3 +12,16 @@ def does_user_have_nft_collection(user_id: int, nft_collection: str):
# db = db_session.get_db_read_replica()
# with db.scoped_session() as session:
return True


def does_user_follow_artist(user_id: int, artist_id: int):
db = db_session.get_db_read_replica()
with db.scoped_session() as session:
result = (
session.query(Follow)
.filter(Follow.is_current == True)
.filter(Follow.follower_user_id == user_id)
.filter(Follow.followee_user_id == artist_id)
.one_or_none()
)
return True if result else False
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import json
import logging
from typing import Dict, List, TypedDict, cast
from typing import Any, Callable, Dict, List, TypedDict, cast

from sqlalchemy.orm.session import Session
from src.models.tracks.track import Track
from src.premium_content.helpers import does_user_have_nft_collection
from src.premium_content.premium_content_types import PremiumContentType
from src.premium_content.helpers import (
does_user_follow_artist,
does_user_have_nft_collection,
)
from src.premium_content.premium_content_types import (
PremiumContentConditions,
PremiumContentType,
)
from src.utils import helpers

logger = logging.getLogger(__name__)
Expand All @@ -30,6 +36,14 @@ class PremiumContentAccessBatchResponse(TypedDict):
track: PremiumTrackAccessResult


PREMIUM_CONDITION_TO_HANDLER_MAP: Dict[
PremiumContentConditions, Callable[[int, Any], bool]
] = {
"nft-collection": does_user_have_nft_collection,
"follow": does_user_follow_artist,
}


class PremiumContentAccessChecker:
# Given a user id, premium content id, and premium content type, and premium content entity,
# this method checks for access to the premium contents by the users.
Expand Down Expand Up @@ -203,22 +217,26 @@ def _get_premium_track_data_for_batch(self, session: Session, track_ids: List[in

# There will eventually be another step prior to this one where
# we aggregate multiple conditions and evaluate them altogether.
# For now, we only support one condition, which is the ownership
# of an NFT from a given collection.
# For now, we do not support aggregate conditions.
def _evaluate_conditions(
self, user_id: int, premium_content_owner_id: int, premium_conditions: Dict
):
if len(premium_conditions) != 1:
logging.info(
f"premium_content_access_checker.py | _evaluate_conditions | invalid conditions: {json.dumps(premium_conditions)}"
f"premium_content_access_checker.py | _evaluate_conditions | invalid number of conditions: {json.dumps(premium_conditions)}"
)
return False

# Indexing of the premium content should have already validated
# the premium conditions, but we perform additional checks here just in case.
condition, value = list(premium_conditions.items())[0]
if condition != "nft-collection":
if condition not in set(PREMIUM_CONDITION_TO_HANDLER_MAP.keys()):
logging.info(
f"premium_content_access_checker.py | _evaluate_conditions | invalid condition: {json.dumps(premium_conditions)}"
)
return False

return does_user_have_nft_collection(user_id, value)
return PREMIUM_CONDITION_TO_HANDLER_MAP[condition](user_id, value)


premium_content_access_checker = PremiumContentAccessChecker()
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# will be gated.
# They should match the PremiumConditions property in the track schema
# in src/schemas/track_schema.json
PremiumContentConditions = Literal["nft-collection"]
PremiumContentConditions = Literal["nft-collection", "follow"]

# This is for when we support the combination of different conditions
# for premium content access based on AND'ing / OR'ing them together.
Expand Down
8 changes: 4 additions & 4 deletions discovery-provider/src/schemas/track_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@
"properties": {
"nft-collection": {
"type": "string"
},
"follow": {
"type": "number"
}
},
"required": [
"nft-collection"
],
"$comment": "Once we allow other conditions, we need to update the required property to [] as users will determine what conditions they're gating their content on; the conditions won't necessarily include 'nft-collection'.",
"required": [],
"title": "PremiumConditions"
}
}
Expand Down
8 changes: 4 additions & 4 deletions libs/src/services/schemaValidator/schemas/trackSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@
"properties": {
"nft-collection": {
"type": "string"
},
"follow": {
"type": "number"
}
},
"required": [
"nft-collection"
],
"$comment": "Once we allow other conditions, we need to update the required property to [] as users will determine what conditions they're gating their content on; the conditions won't necessarily include 'nft-collection'.",
"required": [],
"title": "PremiumConditions"
}
}
Expand Down

0 comments on commit 7b6e4ae

Please sign in to comment.