mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-10 23:12:28 +08:00
refactor: rule package
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/slice"
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
||||||
"github.com/krau/SaveAny-Bot/database"
|
"github.com/krau/SaveAny-Bot/database"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/rule"
|
"github.com/krau/SaveAny-Bot/pkg/rule"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
|
func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/krau/SaveAny-Bot/database"
|
"github.com/krau/SaveAny-Bot/database"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/consts"
|
|
||||||
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
|
|
||||||
"github.com/krau/SaveAny-Bot/pkg/rule"
|
"github.com/krau/SaveAny-Bot/pkg/rule"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/tfile"
|
"github.com/krau/SaveAny-Bot/pkg/tfile"
|
||||||
)
|
)
|
||||||
@@ -37,7 +35,7 @@ func (m matchedStorName) String() string {
|
|||||||
|
|
||||||
// can we use this storage name directly?
|
// can we use this storage name directly?
|
||||||
func (m matchedStorName) IsUsable() bool {
|
func (m matchedStorName) IsUsable() bool {
|
||||||
return m != "" && m != consts.RuleStorNameChosen
|
return m != "" && m != rule.RuleStorNameChosen
|
||||||
}
|
}
|
||||||
|
|
||||||
type MatchedDirPath string
|
type MatchedDirPath string
|
||||||
@@ -47,7 +45,7 @@ func (m MatchedDirPath) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m MatchedDirPath) NeedNewForAlbum() bool {
|
func (m MatchedDirPath) NeedNewForAlbum() bool {
|
||||||
return m != "" && m == consts.RuleDirPathNewForAlbum
|
return m != "" && m == rule.RuleDirPathNewForAlbum
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (matchedStorageName matchedStorName, dirPath MatchedDirPath) {
|
func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (matchedStorageName matchedStorName, dirPath MatchedDirPath) {
|
||||||
@@ -57,7 +55,7 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
|
|||||||
logger := log.FromContext(ctx)
|
logger := log.FromContext(ctx)
|
||||||
for _, ur := range rules {
|
for _, ur := range rules {
|
||||||
switch ur.Type {
|
switch ur.Type {
|
||||||
case ruleenum.FileNameRegex.String():
|
case rule.FileNameRegex.String():
|
||||||
ru, err := rule.NewRuleFileNameRegex(ur.StorageName, ur.DirPath, ur.Data)
|
ru, err := rule.NewRuleFileNameRegex(ur.StorageName, ur.DirPath, ur.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Failed to create rule: %s", err)
|
logger.Errorf("Failed to create rule: %s", err)
|
||||||
@@ -72,7 +70,7 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
|
|||||||
dirPath = MatchedDirPath(ru.StoragePath())
|
dirPath = MatchedDirPath(ru.StoragePath())
|
||||||
matchedStorageName = matchedStorName(ru.StorageName())
|
matchedStorageName = matchedStorName(ru.StorageName())
|
||||||
}
|
}
|
||||||
case ruleenum.MessageRegex.String():
|
case rule.MessageRegex.String():
|
||||||
ru, err := rule.NewRuleMessageRegex(ur.StorageName, ur.DirPath, ur.Data)
|
ru, err := rule.NewRuleMessageRegex(ur.StorageName, ur.DirPath, ur.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Failed to create rule: %s", err)
|
logger.Errorf("Failed to create rule: %s", err)
|
||||||
@@ -87,7 +85,7 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
|
|||||||
dirPath = MatchedDirPath(ru.StoragePath())
|
dirPath = MatchedDirPath(ru.StoragePath())
|
||||||
matchedStorageName = matchedStorName(ru.StorageName())
|
matchedStorageName = matchedStorName(ru.StorageName())
|
||||||
}
|
}
|
||||||
case ruleenum.IsAlbum.String():
|
case rule.IsAlbum.String():
|
||||||
matchAlbum, err := convertor.ToBool(ur.Data)
|
matchAlbum, err := convertor.ToBool(ur.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
matchAlbum = false
|
matchAlbum = false
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package consts
|
package rule
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RuleStorNameChosen = "CHOSEN"
|
RuleStorNameChosen = "CHOSEN"
|
||||||
@@ -3,7 +3,6 @@ package rule
|
|||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
|
|
||||||
"github.com/krau/SaveAny-Bot/pkg/tfile"
|
"github.com/krau/SaveAny-Bot/pkg/tfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,8 +13,8 @@ type RuleFileNameRegex struct {
|
|||||||
|
|
||||||
var _ RuleClass[tfile.TGFile] = (*RuleFileNameRegex)(nil)
|
var _ RuleClass[tfile.TGFile] = (*RuleFileNameRegex)(nil)
|
||||||
|
|
||||||
func (r RuleFileNameRegex) Type() ruleenum.RuleType {
|
func (r RuleFileNameRegex) Type() RuleType {
|
||||||
return ruleenum.FileNameRegex
|
return FileNameRegex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RuleFileNameRegex) Match(input tfile.TGFile) (bool, error) {
|
func (r RuleFileNameRegex) Match(input tfile.TGFile) (bool, error) {
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
package rule
|
package rule
|
||||||
|
|
||||||
import (
|
|
||||||
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ RuleClass[bool] = (*RuleMediaType)(nil)
|
var _ RuleClass[bool] = (*RuleMediaType)(nil)
|
||||||
|
|
||||||
type RuleMediaType struct {
|
type RuleMediaType struct {
|
||||||
@@ -11,8 +7,8 @@ type RuleMediaType struct {
|
|||||||
matchAlbum bool
|
matchAlbum bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RuleMediaType) Type() ruleenum.RuleType {
|
func (r RuleMediaType) Type() RuleType {
|
||||||
return ruleenum.IsAlbum
|
return IsAlbum
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RuleMediaType) Match(input bool) (bool, error) {
|
func (r RuleMediaType) Match(input bool) (bool, error) {
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package rule
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ RuleClass[string] = (*RuleMessageRegex)(nil)
|
var _ RuleClass[string] = (*RuleMessageRegex)(nil)
|
||||||
@@ -13,8 +11,8 @@ type RuleMessageRegex struct {
|
|||||||
regex *regexp.Regexp
|
regex *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RuleMessageRegex) Type() ruleenum.RuleType {
|
func (r RuleMessageRegex) Type() RuleType {
|
||||||
return ruleenum.MessageRegex
|
return MessageRegex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RuleMessageRegex) Match(input string) (bool, error) {
|
func (r RuleMessageRegex) Match(input string) (bool, error) {
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package rule
|
package rule
|
||||||
|
|
||||||
import (
|
|
||||||
ruleenum "github.com/krau/SaveAny-Bot/pkg/enums/rule"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RuleClass[InputType any] interface {
|
type RuleClass[InputType any] interface {
|
||||||
Type() ruleenum.RuleType
|
Type() RuleType
|
||||||
Match(input InputType) (bool, error)
|
Match(input InputType) (bool, error)
|
||||||
StorageName() string
|
StorageName() string
|
||||||
StoragePath() string
|
StoragePath() string
|
||||||
|
|||||||
Reference in New Issue
Block a user