summaryrefslogtreecommitdiff
path: root/internal/db/bundb
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/bundb')
-rw-r--r--internal/db/bundb/account.go18
-rw-r--r--internal/db/bundb/account_test.go8
-rw-r--r--internal/db/bundb/migrations/20221006114842_add_rss_functionality.go46
3 files changed, 66 insertions, 6 deletions
diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go
index c04948fee..4813f4e17 100644
--- a/internal/db/bundb/account.go
+++ b/internal/db/bundb/account.go
@@ -253,21 +253,29 @@ func (a *accountDB) GetInstanceAccount(ctx context.Context, domain string) (*gts
return account, nil
}
-func (a *accountDB) GetAccountLastPosted(ctx context.Context, accountID string) (time.Time, db.Error) {
- status := new(gtsmodel.Status)
+func (a *accountDB) GetAccountLastPosted(ctx context.Context, accountID string, webOnly bool) (time.Time, db.Error) {
+ createdAt := time.Time{}
q := a.conn.
NewSelect().
- Model(status).
+ TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")).
Column("status.created_at").
Where("? = ?", bun.Ident("status.account_id"), accountID).
Order("status.id DESC").
Limit(1)
- if err := q.Scan(ctx); err != nil {
+ if webOnly {
+ q = q.
+ WhereGroup(" AND ", whereEmptyOrNull("status.in_reply_to_uri")).
+ WhereGroup(" AND ", whereEmptyOrNull("status.boost_of_id")).
+ Where("? = ?", bun.Ident("status.visibility"), gtsmodel.VisibilityPublic).
+ Where("? = ?", bun.Ident("status.federated"), true)
+ }
+
+ if err := q.Scan(ctx, &createdAt); err != nil {
return time.Time{}, a.conn.ProcessError(err)
}
- return status.CreatedAt, nil
+ return createdAt, nil
}
func (a *accountDB) SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachment *gtsmodel.MediaAttachment, accountID string) db.Error {
diff --git a/internal/db/bundb/account_test.go b/internal/db/bundb/account_test.go
index 72adba487..29594a740 100644
--- a/internal/db/bundb/account_test.go
+++ b/internal/db/bundb/account_test.go
@@ -152,11 +152,17 @@ func (suite *AccountTestSuite) TestUpdateAccount() {
}
func (suite *AccountTestSuite) TestGetAccountLastPosted() {
- lastPosted, err := suite.db.GetAccountLastPosted(context.Background(), suite.testAccounts["local_account_1"].ID)
+ lastPosted, err := suite.db.GetAccountLastPosted(context.Background(), suite.testAccounts["local_account_1"].ID, false)
suite.NoError(err)
suite.EqualValues(1653046675, lastPosted.Unix())
}
+func (suite *AccountTestSuite) TestGetAccountLastPostedWebOnly() {
+ lastPosted, err := suite.db.GetAccountLastPosted(context.Background(), suite.testAccounts["local_account_1"].ID, true)
+ suite.NoError(err)
+ suite.EqualValues(1634726437, lastPosted.Unix())
+}
+
func (suite *AccountTestSuite) TestInsertAccountWithDefaults() {
key, err := rsa.GenerateKey(rand.Reader, 2048)
suite.NoError(err)
diff --git a/internal/db/bundb/migrations/20221006114842_add_rss_functionality.go b/internal/db/bundb/migrations/20221006114842_add_rss_functionality.go
new file mode 100644
index 000000000..94c21fe58
--- /dev/null
+++ b/internal/db/bundb/migrations/20221006114842_add_rss_functionality.go
@@ -0,0 +1,46 @@
+/*
+ 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 migrations
+
+import (
+ "context"
+ "strings"
+
+ "github.com/uptrace/bun"
+)
+
+func init() {
+ up := func(ctx context.Context, db *bun.DB) error {
+ _, err := db.ExecContext(ctx, "ALTER TABLE ? ADD COLUMN ? BOOLEAN DEFAULT false", bun.Ident("accounts"), bun.Ident("enable_rss"))
+ if err != nil && !(strings.Contains(err.Error(), "already exists") || strings.Contains(err.Error(), "duplicate column name") || strings.Contains(err.Error(), "SQLSTATE 42701")) {
+ return err
+ }
+ return nil
+ }
+
+ down := func(ctx context.Context, db *bun.DB) error {
+ return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
+ return nil
+ })
+ }
+
+ if err := Migrations.Register(up, down); err != nil {
+ panic(err)
+ }
+}