code.gitea.io/gitea@v1.22.3/models/git/lfs_lock_list.go (about)

     1  // Copyright 2024 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package git
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"code.gitea.io/gitea/models/db"
    11  	user_model "code.gitea.io/gitea/models/user"
    12  	"code.gitea.io/gitea/modules/container"
    13  )
    14  
    15  // LFSLockList is a list of LFSLock
    16  type LFSLockList []*LFSLock
    17  
    18  // LoadAttributes loads the attributes for the given locks
    19  func (locks LFSLockList) LoadAttributes(ctx context.Context) error {
    20  	if len(locks) == 0 {
    21  		return nil
    22  	}
    23  
    24  	if err := locks.LoadOwner(ctx); err != nil {
    25  		return fmt.Errorf("load owner: %w", err)
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  // LoadOwner loads the owner of the locks
    32  func (locks LFSLockList) LoadOwner(ctx context.Context) error {
    33  	if len(locks) == 0 {
    34  		return nil
    35  	}
    36  
    37  	usersIDs := container.FilterSlice(locks, func(lock *LFSLock) (int64, bool) {
    38  		return lock.OwnerID, true
    39  	})
    40  	users := make(map[int64]*user_model.User, len(usersIDs))
    41  	if err := db.GetEngine(ctx).
    42  		In("id", usersIDs).
    43  		Find(&users); err != nil {
    44  		return fmt.Errorf("find users: %w", err)
    45  	}
    46  	for _, v := range locks {
    47  		v.Owner = users[v.OwnerID]
    48  		if v.Owner == nil { // not exist
    49  			v.Owner = user_model.NewGhostUser()
    50  		}
    51  	}
    52  
    53  	return nil
    54  }