1
0

home.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package route
  2. import (
  3. gocontext "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/flamego/i18n"
  7. "github.com/unknwon/paginater"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/database"
  11. "gogs.io/gogs/internal/route/user"
  12. )
  13. const (
  14. tmplHome = "home"
  15. tmplExploreRepos = "explore/repos"
  16. tmplExploreUsers = "explore/users"
  17. tmplExploreOrganizations = "explore/organizations"
  18. )
  19. func Home(c *context.Context) {
  20. if c.IsLogged {
  21. if !c.User.IsActive && conf.Auth.RequireEmailConfirmation {
  22. c.Data["Title"] = c.Tr("auth.active_your_account")
  23. c.Success(user.TmplUserAuthActivate)
  24. } else {
  25. user.Dashboard(c)
  26. }
  27. return
  28. }
  29. // Check auto-login.
  30. uname := c.GetCookie(conf.Security.CookieUsername)
  31. if uname != "" {
  32. c.Redirect(conf.Server.Subpath + "/user/login")
  33. return
  34. }
  35. c.Data["PageIsHome"] = true
  36. c.Success(tmplHome)
  37. }
  38. func ExploreRepos(c *context.Context) {
  39. c.Data["Title"] = c.Tr("explore")
  40. c.Data["PageIsExplore"] = true
  41. c.Data["PageIsExploreRepositories"] = true
  42. page := c.QueryInt("page")
  43. if page <= 0 {
  44. page = 1
  45. }
  46. keyword := c.Query("q")
  47. repos, count, err := database.SearchRepositoryByName(&database.SearchRepoOptions{
  48. Keyword: keyword,
  49. UserID: c.UserID(),
  50. OrderBy: "updated_unix DESC",
  51. Page: page,
  52. PageSize: conf.UI.ExplorePagingNum,
  53. })
  54. if err != nil {
  55. c.Error(err, "search repository by name")
  56. return
  57. }
  58. c.Data["Keyword"] = keyword
  59. c.Data["Total"] = count
  60. c.Data["Page"] = paginater.New(int(count), conf.UI.ExplorePagingNum, page, 5)
  61. if err = database.RepositoryList(repos).LoadAttributes(); err != nil {
  62. c.Error(err, "load attributes")
  63. return
  64. }
  65. c.Data["Repos"] = repos
  66. c.Success(tmplExploreRepos)
  67. }
  68. type UserSearchOptions struct {
  69. Type database.UserType
  70. Counter func(ctx gocontext.Context) int64
  71. Ranger func(ctx gocontext.Context, page, pageSize int) ([]*database.User, error)
  72. PageSize int
  73. OrderBy string
  74. TplName string
  75. }
  76. func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
  77. page := c.QueryInt("page")
  78. if page <= 1 {
  79. page = 1
  80. }
  81. var (
  82. users []*database.User
  83. count int64
  84. err error
  85. )
  86. keyword := c.Query("q")
  87. if keyword == "" {
  88. users, err = opts.Ranger(c.Req.Context(), page, opts.PageSize)
  89. if err != nil {
  90. c.Error(err, "ranger")
  91. return
  92. }
  93. count = opts.Counter(c.Req.Context())
  94. } else {
  95. search := database.Handle.Users().SearchByName
  96. if opts.Type == database.UserTypeOrganization {
  97. search = database.Handle.Organizations().SearchByName
  98. }
  99. users, count, err = search(c.Req.Context(), keyword, page, opts.PageSize, opts.OrderBy)
  100. if err != nil {
  101. c.Error(err, "search by name")
  102. return
  103. }
  104. }
  105. c.Data["Keyword"] = keyword
  106. c.Data["Total"] = count
  107. c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  108. c.Data["Users"] = users
  109. c.Success(opts.TplName)
  110. }
  111. func ExploreUsers(c *context.Context) {
  112. c.Data["Title"] = c.Tr("explore")
  113. c.Data["PageIsExplore"] = true
  114. c.Data["PageIsExploreUsers"] = true
  115. RenderUserSearch(c, &UserSearchOptions{
  116. Type: database.UserTypeIndividual,
  117. Counter: database.Handle.Users().Count,
  118. Ranger: database.Handle.Users().List,
  119. PageSize: conf.UI.ExplorePagingNum,
  120. OrderBy: "updated_unix DESC",
  121. TplName: tmplExploreUsers,
  122. })
  123. }
  124. func ExploreOrganizations(c *context.Context) {
  125. c.Data["Title"] = c.Tr("explore")
  126. c.Data["PageIsExplore"] = true
  127. c.Data["PageIsExploreOrganizations"] = true
  128. RenderUserSearch(c, &UserSearchOptions{
  129. Type: database.UserTypeOrganization,
  130. Counter: func(gocontext.Context) int64 {
  131. return database.CountOrganizations()
  132. },
  133. Ranger: func(_ gocontext.Context, page, pageSize int) ([]*database.User, error) {
  134. return database.Organizations(page, pageSize)
  135. },
  136. PageSize: conf.UI.ExplorePagingNum,
  137. OrderBy: "updated_unix DESC",
  138. TplName: tmplExploreOrganizations,
  139. })
  140. }
  141. func NotFound(c *macaron.Context, l i18n.Locale) {
  142. c.Data["Title"] = l.Tr("status.page_not_found")
  143. c.HTML(http.StatusNotFound, fmt.Sprintf("status/%d", http.StatusNotFound))
  144. }