profile.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package user
  2. import (
  3. "fmt"
  4. "gitote/gitote/models"
  5. "gitote/gitote/models/errors"
  6. "gitote/gitote/pkg/context"
  7. "gitote/gitote/pkg/setting"
  8. "gitote/gitote/pkg/tool"
  9. "gitote/gitote/routes/repo"
  10. "path"
  11. "strings"
  12. "gitlab.com/yoginth/paginater"
  13. )
  14. const (
  15. FOLLOWERS = "user/meta/followers"
  16. STARS = "user/meta/stars"
  17. )
  18. func GetUserByName(c *context.Context, name string) *models.User {
  19. user, err := models.GetUserByName(name)
  20. if err != nil {
  21. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  22. return nil
  23. }
  24. return user
  25. }
  26. // GetUserByParams returns user whose name is presented in URL paramenter.
  27. func GetUserByParams(c *context.Context) *models.User {
  28. return GetUserByName(c, c.Params(":username"))
  29. }
  30. func Profile(c *context.Context) {
  31. uname := c.Params(":username")
  32. // Special handle for FireFox requests favicon.ico.
  33. if uname == "favicon.ico" {
  34. c.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png"))
  35. return
  36. } else if strings.HasSuffix(uname, ".png") {
  37. c.Error(404)
  38. return
  39. }
  40. isShowKeys := false
  41. if strings.HasSuffix(uname, ".keys") {
  42. isShowKeys = true
  43. }
  44. ctxUser := GetUserByName(c, strings.TrimSuffix(uname, ".keys"))
  45. if c.Written() {
  46. return
  47. }
  48. // Show SSH keys.
  49. if isShowKeys {
  50. ShowSSHKeys(c, ctxUser.ID)
  51. return
  52. }
  53. if ctxUser.IsOrganization() {
  54. showOrgProfile(c)
  55. return
  56. }
  57. c.Data["Title"] = ctxUser.DisplayName()
  58. c.Data["PageIsUserProfile"] = true
  59. c.Data["Owner"] = ctxUser
  60. orgs, err := models.GetOrgsByUserID(ctxUser.ID, c.IsLogged && (c.User.IsAdmin || c.User.ID == ctxUser.ID))
  61. if err != nil {
  62. c.Handle(500, "GetOrgsByUserIDDesc", err)
  63. return
  64. }
  65. c.Data["Orgs"] = orgs
  66. tab := c.Query("tab")
  67. c.Data["TabName"] = tab
  68. switch tab {
  69. case "activity":
  70. retrieveFeeds(c, ctxUser, -1, true)
  71. if c.Written() {
  72. return
  73. }
  74. default:
  75. page := c.QueryInt("page")
  76. if page <= 0 {
  77. page = 1
  78. }
  79. showPrivate := c.IsLogged && (ctxUser.ID == c.User.ID || c.User.IsAdmin)
  80. c.Data["Repos"], err = models.GetUserRepositories(&models.UserRepoOptions{
  81. UserID: ctxUser.ID,
  82. Private: showPrivate,
  83. Page: page,
  84. PageSize: setting.UI.User.RepoPagingNum,
  85. })
  86. if err != nil {
  87. c.Handle(500, "GetRepositories", err)
  88. return
  89. }
  90. count := models.CountUserRepositories(ctxUser.ID, showPrivate)
  91. c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  92. }
  93. c.HTML(200, PROFILE)
  94. }
  95. func Followers(c *context.Context) {
  96. u := GetUserByParams(c)
  97. if c.Written() {
  98. return
  99. }
  100. c.Data["Title"] = u.DisplayName()
  101. c.Data["CardsTitle"] = c.Tr("user.followers")
  102. c.Data["PageIsFollowers"] = true
  103. c.Data["Owner"] = u
  104. repo.RenderUserCards(c, u.NumFollowers, u.GetFollowers, FOLLOWERS)
  105. }
  106. func Following(c *context.Context) {
  107. u := GetUserByParams(c)
  108. if c.Written() {
  109. return
  110. }
  111. c.Data["Title"] = u.DisplayName()
  112. c.Data["CardsTitle"] = c.Tr("user.following")
  113. c.Data["PageIsFollowing"] = true
  114. c.Data["Owner"] = u
  115. repo.RenderUserCards(c, u.NumFollowing, u.GetFollowing, FOLLOWERS)
  116. }
  117. func Stars(c *context.Context) {
  118. }
  119. func Action(c *context.Context) {
  120. u := GetUserByParams(c)
  121. if c.Written() {
  122. return
  123. }
  124. var err error
  125. switch c.Params(":action") {
  126. case "follow":
  127. err = models.FollowUser(c.User.ID, u.ID)
  128. case "unfollow":
  129. err = models.UnfollowUser(c.User.ID, u.ID)
  130. }
  131. if err != nil {
  132. c.ServerError(fmt.Sprintf("Action (%s)", c.Params(":action")), err)
  133. return
  134. }
  135. redirectTo := c.Query("redirect_to")
  136. if !tool.IsSameSiteURLPath(redirectTo) {
  137. redirectTo = u.HomeLink()
  138. }
  139. c.Redirect(redirectTo)
  140. }