home.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. page := c.QueryInt("page")
  54. if page <= 0 {
  55. page = 1
  56. }
  57. keyword := c.Query("q")
  58. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  59. Keyword: keyword,
  60. UserID: c.UserID(),
  61. OrderBy: "updated_unix DESC",
  62. Page: page,
  63. PageSize: setting.UI.ExplorePagingNum,
  64. })
  65. if err != nil {
  66. c.ServerError("SearchRepositoryByName", err)
  67. return
  68. }
  69. c.Data["Keyword"] = keyword
  70. c.Data["Total"] = count
  71. c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  72. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  73. c.ServerError("RepositoryList.LoadAttributes", err)
  74. return
  75. }
  76. c.Data["Repos"] = repos
  77. c.Success(EXPLORE_HOME)
  78. }
  79. // ExploreRepos shows explore repositories page
  80. func ExploreRepos(c *context.Context) {
  81. c.Data["Title"] = c.Tr("explore")
  82. c.Data["PageIsExplore"] = true
  83. c.Data["PageIsExploreRepositories"] = true
  84. page := c.QueryInt("page")
  85. if page <= 0 {
  86. page = 1
  87. }
  88. keyword := c.Query("q")
  89. // Search a repository
  90. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  91. Keyword: keyword,
  92. UserID: c.UserID(),
  93. OrderBy: "updated_unix DESC",
  94. Page: page,
  95. PageSize: setting.UI.ExplorePagingNum,
  96. })
  97. if err != nil {
  98. c.ServerError("SearchRepositoryByName", err)
  99. return
  100. }
  101. c.Data["Keyword"] = keyword
  102. c.Data["Total"] = count
  103. c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
  104. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  105. c.ServerError("RepositoryList.LoadAttributes", err)
  106. return
  107. }
  108. c.Data["Repos"] = repos
  109. c.Success(EXPLORE_REPOS)
  110. }
  111. // ExploreTrending shows trending repositories page
  112. func ExploreTrending(c *context.Context) {
  113. c.Data["Title"] = c.Tr("explore.trending")
  114. c.Data["PageIsTrending"] = true
  115. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  116. UserID: c.UserID(),
  117. OrderBy: "num_stars DESC",
  118. PageSize: 15,
  119. })
  120. if err != nil {
  121. c.ServerError("SearchRepositoryByName", err)
  122. return
  123. }
  124. c.Data["Total"] = count
  125. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  126. c.ServerError("RepositoryList.LoadAttributes", err)
  127. return
  128. }
  129. c.Data["Repos"] = repos
  130. c.Success(EXPLORE_TRENDING)
  131. }
  132. // UserSearchOptions is the structure of the options choosed by the user
  133. type UserSearchOptions struct {
  134. Type models.UserType
  135. Counter func() int64
  136. Ranger func(int, int) ([]*models.User, error)
  137. PageSize int
  138. OrderBy string
  139. TplName string
  140. }
  141. // RenderUserSearch renders user search
  142. func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
  143. page := c.QueryInt("page")
  144. if page <= 1 {
  145. page = 1
  146. }
  147. var (
  148. users []*models.User
  149. count int64
  150. err error
  151. )
  152. keyword := c.Query("q")
  153. if len(keyword) == 0 {
  154. users, err = opts.Ranger(page, opts.PageSize)
  155. if err != nil {
  156. c.ServerError("Ranger", err)
  157. return
  158. }
  159. count = opts.Counter()
  160. } else {
  161. users, count, err = models.SearchUserByName(&models.SearchUserOptions{
  162. Keyword: keyword,
  163. Type: opts.Type,
  164. OrderBy: opts.OrderBy,
  165. Page: page,
  166. PageSize: opts.PageSize,
  167. })
  168. if err != nil {
  169. c.ServerError("SearchUserByName", err)
  170. return
  171. }
  172. }
  173. c.Data["Keyword"] = keyword
  174. c.Data["Total"] = count
  175. c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
  176. c.Data["Users"] = users
  177. c.Success(opts.TplName)
  178. }
  179. // ExploreUsers shows explore users page
  180. func ExploreUsers(c *context.Context) {
  181. c.Data["Title"] = c.Tr("explore")
  182. c.Data["PageIsExplore"] = true
  183. c.Data["PageIsExploreUsers"] = true
  184. RenderUserSearch(c, &UserSearchOptions{
  185. Type: models.USER_TYPE_INDIVIDUAL,
  186. Counter: models.CountUsers,
  187. Ranger: models.Users,
  188. PageSize: setting.UI.ExplorePagingNum,
  189. OrderBy: "updated_unix DESC",
  190. TplName: EXPLORE_USERS,
  191. })
  192. }
  193. //ExploreOrganizations shows explore organizations page
  194. func ExploreOrganizations(c *context.Context) {
  195. c.Data["Title"] = c.Tr("explore")
  196. c.Data["PageIsExplore"] = true
  197. c.Data["PageIsExploreOrganizations"] = true
  198. RenderUserSearch(c, &UserSearchOptions{
  199. Type: models.USER_TYPE_ORGANIZATION,
  200. Counter: models.CountOrganizations,
  201. Ranger: models.Organizations,
  202. PageSize: setting.UI.ExplorePagingNum,
  203. OrderBy: "updated_unix DESC",
  204. TplName: EXPLORE_ORGANIZATIONS,
  205. })
  206. }
  207. // NotFound renders 404 page if page not found
  208. func NotFound(c *context.Context) {
  209. c.Data["Title"] = "Page Not Found"
  210. c.NotFound()
  211. }