home.go 4.7 KB

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