(ctx context.Context, currentUser string, id int64)
| 81 | } |
| 82 | |
| 83 | func (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 |
no test coverage detected