profile.go 3.8 KB

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