Skip to content

Commit

Permalink
allow user seed org's assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Lei Da authored and Rader committed Sep 14, 2024
1 parent 1d386a0 commit 3b3c1ba
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 38 deletions.
27 changes: 27 additions & 0 deletions builder/rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,30 @@ type Namespace struct {
Type string `json:"type"`
Avatar string `json:"avatar,omitempty"`
}

type User struct {
ID int64 `json:"id,omitempty"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
UUID string `json:"uuid,omitempty"`
Avatar string `json:"avatar,omitempty"`
Bio string `json:"bio,omitempty"`
Homepage string `json:"homepage,omitempty"`
Roles []string `json:"roles,omitempty"`
LastLoginAt string `json:"last_login_at,omitempty"`
Orgs []Organization `json:"orgs,omitempty"`
CanChangeUserName bool `json:"can_change_username,omitempty"`
}

type Organization struct {
// unique name of the organization
Name string `json:"path"`
Nickname string `json:"name,omitempty"`
Homepage string `json:"homepage,omitempty"`
Logo string `json:"logo,omitempty"`
OrgType string `json:"org_type,omitempty"`
Verified bool `json:"verified"`
UserID int64 `json:"user_id,omitempty"`
}
13 changes: 13 additions & 0 deletions builder/rpc/user_svc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type UserSvcClient interface {
GetMemberRole(ctx context.Context, orgName, userName string) (membership.Role, error)
GetNameSpaceInfo(ctx context.Context, path string) (*Namespace, error)
GetUserInfo(ctx context.Context, userName, visitorName string) (*User, error)
}

//go:generate mockgen -destination=mocks/client.go -package=mocks . Client
Expand Down Expand Up @@ -54,3 +55,15 @@ func (c *UserSvcHttpClient) GetNameSpaceInfo(ctx context.Context, path string) (

return r.Data.(*Namespace), nil
}

func (c *UserSvcHttpClient) GetUserInfo(ctx context.Context, userName, visitorName string) (*User, error) {
url := fmt.Sprintf("/api/v1/user/%s", userName)
var r httpbase.R
r.Data = &User{}
err := c.hc.Get(ctx, url, &r)
if err != nil {
return nil, fmt.Errorf("failed to get user '%s' info: %w", userName, err)
}

return r.Data.(*User), nil
}
8 changes: 6 additions & 2 deletions builder/store/database/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,19 @@ func (s *RepoStore) SetUpdateTimeByPath(ctx context.Context, repoType types.Repo
return err
}

func (s *RepoStore) PublicToUser(ctx context.Context, repoType types.RepositoryType, userID int64, filter *types.RepoFilter, per, page int) (repos []*Repository, count int, err error) {
func (s *RepoStore) PublicToUser(ctx context.Context, repoType types.RepositoryType, userIDs []int64, filter *types.RepoFilter, per, page int) (repos []*Repository, count int, err error) {
q := s.db.Operator.Core.
NewSelect().
Column("repository.*").
Model(&repos).
Relation("Tags")

q.Where("repository.repository_type = ?", repoType)
q.Where("repository.private = ? or repository.user_id = ?", false, userID)
if len(userIDs) > 0 {
q.Where("repository.private = ? or repository.user_id in (?)", false, bun.In(userIDs))
} else {
q.Where("repository.private = ?", false)
}

if filter.Source != "" {
q.Where("repository.source = ?", filter.Source)
Expand Down
1 change: 1 addition & 0 deletions common/types/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type Organization struct {
Logo string `json:"logo,omitempty"`
OrgType string `json:"org_type,omitempty"`
Verified bool `json:"verified"`
UserID int64 `json:"user_id,omitempty"`
}

type Member struct {
Expand Down
1 change: 1 addition & 0 deletions common/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type PageOpts struct {
}

type User struct {
ID int64 `json:"id,omitempty"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Phone string `json:"phone,omitempty"`
Expand Down
10 changes: 1 addition & 9 deletions component/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,10 @@ func (c *CodeComponent) Create(ctx context.Context, req *types.CreateCodeReq) (*

func (c *CodeComponent) Index(ctx context.Context, filter *types.RepoFilter, per, page int) ([]types.Code, int, error) {
var (
user database.User
err error
resCodes []types.Code
)
if filter.Username != "" {
user, err = c.user.FindByUsername(ctx, filter.Username)
if err != nil {
newError := fmt.Errorf("failed to get current user,error:%w", err)
return nil, 0, newError
}
}
repos, total, err := c.rs.PublicToUser(ctx, types.CodeRepo, user.ID, filter, per, page)
repos, total, err := c.PublicToUser(ctx, types.CodeRepo, filter.Username, filter, per, page)
if err != nil {
newError := fmt.Errorf("failed to get public code repos,error:%w", err)
return nil, 0, newError
Expand Down
10 changes: 1 addition & 9 deletions component/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,10 @@ license: ` + license + `

func (c *DatasetComponent) Index(ctx context.Context, filter *types.RepoFilter, per, page int) ([]types.Dataset, int, error) {
var (
user database.User
err error
resDatasets []types.Dataset
)
if filter.Username != "" {
user, err = c.user.FindByUsername(ctx, filter.Username)
if err != nil {
newError := fmt.Errorf("failed to get current user,error:%w", err)
return nil, 0, newError
}
}
repos, total, err := c.rs.PublicToUser(ctx, types.DatasetRepo, user.ID, filter, per, page)
repos, total, err := c.PublicToUser(ctx, types.DatasetRepo, filter.Username, filter, per, page)
if err != nil {
newError := fmt.Errorf("failed to get public dataset repos,error:%w", err)
return nil, 0, newError
Expand Down
10 changes: 1 addition & 9 deletions component/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,10 @@ type ModelComponent struct {

func (c *ModelComponent) Index(ctx context.Context, filter *types.RepoFilter, per, page int) ([]types.Model, int, error) {
var (
user database.User
err error
resModels []types.Model
)
if filter.Username != "" {
user, err = c.user.FindByUsername(ctx, filter.Username)
if err != nil {
newError := fmt.Errorf("failed to get current user,error:%w", err)
return nil, 0, newError
}
}
repos, total, err := c.rs.PublicToUser(ctx, types.ModelRepo, user.ID, filter, per, page)
repos, total, err := c.PublicToUser(ctx, types.ModelRepo, filter.Username, filter, per, page)
if err != nil {
newError := fmt.Errorf("failed to get public model repos,error:%w", err)
return nil, 0, newError
Expand Down
24 changes: 24 additions & 0 deletions component/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ func (c *RepoComponent) DeleteRepo(ctx context.Context, req types.DeleteRepoReq)
return repo, nil
}

// PublicToUser gets visible repos of the given user and user's orgs
func (c *RepoComponent) PublicToUser(ctx context.Context, repoType types.RepositoryType, userName string, filter *types.RepoFilter, per, page int) (repos []*database.Repository, count int, err error) {
var repoOwnerIDs []int64
if len(userName) > 0 {
// get user orgs from user service
user, err := c.userSvcClient.GetUserInfo(ctx, userName, userName)
if err != nil {
return nil, 0, fmt.Errorf("failed to get user info, error: %w", err)
}

repoOwnerIDs = append(repoOwnerIDs, user.ID)
//get user's orgs
for _, org := range user.Orgs {
repoOwnerIDs = append(repoOwnerIDs, org.UserID)
}
}
repos, count, err = c.tc.rs.PublicToUser(ctx, repoType, repoOwnerIDs, filter, per, page)
if err != nil {
return nil, 0, fmt.Errorf("failed to get user public repos, error: %w", err)
}

return repos, count, nil
}

// relatedRepos gets all repos related to the given repo, and return them by repo type
func (c *RepoComponent) relatedRepos(ctx context.Context, repoID int64, currentUser string) (map[types.RepositoryType][]*database.Repository, error) {
fromRelations, err := c.rel.From(ctx, repoID)
Expand Down
10 changes: 1 addition & 9 deletions component/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,9 @@ func (c *SpaceComponent) Update(ctx context.Context, req *types.UpdateSpaceReq)
func (c *SpaceComponent) Index(ctx context.Context, filter *types.RepoFilter, per, page int) ([]types.Space, int, error) {
var (
resSpaces []types.Space
user database.User
err error
)
if filter.Username != "" {
user, err = c.user.FindByUsername(ctx, filter.Username)
if err != nil {
newError := fmt.Errorf("failed to get current user,error:%w", err)
return nil, 0, newError
}
}
repos, total, err := c.rs.PublicToUser(ctx, types.SpaceRepo, user.ID, filter, per, page)
repos, total, err := c.PublicToUser(ctx, types.SpaceRepo, filter.Username, filter, per, page)
if err != nil {
newError := fmt.Errorf("failed to get public space repos,error:%w", err)
return nil, 0, newError
Expand Down
2 changes: 2 additions & 0 deletions user/component/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func (c *UserComponent) Get(ctx context.Context, userName, visitorName string) (
}

if !onlyBasicInfo {
u.ID = dbuser.ID
u.Email = dbuser.Email
u.UUID = dbuser.UUID
u.Bio = dbuser.Bio
Expand All @@ -388,6 +389,7 @@ func (c *UserComponent) Get(ctx context.Context, userName, visitorName string) (
Logo: org.Logo,
OrgType: org.OrgType,
Verified: org.Verified,
UserID: org.UserID,
})
}
}
Expand Down

0 comments on commit 3b3c1ba

Please sign in to comment.