Refactor email domain blocklist config field in importer package.

This commit is contained in:
Kailash Nadh 2023-04-08 09:23:47 +05:30
parent 476d5bebf2
commit 98729f63df
2 changed files with 15 additions and 12 deletions

View file

@ -70,7 +70,7 @@ type constants struct {
AllowExport bool `koanf:"allow_export"`
AllowWipe bool `koanf:"allow_wipe"`
Exportable map[string]bool `koanf:"-"`
DomainBlocklist map[string]bool `koanf:"-"`
DomainBlocklist []string `koanf:"-"`
} `koanf:"privacy"`
Security struct {
EnableCaptcha bool `koanf:"enable_captcha"`
@ -370,7 +370,7 @@ func initConstants() *constants {
c.Lang = ko.String("app.lang")
c.Privacy.Exportable = maps.StringSliceToLookupMap(ko.Strings("privacy.exportable"))
c.MediaProvider = ko.String("upload.provider")
c.Privacy.DomainBlocklist = maps.StringSliceToLookupMap(ko.Strings("privacy.domain_blocklist"))
c.Privacy.DomainBlocklist = ko.Strings("privacy.domain_blocklist")
// Static URLS.
// url.com/subscription/{campaign_uuid}/{subscriber_uuid}

View file

@ -24,6 +24,7 @@ import (
"sync"
"github.com/gofrs/uuid"
"github.com/knadh/koanf/maps"
"github.com/knadh/listmonk/internal/i18n"
"github.com/knadh/listmonk/models"
"github.com/lib/pq"
@ -54,6 +55,7 @@ type Importer struct {
opt Options
db *sql.DB
i18n *i18n.I18n
domainBlocklist map[string]bool
stop chan bool
status Status
@ -68,7 +70,7 @@ type Options struct {
NotifCB models.AdminNotifCallback
// Lookup table for blocklisted domains.
DomainBlocklist map[string]bool
DomainBlocklist []string
}
// Session represents a single import session.
@ -133,6 +135,7 @@ func New(opt Options, db *sql.DB, i *i18n.I18n) *Importer {
opt: opt,
db: db,
i18n: i,
domainBlocklist: maps.StringSliceToLookupMap(opt.DomainBlocklist),
status: Status{Status: StatusNone, logBuf: bytes.NewBuffer(nil)},
stop: make(chan bool, 1),
}
@ -587,7 +590,7 @@ func (im *Importer) SanitizeEmail(email string) (string, error) {
// Check if the e-mail's domain is blocklisted.
d := strings.Split(em.Address, "@")
if len(d) == 2 {
_, ok := im.opt.DomainBlocklist[d[1]]
_, ok := im.domainBlocklist[d[1]]
if ok {
return "", errors.New(im.i18n.T("subscribers.domainBlocklisted"))
}