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