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