Added arn to the main repository
This commit is contained in:
41
arn/search/Posts.go
Normal file
41
arn/search/Posts.go
Normal file
@ -0,0 +1,41 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/animenotifier/notify.moe/arn"
|
||||
"github.com/animenotifier/notify.moe/arn/stringutils"
|
||||
)
|
||||
|
||||
// Posts searches all posts.
|
||||
func Posts(originalTerm string, maxLength int) []*arn.Post {
|
||||
term := strings.ToLower(stringutils.RemoveSpecialCharacters(originalTerm))
|
||||
results := make([]*arn.Post, 0, maxLength)
|
||||
|
||||
for post := range arn.StreamPosts() {
|
||||
if post.ID == originalTerm {
|
||||
return []*arn.Post{post}
|
||||
}
|
||||
|
||||
text := strings.ToLower(post.Text)
|
||||
|
||||
if !strings.Contains(text, term) {
|
||||
continue
|
||||
}
|
||||
|
||||
results = append(results, post)
|
||||
}
|
||||
|
||||
// Sort
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
return results[i].Created > results[j].Created
|
||||
})
|
||||
|
||||
// Limit
|
||||
if len(results) >= maxLength {
|
||||
results = results[:maxLength]
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
Reference in New Issue
Block a user