add filter words and links
This commit is contained in:
parent
877ec0dd89
commit
61737b124f
@ -9,7 +9,9 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceDonat struct {
|
type ServiceDonat struct {
|
||||||
@ -682,17 +684,52 @@ func (donatService *ServiceDonat) InitNewStreamer(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Добавляем функцию замены слов
|
||||||
|
func replaceFilteredWords(text string, words []string) string {
|
||||||
|
if len(words) == 0 {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
// Экранируем специальные символы в словах
|
||||||
|
escapedWords := make([]string, len(words))
|
||||||
|
for i, word := range words {
|
||||||
|
escapedWords[i] = regexp.QuoteMeta(word)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Создаем регулярное выражение для поиска целых слов без учета регистра
|
||||||
|
pattern := `(?i)\b(` + strings.Join(escapedWords, "|") + `)\b`
|
||||||
|
re := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
// Заменяем найденные слова на звёздочки
|
||||||
|
return re.ReplaceAllStringFunc(text, func(match string) string {
|
||||||
|
return strings.Repeat("*", len(match))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (donatService *ServiceDonat) GetPlayingDonat(
|
func (donatService *ServiceDonat) GetPlayingDonat(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
streamerID int,
|
streamerID int,
|
||||||
) (model.PlayingDonat, error) {
|
) (model.PlayingDonat, error) {
|
||||||
playingDonat, err := donatService.donatRepo.GetPlayingDonat(ctx, streamerID)
|
playingDonat, err := donatService.donatRepo.GetPlayingDonat(ctx, streamerID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to get playing donat", "error", err)
|
slog.Error("Failed to get playing donat", "error", err)
|
||||||
return model.PlayingDonat{}, err
|
return model.PlayingDonat{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filteredSettings, err := donatService.GetFiltersSettings(ctx, streamerID)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to get filtered words", "error", err)
|
||||||
|
return model.PlayingDonat{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(filteredSettings.FilteredWords) > 0 {
|
||||||
|
playingDonat.Text = donatService.replaceFilteredWords(playingDonat.Text, filteredSettings.FilteredWords)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !filteredSettings.EnableLinks {
|
||||||
|
playingDonat.Text = donatService.replaceLinks(playingDonat.Text)
|
||||||
|
}
|
||||||
|
|
||||||
if playingDonat.OrderID == "" {
|
if playingDonat.OrderID == "" {
|
||||||
return playingDonat, nil
|
return playingDonat, nil
|
||||||
}
|
}
|
||||||
@ -744,3 +781,28 @@ func (donatService *ServiceDonat) UpdateAvatarStreamer(
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (donatService *ServiceDonat) replaceFilteredWords(text string, words []string) string {
|
||||||
|
if len(words) == 0 {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
escapedWords := make([]string, len(words))
|
||||||
|
for i, word := range words {
|
||||||
|
escapedWords[i] = regexp.QuoteMeta(word)
|
||||||
|
}
|
||||||
|
|
||||||
|
pattern := `(?i)\b(` + strings.Join(escapedWords, "|") + `)\b`
|
||||||
|
re := regexp.MustCompile(pattern)
|
||||||
|
|
||||||
|
return re.ReplaceAllStringFunc(text, func(match string) string {
|
||||||
|
return strings.Repeat("*", len(match))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (donatService *ServiceDonat) replaceLinks(text string) string {
|
||||||
|
re := regexp.MustCompile(`(?i)\bhttps?://\S+\b`)
|
||||||
|
return re.ReplaceAllStringFunc(text, func(match string) string {
|
||||||
|
return strings.Repeat("*", len(match))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user