Skip to content
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

Handle conflicts with handles #318

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions bgs/bgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,14 @@ func (bgs *BGS) checkAdminAuth(next echo.HandlerFunc) echo.HandlerFunc {
}

type User struct {
ID models.Uid `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Handle string `gorm:"uniqueIndex"`
Did string `gorm:"uniqueIndex"`
PDS uint
ID models.Uid `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Handle string `gorm:"uniqueIndex"`
Did string `gorm:"uniqueIndex"`
PDS uint
ValidHandle bool `gorm:"default:true"`

// TakenDown is set to true if the user in question has been taken down.
// A user in this state will have all future events related to it dropped
Expand Down Expand Up @@ -992,7 +993,33 @@ func (s *BGS) createExternalUser(ctx context.Context, did string) (*models.Actor
}

if err := s.db.Create(&u).Error; err != nil {
// some debugging...
// If the new user's handle conflicts with an existing user,
// since we just validated the handle for this user, we'll assume
// the existing user no longer has control of the handle
if errors.Is(err, gorm.ErrDuplicatedKey) {
// Get the UID of the existing user
var existingUser User
if err := s.db.Find(&existingUser, "handle = ?", handle).Error; err != nil {
return nil, fmt.Errorf("failed to find existing user: %w", err)
}

// Set the existing user's handle to NULL and set the valid_handle flag to false
if err := s.db.Model(User{}).Where("id = ?", existingUser.ID).Update("handle", nil).Update("valid_handle", false).Error; err != nil {
return nil, fmt.Errorf("failed to update outdated user's handle: %w", err)
}

// Do the same thing for the ActorInfo if it exists
if err := s.db.Model(models.ActorInfo{}).Where("uid = ?", existingUser.ID).Update("handle", nil).Update("valid_handle", false).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("failed to update outdated actorInfo's handle: %w", err)
}
}

// Create the new user
if err := s.db.Create(&u).Error; err != nil {
return nil, fmt.Errorf("failed to create user after handle conflict: %w", err)
}
}
ericvolp12 marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("failed to create other pds user: %w", err)
}

Expand Down
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ActorInfo struct {
Posts int64
Type string
PDS uint
ValidHandle bool `gorm:"default:true"`
}

func (ai *ActorInfo) ActorRef() *bsky.ActorDefs_ProfileViewBasic {
Expand Down
Loading