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.

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