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.

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