From 54f6caed659e8d7d94ad8a0a76405fd0870f297c Mon Sep 17 00:00:00 2001 From: Blackle Morisanchetto Date: Sat, 27 Aug 2022 05:35:31 -0400 Subject: [PATCH] [bugfix] Status visibility + `excludeReplies` fixes (#769) * Fix some bugs when viewing a user's posts: include their self-replies (threads) even when excludeReplies is set, and use in_reply_to_uri instead of in_reply_to_id to filter out replies * Assign values to InReplyToURI when creating statuses. Add index and update old statuses with a migration --- internal/db/bundb/account.go | 11 +++- ...7085121_assign_missing_in_reply_to_uris.go | 66 +++++++++++++++++++ internal/processing/status/util.go | 1 + 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 internal/db/bundb/migrations/20220827085121_assign_missing_in_reply_to_uris.go diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index 95c3d80d..f4614340 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -270,7 +270,14 @@ func (a *accountDB) GetAccountStatuses(ctx context.Context, accountID string, li } if excludeReplies { - q = q.WhereGroup(" AND ", whereEmptyOrNull("in_reply_to_id")) + // include self-replies (threads) + whereGroup := func(*bun.SelectQuery) *bun.SelectQuery { + return q. + WhereOr("in_reply_to_account_id = ?", accountID). + WhereGroup(" OR ", whereEmptyOrNull("in_reply_to_uri")) + } + + q = q.WhereGroup(" AND ", whereGroup) } if excludeReblogs { @@ -332,7 +339,7 @@ func (a *accountDB) GetAccountWebStatuses(ctx context.Context, accountID string, Table("statuses"). Column("id"). Where("account_id = ?", accountID). - WhereGroup(" AND ", whereEmptyOrNull("in_reply_to_id")). + WhereGroup(" AND ", whereEmptyOrNull("in_reply_to_uri")). WhereGroup(" AND ", whereEmptyOrNull("boost_of_id")). Where("visibility = ?", gtsmodel.VisibilityPublic). Where("federated = ?", true) diff --git a/internal/db/bundb/migrations/20220827085121_assign_missing_in_reply_to_uris.go b/internal/db/bundb/migrations/20220827085121_assign_missing_in_reply_to_uris.go new file mode 100644 index 00000000..29a5ee4a --- /dev/null +++ b/internal/db/bundb/migrations/20220827085121_assign_missing_in_reply_to_uris.go @@ -0,0 +1,66 @@ +/* + 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 . +*/ + +package migrations + +import ( + "context" + + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/uptrace/bun" +) + +func init() { + up := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // set in_reply_to_uri to the uri of the status pointed to by in_reply_to_id + if _, err := tx.NewUpdate(). + Table("statuses"). + TableExpr("statuses AS secondary"). + SetColumn("in_reply_to_uri", "secondary.uri"). + Where("statuses.in_reply_to_id = secondary.id"). + Where("statuses.in_reply_to_id IS NOT null"). + Where("statuses.in_reply_to_uri IS null"). + Exec(ctx); err != nil { + return err + } + + // add index to in_reply_to_uri + if _, err := tx. + NewCreateIndex(). + Model(>smodel.Status{}). + Index("statuses_in_reply_to_uri_idx"). + Column("in_reply_to_uri"). + Exec(ctx); err != nil { + 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) + } +} diff --git a/internal/processing/status/util.go b/internal/processing/status/util.go index 5e961e2e..13c5b958 100644 --- a/internal/processing/status/util.go +++ b/internal/processing/status/util.go @@ -151,6 +151,7 @@ func (p *processor) ProcessReplyToID(ctx context.Context, form *apimodel.Advance } status.InReplyToID = repliedStatus.ID + status.InReplyToURI = repliedStatus.URI status.InReplyToAccountID = repliedAccount.ID return nil