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.

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