home.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/context"
  11. "github.com/gogits/gogs/modules/setting"
  12. "github.com/gogits/gogs/routers/user"
  13. )
  14. const (
  15. HOME base.TplName = "home"
  16. EXPLORE_REPOS base.TplName = "explore/repos"
  17. EXPLORE_USERS base.TplName = "explore/users"
  18. EXPLORE_ORGANIZATIONS base.TplName = "explore/organizations"
  19. )
  20. func Home(ctx *context.Context) {
  21. if ctx.IsSigned {
  22. if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  23. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  24. ctx.HTML(200, user.ACTIVATE)
  25. } else {
  26. user.Dashboard(ctx)
  27. }
  28. return
  29. }
  30. // Check auto-login.
  31. uname := ctx.GetCookie(setting.CookieUserName)
  32. if len(uname) != 0 {
  33. ctx.Redirect(setting.AppSubUrl + "/user/login")
  34. return
  35. }
  36. ctx.Data["PageIsHome"] = true
  37. ctx.HTML(200, HOME)
  38. }
  39. type RepoSearchOptions struct {
  40. Counter func(bool) int64
  41. Ranger func(int, int) ([]*models.Repository, error)
  42. Private bool
  43. PageSize int
  44. OrderBy string
  45. TplName base.TplName
  46. }
  47. func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
  48. page := ctx.QueryInt("page")
  49. if page <= 0 {
  50. page = 1
  51. }
  52. var (
  53. repos []*models.Repository
  54. count int64
  55. err error
  56. )
  57. keyword := ctx.Query("q")
  58. if len(keyword) == 0 {
  59. repos, err = opts.Ranger(page, opts.PageSize)
  60. if err != nil {
  61. ctx.Handle(500, "opts.Ranger", err)
  62. return
  63. }
  64. count = opts.Counter(opts.Private)
  65. } else {
  66. var ctxUserID int64
  67. if ctx.IsSigned {
  68. ctxUserID = ctx.User.ID
  69. }
  70. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  71. Keyword: keyword,
  72. UserID: ctxUserID,
  73. OrderBy: opts.OrderBy,
  74. Private: opts.Private,
  75. Page: page,
  76. PageSize: opts.PageSize,
  77. })
  78. if err != nil {
  79. ctx.Handle(500, "SearchRepositoryByName", err)
  80. return
  81. }
  82. }
  83. ctx.Data["Keyword"] = keyword
  84. ctx.Data["Total"] = count
  85. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  86. for _, repo := range repos {
  87. if err = repo.GetOwner(); err != nil {
  88. ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
  89. return
  90. }
  91. }
  92. ctx.Data["Repos"] = repos
  93. ctx.HTML(200, opts.TplName)
  94. }
  95. func ExploreRepos(ctx *context.Context) {
  96. ctx.Data["Title"] = ctx.Tr("explore")
  97. ctx.Data["PageIsExplore"] = true
  98. ctx.Data["PageIsExploreRepositories"] = true
  99. RenderRepoSearch(ctx, &RepoSearchOptions{
  100. Counter: models.CountRepositories,
  101. Ranger: models.GetRecentUpdatedRepositories,
  102. PageSize: setting.UI.ExplorePagingNum,
  103. OrderBy: "updated_unix DESC",
  104. TplName: EXPLORE_REPOS,
  105. })
  106. }
  107. type UserSearchOptions struct {
  108. Type models.UserType
  109. Counter func() int64
  110. Ranger func(int, int) ([]*models.User, error)
  111. PageSize int
  112. OrderBy string
  113. TplName base.TplName
  114. }
  115. func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
  116. page := ctx.QueryInt("page")
  117. if page <= 1 {
  118. page = 1
  119. }
  120. var (
  121. users []*models.User
  122. count int64
  123. err error
  124. )
  125. keyword := ctx.Query("q")
  126. if len(keyword) == 0 {
  127. users, err = opts.Ranger(page, opts.PageSize)
  128. if err != nil {
  129. ctx.Handle(500, "opts.Ranger", err)
  130. return
  131. }
  132. count = opts.Counter()
  133. } else {
  134. users, count, err = models.SearchUserByName(&models.SearchUserOptions{
  135. Keyword: keyword,
  136. Type: opts.Type,
  137. OrderBy: opts.OrderBy,
  138. Page: page,
  139. PageSize: opts.PageSize,
  140. })
  141. if err != nil {
  142. ctx.Handle(500, "SearchUserByName", err)
  143. return
  144. }
  145. }
  146. ctx.Data["Keyword"] = keyword
  147. ctx.Data["Total"] = count
  148. ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  149. ctx.Data["Users"] = users
  150. ctx.HTML(200, opts.TplName)
  151. }
  152. func ExploreUsers(ctx *context.Context) {
  153. ctx.Data["Title"] = ctx.Tr("explore")
  154. ctx.Data["PageIsExplore"] = true
  155. ctx.Data["PageIsExploreUsers"] = true
  156. RenderUserSearch(ctx, &UserSearchOptions{
  157. Type: models.USER_TYPE_INDIVIDUAL,
  158. Counter: models.CountUsers,
  159. Ranger: models.Users,
  160. PageSize: setting.UI.ExplorePagingNum,
  161. OrderBy: "updated_unix DESC",
  162. TplName: EXPLORE_USERS,
  163. })
  164. }
  165. func ExploreOrganizations(ctx *context.Context) {
  166. ctx.Data["Title"] = ctx.Tr("explore")
  167. ctx.Data["PageIsExplore"] = true
  168. ctx.Data["PageIsExploreOrganizations"] = true
  169. RenderUserSearch(ctx, &UserSearchOptions{
  170. Type: models.USER_TYPE_ORGANIZATION,
  171. Counter: models.CountOrganizations,
  172. Ranger: models.Organizations,
  173. PageSize: setting.UI.ExplorePagingNum,
  174. OrderBy: "updated_unix DESC",
  175. TplName: EXPLORE_ORGANIZATIONS,
  176. })
  177. }
  178. func NotFound(ctx *context.Context) {
  179. ctx.Data["Title"] = "Page Not Found"
  180. ctx.Handle(404, "home.NotFound", nil)
  181. }