Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

home.go 6.5KB

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