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.

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