You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

star.go 4.9KB

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