profile.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if ctxUser.Suspended == true {
  94. c.Handle(404, "Suspended", err)
  95. } else {
  96. c.HTML(200, PROFILE)
  97. }
  98. }
  99. func Followers(c *context.Context) {
  100. u := GetUserByParams(c)
  101. if c.Written() {
  102. return
  103. }
  104. c.Data["Title"] = u.DisplayName()
  105. c.Data["CardsTitle"] = c.Tr("user.followers")
  106. c.Data["PageIsFollowers"] = true
  107. c.Data["Owner"] = u
  108. repo.RenderUserCards(c, u.NumFollowers, u.GetFollowers, FOLLOWERS)
  109. }
  110. func Following(c *context.Context) {
  111. u := GetUserByParams(c)
  112. if c.Written() {
  113. return
  114. }
  115. c.Data["Title"] = u.DisplayName()
  116. c.Data["CardsTitle"] = c.Tr("user.following")
  117. c.Data["PageIsFollowing"] = true
  118. c.Data["Owner"] = u
  119. repo.RenderUserCards(c, u.NumFollowing, u.GetFollowing, FOLLOWERS)
  120. }
  121. func Stars(c *context.Context) {
  122. }
  123. func Action(c *context.Context) {
  124. u := GetUserByParams(c)
  125. if c.Written() {
  126. return
  127. }
  128. var err error
  129. switch c.Params(":action") {
  130. case "follow":
  131. err = models.FollowUser(c.User.ID, u.ID)
  132. case "unfollow":
  133. err = models.UnfollowUser(c.User.ID, u.ID)
  134. }
  135. if err != nil {
  136. c.ServerError(fmt.Sprintf("Action (%s)", c.Params(":action")), err)
  137. return
  138. }
  139. redirectTo := c.Query("redirect_to")
  140. if !tool.IsSameSiteURLPath(redirectTo) {
  141. redirectTo = u.HomeLink()
  142. }
  143. c.Redirect(redirectTo)
  144. }