profile.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.Handle(404, "GetUserByName", nil)
  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. orgs, err := models.GetPublicOrgsByUserIDDesc(u.Id, "updated")
  66. if err != nil {
  67. ctx.Handle(500, "GetPublicOrgsByUserIDDesc", err)
  68. return
  69. }
  70. ctx.Data["Orgs"] = orgs
  71. tab := ctx.Query("tab")
  72. ctx.Data["TabName"] = tab
  73. switch tab {
  74. case "activity":
  75. retrieveFeeds(ctx, u.Id, 0, true)
  76. if ctx.Written() {
  77. return
  78. }
  79. default:
  80. var err error
  81. ctx.Data["Repos"], err = models.GetRepositories(u.Id, ctx.IsSigned && ctx.User.Id == u.Id)
  82. if err != nil {
  83. ctx.Handle(500, "GetRepositories", err)
  84. return
  85. }
  86. }
  87. ctx.HTML(200, PROFILE)
  88. }
  89. func Followers(ctx *middleware.Context) {
  90. u := GetUserByParams(ctx)
  91. if ctx.Written() {
  92. return
  93. }
  94. ctx.Data["Title"] = u.DisplayName()
  95. ctx.Data["CardsTitle"] = ctx.Tr("user.followers")
  96. ctx.Data["PageIsFollowers"] = true
  97. ctx.Data["Owner"] = u
  98. repo.RenderUserCards(ctx, u.NumFollowers, u.GetFollowers, FOLLOWERS)
  99. }
  100. func Following(ctx *middleware.Context) {
  101. u := GetUserByParams(ctx)
  102. if ctx.Written() {
  103. return
  104. }
  105. ctx.Data["Title"] = u.DisplayName()
  106. ctx.Data["CardsTitle"] = ctx.Tr("user.following")
  107. ctx.Data["PageIsFollowing"] = true
  108. ctx.Data["Owner"] = u
  109. repo.RenderUserCards(ctx, u.NumFollowing, u.GetFollowing, FOLLOWERS)
  110. }
  111. func Stars(ctx *middleware.Context) {
  112. }
  113. func Action(ctx *middleware.Context) {
  114. u := GetUserByParams(ctx)
  115. if ctx.Written() {
  116. return
  117. }
  118. var err error
  119. switch ctx.Params(":action") {
  120. case "follow":
  121. err = models.FollowUser(ctx.User.Id, u.Id)
  122. case "unfollow":
  123. err = models.UnfollowUser(ctx.User.Id, u.Id)
  124. }
  125. if err != nil {
  126. ctx.Handle(500, fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  127. return
  128. }
  129. redirectTo := ctx.Query("redirect_to")
  130. if len(redirectTo) == 0 {
  131. redirectTo = u.HomeLink()
  132. }
  133. ctx.Redirect(redirectTo)
  134. }