home.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 routes
  5. import (
  6. "github.com/Unknwon/paginater"
  7. "github.com/G-Node/gogs/models"
  8. "github.com/G-Node/gogs/pkg/context"
  9. "github.com/G-Node/gogs/pkg/setting"
  10. "github.com/G-Node/gogs/routes/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 && setting.Service.RegisterEmailConfirm {
  21. c.Data["Title"] = c.Tr("auth.active_your_account")
  22. c.HTML(200, user.ACTIVATE)
  23. } else {
  24. user.Dashboard(c)
  25. }
  26. return
  27. }
  28. // Check auto-login.
  29. uname := c.GetCookie(setting.CookieUserName)
  30. if len(uname) != 0 {
  31. c.Redirect(setting.AppSubURL + "/user/login")
  32. return
  33. }
  34. c.Data["PageIsHome"] = true
  35. c.HTML(200, 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 := models.SearchRepositoryByName(&models.SearchRepoOptions{
  47. Keyword: keyword,
  48. UserID: c.UserID(),
  49. OrderBy: "updated_unix DESC",
  50. Page: page,
  51. PageSize: setting.UI.ExplorePagingNum,
  52. })
  53. if err != nil {
  54. c.Handle(500, "SearchRepositoryByName", err)
  55. return
  56. }
  57. c.Data["Keyword"] = keyword
  58. c.Data["Total"] = count
  59. c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  60. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  61. c.Handle(500, "LoadAttributes", err)
  62. return
  63. }
  64. // filter repos we eant to not show in list
  65. var showRep []*models.Repository
  66. for _, repo := range repos {
  67. if !repo.Unlisted {
  68. showRep = append(showRep, repo)
  69. }
  70. }
  71. c.Data["Repos"] = showRep
  72. c.HTML(200, EXPLORE_REPOS)
  73. }
  74. type UserSearchOptions struct {
  75. Type models.UserType
  76. Counter func() int64
  77. Ranger func(int, int) ([]*models.User, error)
  78. PageSize int
  79. OrderBy string
  80. TplName string
  81. }
  82. func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
  83. page := c.QueryInt("page")
  84. if page <= 1 {
  85. page = 1
  86. }
  87. var (
  88. users []*models.User
  89. count int64
  90. err error
  91. )
  92. keyword := c.Query("q")
  93. if len(keyword) == 0 {
  94. users, err = opts.Ranger(page, opts.PageSize)
  95. if err != nil {
  96. c.Handle(500, "opts.Ranger", err)
  97. return
  98. }
  99. count = opts.Counter()
  100. } else {
  101. users, count, err = models.SearchUserByName(&models.SearchUserOptions{
  102. Keyword: keyword,
  103. Type: opts.Type,
  104. OrderBy: opts.OrderBy,
  105. Page: page,
  106. PageSize: opts.PageSize,
  107. })
  108. if err != nil {
  109. c.Handle(500, "SearchUserByName", err)
  110. return
  111. }
  112. }
  113. c.Data["Keyword"] = keyword
  114. c.Data["Total"] = count
  115. c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  116. c.Data["Users"] = users
  117. c.HTML(200, opts.TplName)
  118. }
  119. func ExploreUsers(c *context.Context) {
  120. c.Data["Title"] = c.Tr("explore")
  121. c.Data["PageIsExplore"] = true
  122. c.Data["PageIsExploreUsers"] = true
  123. RenderUserSearch(c, &UserSearchOptions{
  124. Type: models.USER_TYPE_INDIVIDUAL,
  125. Counter: models.CountUsers,
  126. Ranger: models.Users,
  127. PageSize: setting.UI.ExplorePagingNum,
  128. OrderBy: "updated_unix DESC",
  129. TplName: EXPLORE_USERS,
  130. })
  131. }
  132. func ExploreOrganizations(c *context.Context) {
  133. c.Data["Title"] = c.Tr("explore")
  134. c.Data["PageIsExplore"] = true
  135. c.Data["PageIsExploreOrganizations"] = true
  136. RenderUserSearch(c, &UserSearchOptions{
  137. Type: models.USER_TYPE_ORGANIZATION,
  138. Counter: models.CountOrganizations,
  139. Ranger: models.Organizations,
  140. PageSize: setting.UI.ExplorePagingNum,
  141. OrderBy: "updated_unix DESC",
  142. TplName: EXPLORE_ORGANIZATIONS,
  143. })
  144. }
  145. func NotFound(c *context.Context) {
  146. c.Data["Title"] = "Page Not Found"
  147. c.NotFound()
  148. }