home.go 5.3 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 routes
  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/routes/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(c *context.Context) {
  25. if c.IsLogged {
  26. if !c.User.IsActive && setting.Service.RegisterEmailConfirm {
  27. c.Data["Title"] = c.Tr("auth.active_your_account")
  28. c.Success(user.ACTIVATE)
  29. } else {
  30. user.Dashboard(c)
  31. }
  32. return
  33. }
  34. // Check auto-login.
  35. uname := c.GetCookie(setting.CookieUserName)
  36. if len(uname) != 0 {
  37. c.Redirect(setting.AppSubURL + "/user/login")
  38. return
  39. }
  40. c.Data["PageIsHome"] = true
  41. c.Success(HOME)
  42. }
  43. func ExploreRepos(c *context.Context) {
  44. c.Data["Title"] = c.Tr("explore")
  45. c.Data["PageIsExplore"] = true
  46. c.Data["PageIsExploreRepositories"] = true
  47. page := c.QueryInt("page")
  48. if page <= 0 {
  49. page = 1
  50. }
  51. keyword := c.Query("q")
  52. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  53. Keyword: keyword,
  54. UserID: c.UserID(),
  55. OrderBy: "updated_unix DESC",
  56. Page: page,
  57. PageSize: setting.UI.ExplorePagingNum,
  58. NoMirrors: true,
  59. })
  60. if err != nil {
  61. c.Handle(500, "SearchRepositoryByName", err)
  62. return
  63. }
  64. c.Data["Keyword"] = keyword
  65. c.Data["Total"] = count
  66. c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  67. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  68. c.Handle(500, "LoadAttributes", err)
  69. return
  70. }
  71. c.Data["Repos"] = repos
  72. c.HTML(200, EXPLORE_REPOS)
  73. }
  74. func ExploreMirrors(c *context.Context) {
  75. c.Data["Title"] = c.Tr("explore")
  76. c.Data["PageIsExplore"] = true
  77. c.Data["PageIsExploreMirrors"] = true
  78. page := c.QueryInt("page")
  79. if page <= 0 {
  80. page = 1
  81. }
  82. keyword := c.Query("q")
  83. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  84. Keyword: keyword,
  85. UserID: c.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. c.ServerError("SearchRepositoryByName", err)
  94. return
  95. }
  96. c.Data["Keyword"] = keyword
  97. c.Data["Total"] = count
  98. c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  99. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  100. c.ServerError("RepositoryList.LoadAttributes", err)
  101. return
  102. }
  103. c.Data["Repos"] = repos
  104. c.Success(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(c *context.Context, opts *UserSearchOptions) {
  115. page := c.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 := c.Query("q")
  125. if len(keyword) == 0 {
  126. users, err = opts.Ranger(page, opts.PageSize)
  127. if err != nil {
  128. c.ServerError("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. c.ServerError("SearchUserByName", err)
  142. return
  143. }
  144. }
  145. c.Data["Keyword"] = keyword
  146. c.Data["Total"] = count
  147. c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  148. c.Data["Users"] = users
  149. c.Success(opts.TplName)
  150. }
  151. func ExploreUsers(c *context.Context) {
  152. c.Data["Title"] = c.Tr("explore")
  153. c.Data["PageIsExplore"] = true
  154. c.Data["PageIsExploreUsers"] = true
  155. RenderUserSearch(c, &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(c *context.Context) {
  165. c.Data["Title"] = c.Tr("explore")
  166. c.Data["PageIsExplore"] = true
  167. c.Data["PageIsExploreOrganizations"] = true
  168. RenderUserSearch(c, &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(c *context.Context) {
  178. c.Data["Title"] = "Page Not Found"
  179. c.NotFound()
  180. }
  181. func About(c *context.Context) {
  182. c.Data["Title"] = c.Tr("about")
  183. c.HTML(200, ABOUT)
  184. }
  185. func Tor(c *context.Context) {
  186. c.Data["Title"] = c.Tr("tor")
  187. c.HTML(200, TOR)
  188. }
  189. func Help(c *context.Context) {
  190. c.Data["Title"] = c.Tr("help")
  191. c.HTML(200, HELP)
  192. }
  193. func Outages(c *context.Context) {
  194. c.Data["Title"] = c.Tr("outages")
  195. c.HTML(200, OUTAGES)
  196. }
  197. func Tos(c *context.Context) {
  198. c.Data["Title"] = c.Tr("tos")
  199. c.HTML(200, TOS)
  200. }
  201. func Fingerprints(c *context.Context) {
  202. c.Data["Title"] = c.Tr("fingerprints")
  203. c.HTML(200, FINGERPRINTS)
  204. }