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.

273 lines
6.5 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/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. Ranger func(*models.SearchRepoOptions) (models.RepositoryList, int64, error)
  54. Searcher *models.User
  55. Private bool
  56. PageSize int
  57. TplName base.TplName
  58. }
  59. var (
  60. nullByte = []byte{0x00}
  61. )
  62. func isKeywordValid(keyword string) bool {
  63. return !bytes.Contains([]byte(keyword), nullByte)
  64. }
  65. // RenderRepoSearch render repositories search page
  66. func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
  67. page := ctx.QueryInt("page")
  68. if page <= 0 {
  69. page = 1
  70. }
  71. var (
  72. repos []*models.Repository
  73. count int64
  74. err error
  75. orderBy string
  76. )
  77. ctx.Data["SortType"] = ctx.Query("sort")
  78. switch ctx.Query("sort") {
  79. case "oldest":
  80. orderBy = "created_unix ASC"
  81. case "recentupdate":
  82. orderBy = "updated_unix DESC"
  83. case "leastupdate":
  84. orderBy = "updated_unix ASC"
  85. case "reversealphabetically":
  86. orderBy = "name DESC"
  87. case "alphabetically":
  88. orderBy = "name ASC"
  89. case "reversesize":
  90. orderBy = "size DESC"
  91. case "size":
  92. orderBy = "size ASC"
  93. default:
  94. orderBy = "created_unix DESC"
  95. }
  96. keyword := strings.Trim(ctx.Query("q"), " ")
  97. if len(keyword) == 0 {
  98. repos, count, err = opts.Ranger(&models.SearchRepoOptions{
  99. Page: page,
  100. PageSize: opts.PageSize,
  101. Searcher: ctx.User,
  102. OrderBy: orderBy,
  103. Private: opts.Private,
  104. })
  105. if err != nil {
  106. ctx.Handle(500, "opts.Ranger", err)
  107. return
  108. }
  109. } else {
  110. if isKeywordValid(keyword) {
  111. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  112. Keyword: keyword,
  113. OrderBy: orderBy,
  114. Private: opts.Private,
  115. Page: page,
  116. PageSize: opts.PageSize,
  117. Searcher: ctx.User,
  118. })
  119. if err != nil {
  120. ctx.Handle(500, "SearchRepositoryByName", err)
  121. return
  122. }
  123. }
  124. }
  125. ctx.Data["Keyword"] = keyword
  126. ctx.Data["Total"] = count
  127. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  128. ctx.Data["Repos"] = repos
  129. ctx.HTML(200, opts.TplName)
  130. }
  131. // ExploreRepos render explore repositories page
  132. func ExploreRepos(ctx *context.Context) {
  133. ctx.Data["Title"] = ctx.Tr("explore")
  134. ctx.Data["PageIsExplore"] = true
  135. ctx.Data["PageIsExploreRepositories"] = true
  136. RenderRepoSearch(ctx, &RepoSearchOptions{
  137. Ranger: models.GetRecentUpdatedRepositories,
  138. PageSize: setting.UI.ExplorePagingNum,
  139. Searcher: ctx.User,
  140. Private: ctx.User != nil && ctx.User.IsAdmin,
  141. TplName: tplExploreRepos,
  142. })
  143. }
  144. // UserSearchOptions options when render search user page
  145. type UserSearchOptions struct {
  146. Type models.UserType
  147. Counter func() int64
  148. Ranger func(*models.SearchUserOptions) ([]*models.User, error)
  149. PageSize int
  150. TplName base.TplName
  151. }
  152. // RenderUserSearch render user search page
  153. func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
  154. page := ctx.QueryInt("page")
  155. if page <= 1 {
  156. page = 1
  157. }
  158. var (
  159. users []*models.User
  160. count int64
  161. err error
  162. orderBy string
  163. )
  164. ctx.Data["SortType"] = ctx.Query("sort")
  165. switch ctx.Query("sort") {
  166. case "oldest":
  167. orderBy = "id ASC"
  168. case "recentupdate":
  169. orderBy = "updated_unix DESC"
  170. case "leastupdate":
  171. orderBy = "updated_unix ASC"
  172. case "reversealphabetically":
  173. orderBy = "name DESC"
  174. case "alphabetically":
  175. orderBy = "name ASC"
  176. default:
  177. orderBy = "id DESC"
  178. }
  179. keyword := strings.Trim(ctx.Query("q"), " ")
  180. if len(keyword) == 0 {
  181. users, err = opts.Ranger(&models.SearchUserOptions{
  182. OrderBy: orderBy,
  183. Page: page,
  184. PageSize: opts.PageSize,
  185. })
  186. if err != nil {
  187. ctx.Handle(500, "opts.Ranger", err)
  188. return
  189. }
  190. count = opts.Counter()
  191. } else {
  192. if isKeywordValid(keyword) {
  193. users, count, err = models.SearchUserByName(&models.SearchUserOptions{
  194. Keyword: keyword,
  195. Type: opts.Type,
  196. OrderBy: orderBy,
  197. Page: page,
  198. PageSize: opts.PageSize,
  199. })
  200. if err != nil {
  201. ctx.Handle(500, "SearchUserByName", err)
  202. return
  203. }
  204. }
  205. }
  206. ctx.Data["Keyword"] = keyword
  207. ctx.Data["Total"] = count
  208. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  209. ctx.Data["Users"] = users
  210. ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
  211. ctx.HTML(200, opts.TplName)
  212. }
  213. // ExploreUsers render explore users page
  214. func ExploreUsers(ctx *context.Context) {
  215. ctx.Data["Title"] = ctx.Tr("explore")
  216. ctx.Data["PageIsExplore"] = true
  217. ctx.Data["PageIsExploreUsers"] = true
  218. RenderUserSearch(ctx, &UserSearchOptions{
  219. Type: models.UserTypeIndividual,
  220. Counter: models.CountUsers,
  221. Ranger: models.Users,
  222. PageSize: setting.UI.ExplorePagingNum,
  223. TplName: tplExploreUsers,
  224. })
  225. }
  226. // ExploreOrganizations render explore organizations page
  227. func ExploreOrganizations(ctx *context.Context) {
  228. ctx.Data["Title"] = ctx.Tr("explore")
  229. ctx.Data["PageIsExplore"] = true
  230. ctx.Data["PageIsExploreOrganizations"] = true
  231. RenderUserSearch(ctx, &UserSearchOptions{
  232. Type: models.UserTypeOrganization,
  233. Counter: models.CountOrganizations,
  234. Ranger: models.Organizations,
  235. PageSize: setting.UI.ExplorePagingNum,
  236. TplName: tplExploreOrganizations,
  237. })
  238. }
  239. // NotFound render 404 page
  240. func NotFound(ctx *context.Context) {
  241. ctx.Data["Title"] = "Page Not Found"
  242. ctx.Handle(404, "home.NotFound", nil)
  243. }