2
0

profile.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2015 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. "strings"
  7. "github.com/unknwon/paginater"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/db"
  11. "gogs.io/gogs/internal/route/repo"
  12. "gogs.io/gogs/internal/tool"
  13. )
  14. const (
  15. FOLLOWERS = "user/meta/followers"
  16. STARS = "user/meta/stars"
  17. )
  18. func Profile(c *context.Context, puser *context.ParamsUser) {
  19. isShowKeys := false
  20. if strings.HasSuffix(c.Params(":username"), ".keys") {
  21. isShowKeys = true
  22. }
  23. // Show SSH keys.
  24. if isShowKeys {
  25. ShowSSHKeys(c, puser.ID)
  26. return
  27. }
  28. if puser.IsOrganization() {
  29. showOrgProfile(c)
  30. return
  31. }
  32. c.Title(puser.DisplayName())
  33. c.PageIs("UserProfile")
  34. c.Data["Owner"] = puser
  35. orgs, err := db.GetOrgsByUserID(puser.ID, c.IsLogged && (c.User.IsAdmin || c.User.ID == puser.ID))
  36. if err != nil {
  37. c.Error(err, "get organizations by user ID")
  38. return
  39. }
  40. c.Data["Orgs"] = orgs
  41. tab := c.Query("tab")
  42. c.Data["TabName"] = tab
  43. switch tab {
  44. case "activity":
  45. retrieveFeeds(c, puser.User, -1, true)
  46. if c.Written() {
  47. return
  48. }
  49. default:
  50. page := c.QueryInt("page")
  51. if page <= 0 {
  52. page = 1
  53. }
  54. showPrivate := c.IsLogged && (puser.ID == c.User.ID || c.User.IsAdmin)
  55. c.Data["Repos"], err = db.GetUserRepositories(&db.UserRepoOptions{
  56. UserID: puser.ID,
  57. Private: showPrivate,
  58. Page: page,
  59. PageSize: conf.UI.User.RepoPagingNum,
  60. })
  61. if err != nil {
  62. c.Error(err, "get user repositories")
  63. return
  64. }
  65. count := db.CountUserRepositories(puser.ID, showPrivate)
  66. c.Data["Page"] = paginater.New(int(count), conf.UI.User.RepoPagingNum, page, 5)
  67. }
  68. c.Success(PROFILE)
  69. }
  70. func Followers(c *context.Context, puser *context.ParamsUser) {
  71. c.Title(puser.DisplayName())
  72. c.PageIs("Followers")
  73. c.Data["CardsTitle"] = c.Tr("user.followers")
  74. c.Data["Owner"] = puser
  75. repo.RenderUserCards(c, puser.NumFollowers, puser.GetFollowers, FOLLOWERS)
  76. }
  77. func Following(c *context.Context, puser *context.ParamsUser) {
  78. c.Title(puser.DisplayName())
  79. c.PageIs("Following")
  80. c.Data["CardsTitle"] = c.Tr("user.following")
  81. c.Data["Owner"] = puser
  82. repo.RenderUserCards(c, puser.NumFollowing, puser.GetFollowing, FOLLOWERS)
  83. }
  84. func Stars(c *context.Context) {
  85. }
  86. func Action(c *context.Context, puser *context.ParamsUser) {
  87. var err error
  88. switch c.Params(":action") {
  89. case "follow":
  90. err = db.FollowUser(c.UserID(), puser.ID)
  91. case "unfollow":
  92. err = db.UnfollowUser(c.UserID(), puser.ID)
  93. }
  94. if err != nil {
  95. c.Errorf(err, "action %q", c.Params(":action"))
  96. return
  97. }
  98. redirectTo := c.Query("redirect_to")
  99. if !tool.IsSameSiteURLPath(redirectTo) {
  100. redirectTo = puser.HomeLink()
  101. }
  102. c.Redirect(redirectTo)
  103. }