mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-16 18:37:51 +01:00
webapi: Search: Add support for additional sorting parameters (Size ASC/DESC, Shared by count ASC/DESC).
This commit is contained in:
@@ -186,3 +186,25 @@ func apiBlockchainSelfDeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
// ---- metadata functions ----
|
||||
|
||||
// GetMetadata returns the specified metadata or nil if not available.
|
||||
func (file *apiFile) GetMetadata(Type uint16) (info *apiFileMetadata) {
|
||||
for n := range file.Metadata {
|
||||
if file.Metadata[n].Type == Type {
|
||||
return &file.Metadata[n]
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetNumber returns the data as number. 0 if not available.
|
||||
func (info *apiFileMetadata) GetNumber() uint64 {
|
||||
if info == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return info.Number
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@@ -250,22 +251,38 @@ func (job *SearchJob) isFileFiltered(file *apiFile) bool {
|
||||
|
||||
// SortItems sorts a list of files. It returns a sorted list. 0 = no sorting, 1 = Relevance ASC, 2 = Relevance DESC, 3 = Date ASC, 4 = Date DESC, 5 = Name ASC, 6 = Name DESC
|
||||
func SortItems(files []*apiFile, Sort int) (sorted []*apiFile) {
|
||||
if Sort == 0 {
|
||||
return files
|
||||
}
|
||||
|
||||
// sort!
|
||||
switch Sort {
|
||||
case 1: // Relevance Score ASC
|
||||
case SortRelevanceAsc:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[i].Date.Before(files[j].Date) }) // first as date for secondary sorting
|
||||
//sort.SliceStable(files, func(i, j int) bool { return files[i].Score < files[j].Score }) // TODO
|
||||
case 2: // Relevance Score DESC
|
||||
case SortRelevanceDec:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[j].Date.Before(files[i].Date) }) // first as date for secondary sorting
|
||||
//sort.SliceStable(files, func(i, j int) bool { return files[i].Score > files[j].Score }) // TODO
|
||||
case 3: // Date ASC
|
||||
|
||||
case SortDateAsc:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[i].Date.Before(files[j].Date) })
|
||||
case 4: // Date DESC
|
||||
case SortDateDesc:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[j].Date.Before(files[i].Date) })
|
||||
|
||||
case SortNameAsc:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[i].Name < files[j].Name })
|
||||
case SortNameDesc:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[i].Name > files[j].Name })
|
||||
|
||||
case SortSizeAsc:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[i].Size < files[j].Size })
|
||||
case SortSizeDesc:
|
||||
sort.SliceStable(files, func(i, j int) bool { return files[i].Size > files[j].Size })
|
||||
|
||||
case SortSharedByCountAsc:
|
||||
sort.SliceStable(files, func(i, j int) bool {
|
||||
return files[i].GetMetadata(core.TagSharedByCount).Number < files[j].GetMetadata(core.TagSharedByCount).Number
|
||||
})
|
||||
case SortSharedByCountDesc:
|
||||
sort.SliceStable(files, func(i, j int) bool {
|
||||
return files[i].GetMetadata(core.TagSharedByCount).Number > files[j].GetMetadata(core.TagSharedByCount).Number
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return files
|
||||
|
||||
@@ -38,13 +38,17 @@ type SearchRequest struct {
|
||||
|
||||
// Sort orders
|
||||
const (
|
||||
SortNone = 0 // No sorting. Results are returned as they come in.
|
||||
SortRelevanceAsc = 1 // Least relevant results first.
|
||||
SortRelevanceDec = 2 // Most relevant results first.
|
||||
SortDateAsc = 3 // Oldest first.
|
||||
SortDateDesc = 4 // Newest first.
|
||||
SortNameAsc = 5 // File name ascending. The folder name is not used for sorting.
|
||||
SortNameDesc = 6 // File name descending. The folder name is not used for sorting.
|
||||
SortNone = 0 // No sorting. Results are returned as they come in.
|
||||
SortRelevanceAsc = 1 // Least relevant results first.
|
||||
SortRelevanceDec = 2 // Most relevant results first.
|
||||
SortDateAsc = 3 // Oldest first.
|
||||
SortDateDesc = 4 // Newest first.
|
||||
SortNameAsc = 5 // File name ascending. The folder name is not used for sorting.
|
||||
SortNameDesc = 6 // File name descending. The folder name is not used for sorting.
|
||||
SortSizeAsc = 7 // File size ascending. Smallest files first.
|
||||
SortSizeDesc = 8 // File size descending. Largest files first.
|
||||
SortSharedByCountAsc = 9 // Shared by count ascending. Files that are shared by the least count of peers first.
|
||||
SortSharedByCountDesc = 10 // Shared by count descending. Files that are shared by the most count of peers first.
|
||||
)
|
||||
|
||||
// SearchRequestResponse is the result to the initial search request
|
||||
|
||||
@@ -508,6 +508,10 @@ These are the available sort options:
|
||||
| 4 | SortDateDesc | Newest first. |
|
||||
| 5 | SortNameAsc | File name ascending. The folder name is not used for sorting. |
|
||||
| 6 | SortNameDesc | File name descending. The folder name is not used for sorting. |
|
||||
| 7 | SortSizeAsc | File size ascending. Smallest files first. |
|
||||
| 8 | SortSizeDesc | File size descending. Largest files first. |
|
||||
| 9 | SortSharedByCountAsc | Shared by count ascending. Files that are shared by the least count of peers first. |
|
||||
| 10 | SortSharedByCountDesc | Shared by count descending. Files that are shared by the most count of peers first. |
|
||||
|
||||
The following filters are supported:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user