profile.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package user
  7. import (
  8. "fmt"
  9. "gitote/gitote/models"
  10. "gitote/gitote/models/errors"
  11. "gitote/gitote/pkg/context"
  12. "gitote/gitote/pkg/setting"
  13. "gitote/gitote/pkg/tool"
  14. "gitote/gitote/routes/repo"
  15. "strings"
  16. "gitlab.com/yoginth/paginater"
  17. )
  18. const (
  19. FOLLOWERS = "user/meta/followers"
  20. STARS = "user/meta/stars"
  21. )
  22. func GetUserByName(c *context.Context, name string) *models.User {
  23. user, err := models.GetUserByName(name)
  24. if err != nil {
  25. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  26. return nil
  27. }
  28. return user
  29. }
  30. func Profile(c *context.Context, puser *context.ParamsUser) {
  31. isShowKeys := false
  32. if strings.HasSuffix(c.Params(":username"), ".keys") {
  33. isShowKeys = true
  34. }
  35. // Show SSH keys.
  36. if isShowKeys {
  37. ShowSSHKeys(c, puser.ID)
  38. return
  39. }
  40. if puser.IsOrganization() {
  41. showOrgProfile(c)
  42. return
  43. }
  44. c.Title(puser.DisplayName())
  45. c.PageIs("UserProfile")
  46. c.Data["Owner"] = puser
  47. orgs, err := models.GetOrgsByUserID(puser.ID, c.IsLogged && (c.User.IsAdmin || c.User.ID == puser.ID))
  48. if err != nil {
  49. c.ServerError("GetOrgsByUserIDDesc", err)
  50. return
  51. }
  52. c.Data["Orgs"] = orgs
  53. tab := c.Query("tab")
  54. c.Data["TabName"] = tab
  55. switch tab {
  56. case "activity":
  57. retrieveFeeds(c, puser.User, -1, true)
  58. if c.Written() {
  59. return
  60. }
  61. default:
  62. page := c.QueryInt("page")
  63. if page <= 0 {
  64. page = 1
  65. }
  66. showPrivate := c.IsLogged && (puser.ID == c.User.ID || c.User.IsAdmin)
  67. c.Data["Repos"], err = models.GetUserRepositories(&models.UserRepoOptions{
  68. UserID: puser.ID,
  69. Private: showPrivate,
  70. Page: page,
  71. PageSize: setting.UI.User.RepoPagingNum,
  72. })
  73. if err != nil {
  74. c.ServerError("GetRepositories", err)
  75. return
  76. }
  77. count := models.CountUserRepositories(puser.ID, showPrivate)
  78. c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  79. }
  80. if puser.Suspended == true {
  81. c.Handle(404, "Suspended", err)
  82. } else {
  83. c.Success(PROFILE)
  84. }
  85. }
  86. func Followers(c *context.Context, puser *context.ParamsUser) {
  87. c.Title(puser.DisplayName())
  88. c.PageIs("Followers")
  89. c.Data["CardsTitle"] = c.Tr("user.followers")
  90. c.Data["Owner"] = puser
  91. repo.RenderUserCards(c, puser.NumFollowers, puser.GetFollowers, FOLLOWERS)
  92. }
  93. func Following(c *context.Context, puser *context.ParamsUser) {
  94. c.Title(puser.DisplayName())
  95. c.PageIs("Following")
  96. c.Data["CardsTitle"] = c.Tr("user.following")
  97. c.Data["Owner"] = puser
  98. repo.RenderUserCards(c, puser.NumFollowing, puser.GetFollowing, FOLLOWERS)
  99. }
  100. func Stars(c *context.Context) {
  101. }
  102. func Action(c *context.Context, puser *context.ParamsUser) {
  103. var err error
  104. switch c.Params(":action") {
  105. case "follow":
  106. err = models.FollowUser(c.UserID(), puser.ID)
  107. case "unfollow":
  108. err = models.UnfollowUser(c.UserID(), puser.ID)
  109. }
  110. if err != nil {
  111. c.ServerError(fmt.Sprintf("Action (%s)", c.Params(":action")), err)
  112. return
  113. }
  114. redirectTo := c.Query("redirect_to")
  115. if !tool.IsSameSiteURLPath(redirectTo) {
  116. redirectTo = puser.HomeLink()
  117. }
  118. c.Redirect(redirectTo)
  119. }