home.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package routes
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. "gitote/gitote/pkg/setting"
  11. "gitote/gitote/routes/user"
  12. "gitlab.com/yoginth/paginater"
  13. )
  14. const (
  15. // HOME page template
  16. HOME = "home"
  17. // EXPLORE_HOME page template
  18. EXPLORE_HOME = "explore/home"
  19. // EXPLORE_REPOS page template
  20. EXPLORE_REPOS = "explore/repos"
  21. // EXPLORE_USERS page template
  22. EXPLORE_USERS = "explore/users"
  23. // EXPLORE_TRENDING page template
  24. EXPLORE_TRENDING = "explore/trending"
  25. // EXPLORE_ORGANIZATIONS page template
  26. EXPLORE_ORGANIZATIONS = "explore/organizations"
  27. )
  28. // Home shows the home page
  29. func Home(c *context.Context) {
  30. if c.IsLogged {
  31. if !c.User.IsActive && setting.Service.RegisterEmailConfirm {
  32. c.Data["Title"] = c.Tr("auth.active_your_account")
  33. c.Success(user.ACTIVATE)
  34. } else {
  35. user.Dashboard(c)
  36. }
  37. return
  38. }
  39. // Check auto-login.
  40. uname := c.GetCookie(setting.CookieUserName)
  41. if len(uname) != 0 {
  42. c.Redirect(setting.AppSubURL + "/login")
  43. return
  44. }
  45. c.Data["PageIsHome"] = true
  46. c.Success(HOME)
  47. }
  48. // ExploreHome shows explore page
  49. func ExploreHome(c *context.Context) {
  50. c.Data["Title"] = c.Tr("explore")
  51. c.Data["PageIsExplore"] = true
  52. c.Data["PageIsExploreRepositories"] = true
  53. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  54. UserID: c.UserID(),
  55. OrderBy: "num_stars DESC",
  56. PageSize: 4,
  57. })
  58. if err != nil {
  59. c.ServerError("SearchRepositoryByName", err)
  60. return
  61. }
  62. c.Data["Total"] = count
  63. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  64. c.ServerError("RepositoryList.LoadAttributes", err)
  65. return
  66. }
  67. c.Data["Repos"] = repos
  68. c.Success(EXPLORE_HOME)
  69. }
  70. // ExploreRepos shows explore repositories page
  71. func ExploreRepos(c *context.Context) {
  72. c.Data["Title"] = c.Tr("explore")
  73. c.Data["PageIsExplore"] = true
  74. c.Data["PageIsExploreRepositories"] = true
  75. page := c.QueryInt("page")
  76. if page <= 0 {
  77. page = 1
  78. }
  79. keyword := c.Query("q")
  80. // Search a repository
  81. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  82. Keyword: keyword,
  83. UserID: c.UserID(),
  84. OrderBy: "updated_unix DESC",
  85. Page: page,
  86. PageSize: setting.UI.ExplorePagingNum,
  87. })
  88. if err != nil {
  89. c.ServerError("SearchRepositoryByName", err)
  90. return
  91. }
  92. c.Data["Keyword"] = keyword
  93. c.Data["Total"] = count
  94. c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  95. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  96. c.ServerError("RepositoryList.LoadAttributes", err)
  97. return
  98. }
  99. c.Data["Repos"] = repos
  100. c.Success(EXPLORE_REPOS)
  101. }
  102. // ExploreTrending shows trending repositories page
  103. func ExploreTrending(c *context.Context) {
  104. c.Data["Title"] = c.Tr("explore.trending")
  105. c.Data["PageIsTrending"] = true
  106. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  107. UserID: c.UserID(),
  108. OrderBy: "num_stars DESC",
  109. PageSize: 15,
  110. })
  111. if err != nil {
  112. c.ServerError("SearchRepositoryByName", err)
  113. return
  114. }
  115. c.Data["Total"] = count
  116. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  117. c.ServerError("RepositoryList.LoadAttributes", err)
  118. return
  119. }
  120. c.Data["Repos"] = repos
  121. c.Success(EXPLORE_TRENDING)
  122. }
  123. // UserSearchOptions is the structure of the options choosed by the user
  124. type UserSearchOptions struct {
  125. Type models.UserType
  126. Counter func() int64
  127. Ranger func(int, int) ([]*models.User, error)
  128. PageSize int
  129. OrderBy string
  130. TplName string
  131. }
  132. // RenderUserSearch renders user search
  133. func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
  134. page := c.QueryInt("page")
  135. if page <= 1 {
  136. page = 1
  137. }
  138. var (
  139. users []*models.User
  140. count int64
  141. err error
  142. )
  143. keyword := c.Query("q")
  144. if len(keyword) == 0 {
  145. users, err = opts.Ranger(page, opts.PageSize)
  146. if err != nil {
  147. c.ServerError("Ranger", err)
  148. return
  149. }
  150. count = opts.Counter()
  151. } else {
  152. users, count, err = models.SearchUserByName(&models.SearchUserOptions{
  153. Keyword: keyword,
  154. Type: opts.Type,
  155. OrderBy: opts.OrderBy,
  156. Page: page,
  157. PageSize: opts.PageSize,
  158. })
  159. if err != nil {
  160. c.ServerError("SearchUserByName", err)
  161. return
  162. }
  163. }
  164. c.Data["Keyword"] = keyword
  165. c.Data["Total"] = count
  166. c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  167. c.Data["Users"] = users
  168. c.Success(opts.TplName)
  169. }
  170. // ExploreUsers shows explore users page
  171. func ExploreUsers(c *context.Context) {
  172. c.Data["Title"] = c.Tr("explore")
  173. c.Data["PageIsExplore"] = true
  174. c.Data["PageIsExploreUsers"] = true
  175. RenderUserSearch(c, &UserSearchOptions{
  176. Type: models.USER_TYPE_INDIVIDUAL,
  177. Counter: models.CountUsers,
  178. Ranger: models.Users,
  179. PageSize: setting.UI.ExplorePagingNum,
  180. OrderBy: "updated_unix DESC",
  181. TplName: EXPLORE_USERS,
  182. })
  183. }
  184. //ExploreOrganizations shows explore organizations page
  185. func ExploreOrganizations(c *context.Context) {
  186. c.Data["Title"] = c.Tr("explore")
  187. c.Data["PageIsExplore"] = true
  188. c.Data["PageIsExploreOrganizations"] = true
  189. RenderUserSearch(c, &UserSearchOptions{
  190. Type: models.USER_TYPE_ORGANIZATION,
  191. Counter: models.CountOrganizations,
  192. Ranger: models.Organizations,
  193. PageSize: setting.UI.ExplorePagingNum,
  194. OrderBy: "updated_unix DESC",
  195. TplName: EXPLORE_ORGANIZATIONS,
  196. })
  197. }
  198. // NotFound renders 404 page if page not found
  199. func NotFound(c *context.Context) {
  200. c.Data["Title"] = "Page Not Found"
  201. c.NotFound()
  202. }