mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-28 15:53:32 -06:00
[chore] move status filtering from type converter (#4306)
This finalizes the moving status filtering out of the type converter, and into its own `./internal/filter/` subpkg :) Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4306 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
79914bdbf7
commit
66e1ec14aa
38 changed files with 565 additions and 846 deletions
|
|
@ -23,6 +23,7 @@ import (
|
|||
|
||||
"code.superseriousbusiness.org/gotosocial/internal/db"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/mutes"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/status"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/visibility"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
||||
|
|
@ -31,10 +32,11 @@ import (
|
|||
)
|
||||
|
||||
type Processor struct {
|
||||
state *state.State
|
||||
converter *typeutils.Converter
|
||||
visFilter *visibility.Filter
|
||||
muteFilter *mutes.Filter
|
||||
state *state.State
|
||||
converter *typeutils.Converter
|
||||
visFilter *visibility.Filter
|
||||
muteFilter *mutes.Filter
|
||||
statusFilter *status.Filter
|
||||
}
|
||||
|
||||
func New(
|
||||
|
|
@ -42,12 +44,14 @@ func New(
|
|||
converter *typeutils.Converter,
|
||||
visFilter *visibility.Filter,
|
||||
muteFilter *mutes.Filter,
|
||||
statusFilter *status.Filter,
|
||||
) Processor {
|
||||
return Processor{
|
||||
state: state,
|
||||
converter: converter,
|
||||
visFilter: visFilter,
|
||||
muteFilter: muteFilter,
|
||||
state: state,
|
||||
converter: converter,
|
||||
visFilter: visFilter,
|
||||
muteFilter: muteFilter,
|
||||
statusFilter: statusFilter,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95,21 +99,3 @@ func (p *Processor) getConversationOwnedBy(
|
|||
|
||||
return conversation, nil
|
||||
}
|
||||
|
||||
// getFiltersAndMutes gets the given account's filters and compiled mute list.
|
||||
func (p *Processor) getFilters(
|
||||
ctx context.Context,
|
||||
requestingAccount *gtsmodel.Account,
|
||||
) ([]*gtsmodel.Filter, gtserror.WithCode) {
|
||||
filters, err := p.state.DB.GetFiltersByAccountID(ctx, requestingAccount.ID)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(
|
||||
gtserror.Newf(
|
||||
"DB error getting filters for account %s: %w",
|
||||
requestingAccount.ID,
|
||||
err,
|
||||
),
|
||||
)
|
||||
}
|
||||
return filters, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import (
|
|||
"code.superseriousbusiness.org/gotosocial/internal/email"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/federation"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/mutes"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/status"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/visibility"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/log"
|
||||
|
|
@ -118,7 +119,7 @@ func (suite *ConversationsTestSuite) SetupTest() {
|
|||
suite.sentEmails = make(map[string]string)
|
||||
suite.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails)
|
||||
|
||||
suite.conversationsProcessor = conversations.New(&suite.state, suite.tc, suite.visFilter, suite.muteFilter)
|
||||
suite.conversationsProcessor = conversations.New(&suite.state, suite.tc, suite.visFilter, suite.muteFilter, status.NewFilter(&suite.state))
|
||||
testrig.StandardDBSetup(suite.db, nil)
|
||||
testrig.StandardStorageSetup(suite.storage, "../../../testrig/media")
|
||||
|
||||
|
|
|
|||
|
|
@ -64,17 +64,26 @@ func (p *Processor) GetAll(
|
|||
|
||||
items := make([]interface{}, 0, count)
|
||||
|
||||
filters, errWithCode := p.getFilters(ctx, requestingAccount)
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
}
|
||||
|
||||
for _, conversation := range conversations {
|
||||
// Check whether status if filtered by local participant in context.
|
||||
filtered, hide, err := p.statusFilter.StatusFilterResultsInContext(ctx,
|
||||
requestingAccount,
|
||||
conversation.LastStatus,
|
||||
gtsmodel.FilterContextNotifications,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "error filtering status: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if hide {
|
||||
continue
|
||||
}
|
||||
|
||||
// Convert conversation to frontend API model.
|
||||
apiConversation, err := p.converter.ConversationToAPIConversation(ctx,
|
||||
conversation,
|
||||
requestingAccount,
|
||||
filters,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf(ctx,
|
||||
|
|
@ -85,6 +94,9 @@ func (p *Processor) GetAll(
|
|||
continue
|
||||
}
|
||||
|
||||
// Set filter results on attached status model.
|
||||
apiConversation.LastStatus.Filtered = filtered
|
||||
|
||||
// Append conversation to return items.
|
||||
items = append(items, apiConversation)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import (
|
|||
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/log"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
|
|
@ -44,20 +45,27 @@ func (p *Processor) Read(
|
|||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
filters, errWithCode := p.getFilters(ctx, requestingAccount)
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
// Check whether status if filtered by local participant in context.
|
||||
filtered, _, err := p.statusFilter.StatusFilterResultsInContext(ctx,
|
||||
requestingAccount,
|
||||
conversation.LastStatus,
|
||||
gtsmodel.FilterContextNotifications,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "error filtering status: %v", err)
|
||||
}
|
||||
|
||||
apiConversation, err := p.converter.ConversationToAPIConversation(ctx,
|
||||
conversation,
|
||||
requestingAccount,
|
||||
filters,
|
||||
)
|
||||
if err != nil {
|
||||
err = gtserror.Newf("error converting conversation %s to API representation: %w", id, err)
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
// Set filter results on attached status model.
|
||||
apiConversation.LastStatus.Filtered = filtered
|
||||
|
||||
return apiConversation, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ import (
|
|||
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/id"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/log"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/typeutils"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
|
|
@ -158,26 +157,6 @@ func (p *Processor) UpdateConversationsForStatus(ctx context.Context, status *gt
|
|||
continue
|
||||
}
|
||||
|
||||
// Convert the conversation to API representation.
|
||||
apiConversation, err := p.converter.ConversationToAPIConversation(ctx,
|
||||
conversation,
|
||||
localAccount,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
// If the conversation's last status matched a hide filter, skip it.
|
||||
// If there was another kind of error, log that and skip it anyway.
|
||||
if !errors.Is(err, typeutils.ErrHideStatus) {
|
||||
log.Errorf(ctx,
|
||||
"error converting conversation %s to API representation for account %s: %v",
|
||||
status.ID,
|
||||
localAccount.ID,
|
||||
err,
|
||||
)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// If status was authored by this participant,
|
||||
// don't bother notifying, they already know!
|
||||
if status.AccountID == localAccount.ID {
|
||||
|
|
@ -198,6 +177,38 @@ func (p *Processor) UpdateConversationsForStatus(ctx context.Context, status *gt
|
|||
continue
|
||||
}
|
||||
|
||||
// Check whether status if filtered by local participant in context.
|
||||
filtered, hide, err := p.statusFilter.StatusFilterResultsInContext(ctx,
|
||||
localAccount,
|
||||
status,
|
||||
gtsmodel.FilterContextNotifications,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "error filtering status: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if hide {
|
||||
continue
|
||||
}
|
||||
|
||||
// Convert the conversation to API representation.
|
||||
apiConversation, err := p.converter.ConversationToAPIConversation(ctx,
|
||||
conversation,
|
||||
localAccount,
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "error converting conversation %s to API representation for account %s: %v",
|
||||
status.ID,
|
||||
localAccount.ID,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
// Set filter results on attached status model.
|
||||
apiConversation.LastStatus.Filtered = filtered
|
||||
|
||||
// Generate a notification,
|
||||
notifications = append(notifications, ConversationNotification{
|
||||
AccountID: localAccount.ID,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue