home.go 3.9 KB

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