profile.go 3.6 KB

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