profile.go 3.8 KB

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