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.

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