You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. )
  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. type RepoSearchOptions struct {
  39. Counter func() int64
  40. Ranger func(int, int) ([]*models.Repository, error)
  41. Private bool
  42. PageSize int
  43. OrderBy string
  44. TplName base.TplName
  45. }
  46. func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
  47. page := ctx.QueryInt("page")
  48. if page <= 1 {
  49. page = 1
  50. }
  51. var (
  52. repos []*models.Repository
  53. count int64
  54. err error
  55. )
  56. keyword := ctx.Query("q")
  57. if len(keyword) == 0 {
  58. repos, err = opts.Ranger(page, opts.PageSize)
  59. if err != nil {
  60. ctx.Handle(500, "opts.Ranger", err)
  61. return
  62. }
  63. count = opts.Counter()
  64. } else {
  65. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  66. Keyword: keyword,
  67. OrderBy: opts.OrderBy,
  68. Private: opts.Private,
  69. Page: page,
  70. PageSize: opts.PageSize,
  71. })
  72. if err != nil {
  73. ctx.Handle(500, "SearchRepositoryByName", err)
  74. return
  75. }
  76. }
  77. ctx.Data["Keyword"] = keyword
  78. ctx.Data["Total"] = count
  79. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  80. for _, repo := range repos {
  81. if err = repo.GetOwner(); err != nil {
  82. ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
  83. return
  84. }
  85. }
  86. ctx.Data["Repos"] = repos
  87. ctx.HTML(200, opts.TplName)
  88. }
  89. func ExploreRepos(ctx *context.Context) {
  90. ctx.Data["Title"] = ctx.Tr("explore")
  91. ctx.Data["PageIsExplore"] = true
  92. ctx.Data["PageIsExploreRepositories"] = true
  93. RenderRepoSearch(ctx, &RepoSearchOptions{
  94. Counter: models.CountPublicRepositories,
  95. Ranger: models.GetRecentUpdatedRepositories,
  96. PageSize: setting.ExplorePagingNum,
  97. OrderBy: "updated_unix DESC",
  98. TplName: EXPLORE_REPOS,
  99. })
  100. }
  101. type UserSearchOptions struct {
  102. Type models.UserType
  103. Counter func() int64
  104. Ranger func(int, int) ([]*models.User, error)
  105. PageSize int
  106. OrderBy string
  107. TplName base.TplName
  108. }
  109. func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
  110. page := ctx.QueryInt("page")
  111. if page <= 1 {
  112. page = 1
  113. }
  114. var (
  115. users []*models.User
  116. count int64
  117. err error
  118. )
  119. keyword := ctx.Query("q")
  120. if len(keyword) == 0 {
  121. users, err = opts.Ranger(page, opts.PageSize)
  122. if err != nil {
  123. ctx.Handle(500, "opts.Ranger", err)
  124. return
  125. }
  126. count = opts.Counter()
  127. } else {
  128. users, count, err = models.SearchUserByName(&models.SearchUserOptions{
  129. Keyword: keyword,
  130. Type: opts.Type,
  131. OrderBy: opts.OrderBy,
  132. Page: page,
  133. PageSize: opts.PageSize,
  134. })
  135. if err != nil {
  136. ctx.Handle(500, "SearchUserByName", err)
  137. return
  138. }
  139. }
  140. ctx.Data["Keyword"] = keyword
  141. ctx.Data["Total"] = count
  142. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  143. ctx.Data["Users"] = users
  144. ctx.HTML(200, opts.TplName)
  145. }
  146. func ExploreUsers(ctx *context.Context) {
  147. ctx.Data["Title"] = ctx.Tr("explore")
  148. ctx.Data["PageIsExplore"] = true
  149. ctx.Data["PageIsExploreUsers"] = true
  150. RenderUserSearch(ctx, &UserSearchOptions{
  151. Type: models.USER_TYPE_INDIVIDUAL,
  152. Counter: models.CountUsers,
  153. Ranger: models.Users,
  154. PageSize: setting.ExplorePagingNum,
  155. OrderBy: "updated_unix DESC",
  156. TplName: EXPLORE_USERS,
  157. })
  158. }
  159. func NotFound(ctx *context.Context) {
  160. ctx.Data["Title"] = "Page Not Found"
  161. ctx.Handle(404, "home.NotFound", nil)
  162. }