2021-06-09 07:33:54 +08:00
// Copyright 2021 The Gitea 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 explore
import (
"net/http"
"code.gitea.io/gitea/models"
2021-09-24 19:32:56 +08:00
"code.gitea.io/gitea/models/db"
2021-12-10 09:27:50 +08:00
repo_model "code.gitea.io/gitea/models/repo"
2021-06-09 07:33:54 +08:00
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
)
const (
// tplExploreRepos explore repositories page template
tplExploreRepos base . TplName = "explore/repos"
)
// RepoSearchOptions when calling search repositories
type RepoSearchOptions struct {
OwnerID int64
Private bool
Restricted bool
PageSize int
TplName base . TplName
}
// RenderRepoSearch render repositories search page
func RenderRepoSearch ( ctx * context . Context , opts * RepoSearchOptions ) {
2021-07-29 09:42:15 +08:00
page := ctx . FormInt ( "page" )
2021-06-09 07:33:54 +08:00
if page <= 0 {
page = 1
}
var (
2021-12-10 09:27:50 +08:00
repos [ ] * repo_model . Repository
2021-06-09 07:33:54 +08:00
count int64
err error
2021-11-24 17:49:20 +08:00
orderBy db . SearchOrderBy
2021-06-09 07:33:54 +08:00
)
2021-08-11 02:31:13 +02:00
ctx . Data [ "SortType" ] = ctx . FormString ( "sort" )
switch ctx . FormString ( "sort" ) {
2021-06-09 07:33:54 +08:00
case "newest" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByNewest
2021-06-09 07:33:54 +08:00
case "oldest" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByOldest
2021-06-09 07:33:54 +08:00
case "recentupdate" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByRecentUpdated
2021-06-09 07:33:54 +08:00
case "leastupdate" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByLeastUpdated
2021-06-09 07:33:54 +08:00
case "reversealphabetically" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByAlphabeticallyReverse
2021-06-09 07:33:54 +08:00
case "alphabetically" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByAlphabetically
2021-06-09 07:33:54 +08:00
case "reversesize" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderBySizeReverse
2021-06-09 07:33:54 +08:00
case "size" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderBySize
2021-06-09 07:33:54 +08:00
case "moststars" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByStarsReverse
2021-06-09 07:33:54 +08:00
case "feweststars" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByStars
2021-06-09 07:33:54 +08:00
case "mostforks" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByForksReverse
2021-06-09 07:33:54 +08:00
case "fewestforks" :
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByForks
2021-06-09 07:33:54 +08:00
default :
ctx . Data [ "SortType" ] = "recentupdate"
2021-11-24 17:49:20 +08:00
orderBy = db . SearchOrderByRecentUpdated
2021-06-09 07:33:54 +08:00
}
2021-08-11 17:08:52 +02:00
keyword := ctx . FormTrim ( "q" )
2021-07-29 09:42:15 +08:00
topicOnly := ctx . FormBool ( "topic" )
2021-06-09 07:33:54 +08:00
ctx . Data [ "TopicOnly" ] = topicOnly
2022-01-28 13:29:04 +02:00
language := ctx . FormTrim ( "language" )
ctx . Data [ "Language" ] = language
2021-06-09 07:33:54 +08:00
repos , count , err = models . SearchRepository ( & models . SearchRepoOptions {
2021-09-24 19:32:56 +08:00
ListOptions : db . ListOptions {
2021-06-09 07:33:54 +08:00
Page : page ,
PageSize : opts . PageSize ,
} ,
Actor : ctx . User ,
OrderBy : orderBy ,
Private : opts . Private ,
Keyword : keyword ,
OwnerID : opts . OwnerID ,
AllPublic : true ,
AllLimited : true ,
TopicOnly : topicOnly ,
2022-01-28 13:29:04 +02:00
Language : language ,
2021-06-09 07:33:54 +08:00
IncludeDescription : setting . UI . SearchRepoDescription ,
} )
if err != nil {
ctx . ServerError ( "SearchRepository" , err )
return
}
ctx . Data [ "Keyword" ] = keyword
ctx . Data [ "Total" ] = count
ctx . Data [ "Repos" ] = repos
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
pager := context . NewPagination ( int ( count ) , opts . PageSize , page , 5 )
pager . SetDefaultParams ( ctx )
pager . AddParam ( ctx , "topic" , "TopicOnly" )
2022-01-28 13:29:04 +02:00
pager . AddParam ( ctx , "language" , "Language" )
2021-06-09 07:33:54 +08:00
ctx . Data [ "Page" ] = pager
ctx . HTML ( http . StatusOK , opts . TplName )
}
// Repos render explore repositories page
func Repos ( ctx * context . Context ) {
ctx . Data [ "UsersIsDisabled" ] = setting . Service . Explore . DisableUsersPage
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExploreRepositories" ] = true
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
var ownerID int64
if ctx . User != nil && ! ctx . User . IsAdmin {
ownerID = ctx . User . ID
}
RenderRepoSearch ( ctx , & RepoSearchOptions {
PageSize : setting . UI . ExplorePagingNum ,
OwnerID : ownerID ,
Private : ctx . User != nil ,
TplName : tplExploreRepos ,
} )
}