profile.go 3.9 KB

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