profile.go 3.7 KB

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