2021-08-20 12:26:56 +02: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 15:34:33 +02:00
|
|
|
package bundb
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-25 15:34:33 +02:00
|
|
|
"strings"
|
2021-08-20 12:26:56 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-08-25 15:34:33 +02:00
|
|
|
"github.com/uptrace/bun"
|
2021-08-20 12:26:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type accountDB struct {
|
|
|
|
config *config.Config
|
2021-08-25 15:34:33 +02:00
|
|
|
conn *bun.DB
|
2021-08-20 12:26:56 +02:00
|
|
|
log *logrus.Logger
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) newAccountQ(account *gtsmodel.Account) *bun.SelectQuery {
|
|
|
|
return a.conn.
|
|
|
|
NewSelect().
|
|
|
|
Model(account).
|
2021-08-20 12:26:56 +02:00
|
|
|
Relation("AvatarMediaAttachment").
|
|
|
|
Relation("HeaderMediaAttachment")
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetAccountByID(ctx context.Context, id string) (*gtsmodel.Account, db.Error) {
|
|
|
|
account := new(gtsmodel.Account)
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
q := a.newAccountQ(account).
|
|
|
|
Where("account.id = ?", id)
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
err := processErrorResponse(q.Scan(ctx))
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetAccountByURI(ctx context.Context, uri string) (*gtsmodel.Account, db.Error) {
|
|
|
|
account := new(gtsmodel.Account)
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
q := a.newAccountQ(account).
|
|
|
|
Where("account.uri = ?", uri)
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
err := processErrorResponse(q.Scan(ctx))
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetAccountByURL(ctx context.Context, uri string) (*gtsmodel.Account, db.Error) {
|
|
|
|
account := new(gtsmodel.Account)
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
q := a.newAccountQ(account).
|
|
|
|
Where("account.url = ?", uri)
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
err := processErrorResponse(q.Scan(ctx))
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) UpdateAccount(ctx context.Context, account *gtsmodel.Account) (*gtsmodel.Account, db.Error) {
|
|
|
|
if strings.TrimSpace(account.ID) == "" {
|
|
|
|
return nil, errors.New("account had no ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
account.UpdatedAt = time.Now()
|
|
|
|
|
|
|
|
q := a.conn.
|
|
|
|
NewUpdate().
|
|
|
|
Model(account).
|
|
|
|
WherePK()
|
|
|
|
|
|
|
|
_, err := q.Exec(ctx)
|
|
|
|
|
|
|
|
err = processErrorResponse(err)
|
|
|
|
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *accountDB) GetInstanceAccount(ctx context.Context, domain string) (*gtsmodel.Account, db.Error) {
|
|
|
|
account := new(gtsmodel.Account)
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
q := a.newAccountQ(account)
|
|
|
|
|
|
|
|
if domain == "" {
|
|
|
|
q = q.
|
|
|
|
Where("account.username = ?", domain).
|
|
|
|
Where("account.domain = ?", domain)
|
|
|
|
} else {
|
|
|
|
q = q.
|
|
|
|
Where("account.username = ?", domain).
|
2021-08-25 15:34:33 +02:00
|
|
|
Where("? IS NULL", bun.Ident("domain"))
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
err := processErrorResponse(q.Scan(ctx))
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetAccountLastPosted(ctx context.Context, accountID string) (time.Time, db.Error) {
|
|
|
|
status := new(gtsmodel.Status)
|
2021-08-20 12:26:56 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
q := a.conn.
|
|
|
|
NewSelect().
|
|
|
|
Model(status).
|
2021-08-20 12:26:56 +02:00
|
|
|
Order("id DESC").
|
|
|
|
Limit(1).
|
|
|
|
Where("account_id = ?", accountID).
|
|
|
|
Column("created_at")
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
err := processErrorResponse(q.Scan(ctx))
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
return status.CreatedAt, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) db.Error {
|
2021-08-20 12:26:56 +02:00
|
|
|
if mediaAttachment.Avatar && mediaAttachment.Header {
|
|
|
|
return errors.New("one media attachment cannot be both header and avatar")
|
|
|
|
}
|
|
|
|
|
|
|
|
var headerOrAVI string
|
|
|
|
if mediaAttachment.Avatar {
|
|
|
|
headerOrAVI = "avatar"
|
|
|
|
} else if mediaAttachment.Header {
|
|
|
|
headerOrAVI = "header"
|
|
|
|
} else {
|
|
|
|
return errors.New("given media attachment was neither a header nor an avatar")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: there are probably more side effects here that need to be handled
|
2021-08-25 15:34:33 +02:00
|
|
|
if _, err := a.conn.
|
|
|
|
NewInsert().
|
|
|
|
Model(mediaAttachment).
|
|
|
|
Exec(ctx); err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if _, err := a.conn.
|
|
|
|
NewUpdate().
|
|
|
|
Model(>smodel.Account{}).
|
|
|
|
Set(fmt.Sprintf("%s_media_attachment_id = ?", headerOrAVI), mediaAttachment.ID).
|
|
|
|
Where("id = ?", accountID).
|
|
|
|
Exec(ctx); err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetLocalAccountByUsername(ctx context.Context, username string) (*gtsmodel.Account, db.Error) {
|
|
|
|
account := new(gtsmodel.Account)
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
q := a.newAccountQ(account).
|
|
|
|
Where("username = ?", username).
|
2021-08-25 15:34:33 +02:00
|
|
|
Where("? IS NULL", bun.Ident("domain"))
|
2021-08-20 12:26:56 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
err := processErrorResponse(q.Scan(ctx))
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetAccountFaves(ctx context.Context, accountID string) ([]*gtsmodel.StatusFave, db.Error) {
|
|
|
|
faves := new([]*gtsmodel.StatusFave)
|
2021-08-20 12:26:56 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := a.conn.
|
|
|
|
NewSelect().
|
|
|
|
Model(faves).
|
2021-08-20 12:26:56 +02:00
|
|
|
Where("account_id = ?", accountID).
|
2021-08-25 15:34:33 +02:00
|
|
|
Scan(ctx); err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
return *faves, nil
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) CountAccountStatuses(ctx context.Context, accountID string) (int, db.Error) {
|
|
|
|
return a.conn.
|
|
|
|
NewSelect().
|
|
|
|
Model(>smodel.Status{}).
|
|
|
|
Where("account_id = ?", accountID).
|
|
|
|
Count(ctx)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetAccountStatuses(ctx context.Context, accountID string, limit int, excludeReplies bool, maxID string, pinnedOnly bool, mediaOnly bool) ([]*gtsmodel.Status, db.Error) {
|
2021-08-20 12:26:56 +02:00
|
|
|
a.log.Debugf("getting statuses for account %s", accountID)
|
|
|
|
statuses := []*gtsmodel.Status{}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
q := a.conn.
|
|
|
|
NewSelect().
|
|
|
|
Model(&statuses).
|
|
|
|
Order("id DESC")
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
if accountID != "" {
|
|
|
|
q = q.Where("account_id = ?", accountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != 0 {
|
|
|
|
q = q.Limit(limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if excludeReplies {
|
2021-08-25 15:34:33 +02:00
|
|
|
q = q.Where("? IS NULL", bun.Ident("in_reply_to_id"))
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if pinnedOnly {
|
|
|
|
q = q.Where("pinned = ?", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxID != "" {
|
|
|
|
q = q.Where("id < ?", maxID)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if mediaOnly {
|
|
|
|
q = q.WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
|
|
|
return q.
|
|
|
|
WhereOr("? IS NOT NULL", bun.Ident("attachments")).
|
|
|
|
WhereOr("attachments != '{}'")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := q.Scan(ctx); err != nil {
|
2021-08-20 12:26:56 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(statuses) == 0 {
|
|
|
|
return nil, db.ErrNoEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
a.log.Debugf("returning statuses for account %s", accountID)
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func (a *accountDB) GetAccountBlocks(ctx context.Context, accountID string, maxID string, sinceID string, limit int) ([]*gtsmodel.Account, string, string, db.Error) {
|
2021-08-20 12:26:56 +02:00
|
|
|
blocks := []*gtsmodel.Block{}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
fq := a.conn.
|
|
|
|
NewSelect().
|
|
|
|
Model(&blocks).
|
2021-08-20 12:26:56 +02:00
|
|
|
Where("block.account_id = ?", accountID).
|
|
|
|
Relation("TargetAccount").
|
|
|
|
Order("block.id DESC")
|
|
|
|
|
|
|
|
if maxID != "" {
|
|
|
|
fq = fq.Where("block.id < ?", maxID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinceID != "" {
|
|
|
|
fq = fq.Where("block.id > ?", sinceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit > 0 {
|
|
|
|
fq = fq.Limit(limit)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
err := fq.Scan(ctx)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(blocks) == 0 {
|
|
|
|
return nil, "", "", db.ErrNoEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts := []*gtsmodel.Account{}
|
|
|
|
for _, b := range blocks {
|
|
|
|
accounts = append(accounts, b.TargetAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
nextMaxID := blocks[len(blocks)-1].ID
|
|
|
|
prevMinID := blocks[0].ID
|
|
|
|
return accounts, nextMaxID, prevMinID, nil
|
|
|
|
}
|