diff options
Diffstat (limited to 'pkg/idec/blacklist.go')
-rw-r--r-- | pkg/idec/blacklist.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/pkg/idec/blacklist.go b/pkg/idec/blacklist.go new file mode 100644 index 0000000..a74f541 --- /dev/null +++ b/pkg/idec/blacklist.go @@ -0,0 +1,65 @@ +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 + }) +} |