2021-08-20 10:26:56 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 17:42:19 +00:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-08-20 10:26:56 +00:00
|
|
|
|
|
|
|
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"
|
2022-05-02 10:53:46 +00:00
|
|
|
"strings"
|
2021-08-20 10:26:56 +00:00
|
|
|
|
2022-05-23 09:46:50 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-08-20 10:26:56 +00:00
|
|
|
"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) {
|
2022-05-30 12:41:24 +00:00
|
|
|
if domain == "" || domain == config.GetHost() {
|
2021-08-20 10:26:56 +00:00
|
|
|
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{}).
|
2022-05-02 10:53:46 +00:00
|
|
|
ExcludeColumn("id", "created_at", "updated_at", "created_by_account_id", "private_comment", "public_comment", "obfuscate", "subscription_id").
|
|
|
|
Where("domain = ?", 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 {
|
2022-05-02 10:53:46 +00:00
|
|
|
if blocked, err := d.IsDomainBlocked(ctx, strings.ToLower(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
|
|
|
}
|