Skip to content

Commit

Permalink
fix: optionally join the [email protected] team
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Feb 19, 2024
1 parent d9bd0fb commit 41c6a06
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
22 changes: 0 additions & 22 deletions backend/repos/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from sqlalchemy import and_, exists, select

from ..config import get_config
from ..models.team_topic import TeamTopic
from ..models.user import User
from .base import DatabaseAbstractRepository
Expand Down Expand Up @@ -44,27 +43,6 @@ def search_username(self, name, limit=10):
select(User).filter(User.username.like(f"%{name}%")).limit(limit)
)

def create_from_kwargs(self, **kwargs):
from ..exceptions import BadRequest, NotAllowed
from ..repos.team_topic import TeamTopicRepo

user = super().create_from_kwargs(**kwargs)

# Automatically subscribe to some team topics
team_topic_repo = TeamTopicRepo(self._db)
user_team_topic_repo = UserTeamTopicRepo(self._db)
for subscribe_to in get_config().NEW_USER_SUBSCRIBE_TO:
try:
team_topic = team_topic_repo.from_string(subscribe_to, user)
user_team_topic_repo.create_from_kwargs(
user_id=user.id, team_topic_id=team_topic.id
)
except (BadRequest, NotAllowed):
# may fail if the team topic does not exist
# or creation of topic is not allowed
pass
return user

def get_subscriptions(self, user: User, team: Team):
return self._db.execute(
select(
Expand Down
7 changes: 7 additions & 0 deletions backend/repos/user_team.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from typing import Optional
from uuid import UUID

from ..models.user_team import UserTeam
Expand All @@ -13,3 +14,9 @@ def add_member(self, user_id: UUID, team_id: UUID):

def remove_member(self, membership):
self.delete(membership)

def ensure_member(self, user_id: UUID, team_id: UUID) -> Optional[object]:
"""Ensure that user_id is member of team"""
if not self.get_by_kwargs(user_id=user_id, team_id=team_id):
return self.create_from_kwargs(user_id=user_id, team_id=team_id)
return None
17 changes: 17 additions & 0 deletions backend/repos/user_team_topic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# -*- coding: utf-8 -*-
from ..models.user_team_topic import UserTeamTopic
from .base import DatabaseAbstractRepository
from .team_topic import TeamTopicRepo
from .user_team import UserTeamRepo


class UserTeamTopicRepo(DatabaseAbstractRepository):
ORM_Model = UserTeamTopic

def create_from_kwargs(self, **kwargs):
user_team_topic = super().create_from_kwargs(**kwargs)

team_topic_repo = TeamTopicRepo(self._db)
team_topic = team_topic_repo.get_by_id(user_team_topic.team_topic_id)

# Let's make sure we are subscribed to the team when we subscribe to a
# topic!
user_team_repo = UserTeamRepo(self._db)
user_team_repo.ensure_member(
user_id=user_team_topic.user_id, team_id=team_topic.team_id
)

return user_team_topic
30 changes: 28 additions & 2 deletions backend/routes/login/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

from ...config import Settings, get_config
from ...database import Session, get_session
from ...exceptions import BadRequest
from ...exceptions import BadRequest, NotAllowed
from ...models.user import OauthProvider
from ...repos.access_token import AccessTokenRepo
from ...repos.newsletter import NewsletterRepo
from ...repos.team_topic import TeamTopicRepo
from ...repos.user import UserRepo
from ...repos.user_team_topic import UserTeamTopicRepo
from ...templates import templates
from ...utils.url import get_next_url
from . import oauth
Expand Down Expand Up @@ -75,6 +77,7 @@ async def onboarding_github_post(
accept_tos: bool = Form(default=False),
accept_privacy: bool = Form(default=False),
accept_newsletter: bool = Form(default=False),
join_news_team: bool = Form(default=False),
db: Session = Depends(get_session),
):
if not accept_tos or not accept_privacy:
Expand All @@ -92,27 +95,50 @@ async def onboarding_github_post(
email = next(filter(lambda x: x["primary"] is True, emails))
email = email["email"]
user_repo = UserRepo(db)
access_token_repo = AccessTokenRepo(db)
user = user_repo.create_from_kwargs(
username=username,
email=email.lower(),
name=f"{first_name} {last_name}",
oauth_provider=OauthProvider.GITHUB,
profile_picture_url=github_user["avatar_url"],
)

# Generate access token for the user session
access_token_repo = AccessTokenRepo(db)
access_token = access_token_repo.get_by_kwargs(user_id=user.id)
if not access_token:
access_token = access_token_repo.create_from_kwargs(user_id=user.id)

# Accept newsletter
if accept_newsletter:
newsletter_repo = NewsletterRepo()
try:
newsletter_repo.subscribe(email, first_name, last_name, status="subscribed")
except Exception:
pass

# Automatically subscribe to some team topics
if join_news_team:
team_topic_repo = TeamTopicRepo(db)
user_team_topic_repo = UserTeamTopicRepo(db)
for subscribe_to in get_config().NEW_USER_SUBSCRIBE_TO:
try:
team_topic = team_topic_repo.from_string(subscribe_to, user)
user_team_topic_repo.create_from_kwargs(
user_id=user.id, team_topic_id=team_topic.id
)
except (BadRequest, NotAllowed):
# may fail if the team topic does not exist
# or creation of topic is not allowed
pass

# Store stuff in the user session
request.session["user_id"] = str(user.id)
request.session["access_token"] = str(access_token.token)

# Remove token from session, we don't need it anymore
request.session.pop("token")

return RedirectResponse(
url=get_next_url(request) or "/", status_code=status.HTTP_302_FOUND
)
7 changes: 7 additions & 0 deletions backend/templates/github-onboarding.pug
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
.control.has-icons-left.block
input#accept_newsletter.switch.is-medium.is-rounded.is-success(type="checkbox" name="accept_newsletter")
label(for='accept_newsletter')
.field
label.label Join
tt [email protected]
div.is-size-6.has-text-weight-normal We would like to join the news topic of relay.md?
.control.has-icons-left.block
input#join_news_team.switch.is-medium.is-rounded.is-success(type="checkbox" name="join_news_team" checked="checked")
label(for='join_news_team')
.field
sup.is-size-7.has-text-danger *
span.is-size-6.has-text-weight-normal Required
Expand Down

0 comments on commit 41c6a06

Please sign in to comment.