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.

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