2021-06-09 02:33:54 +03:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2021-06-09 02:33:54 +03:00
package explore
import (
"net/http"
2021-12-10 04:27:50 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-06-09 02:33:54 +03:00
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
code_indexer "code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/setting"
)
const (
// tplExploreCode explore code page template
tplExploreCode base . TplName = "explore/code"
)
// Code render explore code page
func Code ( ctx * context . Context ) {
if ! setting . Indexer . RepoIndexerEnabled {
2022-03-23 07:54:07 +03:00
ctx . Redirect ( setting . AppSubURL + "/explore" )
2021-06-09 02:33:54 +03:00
return
}
ctx . Data [ "UsersIsDisabled" ] = setting . Service . Explore . DisableUsersPage
ctx . Data [ "IsRepoIndexerEnabled" ] = setting . Indexer . RepoIndexerEnabled
ctx . Data [ "Title" ] = ctx . Tr ( "explore" )
ctx . Data [ "PageIsExplore" ] = true
ctx . Data [ "PageIsExploreCode" ] = true
2021-08-11 18:08:52 +03:00
language := ctx . FormTrim ( "l" )
keyword := ctx . FormTrim ( "q" )
2022-10-11 02:12:03 +03:00
queryType := ctx . FormTrim ( "t" )
isMatch := queryType == "match"
ctx . Data [ "Keyword" ] = keyword
ctx . Data [ "Language" ] = language
ctx . Data [ "queryType" ] = queryType
ctx . Data [ "PageIsViewCode" ] = true
if keyword == "" {
ctx . HTML ( http . StatusOK , tplExploreCode )
return
}
2021-07-29 04:42:15 +03:00
page := ctx . FormInt ( "page" )
2021-06-09 02:33:54 +03:00
if page <= 0 {
page = 1
}
2022-10-11 02:12:03 +03:00
var (
repoIDs [ ] int64
err error
isAdmin bool
)
if ctx . Doer != nil {
isAdmin = ctx . Doer . IsAdmin
}
2021-06-09 02:33:54 +03:00
2022-10-11 02:12:03 +03:00
// guest user or non-admin user
if ctx . Doer == nil || ! isAdmin {
repoIDs , err = repo_model . FindUserCodeAccessibleRepoIDs ( ctx , ctx . Doer )
if err != nil {
ctx . ServerError ( "FindUserCodeAccessibleRepoIDs" , err )
return
2021-06-09 02:33:54 +03:00
}
2022-10-11 02:12:03 +03:00
}
2021-06-09 02:33:54 +03:00
2022-10-11 02:12:03 +03:00
var (
total int
searchResults [ ] * code_indexer . Result
searchResultLanguages [ ] * code_indexer . SearchResultLanguages
)
if ( len ( repoIDs ) > 0 ) || isAdmin {
total , searchResults , searchResultLanguages , err = code_indexer . PerformSearch ( ctx , repoIDs , language , keyword , page , setting . UI . RepoSearchPagingNum , isMatch )
if err != nil {
if code_indexer . IsAvailable ( ) {
2022-01-27 11:30:51 +03:00
ctx . ServerError ( "SearchResults" , err )
return
}
2022-10-11 02:12:03 +03:00
ctx . Data [ "CodeIndexerUnavailable" ] = true
} else {
ctx . Data [ "CodeIndexerUnavailable" ] = ! code_indexer . IsAvailable ( )
2021-06-09 02:33:54 +03:00
}
2022-06-16 02:24:10 +03:00
2022-10-11 02:12:03 +03:00
loadRepoIDs := make ( [ ] int64 , 0 , len ( searchResults ) )
for _ , result := range searchResults {
var find bool
for _ , id := range loadRepoIDs {
if id == result . RepoID {
find = true
break
2022-06-16 02:24:10 +03:00
}
2022-01-27 11:30:51 +03:00
}
2022-10-11 02:12:03 +03:00
if ! find {
loadRepoIDs = append ( loadRepoIDs , result . RepoID )
2021-06-09 02:33:54 +03:00
}
2022-10-11 02:12:03 +03:00
}
2021-06-09 02:33:54 +03:00
2022-10-11 02:12:03 +03:00
repoMaps , err := repo_model . GetRepositoriesMapByIDs ( loadRepoIDs )
if err != nil {
ctx . ServerError ( "GetRepositoriesMapByIDs" , err )
return
2021-06-09 02:33:54 +03:00
}
2022-10-11 02:12:03 +03:00
ctx . Data [ "RepoMaps" ] = repoMaps
2022-10-22 12:23:20 +03:00
if len ( loadRepoIDs ) != len ( repoMaps ) {
// Remove deleted repos from search results
cleanedSearchResults := make ( [ ] * code_indexer . Result , 0 , len ( repoMaps ) )
for _ , sr := range searchResults {
if _ , found := repoMaps [ sr . RepoID ] ; found {
cleanedSearchResults = append ( cleanedSearchResults , sr )
}
}
searchResults = cleanedSearchResults
}
2021-06-09 02:33:54 +03:00
}
2022-10-11 02:12:03 +03:00
ctx . Data [ "SearchResults" ] = searchResults
ctx . Data [ "SearchResultLanguages" ] = searchResultLanguages
pager := context . NewPagination ( total , setting . UI . RepoSearchPagingNum , page , 5 )
pager . SetDefaultParams ( ctx )
pager . AddParam ( ctx , "l" , "Language" )
ctx . Data [ "Page" ] = pager
2021-06-09 02:33:54 +03:00
ctx . HTML ( http . StatusOK , tplExploreCode )
}