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.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
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. "fmt"
  7. "github.com/Unknwon/paginater"
  8. "bytes"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/routers/user"
  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.Repository, 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 := 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. for _, repo := range repos {
  120. if err = repo.GetOwner(); err != nil {
  121. ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
  122. return
  123. }
  124. }
  125. ctx.Data["Repos"] = repos
  126. ctx.HTML(200, opts.TplName)
  127. }
  128. // ExploreRepos render explore repositories page
  129. func ExploreRepos(ctx *context.Context) {
  130. ctx.Data["Title"] = ctx.Tr("explore")
  131. ctx.Data["PageIsExplore"] = true
  132. ctx.Data["PageIsExploreRepositories"] = true
  133. RenderRepoSearch(ctx, &RepoSearchOptions{
  134. Counter: models.CountRepositories,
  135. Ranger: models.GetRecentUpdatedRepositories,
  136. PageSize: setting.UI.ExplorePagingNum,
  137. Searcher: ctx.User,
  138. TplName: tplExploreRepos,
  139. })
  140. }
  141. // UserSearchOptions options when render search user page
  142. type UserSearchOptions struct {
  143. Type models.UserType
  144. Counter func() int64
  145. Ranger func(*models.SearchUserOptions) ([]*models.User, error)
  146. PageSize int
  147. TplName base.TplName
  148. }
  149. // RenderUserSearch render user search page
  150. func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
  151. page := ctx.QueryInt("page")
  152. if page <= 1 {
  153. page = 1
  154. }
  155. var (
  156. users []*models.User
  157. count int64
  158. err error
  159. orderBy string
  160. )
  161. ctx.Data["SortType"] = ctx.Query("sort")
  162. //OrderBy: "id ASC",
  163. switch ctx.Query("sort") {
  164. case "oldest":
  165. orderBy = "id ASC"
  166. case "recentupdate":
  167. orderBy = "updated_unix DESC"
  168. case "leastupdate":
  169. orderBy = "updated_unix ASC"
  170. case "reversealphabetically":
  171. orderBy = "name DESC"
  172. case "alphabetically":
  173. orderBy = "name ASC"
  174. default:
  175. orderBy = "id DESC"
  176. }
  177. keyword := ctx.Query("q")
  178. if len(keyword) == 0 {
  179. users, err = opts.Ranger(&models.SearchUserOptions{OrderBy: orderBy,
  180. Page: page,
  181. PageSize: opts.PageSize,
  182. })
  183. if err != nil {
  184. ctx.Handle(500, "opts.Ranger", err)
  185. return
  186. }
  187. count = opts.Counter()
  188. } else {
  189. if isKeywordValid(keyword) {
  190. users, count, err = models.SearchUserByName(&models.SearchUserOptions{
  191. Keyword: keyword,
  192. Type: opts.Type,
  193. OrderBy: orderBy,
  194. Page: page,
  195. PageSize: opts.PageSize,
  196. })
  197. if err != nil {
  198. ctx.Handle(500, "SearchUserByName", err)
  199. return
  200. }
  201. }
  202. }
  203. ctx.Data["Keyword"] = keyword
  204. ctx.Data["Total"] = count
  205. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  206. ctx.Data["Users"] = users
  207. ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
  208. ctx.HTML(200, opts.TplName)
  209. }
  210. // ExploreUsers render explore users page
  211. func ExploreUsers(ctx *context.Context) {
  212. ctx.Data["Title"] = ctx.Tr("explore")
  213. ctx.Data["PageIsExplore"] = true
  214. ctx.Data["PageIsExploreUsers"] = true
  215. RenderUserSearch(ctx, &UserSearchOptions{
  216. Type: models.UserTypeIndividual,
  217. Counter: models.CountUsers,
  218. Ranger: models.Users,
  219. PageSize: setting.UI.ExplorePagingNum,
  220. TplName: tplExploreUsers,
  221. })
  222. }
  223. // ExploreOrganizations render explore organizations page
  224. func ExploreOrganizations(ctx *context.Context) {
  225. ctx.Data["Title"] = ctx.Tr("explore")
  226. ctx.Data["PageIsExplore"] = true
  227. ctx.Data["PageIsExploreOrganizations"] = true
  228. RenderUserSearch(ctx, &UserSearchOptions{
  229. Type: models.UserTypeOrganization,
  230. Counter: models.CountOrganizations,
  231. Ranger: models.Organizations,
  232. PageSize: setting.UI.ExplorePagingNum,
  233. TplName: tplExploreOrganizations,
  234. })
  235. }
  236. // NotFound render 404 page
  237. func NotFound(ctx *context.Context) {
  238. ctx.Data["Title"] = "Page Not Found"
  239. ctx.Handle(404, "home.NotFound", nil)
  240. }