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.

155 lines
4.0 KiB

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