profile.go 3.1 KB

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