home.go 3.7 KB

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