home.go 5.6 KB

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