home.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. 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/db"
  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. page := c.QueryInt("page")
  47. if page <= 0 {
  48. page = 1
  49. }
  50. keyword := c.Query("q")
  51. repos, count, err := db.SearchRepositoryByName(&db.SearchRepoOptions{
  52. Keyword: keyword,
  53. UserID: c.UserID(),
  54. OrderBy: "updated_unix DESC",
  55. Page: page,
  56. PageSize: conf.UI.ExplorePagingNum,
  57. })
  58. if err != nil {
  59. c.Error(err, "search repository by name")
  60. return
  61. }
  62. c.Data["Keyword"] = keyword
  63. c.Data["Total"] = count
  64. c.Data["Page"] = paginater.New(int(count), conf.UI.ExplorePagingNum, page, 5)
  65. if err = db.RepositoryList(repos).LoadAttributes(); err != nil {
  66. c.Error(err, "load attributes")
  67. return
  68. }
  69. c.Data["Repos"] = repos
  70. c.Success(EXPLORE_REPOS)
  71. }
  72. type UserSearchOptions struct {
  73. Type db.UserType
  74. Counter func(ctx gocontext.Context) int64
  75. Ranger func(ctx gocontext.Context, page, pageSize int) ([]*db.User, error)
  76. PageSize int
  77. OrderBy string
  78. TplName string
  79. }
  80. func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
  81. page := c.QueryInt("page")
  82. if page <= 1 {
  83. page = 1
  84. }
  85. var (
  86. users []*db.User
  87. count int64
  88. err error
  89. )
  90. keyword := c.Query("q")
  91. if keyword == "" {
  92. users, err = opts.Ranger(c.Req.Context(), page, opts.PageSize)
  93. if err != nil {
  94. c.Error(err, "ranger")
  95. return
  96. }
  97. count = opts.Counter(c.Req.Context())
  98. } else {
  99. search := db.Users.SearchByName
  100. if opts.Type == db.UserTypeOrganization {
  101. search = db.Organizations.SearchByName
  102. }
  103. users, count, err = search(c.Req.Context(), keyword, page, opts.PageSize, opts.OrderBy)
  104. if err != nil {
  105. c.Error(err, "search by name")
  106. return
  107. }
  108. }
  109. c.Data["Keyword"] = keyword
  110. c.Data["Total"] = count
  111. c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  112. c.Data["Users"] = users
  113. c.Success(opts.TplName)
  114. }
  115. func ExploreUsers(c *context.Context) {
  116. c.Data["Title"] = c.Tr("explore")
  117. c.Data["PageIsExplore"] = true
  118. c.Data["PageIsExploreUsers"] = true
  119. RenderUserSearch(c, &UserSearchOptions{
  120. Type: db.UserTypeIndividual,
  121. Counter: db.Users.Count,
  122. Ranger: db.Users.List,
  123. PageSize: conf.UI.ExplorePagingNum,
  124. OrderBy: "updated_unix DESC",
  125. TplName: EXPLORE_USERS,
  126. })
  127. }
  128. func ExploreOrganizations(c *context.Context) {
  129. c.Data["Title"] = c.Tr("explore")
  130. c.Data["PageIsExplore"] = true
  131. c.Data["PageIsExploreOrganizations"] = true
  132. RenderUserSearch(c, &UserSearchOptions{
  133. Type: db.UserTypeOrganization,
  134. Counter: db.Organizations.Count,
  135. Ranger: func(ctx gocontext.Context, page, pageSize int) ([]*db.User, error) {
  136. return db.Organizations.List(
  137. ctx,
  138. db.ListOrganizationsOptions{
  139. Page: page,
  140. PageSize: pageSize,
  141. },
  142. )
  143. },
  144. PageSize: conf.UI.ExplorePagingNum,
  145. OrderBy: "updated_unix DESC",
  146. TplName: EXPLORE_ORGANIZATIONS,
  147. })
  148. }
  149. func NotFound(c *macaron.Context, l i18n.Locale) {
  150. c.Data["Title"] = l.Tr("status.page_not_found")
  151. c.HTML(http.StatusNotFound, fmt.Sprintf("status/%d", http.StatusNotFound))
  152. }