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

Fix loading edits and comments from deleted users #674

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion pkg/api/resolver_model_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ func (r *editResolver) ID(ctx context.Context, obj *models.Edit) (string, error)
func (r *editResolver) User(ctx context.Context, obj *models.Edit) (*models.User, error) {
fac := r.getRepoFactory(ctx)
qb := fac.User()
user, err := qb.Find(obj.UserID)

if obj.UserID.UUID.IsNil() {
return nil, nil
}

user, err := qb.Find(obj.UserID.UUID)

if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion pkg/api/resolver_model_edit_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func (r *editCommentResolver) Date(ctx context.Context, obj *models.EditComment)
func (r *editCommentResolver) User(ctx context.Context, obj *models.EditComment) (*models.User, error) {
fac := r.getRepoFactory(ctx)
qb := fac.User()
user, err := qb.Find(obj.UserID)

if obj.UserID.UUID.IsNil() {
return nil, nil
}

user, err := qb.Find(obj.UserID.UUID)

if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion pkg/api/resolver_model_edit_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ func (r *editVoteResolver) User(ctx context.Context, obj *models.EditVote) (*mod

fac := r.getRepoFactory(ctx)
qb := fac.User()
user, err := qb.Find(obj.UserID)

if obj.UserID.UUID.IsNil() {
return nil, nil
}

user, err := qb.Find(obj.UserID.UUID)

if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/resolver_mutation_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *mutationResolver) SceneEditUpdate(ctx context.Context, id uuid.UUID, in
return nil, err
}

if existingEdit.UserID != currentUser.ID {
if existingEdit.UserID.UUID != currentUser.ID {
return nil, ErrUnauthorizedUpdate
}

Expand Down Expand Up @@ -151,7 +151,7 @@ func (r *mutationResolver) StudioEditUpdate(ctx context.Context, id uuid.UUID, i
return nil, err
}

if existingEdit.UserID != currentUser.ID {
if existingEdit.UserID.UUID != currentUser.ID {
return nil, ErrUnauthorizedUpdate
}

Expand Down Expand Up @@ -228,7 +228,7 @@ func (r *mutationResolver) TagEditUpdate(ctx context.Context, id uuid.UUID, inpu
return nil, err
}

if existingEdit.UserID != currentUser.ID {
if existingEdit.UserID.UUID != currentUser.ID {
return nil, ErrUnauthorizedUpdate
}

Expand Down Expand Up @@ -311,7 +311,7 @@ func (r *mutationResolver) PerformerEditUpdate(ctx context.Context, id uuid.UUID
return nil, err
}

if existingEdit.UserID != currentUser.ID {
if existingEdit.UserID.UUID != currentUser.ID {
return nil, ErrUnauthorizedUpdate
}

Expand Down Expand Up @@ -354,7 +354,7 @@ func (r *mutationResolver) EditVote(ctx context.Context, input models.EditVoteIn
return ErrClosedEdit
}

if err := user.ValidateOwner(ctx, voteEdit.UserID); err == nil {
if err := user.ValidateOwner(ctx, voteEdit.UserID.UUID); err == nil {
return user.ErrUnauthorized
}

Expand Down Expand Up @@ -421,7 +421,7 @@ func (r *mutationResolver) CancelEdit(ctx context.Context, input models.CancelEd
return nil, err
}

if err = validateUser(ctx, e.UserID); err == nil {
if err = validateUser(ctx, e.UserID.UUID); err == nil {
return edit.CloseEdit(fac, input.ID, models.VoteStatusEnumCanceled)
} else if err = validateAdmin(ctx); err == nil {
currentUser := getCurrentUser(ctx)
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/resolver_mutation_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ func (r *mutationResolver) UserDestroy(ctx context.Context, input models.UserDes
return err
}

ret, err = user.Destroy(fac, input)

if err != nil {
if err := fac.Edit().CancelUserEdits(input.ID); err != nil {
return err
}

return nil
ret, err = user.Destroy(fac, input)

return err
})

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/manager/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func ApplyEdit(fac models.Repo, editID uuid.UUID, immediate bool) (*models.Edit,

if success {
userPromotionThreshold := config.GetVotePromotionThreshold()
if userPromotionThreshold != nil {
return user.PromoteUserVoteRights(fac, updatedEdit.UserID, *userPromotionThreshold)
if userPromotionThreshold != nil && updatedEdit.UserID.Valid {
return user.PromoteUserVoteRights(fac, updatedEdit.UserID.UUID, *userPromotionThreshold)
}
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/manager/edit/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ func (m *SceneEditProcessor) applyEdit(scene *models.Scene) (*models.Scene, erro

switch operation {
case models.OperationEnumCreate:
return m.applyCreate(data, &m.edit.UserID)
var userID *uuid.UUID
if m.edit.UserID.Valid {
userID = &m.edit.UserID.UUID
}
return m.applyCreate(data, userID)
case models.OperationEnumDestroy:
return m.applyDestroy(scene)
case models.OperationEnumModify:
Expand Down
1 change: 1 addition & 0 deletions pkg/models/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ type EditRepo interface {
FindBySceneID(id uuid.UUID) ([]*Edit, error)
FindCompletedEdits(int, int, int) ([]*Edit, error)
FindPendingSceneCreation(input QueryExistingSceneInput) ([]*Edit, error)
CancelUserEdits(userID uuid.UUID) error
}
37 changes: 20 additions & 17 deletions pkg/models/model_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type Edit struct {
ID uuid.UUID `db:"id" json:"id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
UserID uuid.NullUUID `db:"user_id" json:"user_id"`
TargetType string `db:"target_type" json:"target_type"`
Operation string `db:"operation" json:"operation"`
VoteCount int `db:"votes" json:"votes"`
Expand All @@ -27,24 +27,25 @@ type Edit struct {
}

type EditComment struct {
ID uuid.UUID `db:"id" json:"id"`
EditID uuid.UUID `db:"edit_id" json:"edit_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
Text string `db:"text" json:"text"`
ID uuid.UUID `db:"id" json:"id"`
EditID uuid.UUID `db:"edit_id" json:"edit_id"`
UserID uuid.NullUUID `db:"user_id" json:"user_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
Text string `db:"text" json:"text"`
}

type EditVote struct {
EditID uuid.UUID `db:"edit_id" json:"edit_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
Vote string `db:"vote" json:"vote"`
EditID uuid.UUID `db:"edit_id" json:"edit_id"`
UserID uuid.NullUUID `db:"user_id" json:"user_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
Vote string `db:"vote" json:"vote"`
}

func NewEdit(uuid uuid.UUID, user *User, targetType TargetTypeEnum, input *EditInput) *Edit {
func NewEdit(id uuid.UUID, user *User, targetType TargetTypeEnum, input *EditInput) *Edit {
userID := uuid.NullUUID{UUID: user.ID, Valid: true}
ret := &Edit{
ID: uuid,
UserID: user.ID,
ID: id,
UserID: userID,
TargetType: targetType.String(),
Status: VoteStatusEnumPending.String(),
Operation: input.Operation.String(),
Expand All @@ -60,11 +61,12 @@ func NewEdit(uuid uuid.UUID, user *User, targetType TargetTypeEnum, input *EditI
return ret
}

func NewEditComment(uuid uuid.UUID, user *User, edit *Edit, text string) *EditComment {
func NewEditComment(id uuid.UUID, user *User, edit *Edit, text string) *EditComment {
userID := uuid.NullUUID{UUID: user.ID, Valid: true}
ret := &EditComment{
ID: uuid,
ID: id,
EditID: edit.ID,
UserID: user.ID,
UserID: userID,
CreatedAt: time.Now(),
Text: text,
}
Expand All @@ -77,9 +79,10 @@ func (e Edit) GetID() uuid.UUID {
}

func NewEditVote(user *User, edit *Edit, vote VoteTypeEnum) *EditVote {
userID := uuid.NullUUID{UUID: user.ID, Valid: true}
ret := &EditVote{
EditID: edit.ID,
UserID: user.ID,
UserID: userID,
CreatedAt: time.Now(),
Vote: vote.String(),
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/sqlx/querybuilder_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,11 @@ func (qb *editQueryBuilder) FindPendingSceneCreation(input models.QueryExistingS

return qb.queryEdits(query, args)
}

func (qb *editQueryBuilder) CancelUserEdits(userID uuid.UUID) error {
var args []interface{}
args = append(args, userID)
query := `UPDATE edits SET status = 'CANCELED', updated_at = NOW() WHERE user_id = ?`
err := qb.dbi.RawQuery(editDBTable, query, args, nil)
return err
}