123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- // Copyright 2014 The Gogs Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
- package routes
- import (
- "github.com/Unknwon/paginater"
- "notabug.org/hp/gogs/models"
- "notabug.org/hp/gogs/pkg/context"
- "notabug.org/hp/gogs/pkg/setting"
- "notabug.org/hp/gogs/routes/user"
- )
- const (
- HOME = "home"
- EXPLORE_REPOS = "explore/repos"
- EXPLORE_USERS = "explore/users"
- EXPLORE_ORGANIZATIONS = "explore/organizations"
- ABOUT = "about"
- TOR = "tor"
- HELP = "help"
- OUTAGES = "outages"
- TOS = "tos"
- FINGERPRINTS = "fingerprints"
- )
- func Home(c *context.Context) {
- if c.IsLogged {
- if !c.User.IsActive && setting.Service.RegisterEmailConfirm {
- c.Data["Title"] = c.Tr("auth.active_your_account")
- c.Success(user.ACTIVATE)
- } else {
- user.Dashboard(c)
- }
- return
- }
- // Check auto-login.
- uname := c.GetCookie(setting.CookieUserName)
- if len(uname) != 0 {
- c.Redirect(setting.AppSubURL + "/user/login")
- return
- }
- c.Data["PageIsHome"] = true
- c.Success(HOME)
- }
- func ExploreRepos(c *context.Context) {
- c.Data["Title"] = c.Tr("explore")
- c.Data["PageIsExplore"] = true
- c.Data["PageIsExploreRepositories"] = true
- page := c.QueryInt("page")
- if page <= 0 {
- page = 1
- }
- keyword := c.Query("q")
- repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
- Keyword: keyword,
- UserID: c.UserID(),
- OrderBy: "updated_unix DESC",
- Page: page,
- PageSize: setting.UI.ExplorePagingNum,
- NoMirrors: true,
- })
- if err != nil {
- c.Handle(500, "SearchRepositoryByName", err)
- return
- }
- c.Data["Keyword"] = keyword
- c.Data["Total"] = count
- c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
- if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
- c.Handle(500, "LoadAttributes", err)
- return
- }
- c.Data["Repos"] = repos
- c.HTML(200, EXPLORE_REPOS)
- }
- func ExploreMirrors(c *context.Context) {
- c.Data["Title"] = c.Tr("explore")
- c.Data["PageIsExplore"] = true
- c.Data["PageIsExploreMirrors"] = true
- page := c.QueryInt("page")
- if page <= 0 {
- page = 1
- }
- keyword := c.Query("q")
- repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
- Keyword: keyword,
- UserID: c.UserID(),
- OrderBy: "updated_unix DESC",
- Page: page,
- PageSize: setting.UI.ExplorePagingNum,
- NoMirrors: false,
- OnlyMirrors: true,
- })
- if err != nil {
- c.ServerError("SearchRepositoryByName", err)
- return
- }
- c.Data["Keyword"] = keyword
- c.Data["Total"] = count
- c.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
- if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
- c.ServerError("RepositoryList.LoadAttributes", err)
- return
- }
- c.Data["Repos"] = repos
- c.Success(EXPLORE_REPOS)
- }
- type UserSearchOptions struct {
- Type models.UserType
- Counter func() int64
- Ranger func(int, int) ([]*models.User, error)
- PageSize int
- OrderBy string
- TplName string
- }
- func RenderUserSearch(c *context.Context, opts *UserSearchOptions) {
- page := c.QueryInt("page")
- if page <= 1 {
- page = 1
- }
- var (
- users []*models.User
- count int64
- err error
- )
- keyword := c.Query("q")
- if len(keyword) == 0 {
- users, err = opts.Ranger(page, opts.PageSize)
- if err != nil {
- c.ServerError("Ranger", err)
- return
- }
- count = opts.Counter()
- } else {
- users, count, err = models.SearchUserByName(&models.SearchUserOptions{
- Keyword: keyword,
- Type: opts.Type,
- OrderBy: opts.OrderBy,
- Page: page,
- PageSize: opts.PageSize,
- })
- if err != nil {
- c.ServerError("SearchUserByName", err)
- return
- }
- }
- c.Data["Keyword"] = keyword
- c.Data["Total"] = count
- c.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
- c.Data["Users"] = users
- c.Success(opts.TplName)
- }
- func ExploreUsers(c *context.Context) {
- c.Data["Title"] = c.Tr("explore")
- c.Data["PageIsExplore"] = true
- c.Data["PageIsExploreUsers"] = true
- RenderUserSearch(c, &UserSearchOptions{
- Type: models.USER_TYPE_INDIVIDUAL,
- Counter: models.CountUsers,
- Ranger: models.Users,
- PageSize: setting.UI.ExplorePagingNum,
- OrderBy: "updated_unix DESC",
- TplName: EXPLORE_USERS,
- })
- }
- func ExploreOrganizations(c *context.Context) {
- c.Data["Title"] = c.Tr("explore")
- c.Data["PageIsExplore"] = true
- c.Data["PageIsExploreOrganizations"] = true
- RenderUserSearch(c, &UserSearchOptions{
- Type: models.USER_TYPE_ORGANIZATION,
- Counter: models.CountOrganizations,
- Ranger: models.Organizations,
- PageSize: setting.UI.ExplorePagingNum,
- OrderBy: "updated_unix DESC",
- TplName: EXPLORE_ORGANIZATIONS,
- })
- }
- func NotFound(c *context.Context) {
- c.Data["Title"] = "Page Not Found"
- c.NotFound()
- }
- func About(c *context.Context) {
- c.Data["Title"] = c.Tr("about")
- c.HTML(200, ABOUT)
- }
- func Tor(c *context.Context) {
- c.Data["Title"] = c.Tr("tor")
- c.HTML(200, TOR)
- }
- func Help(c *context.Context) {
- c.Data["Title"] = c.Tr("help")
- c.HTML(200, HELP)
- }
- func Outages(c *context.Context) {
- c.Data["Title"] = c.Tr("outages")
- c.HTML(200, OUTAGES)
- }
- func Tos(c *context.Context) {
- c.Data["Title"] = c.Tr("tos")
- c.HTML(200, TOS)
- }
- func Fingerprints(c *context.Context) {
- c.Data["Title"] = c.Tr("fingerprints")
- c.HTML(200, FINGERPRINTS)
- }
|