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.

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