Make use of go≥1.21 slices package instead of hand-rolled loops

This makes the code a tad smaller, moderner,
and maybe even marginally faster, yay!
This commit is contained in:
jvoisin 2024-02-24 14:12:07 +01:00 committed by Frédéric Guillot
parent 2be5051b19
commit b48ad6dbfb
2 changed files with 9 additions and 20 deletions

View file

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"log/slog" "log/slog"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"time" "time"
@ -115,13 +116,9 @@ func ProcessFeedEntries(store *storage.Storage, feed *model.Feed, user *model.Us
func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool { func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool {
if feed.BlocklistRules != "" { if feed.BlocklistRules != "" {
var containsBlockedTag bool = false containsBlockedTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
for _, tag := range entry.Tags { return matchField(feed.BlocklistRules, tag)
if matchField(feed.BlocklistRules, tag) { })
containsBlockedTag = true
break
}
}
if matchField(feed.BlocklistRules, entry.URL) || matchField(feed.BlocklistRules, entry.Title) || matchField(feed.BlocklistRules, entry.Author) || containsBlockedTag { if matchField(feed.BlocklistRules, entry.URL) || matchField(feed.BlocklistRules, entry.Title) || matchField(feed.BlocklistRules, entry.Author) || containsBlockedTag {
slog.Debug("Blocking entry based on rule", slog.Debug("Blocking entry based on rule",
@ -140,13 +137,9 @@ func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool {
func isAllowedEntry(feed *model.Feed, entry *model.Entry) bool { func isAllowedEntry(feed *model.Feed, entry *model.Entry) bool {
if feed.KeeplistRules != "" { if feed.KeeplistRules != "" {
var containsAllowedTag bool = false containsAllowedTag := slices.ContainsFunc(entry.Tags, func(tag string) bool {
for _, tag := range entry.Tags { return matchField(feed.KeeplistRules, tag)
if matchField(feed.KeeplistRules, tag) { })
containsAllowedTag = true
break
}
}
if matchField(feed.KeeplistRules, entry.URL) || matchField(feed.KeeplistRules, entry.Title) || matchField(feed.KeeplistRules, entry.Author) || containsAllowedTag { if matchField(feed.KeeplistRules, entry.URL) || matchField(feed.KeeplistRules, entry.Title) || matchField(feed.KeeplistRules, entry.Author) || containsAllowedTag {
slog.Debug("Allow entry based on rule", slog.Debug("Allow entry based on rule",

View file

@ -8,6 +8,7 @@ import (
"html/template" "html/template"
"math" "math"
"net/mail" "net/mail"
"slices"
"strings" "strings"
"time" "time"
@ -72,12 +73,7 @@ func (f *funcMap) Map() template.FuncMap {
return link return link
}, },
"mustBeProxyfied": func(mediaType string) bool { "mustBeProxyfied": func(mediaType string) bool {
for _, t := range config.Opts.ProxyMediaTypes() { return slices.Contains(config.Opts.ProxyMediaTypes(), mediaType)
if t == mediaType {
return true
}
}
return false
}, },
"domain": func(websiteURL string) string { "domain": func(websiteURL string) string {
return urllib.Domain(websiteURL) return urllib.Domain(websiteURL)