profile.go 3.7 KB

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