home.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2014 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.gogs file.
  4. package route
  5. import (
  6. gocontext "context"
  7. "fmt"
  8. "net/http"
  9. "github.com/go-macaron/i18n"
  10. "github.com/unknwon/paginater"
  11. "gopkg.in/macaron.v1"
  12. "gogs.io/gogs/internal/conf"
  13. "gogs.io/gogs/internal/context"
  14. "gogs.io/gogs/internal/database"
  15. "gogs.io/gogs/internal/route/user"
  16. )
  17. const (
  18. HOME = "home"
  19. EXPLORE_REPOS = "explore/repos"
  20. EXPLORE_USERS = "explore/users"
  21. EXPLORE_ORGANIZATIONS = "explore/organizations"
  22. )
  23. func Home(c *context.Context) {
  24. if c.IsLogged {
  25. if !c.User.IsActive && conf.Auth.RequireEmailConfirmation {
  26. c.Data["Title"] = c.Tr("auth.active_your_account")
  27. c.Success(user.ACTIVATE)
  28. } else {
  29. user.Dashboard(c)
  30. }
  31. return
  32. }
  33. // Check auto-login.
  34. uname := c.GetCookie(conf.Security.CookieUsername)
  35. if uname != "" {
  36. c.Redirect(conf.Server.Subpath + "/user/login")
  37. return
  38. }
  39. c.Data["PageIsHome"] = true
  40. c.Success(HOME)
  41. }
  42. func ExploreRepos(c *context.Context) {
  43. c.Data["Title"] = c.Tr("explore")
  44. c.Data["PageIsExplore"] = true
  45. c.Data["PageIsExploreRepositories"] = true
  46. c.Data["ExploreUser"] = !conf.Admin.DisableRegularExploreUser || (c.User != nil && c.User.IsActive && c.User.IsAdmin)
  47. page := c.QueryInt("page")
  48. if page <= 0 {
  49. page = 1
  50. }
  51. keyword := c.Query("q")
  52. repos, count, err := database.SearchRepositoryByName(&database.SearchRepoOptions{
  53. Keyword: keyword,
  54. UserID: c.UserID(),
  55. OrderBy: "updated_unix DESC",
  56. Page: page,
  57. PageSize: conf.UI.ExplorePagingNum,
  58. })
  59. if err != nil {
  60. c.Error(err, "search repository by name")
  61. return
  62. }
  63. c.Data["Keyword"] = keyword
  64. c.Data["Total"] = count
  65. c.Data["Page"] = paginater.New(int(count), conf.UI.ExplorePagingNum, page, 5)
  66. if err = database.RepositoryList(repos).LoadAttributes(); err != nil {
  67. c.Error(err, "load attributes")
  68. return
  69. }
  70. c.Data["Repos"] = repos
  71. c.Success(EXPLORE_REPOS)
  72. }
  73. type UserSearchOptions struct {
  74. Type database.UserType
  75. Counter func(ctx gocontext.Context) int64
  76. Ranger func(ctx gocontext.Context, page, pageSize int) ([]*database.User, error)
  77. PageSize int
  78. OrderBy string
  79. TplName string
  80. }
  81. func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
  82. page := c.QueryInt("page")
  83. if page <= 1 {
  84. page = 1
  85. }
  86. var (
  87. users []*database.User
  88. count int64
  89. err error
  90. )
  91. keyword := c.Query("q")
  92. if keyword == "" {
  93. users, err = opts.Ranger(c.Req.Context(), page, opts.PageSize)
  94. if err != nil {
  95. c.Error(err, "ranger")
  96. return
  97. }
  98. count = opts.Counter(c.Req.Context())
  99. } else {
  100. search := database.Handle.Users().SearchByName
  101. if opts.Type == database.UserTypeOrganization {
  102. search = database.Handle.Organizations().SearchByName
  103. }
  104. users, count, err = search(c.Req.Context(), keyword, page, opts.PageSize, opts.OrderBy)
  105. if err != nil {
  106. c.Error(err, "search by name")
  107. return
  108. }
  109. }
  110. c.Data["Keyword"] = keyword
  111. c.Data["Total"] = count
  112. c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  113. c.Data["Users"] = users
  114. c.Success(opts.TplName)
  115. }
  116. func ExploreUsers(c *context.Context) {
  117. if conf.Admin.DisableRegularExploreUser && (!c.IsLogged || c.User == nil || !c.User.IsActive || !c.User.IsAdmin) {
  118. c.NotFound()
  119. return
  120. }
  121. c.Data["Title"] = c.Tr("explore")
  122. c.Data["PageIsExplore"] = true
  123. c.Data["PageIsExploreUsers"] = true
  124. c.Data["ExploreUser"] = !conf.Admin.DisableRegularExploreUser || (c.User != nil && c.User.IsActive && c.User.IsAdmin)
  125. RenderUserSearch(c, &UserSearchOptions{
  126. Type: database.UserTypeIndividual,
  127. Counter: database.Handle.Users().Count,
  128. Ranger: database.Handle.Users().List,
  129. PageSize: conf.UI.ExplorePagingNum,
  130. OrderBy: "updated_unix DESC",
  131. TplName: EXPLORE_USERS,
  132. })
  133. }
  134. func ExploreOrganizations(c *context.Context) {
  135. c.Data["Title"] = c.Tr("explore")
  136. c.Data["PageIsExplore"] = true
  137. c.Data["PageIsExploreOrganizations"] = true
  138. c.Data["ExploreUser"] = !conf.Admin.DisableRegularExploreUser || (c.User != nil && c.User.IsActive && c.User.IsAdmin)
  139. RenderUserSearch(c, &UserSearchOptions{
  140. Type: database.UserTypeOrganization,
  141. Counter: func(gocontext.Context) int64 {
  142. return database.CountOrganizations()
  143. },
  144. Ranger: func(_ gocontext.Context, page, pageSize int) ([]*database.User, error) {
  145. return database.Organizations(page, pageSize)
  146. },
  147. PageSize: conf.UI.ExplorePagingNum,
  148. OrderBy: "updated_unix DESC",
  149. TplName: EXPLORE_ORGANIZATIONS,
  150. })
  151. }
  152. func NotFound(c *macaron.Context, l i18n.Locale) {
  153. c.Data["Title"] = l.Tr("status.page_not_found")
  154. c.HTML(http.StatusNotFound, fmt.Sprintf("status/%d", http.StatusNotFound))
  155. }