diff options
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/account.go | 6 | ||||
-rw-r--r-- | internal/db/bundb/admin.go | 4 | ||||
-rw-r--r-- | internal/db/bundb/basic_test.go | 7 | ||||
-rw-r--r-- | internal/db/bundb/instance.go | 6 | ||||
-rw-r--r-- | internal/db/bundb/relationship.go | 10 | ||||
-rw-r--r-- | internal/db/bundb/timeline.go | 6 | ||||
-rw-r--r-- | internal/db/bundb/timeline_test.go | 68 | ||||
-rw-r--r-- | internal/db/bundb/util.go | 14 |
8 files changed, 102 insertions, 19 deletions
diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index 7ebb79a15..c96d0df9e 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -110,7 +110,7 @@ func (a *accountDB) GetInstanceAccount(ctx context.Context, domain string) (*gts } else { q = q. Where("account.username = ?", domain). - Where("? IS NULL", bun.Ident("domain")) + WhereGroup(" AND ", whereEmptyOrNull("domain")) } err := processErrorResponse(q.Scan(ctx)) @@ -172,7 +172,7 @@ func (a *accountDB) GetLocalAccountByUsername(ctx context.Context, username stri q := a.newAccountQ(account). Where("username = ?", username). - Where("? IS NULL", bun.Ident("domain")) + WhereGroup(" AND ", whereEmptyOrNull("domain")) err := processErrorResponse(q.Scan(ctx)) @@ -218,7 +218,7 @@ func (a *accountDB) GetAccountStatuses(ctx context.Context, accountID string, li } if excludeReplies { - q = q.Where("? IS NULL", bun.Ident("in_reply_to_id")) + q = q.WhereGroup(" AND ", whereEmptyOrNull("in_reply_to_id")) } if pinnedOnly { diff --git a/internal/db/bundb/admin.go b/internal/db/bundb/admin.go index 67a1e8a0d..09f2d3bff 100644 --- a/internal/db/bundb/admin.go +++ b/internal/db/bundb/admin.go @@ -97,7 +97,7 @@ func (a *adminDB) NewSignup(ctx context.Context, username string, reason string, err = a.conn.NewSelect(). Model(acct). Where("username = ?", username). - Where("? IS NULL", bun.Ident("domain")). + WhereGroup(" AND ", whereEmptyOrNull("domain")). Scan(ctx) if err != nil { // we just don't have an account yet create one @@ -181,7 +181,7 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error { NewSelect(). Model(>smodel.Account{}). Where("username = ?", username). - Where("? IS NULL", bun.Ident("domain")) + WhereGroup(" AND ", whereEmptyOrNull("domain")) count, err := existsQ.Count(ctx) if err != nil && count == 1 { a.log.Infof("instance account %s already exists", username) diff --git a/internal/db/bundb/basic_test.go b/internal/db/bundb/basic_test.go index 9189618c9..af03eb244 100644 --- a/internal/db/bundb/basic_test.go +++ b/internal/db/bundb/basic_test.go @@ -63,6 +63,13 @@ func (suite *BasicTestSuite) TestGetAccountByID() { suite.NoError(err) } +func (suite *BasicTestSuite) TestGetAllStatuses() { + s := []*gtsmodel.Status{} + err := suite.db.GetAll(context.Background(), &s) + suite.NoError(err) + suite.Len(s, 12) +} + func TestBasicTestSuite(t *testing.T) { suite.Run(t, new(BasicTestSuite)) } diff --git a/internal/db/bundb/instance.go b/internal/db/bundb/instance.go index f9364346e..141b255cf 100644 --- a/internal/db/bundb/instance.go +++ b/internal/db/bundb/instance.go @@ -41,7 +41,7 @@ func (i *instanceDB) CountInstanceUsers(ctx context.Context, domain string) (int if domain == i.config.Host { // if the domain is *this* domain, just count where the domain field is null - q = q.Where("? IS NULL", bun.Ident("domain")) + q = q.WhereGroup(" AND ", whereEmptyOrNull("domain")) } else { q = q.Where("domain = ?", domain) } @@ -83,7 +83,9 @@ func (i *instanceDB) CountInstanceDomains(ctx context.Context, domain string) (i if domain == i.config.Host { // if the domain is *this* domain, just count other instances it knows about // exclude domains that are blocked - q = q.Where("domain != ?", domain).Where("? IS NULL", bun.Ident("suspended_at")) + q = q. + Where("domain != ?", domain). + Where("? IS NULL", bun.Ident("suspended_at")) } else { // TODO: implement federated domain counting properly for remote domains return 0, nil diff --git a/internal/db/bundb/relationship.go b/internal/db/bundb/relationship.go index ccc604baf..ed144669e 100644 --- a/internal/db/bundb/relationship.go +++ b/internal/db/bundb/relationship.go @@ -294,18 +294,10 @@ func (r *relationshipDB) GetAccountFollowedBy(ctx context.Context, accountID str Model(&follows) if localOnly { - // for local accounts let's get where domain is null OR where domain is an empty string, just to be safe - whereGroup := func(q *bun.SelectQuery) *bun.SelectQuery { - q = q. - WhereOr("? IS NULL", bun.Ident("a.domain")). - WhereOr("a.domain = ?", "") - return q - } - q = q.ColumnExpr("follow.*"). Join("JOIN accounts AS a ON follow.account_id = TEXT(a.id)"). Where("follow.target_account_id = ?", accountID). - WhereGroup(" AND ", whereGroup) + WhereGroup(" AND ", whereEmptyOrNull("a.domain")) } else { q = q.Where("target_account_id = ?", accountID) } diff --git a/internal/db/bundb/timeline.go b/internal/db/bundb/timeline.go index b62ad4c50..cd202f436 100644 --- a/internal/db/bundb/timeline.go +++ b/internal/db/bundb/timeline.go @@ -96,9 +96,9 @@ func (t *timelineDB) GetPublicTimeline(ctx context.Context, accountID string, ma NewSelect(). Model(&statuses). Where("visibility = ?", gtsmodel.VisibilityPublic). - Where("? IS NULL", bun.Ident("in_reply_to_id")). - Where("? IS NULL", bun.Ident("in_reply_to_uri")). - Where("? IS NULL", bun.Ident("boost_of_id")). + WhereGroup(" AND ", whereEmptyOrNull("in_reply_to_id")). + WhereGroup(" AND ", whereEmptyOrNull("in_reply_to_uri")). + WhereGroup(" AND ", whereEmptyOrNull("boost_of_id")). Order("status.id DESC") if maxID != "" { diff --git a/internal/db/bundb/timeline_test.go b/internal/db/bundb/timeline_test.go new file mode 100644 index 000000000..f9cf36405 --- /dev/null +++ b/internal/db/bundb/timeline_test.go @@ -0,0 +1,68 @@ +/* + 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 bundb_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type TimelineTestSuite struct { + BunDBStandardTestSuite +} + +func (suite *TimelineTestSuite) SetupSuite() { + suite.testTokens = testrig.NewTestTokens() + suite.testClients = testrig.NewTestClients() + suite.testApplications = testrig.NewTestApplications() + suite.testUsers = testrig.NewTestUsers() + suite.testAccounts = testrig.NewTestAccounts() + suite.testAttachments = testrig.NewTestAttachments() + suite.testStatuses = testrig.NewTestStatuses() + suite.testTags = testrig.NewTestTags() + suite.testMentions = testrig.NewTestMentions() +} + +func (suite *TimelineTestSuite) SetupTest() { + suite.config = testrig.NewTestConfig() + suite.db = testrig.NewTestDB() + suite.log = testrig.NewTestLog() + + testrig.StandardDBSetup(suite.db, suite.testAccounts) +} + +func (suite *TimelineTestSuite) TearDownTest() { + testrig.StandardDBTeardown(suite.db) +} + +func (suite *TimelineTestSuite) TestGetPublicTimeline() { + viewingAccount := suite.testAccounts["local_account_1"] + + s, err := suite.db.GetPublicTimeline(context.Background(), viewingAccount.ID, "", "", "", 20, false) + suite.NoError(err) + + suite.Len(s, 6) +} + +func TestTimelineTestSuite(t *testing.T) { + suite.Run(t, new(TimelineTestSuite)) +} diff --git a/internal/db/bundb/util.go b/internal/db/bundb/util.go index 115d18de2..faa80221f 100644 --- a/internal/db/bundb/util.go +++ b/internal/db/bundb/util.go @@ -76,3 +76,17 @@ func notExists(ctx context.Context, q *bun.SelectQuery) (bool, db.Error) { return notExists, nil } + +// whereEmptyOrNull is a convenience function to return a bun WhereGroup that specifies +// that the given column should be EITHER an empty string OR null. +// +// Use it as follows: +// +// q = q.WhereGroup(" AND ", whereEmptyOrNull("whatever_column")) +func whereEmptyOrNull(column string) func(*bun.SelectQuery) *bun.SelectQuery { + return func(q *bun.SelectQuery) *bun.SelectQuery { + return q. + WhereOr("? IS NULL", bun.Ident(column)). + WhereOr("? = ''", bun.Ident(column)) + } +} |