MCPcopy Create free account
hub / github.com/OpenCSGs/csghub-server / GetCollection

Method GetCollection

component/collection.go:83–139  ·  view source on GitHub ↗
(ctx context.Context, currentUser string, id int64)

Source from the content-addressed store, hash-verified

81}
82
83func (cc *CollectionComponent) GetCollection(ctx context.Context, currentUser string, id int64) (*types.Collection, error) {
84 collection, err := cc.cs.GetCollection(ctx, id)
85 if err != nil {
86 return nil, err
87 }
88 // find by user name
89 avatar := ""
90 if collection.Username != "" {
91 user, err := cc.us.FindByUsername(ctx, collection.Username)
92 if err != nil {
93 return nil, fmt.Errorf("cannot find user for collection, %w", err)
94 }
95 avatar = user.Avatar
96 } else if collection.Namespace != "" {
97 org, err := cc.os.FindByPath(ctx, collection.Namespace)
98 if err != nil {
99 return nil, fmt.Errorf("fail to get org info, path: %s, error: %w", collection.Namespace, err)
100 }
101 avatar = org.Logo
102 }
103
104 permission, err := cc.getUserCollectionPermission(ctx, currentUser, collection)
105 if err != nil {
106 return nil, fmt.Errorf("failed to get user repo permission, error: %w", err)
107 }
108
109 if !permission.CanRead {
110 return nil, ErrUnauthorized
111 }
112
113 var newCollection types.Collection
114 temporaryVariable, _ := json.Marshal(collection)
115 err = json.Unmarshal(temporaryVariable, &newCollection)
116 if err != nil {
117 return nil, err
118 }
119 likeExists, err := cc.uls.IsExistCollection(ctx, currentUser, id)
120 if err != nil {
121 newError := fmt.Errorf("failed to check for the presence of the user likes,error:%w", err)
122 return nil, newError
123 }
124 if !permission.CanWrite {
125 newCollection.Repositories = cc.GetPublicRepos(newCollection)
126 }
127 for i, repo := range newCollection.Repositories {
128 if repo.RepositoryType == types.SpaceRepo && strings.Contains(repo.Path, "/") {
129 namespace, name := repo.NamespaceAndName()
130 _, status, _ := cc.spaceComponent.Status(ctx, namespace, name)
131 newCollection.Repositories[i].Status = status
132 }
133 }
134 newCollection.UserLikes = likeExists
135 newCollection.CanWrite = permission.CanWrite
136 newCollection.CanManage = permission.CanAdmin
137 newCollection.Avatar = avatar
138 return &newCollection, nil
139}
140

Callers 3

UpdateCollectionMethod · 0.45
AddReposToCollectionMethod · 0.45

Calls 7

GetPublicReposMethod · 0.95
FindByUsernameMethod · 0.80
IsExistCollectionMethod · 0.80
StatusMethod · 0.65
FindByPathMethod · 0.45
NamespaceAndNameMethod · 0.45

Tested by

no test coverage detected