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.

76 lines
2.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. repos[i] = starred.APIFormat(&api.Permission{true, true, true})
  20. }
  21. return repos, nil
  22. }
  23. // GetStarredRepos returns the repos that the user specified by the APIContext
  24. // has starred
  25. func GetStarredRepos(ctx *context.APIContext) {
  26. user := GetUserByParams(ctx)
  27. private := user.ID == ctx.User.ID
  28. repos, err := getStarredRepos(user.ID, private)
  29. if err != nil {
  30. ctx.Error(500, "getStarredRepos", err)
  31. }
  32. ctx.JSON(200, &repos)
  33. }
  34. // GetMyStarredRepos returns the repos that the authenticated user has starred
  35. func GetMyStarredRepos(ctx *context.APIContext) {
  36. repos, err := getStarredRepos(ctx.User.ID, true)
  37. if err != nil {
  38. ctx.Error(500, "getStarredRepos", err)
  39. }
  40. ctx.JSON(200, &repos)
  41. }
  42. // IsStarring returns whether the authenticated is starring the repo
  43. func IsStarring(ctx *context.APIContext) {
  44. if models.IsStaring(ctx.User.ID, ctx.Repo.Repository.ID) {
  45. ctx.Status(204)
  46. } else {
  47. ctx.Status(404)
  48. }
  49. }
  50. // Star the repo specified in the APIContext, as the authenticated user
  51. func Star(ctx *context.APIContext) {
  52. err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  53. if err != nil {
  54. ctx.Error(500, "StarRepo", err)
  55. return
  56. }
  57. ctx.Status(204)
  58. }
  59. // Unstar the repo specified in the APIContext, as the authenticated user
  60. func Unstar(ctx *context.APIContext) {
  61. err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  62. if err != nil {
  63. ctx.Error(500, "StarRepo", err)
  64. return
  65. }
  66. ctx.Status(204)
  67. }