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.

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