home.go 5.4 KB

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