-
Notifications
You must be signed in to change notification settings - Fork 23
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
Configure video settings for talks #288
Closed
odkhang
wants to merge
15
commits into
fossasia:development
from
odkhang:configure-video-settings-for-talks
Closed
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f36f656
change create_world api response
lcduong 17349b3
Update code
lcduong baa65d0
Update code
lcduong 77c1c4b
Update code
lcduong 63813f6
Fix isort, flake8 in pipeline
lcduong 81c8710
Config attendee role
lcduong cd9b0cd
Merge branch 'development' into setup-attendee-role
odkhang d176611
Fix black in pipeline
lcduong 8972af1
Merge branch 'setup-attendee-role' of github.com:odkhang/eventyay-vid…
lcduong aea462b
Update code
lcduong 44ba409
Update code
lcduong f8f6b0e
Configure video settings for talks
lcduong 6772b25
Merge branch 'development' of github.com:odkhang/eventyay-video into …
odkhang 320205a
Fix black in pipeline
odkhang 6da5b50
Update code
odkhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import datetime | ||
import datetime as dt | ||
import logging | ||
import uuid | ||
|
||
import jwt | ||
import requests | ||
from celery import shared_task | ||
from django.conf import settings | ||
|
||
from venueless.core.models.auth import ShortToken | ||
from venueless.core.models.world import World | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def generate_video_token(world, days, number, traits, long=False): | ||
""" | ||
Generate video token | ||
:param world: World object | ||
:param days: A integer representing the number of days the token is valid | ||
:param number: A integer representing the number of tokens to generate | ||
:param traits: A dictionary representing the traits of the token | ||
:param long: A boolean representing if the token is long or short | ||
:return: A list of tokens | ||
""" | ||
jwt_secrets = world.config.get("JWT_secrets", []) | ||
if not jwt_secrets: | ||
logger.error("JWT_secrets is missing or empty in the configuration") | ||
return | ||
jwt_config = jwt_secrets[0] | ||
secret = jwt_config.get("secret") | ||
audience = jwt_config.get("audience") | ||
issuer = jwt_config.get("issuer") | ||
iat = datetime.datetime.utcnow() | ||
exp = iat + datetime.timedelta(days=days) | ||
result = [] | ||
bulk_create = [] | ||
for _ in range(number): | ||
payload = { | ||
"iss": issuer, | ||
"aud": audience, | ||
"exp": exp, | ||
"iat": iat, | ||
"uid": str(uuid.uuid4()), | ||
"traits": traits, | ||
} | ||
token = jwt.encode(payload, secret, algorithm="HS256") | ||
if long: | ||
result.append(token) | ||
else: | ||
st = ShortToken(world=world, long_token=token, expires=exp) | ||
result.append(st.short_token) | ||
bulk_create.append(st) | ||
|
||
if not long: | ||
ShortToken.objects.bulk_create(bulk_create) | ||
return result | ||
|
||
|
||
def generate_talk_token(video_settings, video_tokens, event_slug): | ||
""" | ||
Generate talk token | ||
:param video_settings: A dictionary representing the video settings | ||
:param video_tokens: A list of video tokens | ||
:param event_slug: A string representing the event slug | ||
:return: A token | ||
""" | ||
iat = dt.datetime.utcnow() | ||
exp = iat + dt.timedelta(days=30) | ||
payload = { | ||
"exp": exp, | ||
"iat": iat, | ||
"video_tokens": video_tokens, | ||
"slug": event_slug, | ||
} | ||
token = jwt.encode(payload, video_settings.get("secret"), algorithm="HS256") | ||
return token | ||
|
||
|
||
@shared_task(bind=True, max_retries=5, default_retry_delay=60) | ||
def configure_video_settings_for_talks( | ||
self, world_id, days, number, traits, long=False | ||
): | ||
""" | ||
Configure video settings for talks | ||
:param self: instance of the task | ||
:param world_id: A integer representing the world id | ||
:param days: A integer representing the number of days the token is valid | ||
:param number: A integer representing the number of tokens to generate | ||
:param traits: A dictionary representing the traits of the token | ||
:param long: A boolean representing if the token is long or short | ||
""" | ||
world = World.objects.get(id=world_id) | ||
event_slug = world_id | ||
odkhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
jwt_secrets = world.config.get("JWT_secrets", []) | ||
if not jwt_secrets: | ||
logger.error("JWT_secrets is missing or empty in the configuration") | ||
return | ||
jwt_config = jwt_secrets[0] | ||
video_tokens = generate_video_token(world, days, number, traits, long) | ||
talk_token = generate_talk_token(jwt_config, video_tokens, event_slug) | ||
header = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {talk_token}", | ||
} | ||
payload = { | ||
"video_settings": { | ||
"audience": jwt_config.get("audience"), | ||
"issuer": jwt_config.get("issuer"), | ||
"secret": jwt_config.get("secret"), | ||
} | ||
} | ||
try: | ||
requests.post( | ||
"{}/api/configure-video-settings/".format(settings.EVENTYAY_TALK_BASE_PATH), | ||
json=payload, | ||
headers=header, | ||
) | ||
world.config["pretalx"] = { | ||
"event": event_slug, | ||
"domain": "{}".format(settings.EVENTYAY_TALK_BASE_PATH), | ||
"pushed": datetime.datetime.now().isoformat(), | ||
"connected": True, | ||
} | ||
world.save() | ||
except requests.exceptions.ConnectionError as e: | ||
logger.error("Connection error: %s", str(e)) | ||
self.retry(exc=e) | ||
except requests.exceptions.Timeout as e: | ||
logger.error("Request timed out: %s", str(e)) | ||
self.retry(exc=e) | ||
except requests.exceptions.RequestException as e: | ||
odkhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.error("Request failed: %s", str(e)) | ||
self.retry(exc=e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider introducing a TokenService class to encapsulate token generation and management functionality.
The token generation logic can be simplified by introducing a dedicated TokenService class to handle token creation and reduce parameter passing. This separates concerns and makes the code more maintainable. Here's a suggested refactor:
Usage in the task:
This refactor: