mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-27 09:18:52 +01:00
098dbe6ff4
* first commit Signed-off-by: kim <grufwub@gmail.com> * replace logging with our own log library Signed-off-by: kim <grufwub@gmail.com> * fix imports Signed-off-by: kim <grufwub@gmail.com> * fix log imports Signed-off-by: kim <grufwub@gmail.com> * add license text Signed-off-by: kim <grufwub@gmail.com> * fix package import cycle between config and log package Signed-off-by: kim <grufwub@gmail.com> * fix empty kv.Fields{} being passed to WithFields() Signed-off-by: kim <grufwub@gmail.com> * fix uses of log.WithFields() with whitespace issues and empty slices Signed-off-by: kim <grufwub@gmail.com> * *linter related grumbling* Signed-off-by: kim <grufwub@gmail.com> * gofmt the codebase! also fix more log.WithFields() formatting issues Signed-off-by: kim <grufwub@gmail.com> * update testrig code to match new changes Signed-off-by: kim <grufwub@gmail.com> * fix error wrapping in non fmt.Errorf function Signed-off-by: kim <grufwub@gmail.com> * add benchmarking of log.Caller() vs non-cached Signed-off-by: kim <grufwub@gmail.com> * fix syslog tests, add standard build tags to test runner to ensure consistency Signed-off-by: kim <grufwub@gmail.com> * make syslog tests more robust Signed-off-by: kim <grufwub@gmail.com> * fix caller depth arithmatic (is that how you spell it?) Signed-off-by: kim <grufwub@gmail.com> * update to use unkeyed fields in kv.Field{} instances Signed-off-by: kim <grufwub@gmail.com> * update go-kv library Signed-off-by: kim <grufwub@gmail.com> * update libraries list Signed-off-by: kim <grufwub@gmail.com> * fuck you linter get nerfed Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
231 lines
6.2 KiB
Go
231 lines
6.2 KiB
Go
/*
|
|
GoToSocial
|
|
Copyright (C) 2021-2022 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/db"
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
|
"github.com/uptrace/bun"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
type timelineDB struct {
|
|
conn *DBConn
|
|
status *statusDB
|
|
}
|
|
|
|
func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*gtsmodel.Status, db.Error) {
|
|
// Make educated guess for slice size
|
|
statusIDs := make([]string, 0, limit)
|
|
|
|
q := t.conn.
|
|
NewSelect().
|
|
Table("statuses").
|
|
|
|
// Select only IDs from table
|
|
Column("statuses.id").
|
|
// Find out who accountID follows.
|
|
Join("LEFT JOIN follows ON follows.target_account_id = statuses.account_id AND follows.account_id = ?", accountID).
|
|
// Sort by highest ID (newest) to lowest ID (oldest)
|
|
Order("statuses.id DESC")
|
|
|
|
if maxID != "" {
|
|
// return only statuses LOWER (ie., older) than maxID
|
|
q = q.Where("statuses.id < ?", maxID)
|
|
}
|
|
|
|
if sinceID != "" {
|
|
// return only statuses HIGHER (ie., newer) than sinceID
|
|
q = q.Where("statuses.id > ?", sinceID)
|
|
}
|
|
|
|
if minID != "" {
|
|
// return only statuses HIGHER (ie., newer) than minID
|
|
q = q.Where("statuses.id > ?", minID)
|
|
}
|
|
|
|
if local {
|
|
// return only statuses posted by local account havers
|
|
q = q.Where("statuses.local = ?", local)
|
|
}
|
|
|
|
if limit > 0 {
|
|
// limit amount of statuses returned
|
|
q = q.Limit(limit)
|
|
}
|
|
|
|
// Use a WhereGroup here to specify that we want EITHER statuses posted by accounts that accountID follows,
|
|
// OR statuses posted by accountID itself (since a user should be able to see their own statuses).
|
|
//
|
|
// This is equivalent to something like WHERE ... AND (... OR ...)
|
|
// See: https://bun.uptrace.dev/guide/queries.html#select
|
|
whereGroup := func(*bun.SelectQuery) *bun.SelectQuery {
|
|
return q.
|
|
WhereOr("follows.account_id = ?", accountID).
|
|
WhereOr("statuses.account_id = ?", accountID)
|
|
}
|
|
|
|
q = q.WhereGroup(" AND ", whereGroup)
|
|
|
|
if err := q.Scan(ctx, &statusIDs); err != nil {
|
|
return nil, t.conn.ProcessError(err)
|
|
}
|
|
|
|
statuses := make([]*gtsmodel.Status, 0, len(statusIDs))
|
|
|
|
for _, id := range statusIDs {
|
|
// Fetch status from db for ID
|
|
status, err := t.status.GetStatusByID(ctx, id)
|
|
if err != nil {
|
|
log.Errorf("GetHomeTimeline: error fetching status %q: %v", id, err)
|
|
continue
|
|
}
|
|
|
|
// Append status to slice
|
|
statuses = append(statuses, status)
|
|
}
|
|
|
|
return statuses, nil
|
|
}
|
|
|
|
func (t *timelineDB) GetPublicTimeline(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, local bool) ([]*gtsmodel.Status, db.Error) {
|
|
// Ensure reasonable
|
|
if limit < 0 {
|
|
limit = 0
|
|
}
|
|
|
|
// Make educated guess for slice size
|
|
statusIDs := make([]string, 0, limit)
|
|
|
|
q := t.conn.
|
|
NewSelect().
|
|
Table("statuses").
|
|
Column("statuses.id").
|
|
Where("statuses.visibility = ?", gtsmodel.VisibilityPublic).
|
|
WhereGroup(" AND ", whereEmptyOrNull("statuses.in_reply_to_id")).
|
|
WhereGroup(" AND ", whereEmptyOrNull("statuses.in_reply_to_uri")).
|
|
WhereGroup(" AND ", whereEmptyOrNull("statuses.boost_of_id")).
|
|
Order("statuses.id DESC")
|
|
|
|
if maxID != "" {
|
|
q = q.Where("statuses.id < ?", maxID)
|
|
}
|
|
|
|
if sinceID != "" {
|
|
q = q.Where("statuses.id > ?", sinceID)
|
|
}
|
|
|
|
if minID != "" {
|
|
q = q.Where("statuses.id > ?", minID)
|
|
}
|
|
|
|
if local {
|
|
q = q.Where("statuses.local = ?", local)
|
|
}
|
|
|
|
if limit > 0 {
|
|
q = q.Limit(limit)
|
|
}
|
|
|
|
if err := q.Scan(ctx, &statusIDs); err != nil {
|
|
return nil, t.conn.ProcessError(err)
|
|
}
|
|
|
|
statuses := make([]*gtsmodel.Status, 0, len(statusIDs))
|
|
|
|
for _, id := range statusIDs {
|
|
// Fetch status from db for ID
|
|
status, err := t.status.GetStatusByID(ctx, id)
|
|
if err != nil {
|
|
log.Errorf("GetPublicTimeline: error fetching status %q: %v", id, err)
|
|
continue
|
|
}
|
|
|
|
// Append status to slice
|
|
statuses = append(statuses, status)
|
|
}
|
|
|
|
return statuses, nil
|
|
}
|
|
|
|
// TODO optimize this query and the logic here, because it's slow as balls -- it takes like a literal second to return with a limit of 20!
|
|
// It might be worth serving it through a timeline instead of raw DB queries, like we do for Home feeds.
|
|
func (t *timelineDB) GetFavedTimeline(ctx context.Context, accountID string, maxID string, minID string, limit int) ([]*gtsmodel.Status, string, string, db.Error) {
|
|
// Ensure reasonable
|
|
if limit < 0 {
|
|
limit = 0
|
|
}
|
|
|
|
// Make educated guess for slice size
|
|
faves := make([]*gtsmodel.StatusFave, 0, limit)
|
|
|
|
fq := t.conn.
|
|
NewSelect().
|
|
Model(&faves).
|
|
Where("account_id = ?", accountID).
|
|
Order("id DESC")
|
|
|
|
if maxID != "" {
|
|
fq = fq.Where("id < ?", maxID)
|
|
}
|
|
|
|
if minID != "" {
|
|
fq = fq.Where("id > ?", minID)
|
|
}
|
|
|
|
if limit > 0 {
|
|
fq = fq.Limit(limit)
|
|
}
|
|
|
|
err := fq.Scan(ctx)
|
|
if err != nil {
|
|
return nil, "", "", t.conn.ProcessError(err)
|
|
}
|
|
|
|
if len(faves) == 0 {
|
|
return nil, "", "", db.ErrNoEntries
|
|
}
|
|
|
|
// Sort by favourite ID rather than status ID
|
|
slices.SortFunc(faves, func(a, b *gtsmodel.StatusFave) bool {
|
|
return a.ID < b.ID
|
|
})
|
|
|
|
statuses := make([]*gtsmodel.Status, 0, len(faves))
|
|
|
|
for _, fave := range faves {
|
|
// Fetch status from db for corresponding favourite
|
|
status, err := t.status.GetStatusByID(ctx, fave.StatusID)
|
|
if err != nil {
|
|
log.Errorf("GetFavedTimeline: error fetching status for fave %q: %v", fave.ID, err)
|
|
continue
|
|
}
|
|
|
|
// Append status to slice
|
|
statuses = append(statuses, status)
|
|
}
|
|
|
|
nextMaxID := faves[len(faves)-1].ID
|
|
prevMinID := faves[0].ID
|
|
return statuses, nextMaxID, prevMinID, nil
|
|
}
|