2021-08-20 10:26:56 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
package bundb
|
2021-08-20 10:26:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type domainDB struct {
|
2021-12-07 12:31:39 +00:00
|
|
|
conn *DBConn
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (d *domainDB) IsDomainBlocked(ctx context.Context, domain string) (bool, db.Error) {
|
2021-08-20 10:26:56 +00:00
|
|
|
if domain == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
q := d.conn.
|
|
|
|
NewSelect().
|
2021-08-20 10:26:56 +00:00
|
|
|
Model(>smodel.DomainBlock{}).
|
|
|
|
Where("LOWER(domain) = LOWER(?)", domain).
|
2021-08-25 13:34:33 +00:00
|
|
|
Limit(1)
|
2021-08-20 10:26:56 +00:00
|
|
|
|
2021-08-29 14:41:41 +00:00
|
|
|
return d.conn.Exists(ctx, q)
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (d *domainDB) AreDomainsBlocked(ctx context.Context, domains []string) (bool, db.Error) {
|
2021-08-20 10:26:56 +00:00
|
|
|
// filter out any doubles
|
|
|
|
uniqueDomains := util.UniqueStrings(domains)
|
|
|
|
|
|
|
|
for _, domain := range uniqueDomains {
|
2021-08-25 13:34:33 +00:00
|
|
|
if blocked, err := d.IsDomainBlocked(ctx, domain); err != nil {
|
2021-08-20 10:26:56 +00:00
|
|
|
return false, err
|
|
|
|
} else if blocked {
|
|
|
|
return blocked, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no blocks found
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (d *domainDB) IsURIBlocked(ctx context.Context, uri *url.URL) (bool, db.Error) {
|
2021-08-20 10:26:56 +00:00
|
|
|
domain := uri.Hostname()
|
2021-08-25 13:34:33 +00:00
|
|
|
return d.IsDomainBlocked(ctx, domain)
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (d *domainDB) AreURIsBlocked(ctx context.Context, uris []*url.URL) (bool, db.Error) {
|
2021-08-20 10:26:56 +00:00
|
|
|
domains := []string{}
|
|
|
|
for _, uri := range uris {
|
|
|
|
domains = append(domains, uri.Hostname())
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
return d.AreDomainsBlocked(ctx, domains)
|
2021-08-20 10:26:56 +00:00
|
|
|
}
|