home.go 5.5 KB

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