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 2.2 kB

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