2021-08-17 11:18:43 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package pg
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"container/list"
|
2021-08-17 15:23:28 +02:00
|
|
|
"context"
|
2021-08-17 11:18:43 +02:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"github.com/go-pg/pg/v10"
|
2021-08-17 15:23:28 +02:00
|
|
|
"github.com/go-pg/pg/v10/orm"
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-08-17 11:18:43 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
type statusDB struct {
|
|
|
|
|
config *config.Config
|
|
|
|
|
conn *pg.DB
|
|
|
|
|
log *logrus.Logger
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *statusDB) newStatusQ(status *gtsmodel.Status) *orm.Query {
|
|
|
|
|
return s.conn.Model(status).
|
|
|
|
|
Relation("Account").
|
|
|
|
|
Relation("InReplyTo").
|
|
|
|
|
Relation("InReplyToAccount").
|
|
|
|
|
Relation("BoostOf").
|
|
|
|
|
Relation("BoostOfAccount").
|
|
|
|
|
Relation("CreatedWithApplication")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *statusDB) processResponse(status *gtsmodel.Status, err error) (*gtsmodel.Status, db.DBError) {
|
|
|
|
|
switch err {
|
|
|
|
|
case pg.ErrNoRows:
|
|
|
|
|
return nil, db.ErrNoEntries
|
|
|
|
|
case nil:
|
|
|
|
|
return status, nil
|
|
|
|
|
default:
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *statusDB) GetStatusByID(id string) (*gtsmodel.Status, db.DBError) {
|
|
|
|
|
status := >smodel.Status{}
|
|
|
|
|
|
|
|
|
|
q := s.newStatusQ(status).
|
|
|
|
|
Where("status.id = ?", id)
|
|
|
|
|
|
|
|
|
|
return s.processResponse(status, q.Select())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *statusDB) GetStatusByURI(uri string) (*gtsmodel.Status, db.DBError) {
|
|
|
|
|
status := >smodel.Status{}
|
|
|
|
|
|
|
|
|
|
q := s.newStatusQ(status).
|
|
|
|
|
Where("LOWER(status.uri) = LOWER(?)", uri)
|
|
|
|
|
|
|
|
|
|
return s.processResponse(status, q.Select())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *statusDB) StatusParents(status *gtsmodel.Status, onlyDirect bool) ([]*gtsmodel.Status, db.DBError) {
|
2021-08-17 11:18:43 +02:00
|
|
|
parents := []*gtsmodel.Status{}
|
2021-08-17 15:23:28 +02:00
|
|
|
s.statusParent(status, &parents, onlyDirect)
|
2021-08-17 11:18:43 +02:00
|
|
|
|
|
|
|
|
return parents, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) statusParent(status *gtsmodel.Status, foundStatuses *[]*gtsmodel.Status, onlyDirect bool) {
|
2021-08-17 11:18:43 +02:00
|
|
|
if status.InReplyToID == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parentStatus := >smodel.Status{}
|
2021-08-17 15:23:28 +02:00
|
|
|
if err := s.conn.Model(parentStatus).Where("id = ?", status.InReplyToID).Select(); err == nil {
|
2021-08-17 11:18:43 +02:00
|
|
|
*foundStatuses = append(*foundStatuses, parentStatus)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if onlyDirect {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-08-17 15:23:28 +02:00
|
|
|
s.statusParent(parentStatus, foundStatuses, false)
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) StatusChildren(status *gtsmodel.Status, onlyDirect bool, minID string) ([]*gtsmodel.Status, db.DBError) {
|
2021-08-17 11:18:43 +02:00
|
|
|
foundStatuses := &list.List{}
|
|
|
|
|
foundStatuses.PushFront(status)
|
2021-08-17 15:23:28 +02:00
|
|
|
s.statusChildren(status, foundStatuses, onlyDirect, minID)
|
2021-08-17 11:18:43 +02:00
|
|
|
|
|
|
|
|
children := []*gtsmodel.Status{}
|
|
|
|
|
for e := foundStatuses.Front(); e != nil; e = e.Next() {
|
|
|
|
|
entry, ok := e.Value.(*gtsmodel.Status)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic(errors.New("entry in foundStatuses was not a *gtsmodel.Status"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only append children, not the overall parent status
|
|
|
|
|
if entry.ID != status.ID {
|
|
|
|
|
children = append(children, entry)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return children, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) statusChildren(status *gtsmodel.Status, foundStatuses *list.List, onlyDirect bool, minID string) {
|
2021-08-17 11:18:43 +02:00
|
|
|
immediateChildren := []*gtsmodel.Status{}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
q := s.conn.Model(&immediateChildren).Where("in_reply_to_id = ?", status.ID)
|
2021-08-17 11:18:43 +02:00
|
|
|
if minID != "" {
|
|
|
|
|
q = q.Where("status.id > ?", minID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := q.Select(); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, child := range immediateChildren {
|
|
|
|
|
insertLoop:
|
|
|
|
|
for e := foundStatuses.Front(); e != nil; e = e.Next() {
|
|
|
|
|
entry, ok := e.Value.(*gtsmodel.Status)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic(errors.New("entry in foundStatuses was not a *gtsmodel.Status"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if child.InReplyToAccountID != "" && entry.ID == child.InReplyToID {
|
|
|
|
|
foundStatuses.InsertAfter(child, e)
|
|
|
|
|
break insertLoop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only do one loop if we only want direct children
|
|
|
|
|
if onlyDirect {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-08-17 15:23:28 +02:00
|
|
|
s.statusChildren(child, foundStatuses, false, minID)
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) GetReplyCountForStatus(status *gtsmodel.Status) (int, db.DBError) {
|
|
|
|
|
return s.conn.Model(>smodel.Status{}).Where("in_reply_to_id = ?", status.ID).Count()
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) GetReblogCountForStatus(status *gtsmodel.Status) (int, db.DBError) {
|
|
|
|
|
return s.conn.Model(>smodel.Status{}).Where("boost_of_id = ?", status.ID).Count()
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) GetFaveCountForStatus(status *gtsmodel.Status) (int, db.DBError) {
|
|
|
|
|
return s.conn.Model(>smodel.StatusFave{}).Where("status_id = ?", status.ID).Count()
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) StatusFavedBy(status *gtsmodel.Status, accountID string) (bool, db.DBError) {
|
|
|
|
|
return s.conn.Model(>smodel.StatusFave{}).Where("status_id = ?", status.ID).Where("account_id = ?", accountID).Exists()
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) StatusRebloggedBy(status *gtsmodel.Status, accountID string) (bool, db.DBError) {
|
|
|
|
|
return s.conn.Model(>smodel.Status{}).Where("boost_of_id = ?", status.ID).Where("account_id = ?", accountID).Exists()
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) StatusMutedBy(status *gtsmodel.Status, accountID string) (bool, db.DBError) {
|
|
|
|
|
return s.conn.Model(>smodel.StatusMute{}).Where("status_id = ?", status.ID).Where("account_id = ?", accountID).Exists()
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) StatusBookmarkedBy(status *gtsmodel.Status, accountID string) (bool, db.DBError) {
|
|
|
|
|
return s.conn.Model(>smodel.StatusBookmark{}).Where("status_id = ?", status.ID).Where("account_id = ?", accountID).Exists()
|
2021-08-17 11:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) WhoFavedStatus(status *gtsmodel.Status) ([]*gtsmodel.Account, db.DBError) {
|
2021-08-17 11:18:43 +02:00
|
|
|
accounts := []*gtsmodel.Account{}
|
|
|
|
|
|
|
|
|
|
faves := []*gtsmodel.StatusFave{}
|
2021-08-17 15:23:28 +02:00
|
|
|
if err := s.conn.Model(&faves).Where("status_id = ?", status.ID).Select(); err != nil {
|
2021-08-17 11:18:43 +02:00
|
|
|
if err == pg.ErrNoRows {
|
|
|
|
|
return accounts, nil // no rows just means nobody has faved this status, so that's fine
|
|
|
|
|
}
|
|
|
|
|
return nil, err // an actual error has occurred
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, f := range faves {
|
|
|
|
|
acc := >smodel.Account{}
|
2021-08-17 15:23:28 +02:00
|
|
|
if err := s.conn.Model(acc).Where("id = ?", f.AccountID).Select(); err != nil {
|
2021-08-17 11:18:43 +02:00
|
|
|
if err == pg.ErrNoRows {
|
|
|
|
|
continue // the account doesn't exist for some reason??? but this isn't the place to worry about that so just skip it
|
|
|
|
|
}
|
|
|
|
|
return nil, err // an actual error has occurred
|
|
|
|
|
}
|
|
|
|
|
accounts = append(accounts, acc)
|
|
|
|
|
}
|
|
|
|
|
return accounts, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 15:23:28 +02:00
|
|
|
func (s *statusDB) WhoBoostedStatus(status *gtsmodel.Status) ([]*gtsmodel.Account, db.DBError) {
|
2021-08-17 11:18:43 +02:00
|
|
|
accounts := []*gtsmodel.Account{}
|
|
|
|
|
|
|
|
|
|
boosts := []*gtsmodel.Status{}
|
2021-08-17 15:23:28 +02:00
|
|
|
if err := s.conn.Model(&boosts).Where("boost_of_id = ?", status.ID).Select(); err != nil {
|
2021-08-17 11:18:43 +02:00
|
|
|
if err == pg.ErrNoRows {
|
|
|
|
|
return accounts, nil // no rows just means nobody has boosted this status, so that's fine
|
|
|
|
|
}
|
|
|
|
|
return nil, err // an actual error has occurred
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, f := range boosts {
|
|
|
|
|
acc := >smodel.Account{}
|
2021-08-17 15:23:28 +02:00
|
|
|
if err := s.conn.Model(acc).Where("id = ?", f.AccountID).Select(); err != nil {
|
2021-08-17 11:18:43 +02:00
|
|
|
if err == pg.ErrNoRows {
|
|
|
|
|
continue // the account doesn't exist for some reason??? but this isn't the place to worry about that so just skip it
|
|
|
|
|
}
|
|
|
|
|
return nil, err // an actual error has occurred
|
|
|
|
|
}
|
|
|
|
|
accounts = append(accounts, acc)
|
|
|
|
|
}
|
|
|
|
|
return accounts, nil
|
|
|
|
|
}
|