code.gitea.io/gitea@v1.22.3/routers/api/v1/user/star.go (about) 1 // Copyright 2016 The Gogs Authors. All rights reserved. 2 // Copyright 2020 The Gitea Authors. 3 // SPDX-License-Identifier: MIT 4 5 package user 6 7 import ( 8 "errors" 9 "net/http" 10 11 access_model "code.gitea.io/gitea/models/perm/access" 12 repo_model "code.gitea.io/gitea/models/repo" 13 user_model "code.gitea.io/gitea/models/user" 14 api "code.gitea.io/gitea/modules/structs" 15 "code.gitea.io/gitea/routers/api/v1/utils" 16 "code.gitea.io/gitea/services/context" 17 "code.gitea.io/gitea/services/convert" 18 ) 19 20 // getStarredRepos returns the repos that the user with the specified userID has 21 // starred 22 func getStarredRepos(ctx *context.APIContext, user *user_model.User, private bool) ([]*api.Repository, error) { 23 starredRepos, err := repo_model.GetStarredRepos(ctx, &repo_model.StarredReposOptions{ 24 ListOptions: utils.GetListOptions(ctx), 25 StarrerID: user.ID, 26 IncludePrivate: private, 27 }) 28 if err != nil { 29 return nil, err 30 } 31 32 repos := make([]*api.Repository, len(starredRepos)) 33 for i, starred := range starredRepos { 34 permission, err := access_model.GetUserRepoPermission(ctx, starred, user) 35 if err != nil { 36 return nil, err 37 } 38 repos[i] = convert.ToRepo(ctx, starred, permission) 39 } 40 return repos, nil 41 } 42 43 // GetStarredRepos returns the repos that the given user has starred 44 func GetStarredRepos(ctx *context.APIContext) { 45 // swagger:operation GET /users/{username}/starred user userListStarred 46 // --- 47 // summary: The repos that the given user has starred 48 // produces: 49 // - application/json 50 // parameters: 51 // - name: username 52 // in: path 53 // description: username of user 54 // type: string 55 // required: true 56 // - name: page 57 // in: query 58 // description: page number of results to return (1-based) 59 // type: integer 60 // - name: limit 61 // in: query 62 // description: page size of results 63 // type: integer 64 // responses: 65 // "200": 66 // "$ref": "#/responses/RepositoryList" 67 // "404": 68 // "$ref": "#/responses/notFound" 69 70 private := ctx.ContextUser.ID == ctx.Doer.ID 71 repos, err := getStarredRepos(ctx, ctx.ContextUser, private) 72 if err != nil { 73 ctx.Error(http.StatusInternalServerError, "getStarredRepos", err) 74 return 75 } 76 77 ctx.SetTotalCountHeader(int64(ctx.ContextUser.NumStars)) 78 ctx.JSON(http.StatusOK, &repos) 79 } 80 81 // GetMyStarredRepos returns the repos that the authenticated user has starred 82 func GetMyStarredRepos(ctx *context.APIContext) { 83 // swagger:operation GET /user/starred user userCurrentListStarred 84 // --- 85 // summary: The repos that the authenticated user has starred 86 // parameters: 87 // - name: page 88 // in: query 89 // description: page number of results to return (1-based) 90 // type: integer 91 // - name: limit 92 // in: query 93 // description: page size of results 94 // type: integer 95 // produces: 96 // - application/json 97 // responses: 98 // "200": 99 // "$ref": "#/responses/RepositoryList" 100 101 repos, err := getStarredRepos(ctx, ctx.Doer, true) 102 if err != nil { 103 ctx.Error(http.StatusInternalServerError, "getStarredRepos", err) 104 } 105 106 ctx.SetTotalCountHeader(int64(ctx.Doer.NumStars)) 107 ctx.JSON(http.StatusOK, &repos) 108 } 109 110 // IsStarring returns whether the authenticated is starring the repo 111 func IsStarring(ctx *context.APIContext) { 112 // swagger:operation GET /user/starred/{owner}/{repo} user userCurrentCheckStarring 113 // --- 114 // summary: Whether the authenticated is starring the repo 115 // parameters: 116 // - name: owner 117 // in: path 118 // description: owner of the repo 119 // type: string 120 // required: true 121 // - name: repo 122 // in: path 123 // description: name of the repo 124 // type: string 125 // required: true 126 // responses: 127 // "204": 128 // "$ref": "#/responses/empty" 129 // "404": 130 // "$ref": "#/responses/notFound" 131 132 if repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID) { 133 ctx.Status(http.StatusNoContent) 134 } else { 135 ctx.NotFound() 136 } 137 } 138 139 // Star the repo specified in the APIContext, as the authenticated user 140 func Star(ctx *context.APIContext) { 141 // swagger:operation PUT /user/starred/{owner}/{repo} user userCurrentPutStar 142 // --- 143 // summary: Star the given repo 144 // parameters: 145 // - name: owner 146 // in: path 147 // description: owner of the repo to star 148 // type: string 149 // required: true 150 // - name: repo 151 // in: path 152 // description: name of the repo to star 153 // type: string 154 // required: true 155 // responses: 156 // "204": 157 // "$ref": "#/responses/empty" 158 // "403": 159 // "$ref": "#/responses/forbidden" 160 // "404": 161 // "$ref": "#/responses/notFound" 162 163 err := repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, true) 164 if err != nil { 165 if errors.Is(err, user_model.ErrBlockedUser) { 166 ctx.Error(http.StatusForbidden, "BlockedUser", err) 167 } else { 168 ctx.Error(http.StatusInternalServerError, "StarRepo", err) 169 } 170 return 171 } 172 ctx.Status(http.StatusNoContent) 173 } 174 175 // Unstar the repo specified in the APIContext, as the authenticated user 176 func Unstar(ctx *context.APIContext) { 177 // swagger:operation DELETE /user/starred/{owner}/{repo} user userCurrentDeleteStar 178 // --- 179 // summary: Unstar the given repo 180 // parameters: 181 // - name: owner 182 // in: path 183 // description: owner of the repo to unstar 184 // type: string 185 // required: true 186 // - name: repo 187 // in: path 188 // description: name of the repo to unstar 189 // type: string 190 // required: true 191 // responses: 192 // "204": 193 // "$ref": "#/responses/empty" 194 // "404": 195 // "$ref": "#/responses/notFound" 196 197 err := repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, false) 198 if err != nil { 199 ctx.Error(http.StatusInternalServerError, "StarRepo", err) 200 return 201 } 202 ctx.Status(http.StatusNoContent) 203 }