summaryrefslogtreecommitdiff
path: root/pkg/idec/blacklist.go
blob: a74f541bfca4c2bc035ae605acec323eafbb3200 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
	})
}