You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.4 KiB

8 years ago
  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/context"
  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 *context.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 *context.Context) *models.User {
  33. return GetUserByName(ctx, ctx.Params(":username"))
  34. }
  35. func Profile(ctx *context.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.GetOrgsByUserID(u.Id, ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.Id == u.Id))
  66. if err != nil {
  67. ctx.Handle(500, "GetOrgsByUserIDDesc", 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, -1, 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 *context.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 *context.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 *context.Context) {
  112. }
  113. func Action(ctx *context.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. }