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

home.go 5.9 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. Collaborate: true,
  102. AllPublic: true,
  103. })
  104. if err != nil {
  105. ctx.Handle(500, "SearchRepositoryByName", err)
  106. return
  107. }
  108. ctx.Data["Keyword"] = keyword
  109. ctx.Data["Total"] = count
  110. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  111. ctx.Data["Repos"] = repos
  112. ctx.HTML(200, opts.TplName)
  113. }
  114. // ExploreRepos render explore repositories page
  115. func ExploreRepos(ctx *context.Context) {
  116. ctx.Data["Title"] = ctx.Tr("explore")
  117. ctx.Data["PageIsExplore"] = true
  118. ctx.Data["PageIsExploreRepositories"] = true
  119. var ownerID int64
  120. if ctx.User != nil && !ctx.User.IsAdmin {
  121. ownerID = ctx.User.ID
  122. }
  123. RenderRepoSearch(ctx, &RepoSearchOptions{
  124. PageSize: setting.UI.ExplorePagingNum,
  125. OwnerID: ownerID,
  126. Private: ctx.User != nil,
  127. TplName: tplExploreRepos,
  128. })
  129. }
  130. // RenderUserSearch render user search page
  131. func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplName base.TplName) {
  132. opts.Page = ctx.QueryInt("page")
  133. if opts.Page <= 1 {
  134. opts.Page = 1
  135. }
  136. var (
  137. users []*models.User
  138. count int64
  139. err error
  140. orderBy string
  141. )
  142. ctx.Data["SortType"] = ctx.Query("sort")
  143. switch ctx.Query("sort") {
  144. case "newest":
  145. orderBy = "id DESC"
  146. case "oldest":
  147. orderBy = "id ASC"
  148. case "recentupdate":
  149. orderBy = "updated_unix DESC"
  150. case "leastupdate":
  151. orderBy = "updated_unix ASC"
  152. case "reversealphabetically":
  153. orderBy = "name DESC"
  154. case "alphabetically":
  155. orderBy = "name ASC"
  156. default:
  157. ctx.Data["SortType"] = "alphabetically"
  158. orderBy = "name ASC"
  159. }
  160. opts.Keyword = strings.Trim(ctx.Query("q"), " ")
  161. opts.OrderBy = orderBy
  162. if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
  163. users, count, err = models.SearchUsers(opts)
  164. if err != nil {
  165. ctx.Handle(500, "SearchUsers", err)
  166. return
  167. }
  168. }
  169. ctx.Data["Keyword"] = opts.Keyword
  170. ctx.Data["Total"] = count
  171. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, opts.Page, 5)
  172. ctx.Data["Users"] = users
  173. ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
  174. ctx.HTML(200, tplName)
  175. }
  176. // ExploreUsers render explore users page
  177. func ExploreUsers(ctx *context.Context) {
  178. ctx.Data["Title"] = ctx.Tr("explore")
  179. ctx.Data["PageIsExplore"] = true
  180. ctx.Data["PageIsExploreUsers"] = true
  181. RenderUserSearch(ctx, &models.SearchUserOptions{
  182. Type: models.UserTypeIndividual,
  183. PageSize: setting.UI.ExplorePagingNum,
  184. IsActive: util.OptionalBoolTrue,
  185. }, tplExploreUsers)
  186. }
  187. // ExploreOrganizations render explore organizations page
  188. func ExploreOrganizations(ctx *context.Context) {
  189. ctx.Data["Title"] = ctx.Tr("explore")
  190. ctx.Data["PageIsExplore"] = true
  191. ctx.Data["PageIsExploreOrganizations"] = true
  192. RenderUserSearch(ctx, &models.SearchUserOptions{
  193. Type: models.UserTypeOrganization,
  194. PageSize: setting.UI.ExplorePagingNum,
  195. }, tplExploreOrganizations)
  196. }
  197. // NotFound render 404 page
  198. func NotFound(ctx *context.Context) {
  199. ctx.Data["Title"] = "Page Not Found"
  200. ctx.Handle(404, "home.NotFound", nil)
  201. }