home.go 5.0 KB

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