@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/search"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/user"
"code.gitea.io/gitea/routers/user"
@ -27,6 +28,8 @@ const (
tplExploreUsers base . TplName = "explore/users"
tplExploreUsers base . TplName = "explore/users"
// tplExploreOrganizations explore organizations page template
// tplExploreOrganizations explore organizations page template
tplExploreOrganizations base . TplName = "explore/organizations"
tplExploreOrganizations base . TplName = "explore/organizations"
// tplExploreCode explore code page template
tplExploreCode base . TplName = "explore/code"
)
)
// Home render home page
// Home render home page
@ -49,6 +52,7 @@ func Home(ctx *context.Context) {
}
}
ctx . Data [ "PageIsHome" ] = true
ctx . Data [ "PageIsHome" ] = true
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
ctx . HTML ( 200 , tplHome )
ctx . HTML ( 200 , tplHome )
}
}
@ -124,6 +128,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
ctx . Data [ "Total" ] = count
ctx . Data [ "Total" ] = count
ctx . Data [ "Page" ] = paginater . New ( int ( count ) , opts . PageSize , page , 5 )
ctx . Data [ "Page" ] = paginater . New ( int ( count ) , opts . PageSize , page , 5 )
ctx . Data [ "Repos" ] = repos
ctx . Data [ "Repos" ] = repos
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
ctx . HTML ( 200 , opts . TplName )
ctx . HTML ( 200 , opts . TplName )
}
}
@ -133,6 +138,7 @@ func ExploreRepos(ctx *context.Context) {
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExploreRepositories" ] = true
ctx . Data [ "PageIsExploreRepositories" ] = true
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
var ownerID int64
var ownerID int64
if ctx . User != nil && ! ctx . User . IsAdmin {
if ctx . User != nil && ! ctx . User . IsAdmin {
@ -194,6 +200,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
ctx . Data [ "Page" ] = paginater . New ( int ( count ) , opts . PageSize , opts . Page , 5 )
ctx . Data [ "Page" ] = paginater . New ( int ( count ) , opts . PageSize , opts . Page , 5 )
ctx . Data [ "Users" ] = users
ctx . Data [ "Users" ] = users
ctx . Data [ "ShowUserEmail" ] = setting . UI . ShowUserEmail
ctx . Data [ "ShowUserEmail" ] = setting . UI . ShowUserEmail
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
ctx . HTML ( 200 , tplName )
ctx . HTML ( 200 , tplName )
}
}
@ -203,6 +210,7 @@ func ExploreUsers(ctx *context.Context) {
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExploreUsers" ] = true
ctx . Data [ "PageIsExploreUsers" ] = true
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
RenderUserSearch ( ctx , & models . SearchUserOptions {
RenderUserSearch ( ctx , & models . SearchUserOptions {
Type : models . UserTypeIndividual ,
Type : models . UserTypeIndividual ,
@ -216,6 +224,7 @@ func ExploreOrganizations(ctx *context.Context) {
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExploreOrganizations" ] = true
ctx . Data [ "PageIsExploreOrganizations" ] = true
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
RenderUserSearch ( ctx , & models . SearchUserOptions {
RenderUserSearch ( ctx , & models . SearchUserOptions {
Type : models . UserTypeOrganization ,
Type : models . UserTypeOrganization ,
@ -223,6 +232,113 @@ func ExploreOrganizations(ctx *context.Context) {
} , tplExploreOrganizations )
} , tplExploreOrganizations )
}
}
// ExploreCode render explore code page
func ExploreCode ( ctx * context . Context ) {
if ! setting . Indexer . RepoIndexerEnabled {
ctx . Redirect ( "/explore" , 302 )
return
}
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExploreCode" ] = true
keyword := strings . TrimSpace ( ctx . Query ( "q" ) )
page := ctx . QueryInt ( "page" )
if page <= 0 {
page = 1
}
var (
repoIDs [ ] int64
err error
isAdmin bool
userID int64
)
if ctx . User != nil {
userID = ctx . User . ID
isAdmin = ctx . User . IsAdmin
}
// guest user or non-admin user
if ctx . User == nil || ! isAdmin {
repoIDs , err = models . FindUserAccessibleRepoIDs ( userID )
if err != nil {
ctx . ServerError ( "SearchResults" , err )
return
}
}
var (
total int
searchResults [ ] * search . Result
)
// if non-admin login user, we need check UnitTypeCode at first
if ctx . User != nil && len ( repoIDs ) > 0 {
repoMaps , err := models . GetRepositoriesMapByIDs ( repoIDs )
if err != nil {
ctx . ServerError ( "SearchResults" , err )
return
}
var rightRepoMap = make ( map [ int64 ] * models . Repository , len ( repoMaps ) )
repoIDs = make ( [ ] int64 , 0 , len ( repoMaps ) )
for id , repo := range repoMaps {
if repo . CheckUnitUser ( userID , isAdmin , models . UnitTypeCode ) {
rightRepoMap [ id ] = repo
repoIDs = append ( repoIDs , id )
}
}
ctx . Data [ "RepoMaps" ] = rightRepoMap
total , searchResults , err = search . PerformSearch ( repoIDs , keyword , page , setting . UI . RepoSearchPagingNum )
if err != nil {
ctx . ServerError ( "SearchResults" , err )
return
}
// if non-login user or isAdmin, no need to check UnitTypeCode
} else if ( ctx . User == nil && len ( repoIDs ) > 0 ) || isAdmin {
total , searchResults , err = search . PerformSearch ( repoIDs , keyword , page , setting . UI . RepoSearchPagingNum )
if err != nil {
ctx . ServerError ( "SearchResults" , err )
return
}
var loadRepoIDs = make ( [ ] int64 , 0 , len ( searchResults ) )
for _ , result := range searchResults {
var find bool
for _ , id := range loadRepoIDs {
if id == result . RepoID {
find = true
break
}
}
if ! find {
loadRepoIDs = append ( loadRepoIDs , result . RepoID )
}
}
repoMaps , err := models . GetRepositoriesMapByIDs ( loadRepoIDs )
if err != nil {
ctx . ServerError ( "SearchResults" , err )
return
}
ctx . Data [ "RepoMaps" ] = repoMaps
}
ctx . Data [ "Keyword" ] = keyword
pager := paginater . New ( total , setting . UI . RepoSearchPagingNum , page , 5 )
ctx . Data [ "Page" ] = pager
ctx . Data [ "SearchResults" ] = searchResults
ctx . Data [ "RequireHighlightJS" ] = true
ctx . Data [ "PageIsViewCode" ] = true
ctx . HTML ( 200 , tplExploreCode )
}
// NotFound render 404 page
// NotFound render 404 page
func NotFound ( ctx * context . Context ) {
func NotFound ( ctx * context . Context ) {
ctx . Data [ "Title" ] = "Page Not Found"
ctx . Data [ "Title" ] = "Page Not Found"