home.go 4.7 KB

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