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.

230 lines
5.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
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. "bytes"
  7. "strings"
  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/modules/util"
  13. "code.gitea.io/gitea/routers/user"
  14. "github.com/Unknwon/paginater"
  15. )
  16. const (
  17. // tplHome home page template
  18. tplHome base.TplName = "home"
  19. // tplExploreRepos explore repositories page template
  20. tplExploreRepos base.TplName = "explore/repos"
  21. // tplExploreUsers explore users page template
  22. tplExploreUsers base.TplName = "explore/users"
  23. // tplExploreOrganizations explore organizations page template
  24. tplExploreOrganizations base.TplName = "explore/organizations"
  25. )
  26. // Home render home page
  27. func Home(ctx *context.Context) {
  28. if ctx.IsSigned {
  29. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  30. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  31. ctx.HTML(200, user.TplActivate)
  32. } else {
  33. user.Dashboard(ctx)
  34. }
  35. return
  36. }
  37. // Check auto-login.
  38. uname := ctx.GetCookie(setting.CookieUserName)
  39. if len(uname) != 0 {
  40. ctx.Redirect(setting.AppSubURL + "/user/login")
  41. return
  42. }
  43. ctx.Data["PageIsHome"] = true
  44. ctx.HTML(200, tplHome)
  45. }
  46. // RepoSearchOptions when calling search repositories
  47. type RepoSearchOptions struct {
  48. OwnerID int64
  49. Private bool
  50. PageSize int
  51. TplName base.TplName
  52. }
  53. var (
  54. nullByte = []byte{0x00}
  55. )
  56. func isKeywordValid(keyword string) bool {
  57. return !bytes.Contains([]byte(keyword), nullByte)
  58. }
  59. // RenderRepoSearch render repositories search page
  60. func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
  61. page := ctx.QueryInt("page")
  62. if page <= 0 {
  63. page = 1
  64. }
  65. var (
  66. repos []*models.Repository
  67. count int64
  68. err error
  69. orderBy models.SearchOrderBy
  70. )
  71. ctx.Data["SortType"] = ctx.Query("sort")
  72. switch ctx.Query("sort") {
  73. case "newest":
  74. orderBy = models.SearchOrderByNewest
  75. case "oldest":
  76. orderBy = models.SearchOrderByOldest
  77. case "recentupdate":
  78. orderBy = models.SearchOrderByRecentUpdated
  79. case "leastupdate":
  80. orderBy = models.SearchOrderByLeastUpdated
  81. case "reversealphabetically":
  82. orderBy = models.SearchOrderByAlphabeticallyReverse
  83. case "alphabetically":
  84. orderBy = models.SearchOrderByAlphabetically
  85. case "reversesize":
  86. orderBy = models.SearchOrderBySizeReverse
  87. case "size":
  88. orderBy = models.SearchOrderBySize
  89. default:
  90. ctx.Data["SortType"] = "recentupdate"
  91. orderBy = models.SearchOrderByRecentUpdated
  92. }
  93. keyword := strings.Trim(ctx.Query("q"), " ")
  94. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  95. Page: page,
  96. PageSize: opts.PageSize,
  97. OrderBy: orderBy,
  98. Private: opts.Private,
  99. Keyword: keyword,
  100. OwnerID: opts.OwnerID,
  101. AllPublic: true,
  102. })
  103. if err != nil {
  104. ctx.ServerError("SearchRepositoryByName", err)
  105. return
  106. }
  107. ctx.Data["Keyword"] = keyword
  108. ctx.Data["Total"] = count
  109. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  110. ctx.Data["Repos"] = repos
  111. ctx.HTML(200, opts.TplName)
  112. }
  113. // ExploreRepos render explore repositories page
  114. func ExploreRepos(ctx *context.Context) {
  115. ctx.Data["Title"] = ctx.Tr("explore")
  116. ctx.Data["PageIsExplore"] = true
  117. ctx.Data["PageIsExploreRepositories"] = true
  118. var ownerID int64
  119. if ctx.User != nil && !ctx.User.IsAdmin {
  120. ownerID = ctx.User.ID
  121. }
  122. RenderRepoSearch(ctx, &RepoSearchOptions{
  123. PageSize: setting.UI.ExplorePagingNum,
  124. OwnerID: ownerID,
  125. Private: ctx.User != nil,
  126. TplName: tplExploreRepos,
  127. })
  128. }
  129. // RenderUserSearch render user search page
  130. func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplName base.TplName) {
  131. opts.Page = ctx.QueryInt("page")
  132. if opts.Page <= 1 {
  133. opts.Page = 1
  134. }
  135. var (
  136. users []*models.User
  137. count int64
  138. err error
  139. orderBy string
  140. )
  141. ctx.Data["SortType"] = ctx.Query("sort")
  142. switch ctx.Query("sort") {
  143. case "newest":
  144. orderBy = "id DESC"
  145. case "oldest":
  146. orderBy = "id ASC"
  147. case "recentupdate":
  148. orderBy = "updated_unix DESC"
  149. case "leastupdate":
  150. orderBy = "updated_unix ASC"
  151. case "reversealphabetically":
  152. orderBy = "name DESC"
  153. case "alphabetically":
  154. orderBy = "name ASC"
  155. default:
  156. ctx.Data["SortType"] = "alphabetically"
  157. orderBy = "name ASC"
  158. }
  159. opts.Keyword = strings.Trim(ctx.Query("q"), " ")
  160. opts.OrderBy = orderBy
  161. if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
  162. users, count, err = models.SearchUsers(opts)
  163. if err != nil {
  164. ctx.ServerError("SearchUsers", err)
  165. return
  166. }
  167. }
  168. ctx.Data["Keyword"] = opts.Keyword
  169. ctx.Data["Total"] = count
  170. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, opts.Page, 5)
  171. ctx.Data["Users"] = users
  172. ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
  173. ctx.HTML(200, tplName)
  174. }
  175. // ExploreUsers render explore users page
  176. func ExploreUsers(ctx *context.Context) {
  177. ctx.Data["Title"] = ctx.Tr("explore")
  178. ctx.Data["PageIsExplore"] = true
  179. ctx.Data["PageIsExploreUsers"] = true
  180. RenderUserSearch(ctx, &models.SearchUserOptions{
  181. Type: models.UserTypeIndividual,
  182. PageSize: setting.UI.ExplorePagingNum,
  183. IsActive: util.OptionalBoolTrue,
  184. }, tplExploreUsers)
  185. }
  186. // ExploreOrganizations render explore organizations page
  187. func ExploreOrganizations(ctx *context.Context) {
  188. ctx.Data["Title"] = ctx.Tr("explore")
  189. ctx.Data["PageIsExplore"] = true
  190. ctx.Data["PageIsExploreOrganizations"] = true
  191. RenderUserSearch(ctx, &models.SearchUserOptions{
  192. Type: models.UserTypeOrganization,
  193. PageSize: setting.UI.ExplorePagingNum,
  194. }, tplExploreOrganizations)
  195. }
  196. // NotFound render 404 page
  197. func NotFound(ctx *context.Context) {
  198. ctx.Data["Title"] = "Page Not Found"
  199. ctx.NotFound("home.NotFound", nil)
  200. }