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

star.go 4.4 kB

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