home.go 3.6 KB

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