Skip to content

Commit

Permalink
feat: return more data when calling GET /user-session
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 13, 2019
1 parent 4b210fb commit ad0b26e
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 438 deletions.
444 changes: 222 additions & 222 deletions entity/entity.pb.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions entity/entity.proto
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ message User {
string gravatar_url = 102 [(gogoproto.customname) = "GravatarURL"];
string website_url = 103 [(gogoproto.customname) = "WebsiteURL"];
string locale = 104;
string oauth_subject = 105 [(gogoproto.moretags) = "gorm:\"index\""]; // FIXME: gorm:"index"
string oauth_subject = 105 [(gogoproto.moretags) = "gorm:\"unique_index\""];

//
// associations
Expand All @@ -97,7 +97,7 @@ message User {
repeated TournamentMember tournament_memberships = 200 [(gogoproto.moretags) = "gorm:\"PRELOAD:false\""];
repeated Notification notifications = 201 [(gogoproto.moretags) = "gorm:\"PRELOAD:false\""];
repeated TeamMember memberships = 202 [(gogoproto.moretags) = "gorm:\"PRELOAD:false\""];
TournamentMember active_tournament_member = 203 [(gogoproto.moretags) = "gorm:\"foreignkey:ActiveTournamentMemberID\""];
TournamentMember active_tournament_member = 203 [(gogoproto.moretags) = "gorm:\"foreignkey:ActiveTournamentMemberID;PRELOAD:false\""];
string active_tournament_member_id = 204 [(gogoproto.customname) = "ActiveTournamentMemberID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];
}

Expand Down
52 changes: 34 additions & 18 deletions server/api.pub.user-session.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,50 @@ func (s *svc) UserSession(ctx context.Context, _ *Void) (*UserSessionOutput, err
output := &UserSessionOutput{}
output.Claims = client.ClaimsFromToken(token)

var user entity.User
err = s.db.Where(entity.User{OauthSubject: output.Claims.ActionToken.Sub}).First(&user).Error
// try loading it from database
output.User, err = s.loadOauthUser(output.Claims.ActionToken.Sub)
if err != nil && !gorm.IsRecordNotFoundError(err) {
return output, err
// internal error
return nil, err
}

// new user
if gorm.IsRecordNotFoundError(err) {
output.IsNewUser = true
output.User, err = s.newUser(output.Claims)
if err != nil {
return output, err
if _, err = s.newUserFromClaims(output.Claims); err != nil {
return nil, err
}
// FIXME: reload the user with all the preloads? or recursive call
} else {
output.User = &user
if output.User.Username != output.Claims.PreferredUsername {
return output, fmt.Errorf("username differs from JWT token and database")
if output.User, err = s.loadOauthUser(output.Claims.ActionToken.Sub); err != nil {
return nil, err
}
}

// FIXME: ActiveTournamentTeam: ...
// FIXME: ActiveTeam: ...
// FIXME: ActiveTournament: ...
if output.User.Username != output.Claims.PreferredUsername {
return nil, fmt.Errorf("username differs from JWT token and database")
}

// FIXME: Notifications: 0,
// FIXME: output.Notifications = COUNT
output.Notifications = 42

return output, nil
}

func (s *svc) newUser(claims *client.Claims) (*entity.User, error) {
func (s *svc) loadOauthUser(subject string) (*entity.User, error) {
var user entity.User
if err := s.db.
Preload("ActiveTournamentMember").
Preload("ActiveTournamentMember.TournamentTeam").
Preload("ActiveTournamentMember.TournamentTeam.Tournament").
Preload("ActiveTournamentMember.TournamentTeam.Team").
Where(entity.User{OauthSubject: subject}).
First(&user).
Error; err != nil {
return nil, err
}
return &user, nil
}

func (s *svc) newUserFromClaims(claims *client.Claims) (*entity.User, error) {
if claims.EmailVerified == false {
return nil, fmt.Errorf("you need to verify your email address")
}
Expand Down Expand Up @@ -98,7 +112,6 @@ func (s *svc) newUser(claims *client.Claims) (*entity.User, error) {

tx := s.db.Begin()
tx.Create(&tournamentMember)
tx.Create(&user)

// FIXME: create a "welcome" notification

Expand All @@ -107,7 +120,10 @@ func (s *svc) newUser(claims *client.Claims) (*entity.User, error) {
}

// set active tournament
if err := s.db.Model(&user).Updates(entity.User{ActiveTournamentMemberID: tournamentMember.Metadata.ID}).Error; err != nil {
if err := s.db.
Model(&user).
Updates(entity.User{ActiveTournamentMemberID: tournamentMember.Metadata.ID}).
Error; err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit ad0b26e

Please sign in to comment.