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.

117 lines
2.9 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 user specified by the APIContext
  28. // has starred
  29. func GetStarredRepos(ctx *context.APIContext) {
  30. // swagger:route GET /users/{username}/starred userListStarred
  31. //
  32. // Produces:
  33. // - application/json
  34. //
  35. // Responses:
  36. // 200: RepositoryList
  37. // 500: error
  38. user := GetUserByParams(ctx)
  39. private := user.ID == ctx.User.ID
  40. repos, err := getStarredRepos(user.ID, private)
  41. if err != nil {
  42. ctx.Error(500, "getStarredRepos", err)
  43. }
  44. ctx.JSON(200, &repos)
  45. }
  46. // GetMyStarredRepos returns the repos that the authenticated user has starred
  47. func GetMyStarredRepos(ctx *context.APIContext) {
  48. // swagger:route GET /user/starred userCurrentListStarred
  49. //
  50. // Produces:
  51. // - application/json
  52. //
  53. // Responses:
  54. // 200: RepositoryList
  55. // 500: error
  56. repos, err := getStarredRepos(ctx.User.ID, true)
  57. if err != nil {
  58. ctx.Error(500, "getStarredRepos", err)
  59. }
  60. ctx.JSON(200, &repos)
  61. }
  62. // IsStarring returns whether the authenticated is starring the repo
  63. func IsStarring(ctx *context.APIContext) {
  64. // swagger:route GET /user/starred/{username}/{reponame} userCurrentCheckStarring
  65. //
  66. // Responses:
  67. // 204: empty
  68. // 404: notFound
  69. if models.IsStaring(ctx.User.ID, ctx.Repo.Repository.ID) {
  70. ctx.Status(204)
  71. } else {
  72. ctx.Status(404)
  73. }
  74. }
  75. // Star the repo specified in the APIContext, as the authenticated user
  76. func Star(ctx *context.APIContext) {
  77. // swagger:route PUT /user/starred/{username}/{reponame} userCurrentPutStar
  78. //
  79. // Responses:
  80. // 204: empty
  81. // 500: error
  82. err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  83. if err != nil {
  84. ctx.Error(500, "StarRepo", err)
  85. return
  86. }
  87. ctx.Status(204)
  88. }
  89. // Unstar the repo specified in the APIContext, as the authenticated user
  90. func Unstar(ctx *context.APIContext) {
  91. // swagger:route DELETE /user/starred/{username}/{reponame} userCurrentDeleteStar
  92. //
  93. // Responses:
  94. // 204: empty
  95. // 500: error
  96. err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  97. if err != nil {
  98. ctx.Error(500, "StarRepo", err)
  99. return
  100. }
  101. ctx.Status(204)
  102. }