home.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. "github.com/Unknwon/paginater"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/pkg/base"
  9. "github.com/gogits/gogs/pkg/context"
  10. "github.com/gogits/gogs/pkg/setting"
  11. "github.com/gogits/gogs/routers/user"
  12. )
  13. const (
  14. HOME base.TplName = "home"
  15. EXPLORE_REPOS base.TplName = "explore/repos"
  16. EXPLORE_USERS base.TplName = "explore/users"
  17. EXPLORE_ORGANIZATIONS base.TplName = "explore/organizations"
  18. ABOUT base.TplName = "about"
  19. TOR base.TplName = "tor"
  20. HELP base.TplName = "help"
  21. OUTAGES base.TplName = "outages"
  22. TOS base.TplName = "tos"
  23. FINGERPRINTS base.TplName = "fingerprints"
  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. func ExploreRepos(ctx *context.Context) {
  45. ctx.Data["Title"] = ctx.Tr("explore")
  46. ctx.Data["PageIsExplore"] = true
  47. ctx.Data["PageIsExploreRepositories"] = true
  48. page := ctx.QueryInt("page")
  49. if page <= 0 {
  50. page = 1
  51. }
  52. keyword := ctx.Query("q")
  53. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  54. Keyword: keyword,
  55. UserID: ctx.UserID(),
  56. OrderBy: "updated_unix DESC",
  57. Page: page,
  58. PageSize: setting.UI.ExplorePagingNum,
  59. NoMirrors: true,
  60. })
  61. if err != nil {
  62. ctx.Handle(500, "SearchRepositoryByName", err)
  63. return
  64. }
  65. ctx.Data["Keyword"] = keyword
  66. ctx.Data["Total"] = count
  67. ctx.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  68. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  69. ctx.Handle(500, "LoadAttributes", err)
  70. return
  71. }
  72. ctx.Data["Repos"] = repos
  73. ctx.HTML(200, EXPLORE_REPOS)
  74. }
  75. func ExploreMirrors(ctx *context.Context) {
  76. ctx.Data["Title"] = ctx.Tr("explore")
  77. ctx.Data["PageIsExplore"] = true
  78. ctx.Data["PageIsExploreMirrors"] = true
  79. page := ctx.QueryInt("page")
  80. if page <= 0 {
  81. page = 1
  82. }
  83. keyword := ctx.Query("q")
  84. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  85. Keyword: keyword,
  86. UserID: ctx.UserID(),
  87. OrderBy: "updated_unix DESC",
  88. Page: page,
  89. PageSize: setting.UI.ExplorePagingNum,
  90. NoMirrors: false,
  91. OnlyMirrors: true,
  92. })
  93. if err != nil {
  94. ctx.Handle(500, "SearchRepositoryByName", err)
  95. return
  96. }
  97. ctx.Data["Keyword"] = keyword
  98. ctx.Data["Total"] = count
  99. ctx.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  100. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  101. ctx.Handle(500, "LoadAttributes", err)
  102. return
  103. }
  104. ctx.Data["Repos"] = repos
  105. ctx.HTML(200, EXPLORE_REPOS)
  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. }
  182. func About(ctx *context.Context) {
  183. ctx.Data["Title"] = ctx.Tr("about")
  184. ctx.HTML(200, ABOUT)
  185. }
  186. func Tor(ctx *context.Context) {
  187. ctx.Data["Title"] = ctx.Tr("tor")
  188. ctx.HTML(200, TOR)
  189. }
  190. func Help(ctx *context.Context) {
  191. ctx.Data["Title"] = ctx.Tr("help")
  192. ctx.HTML(200, HELP)
  193. }
  194. func Outages(ctx *context.Context) {
  195. ctx.Data["Title"] = ctx.Tr("outages")
  196. ctx.HTML(200, OUTAGES)
  197. }
  198. func Tos(ctx *context.Context) {
  199. ctx.Data["Title"] = ctx.Tr("tos")
  200. ctx.HTML(200, TOS)
  201. }
  202. func Fingerprints(ctx *context.Context) {
  203. ctx.Data["Title"] = ctx.Tr("fingerprints")
  204. ctx.HTML(200, FINGERPRINTS)
  205. }