Repository: go-gitea/gitea
File: models/pull/review_state.go (Go)

Code before:

// ReviewState stores for a user-PR-commit combination which files the user has already viewed
type ReviewState struct {
	ID           int64                  `xorm:"pk autoincr"`
	UserID       int64                  `xorm:"NOT NULL UNIQUE(pull_commit_user)"`
	PullID       int64                  `xorm:"NOT NULL INDEX UNIQUE(pull_commit_user) DEFAULT 0"` // Which PR was the review on?
	CommitSHA    string                 `xorm:"NOT NULL VARCHAR(64) UNIQUE(pull_commit_user)"`     // Which commit was the head commit for the review?
	UpdatedFiles map[string]ViewedState `xorm:"NOT NULL LONGTEXT JSON"`                            // Stores for each of the changed files of a PR whether they have been viewed, changed since last viewed, or not viewed
	UpdatedUnix  timeutil.TimeStamp     `xorm:"updated"`                                           // Is an accurate indicator of the order of commits as we do not expect it to be possible to make reviews on previous commits
}

func init() {
	db.RegisterModel(new(ReviewState))
}

func (rs *ReviewState) GetViewedFileCount() int {
	if len(rs.UpdatedFiles) == 0 {
		return 0
	}
	var viewedFiles int
	for _, state := range rs.UpdatedFiles {
		if state == Viewed {
			viewedFiles++
		}
	}
	return viewedFiles
}

// GetReviewState returns the ReviewState with all given values prefilled, whether or not it exists in the database.
// If the review didn't exist before in the database, it won't afterwards either.
// The returned boolean shows whether the review exists in the database
func GetReviewState(ctx context.Context, userID, pullID int64, commitSHA string) (*ReviewState, bool, error) {
	review := &ReviewState{UserID: userID, PullID: pullID, CommitSHA: commitSHA}
	has, err := db.GetEngine(ctx).Get(review)
	return review, has, err
}

// UpdateReviewState updates the given review inside the database, regardless of whether it existed before or not
// The given map of files with their viewed state will be merged with the previous review, if present
func UpdateReviewState(ctx context.Context, userID, pullID int64, commitSHA string, updatedFiles map[string]ViewedState) error {
	log.Trace("Updating review for user %d, repo %d, commit %s with the updated files %v.", userID, pullID, commitSHA, updatedFiles)

	review, exists, err := GetReviewState(ctx, userID, pullID, commitSHA)
	if err != nil {
		return err
	}

	if exists {
		review.UpdatedFiles = mergeFiles(review.UpdatedFiles, updatedFiles)
	} else if previousReview, err := getNewestReviewStateApartFrom(ctx, userID, pullID, commitSHA); err != nil {
		return err

Review comment (suggestion):
```suggestion
	var numViewedFiles int
	for _, state := range rs.UpdatedFiles {
		if state == Viewed {
			numViewedFiles++
		}
	}
	return numViewedFiles
```

Code after:

// ReviewState stores for a user-PR-commit combination which files the user has already viewed
type ReviewState struct {
	ID           int64                  `xorm:"pk autoincr"`
	UserID       int64                  `xorm:"NOT NULL UNIQUE(pull_commit_user)"`
	PullID       int64                  `xorm:"NOT NULL INDEX UNIQUE(pull_commit_user) DEFAULT 0"` // Which PR was the review on?
	CommitSHA    string                 `xorm:"NOT NULL VARCHAR(64) UNIQUE(pull_commit_user)"`     // Which commit was the head commit for the review?
	UpdatedFiles map[string]ViewedState `xorm:"NOT NULL LONGTEXT JSON"`                            // Stores for each of the changed files of a PR whether they have been viewed, changed since last viewed, or not viewed
	UpdatedUnix  timeutil.TimeStamp     `xorm:"updated"`                                           // Is an accurate indicator of the order of commits as we do not expect it to be possible to make reviews on previous commits
}

func init() {
	db.RegisterModel(new(ReviewState))
}

func (rs *ReviewState) GetViewedFileCount() int {
	if len(rs.UpdatedFiles) == 0 {
		return 0
	}
	var numViewedFiles int
	for _, state := range rs.UpdatedFiles {
		if state == Viewed {
			numViewedFiles++
		}
	}
	return numViewedFiles
}

// GetReviewState returns the ReviewState with all given values prefilled, whether or not it exists in the database.
// If the review didn't exist before in the database, it won't afterwards either.
// The returned boolean shows whether the review exists in the database
func GetReviewState(ctx context.Context, userID, pullID int64, commitSHA string) (*ReviewState, bool, error) {
	review := &ReviewState{UserID: userID, PullID: pullID, CommitSHA: commitSHA}
	has, err := db.GetEngine(ctx).Get(review)
	return review, has, err
}

// UpdateReviewState updates the given review inside the database, regardless of whether it existed before or not
// The given map of files with their viewed state will be merged with the previous review, if present
func UpdateReviewState(ctx context.Context, userID, pullID int64, commitSHA string, updatedFiles map[string]ViewedState) error {
	log.Trace("Updating review for user %d, repo %d, commit %s with the updated files %v.", userID, pullID, commitSHA, updatedFiles)

	review, exists, err := GetReviewState(ctx, userID, pullID, commitSHA)
	if err != nil {
		return err
	}

	if exists {
		review.UpdatedFiles = mergeFiles(review.UpdatedFiles, updatedFiles)
	} else if previousReview, err := getNewestReviewStateApartFrom(ctx, userID, pullID, commitSHA); err != nil {
		return err