profile.go 2.8 KB

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