profile.go 3.2 KB

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