gotosocial/internal/db/bundb/instance.go

146 lines
4.2 KiB
Go
Raw Normal View History

/*
GoToSocial
Copyright (C) 2021-2023 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/>.
*/
package bundb
import (
"context"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/uptrace/bun"
)
type instanceDB struct {
conn *DBConn
}
func (i *instanceDB) CountInstanceUsers(ctx context.Context, domain string) (int, db.Error) {
q := i.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")).
Column("account.id").
Where("? != ?", bun.Ident("account.username"), domain).
Where("? IS NULL", bun.Ident("account.suspended_at"))
if domain == config.GetHost() || domain == config.GetAccountDomain() {
// if the domain is *this* domain, just count where the domain field is null
q = q.WhereGroup(" AND ", whereEmptyOrNull("account.domain"))
} else {
q = q.Where("? = ?", bun.Ident("account.domain"), domain)
}
count, err := q.Count(ctx)
if err != nil {
return 0, i.conn.ProcessError(err)
}
return count, nil
}
func (i *instanceDB) CountInstanceStatuses(ctx context.Context, domain string) (int, db.Error) {
q := i.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status"))
if domain == config.GetHost() || domain == config.GetAccountDomain() {
// if the domain is *this* domain, just count where local is true
q = q.Where("? = ?", bun.Ident("status.local"), true)
} else {
// join on the domain of the account
q = q.
Join("JOIN ? AS ? ON ? = ?", bun.Ident("accounts"), bun.Ident("account"), bun.Ident("account.id"), bun.Ident("status.account_id")).
Where("? = ?", bun.Ident("account.domain"), domain)
}
count, err := q.Count(ctx)
if err != nil {
return 0, i.conn.ProcessError(err)
}
return count, nil
}
func (i *instanceDB) CountInstanceDomains(ctx context.Context, domain string) (int, db.Error) {
q := i.conn.
NewSelect().
TableExpr("? AS ?", bun.Ident("instances"), bun.Ident("instance"))
[chore] Global server configuration overhaul (#575) * move config flag names and usage to config package, rewrite config package to use global Configuration{} struct Signed-off-by: kim <grufwub@gmail.com> * improved code comment Signed-off-by: kim <grufwub@gmail.com> * linter Signed-off-by: kim <grufwub@gmail.com> * fix unmarshaling Signed-off-by: kim <grufwub@gmail.com> * remove kim's custom go compiler changes Signed-off-by: kim <grufwub@gmail.com> * generate setter and flag-name functions, implement these in codebase Signed-off-by: kim <grufwub@gmail.com> * update deps Signed-off-by: kim <grufwub@gmail.com> * small change Signed-off-by: kim <grufwub@gmail.com> * appease the linter... Signed-off-by: kim <grufwub@gmail.com> * move configuration into ConfigState structure, ensure reloading to/from viper settings to keep in sync Signed-off-by: kim <grufwub@gmail.com> * lint Signed-off-by: kim <grufwub@gmail.com> * update code comments Signed-off-by: kim <grufwub@gmail.com> * fix merge issue Signed-off-by: kim <grufwub@gmail.com> * fix merge issue Signed-off-by: kim <grufwub@gmail.com> * improved version string (removes time + go version) Signed-off-by: kim <grufwub@gmail.com> * fix version string build to pass test script + consolidate logic in func Signed-off-by: kim <grufwub@gmail.com> * add license text, update config.Defaults comment Signed-off-by: kim <grufwub@gmail.com> * add license text to generated config helpers file Signed-off-by: kim <grufwub@gmail.com> * defer unlock on config.Set___(), to ensure unlocked on panic Signed-off-by: kim <grufwub@gmail.com> * make it more obvious which cmd flags are being attached Signed-off-by: kim <grufwub@gmail.com>
2022-05-30 12:41:24 +00:00
if domain == config.GetHost() {
// if the domain is *this* domain, just count other instances it knows about
// exclude domains that are blocked
2021-08-26 09:28:16 +00:00
q = q.
Where("? != ?", bun.Ident("instance.domain"), domain).
Where("? IS NULL", bun.Ident("instance.suspended_at"))
} else {
// TODO: implement federated domain counting properly for remote domains
return 0, nil
}
count, err := q.Count(ctx)
if err != nil {
return 0, i.conn.ProcessError(err)
}
return count, nil
}
func (i *instanceDB) GetInstancePeers(ctx context.Context, includeSuspended bool) ([]*gtsmodel.Instance, db.Error) {
instances := []*gtsmodel.Instance{}
q := i.conn.
NewSelect().
Model(&instances).
Where("? != ?", bun.Ident("instance.domain"), config.GetHost())
if !includeSuspended {
q = q.Where("? IS NULL", bun.Ident("instance.suspended_at"))
}
if err := q.Scan(ctx); err != nil {
return nil, i.conn.ProcessError(err)
}
return instances, nil
}
func (i *instanceDB) GetInstanceAccounts(ctx context.Context, domain string, maxID string, limit int) ([]*gtsmodel.Account, db.Error) {
accounts := []*gtsmodel.Account{}
q := i.conn.NewSelect().
Model(&accounts).
Where("? = ?", bun.Ident("account.domain"), domain).
Order("account.id DESC")
if maxID != "" {
q = q.Where("? < ?", bun.Ident("account.id"), maxID)
}
if limit > 0 {
q = q.Limit(limit)
}
if err := q.Scan(ctx); err != nil {
return nil, i.conn.ProcessError(err)
}
if len(accounts) == 0 {
return nil, db.ErrNoEntries
}
return accounts, nil
}