home.go 4.0 KB

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