idecnode/pkg/idec/blacklist.go

66 lines
1.3 KiB
Go
Raw Permalink Normal View History

package idec
import (
"log"
"gitrepo.ru/neonxp/idecnode/pkg/model"
"go.etcd.io/bbolt"
)
const blacklistMessage = "<Удалено по черному списку>"
func (i *IDEC) GetBlacklist() ([]string, error) {
list := []string{}
return list, i.db.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(blacklist))
if bucket == nil {
return nil
}
return bucket.ForEach(func(k, _ []byte) error {
list = append(list, string(k))
return nil
})
})
}
func (i *IDEC) MergeBlacklist(list []string) error {
return i.db.Update(func(tx *bbolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(blacklist))
if err != nil {
return err
}
messages, err := tx.CreateBucketIfNotExists([]byte(msgBucket))
if err != nil {
return err
}
for _, k := range list {
if k == "" {
continue
}
if err := bucket.Put([]byte(k), []byte{}); err != nil {
return err
}
msgBytes := messages.Get([]byte(k))
if msgBytes == nil {
continue
}
msg, err := model.MessageFromBundle(k, string(msgBytes))
if err != nil {
log.Println(err)
continue
}
msg.Subject = blacklistMessage
msg.Message = blacklistMessage
if err := messages.Put([]byte(k), []byte(msg.Bundle())); err != nil {
log.Println(err)
}
}
return nil
})
}