home.go 5.5 KB

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