Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Implement Filter API v2 #2936

Merged
merged 16 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
551 changes: 549 additions & 2 deletions docs/api/swagger.yaml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/api/client/favourites"
"github.com/superseriousbusiness/gotosocial/internal/api/client/featuredtags"
filtersV1 "github.com/superseriousbusiness/gotosocial/internal/api/client/filters/v1"
filtersV2 "github.com/superseriousbusiness/gotosocial/internal/api/client/filters/v2"
"github.com/superseriousbusiness/gotosocial/internal/api/client/followrequests"
"github.com/superseriousbusiness/gotosocial/internal/api/client/instance"
"github.com/superseriousbusiness/gotosocial/internal/api/client/lists"
Expand Down Expand Up @@ -66,6 +67,7 @@ type Client struct {
favourites *favourites.Module // api/v1/favourites
featuredTags *featuredtags.Module // api/v1/featured_tags
filtersV1 *filtersV1.Module // api/v1/filters
filtersV2 *filtersV2.Module // api/v2/filters
followRequests *followrequests.Module // api/v1/follow_requests
instance *instance.Module // api/v1/instance
lists *lists.Module // api/v1/lists
Expand Down Expand Up @@ -110,6 +112,7 @@ func (c *Client) Route(r *router.Router, m ...gin.HandlerFunc) {
c.favourites.Route(h)
c.featuredTags.Route(h)
c.filtersV1.Route(h)
c.filtersV2.Route(h)
c.followRequests.Route(h)
c.instance.Route(h)
c.lists.Route(h)
Expand Down Expand Up @@ -142,6 +145,7 @@ func NewClient(db db.DB, p *processing.Processor) *Client {
favourites: favourites.New(p),
featuredTags: featuredtags.New(p),
filtersV1: filtersV1.New(p),
filtersV2: filtersV2.New(p),
followRequests: followrequests.New(p),
instance: instance.New(p),
lists: lists.New(p),
Expand Down
2 changes: 1 addition & 1 deletion internal/api/client/filters/v1/filterdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
// -
// name: id
// type: string
// description: ID of the list
// description: ID of the filter
// in: path
// required: true
//
Expand Down
3 changes: 3 additions & 0 deletions internal/api/client/filters/v1/filterdelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (suite *FiltersTestSuite) deleteFilter(
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
if expectedBody == "" {
return errs.Combine()
}
}

// if we got an expected body, return early
Expand Down
3 changes: 3 additions & 0 deletions internal/api/client/filters/v1/filterget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func (suite *FiltersTestSuite) getFilter(
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
if expectedBody == "" {
return nil, errs.Combine()
}
}

// if we got an expected body, return early
Expand Down
3 changes: 3 additions & 0 deletions internal/api/client/filters/v1/filterpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
// The text to be filtered.
//
// Sample: fnord
// minLength: 1
// maxLength: 40
// type: string
// -
Expand Down Expand Up @@ -120,6 +121,8 @@ import (
// description: not found
// '406':
// description: not acceptable
// '409':
// description: conflict (duplicate keyword)
// '422':
// description: unprocessable content
// '500':
Expand Down
14 changes: 3 additions & 11 deletions internal/api/client/filters/v1/filterpost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func (suite *FiltersTestSuite) postFilter(
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
if expectedBody == "" {
return nil, errs.Combine()
}
}

// if we got an expected body, return early
Expand Down Expand Up @@ -226,14 +229,3 @@ func (suite *FiltersTestSuite) TestPostFilterTitleConflict() {
suite.FailNow(err.Error())
}
}

// FUTURE: this should be removed once we support server-side filters.
func (suite *FiltersTestSuite) TestPostFilterIrreversibleNotSupported() {
phrase := "GNU/Linux"
context := []string{"home"}
irreversible := true
_, err := suite.postFilter(&phrase, &context, &irreversible, nil, nil, nil, http.StatusUnprocessableEntity, "")
if err != nil {
suite.FailNow(err.Error())
}
}
3 changes: 3 additions & 0 deletions internal/api/client/filters/v1/filterput.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
// The text to be filtered.
//
// Sample: fnord
// minLength: 1
// maxLength: 40
// type: string
// -
Expand Down Expand Up @@ -126,6 +127,8 @@ import (
// description: not found
// '406':
// description: not acceptable
// '409':
// description: conflict (duplicate keyword)
// '422':
// description: unprocessable content
// '500':
Expand Down
13 changes: 3 additions & 10 deletions internal/api/client/filters/v1/filterput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func (suite *FiltersTestSuite) putFilter(
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
if expectedBody == "" {
return nil, errs.Combine()
}
}

// if we got an expected body, return early
Expand Down Expand Up @@ -238,16 +241,6 @@ func (suite *FiltersTestSuite) TestPutFilterTitleConflict() {
}
}

// FUTURE: this should be removed once we support server-side filters.
func (suite *FiltersTestSuite) TestPutFilterIrreversibleNotSupported() {
id := suite.testFilterKeywords["local_account_1_filter_1_keyword_1"].ID
irreversible := true
_, err := suite.putFilter(id, nil, nil, &irreversible, nil, nil, nil, http.StatusUnprocessableEntity, "")
if err != nil {
suite.FailNow(err.Error())
}
}

func (suite *FiltersTestSuite) TestPutAnotherAccountsFilter() {
id := suite.testFilterKeywords["local_account_2_filter_1_keyword_1"].ID
phrase := "GNU/Linux"
Expand Down
3 changes: 3 additions & 0 deletions internal/api/client/filters/v1/filtersget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (suite *FiltersTestSuite) getFilters(
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
if expectedBody == "" {
return nil, errs.Combine()
}
}

// if we got an expected body, return early
Expand Down
4 changes: 4 additions & 0 deletions internal/api/client/filters/v1/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func validateNormalizeCreateUpdateFilter(form *model.FilterCreateUpdateRequestV1
if err := validate.FilterKeyword(form.Phrase); err != nil {
return err
}
// For filter v1 forwards compatibility, the phrase is used as the title of a v2 filter, so it must pass that as well.
if err := validate.FilterTitle(form.Phrase); err != nil {
return err
}
if err := validate.FilterContexts(form.Context); err != nil {
return err
}
Expand Down
80 changes: 80 additions & 0 deletions internal/api/client/filters/v2/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// GoToSocial
// Copyright (C) GoToSocial Authors [email protected]
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 v2

import (
"net/http"

"github.com/gin-gonic/gin"
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/processing"
)

const (
// BasePath is the base path for serving the filters API, minus the 'api' prefix
BasePath = "/v2/filters"
// BasePathWithID is the base path with the ID key in it, for operations on an existing filter.
BasePathWithID = BasePath + "/:" + apiutil.IDKey
// FilterKeywordsPathWithID is the path for operations on an existing filter's keywords.
FilterKeywordsPathWithID = BasePathWithID + "/keywords"
// FilterStatusesPathWithID is the path for operations on an existing filter's statuses.
FilterStatusesPathWithID = BasePathWithID + "/statuses"

// KeywordPath is the base path for operations on filter keywords that don't require a filter ID.
KeywordPath = BasePath + "/keywords"
// KeywordPathWithKeywordID is the path for operations on an existing filter keyword.
KeywordPathWithKeywordID = KeywordPath + "/:" + apiutil.IDKey

// StatusPath is the base path for operations on filter statuses that don't require a filter ID.
StatusPath = BasePath + "/statuses"
// StatusPathWithStatusID is the path for operations on an existing filter status.
StatusPathWithStatusID = StatusPath + "/:" + apiutil.IDKey
)

// Module implements APIs for client-side aka "v1" filtering.
type Module struct {
processor *processing.Processor
}

func New(processor *processing.Processor) *Module {
return &Module{
processor: processor,
}
}

func (m *Module) Route(attachHandler func(method string, path string, f ...gin.HandlerFunc) gin.IRoutes) {
attachHandler(http.MethodGet, BasePath, m.FiltersGETHandler)

attachHandler(http.MethodPost, BasePath, m.FilterPOSTHandler)
attachHandler(http.MethodGet, BasePathWithID, m.FilterGETHandler)
attachHandler(http.MethodPut, BasePathWithID, m.FilterPUTHandler)
attachHandler(http.MethodDelete, BasePathWithID, m.FilterDELETEHandler)

attachHandler(http.MethodGet, FilterKeywordsPathWithID, m.FilterKeywordsGETHandler)
attachHandler(http.MethodPost, FilterKeywordsPathWithID, m.FilterKeywordPOSTHandler)

attachHandler(http.MethodGet, KeywordPathWithKeywordID, m.FilterKeywordGETHandler)
attachHandler(http.MethodPut, KeywordPathWithKeywordID, m.FilterKeywordPUTHandler)
attachHandler(http.MethodDelete, KeywordPathWithKeywordID, m.FilterKeywordDELETEHandler)

attachHandler(http.MethodGet, FilterStatusesPathWithID, m.FilterStatusesGETHandler)
attachHandler(http.MethodPost, FilterStatusesPathWithID, m.FilterStatusPOSTHandler)

attachHandler(http.MethodGet, StatusPathWithStatusID, m.FilterStatusGETHandler)
attachHandler(http.MethodDelete, StatusPathWithStatusID, m.FilterStatusDELETEHandler)
}
118 changes: 118 additions & 0 deletions internal/api/client/filters/v2/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// GoToSocial
// Copyright (C) GoToSocial Authors [email protected]
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 v2_test

import (
"testing"

"github.com/stretchr/testify/suite"
filtersV2 "github.com/superseriousbusiness/gotosocial/internal/api/client/filters/v2"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/email"
"github.com/superseriousbusiness/gotosocial/internal/federation"
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/internal/processing"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/storage"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
"github.com/superseriousbusiness/gotosocial/testrig"
)

type FiltersTestSuite struct {
suite.Suite
db db.DB
storage *storage.Driver
mediaManager *media.Manager
federator *federation.Federator
processor *processing.Processor
emailSender email.Sender
sentEmails map[string]string
state state.State

// standard suite models
testTokens map[string]*gtsmodel.Token
testClients map[string]*gtsmodel.Client
testApplications map[string]*gtsmodel.Application
testUsers map[string]*gtsmodel.User
testAccounts map[string]*gtsmodel.Account
testStatuses map[string]*gtsmodel.Status
testFilters map[string]*gtsmodel.Filter
testFilterKeywords map[string]*gtsmodel.FilterKeyword
testFilterStatuses map[string]*gtsmodel.FilterStatus

// module being tested
filtersModule *filtersV2.Module
}

func (suite *FiltersTestSuite) SetupSuite() {
suite.testTokens = testrig.NewTestTokens()
suite.testClients = testrig.NewTestClients()
suite.testApplications = testrig.NewTestApplications()
suite.testUsers = testrig.NewTestUsers()
suite.testAccounts = testrig.NewTestAccounts()
suite.testStatuses = testrig.NewTestStatuses()
suite.testFilters = testrig.NewTestFilters()
suite.testFilterKeywords = testrig.NewTestFilterKeywords()
suite.testFilterStatuses = testrig.NewTestFilterStatuses()
}

func (suite *FiltersTestSuite) SetupTest() {
suite.state.Caches.Init()
testrig.StartNoopWorkers(&suite.state)

testrig.InitTestConfig()
config.Config(func(cfg *config.Configuration) {
cfg.WebAssetBaseDir = "../../../../../web/assets/"
cfg.WebTemplateBaseDir = "../../../../../web/templates/"
})
testrig.InitTestLog()

suite.db = testrig.NewTestDB(&suite.state)
suite.state.DB = suite.db
suite.storage = testrig.NewInMemoryStorage()
suite.state.Storage = suite.storage

testrig.StartTimelines(
&suite.state,
visibility.NewFilter(&suite.state),
typeutils.NewConverter(&suite.state),
)

suite.mediaManager = testrig.NewTestMediaManager(&suite.state)
suite.federator = testrig.NewTestFederator(&suite.state, testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../../testrig/media")), suite.mediaManager)
suite.sentEmails = make(map[string]string)
suite.emailSender = testrig.NewEmailSender("../../../../../web/template/", suite.sentEmails)
suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager)
suite.filtersModule = filtersV2.New(suite.processor)

testrig.StandardDBSetup(suite.db, nil)
testrig.StandardStorageSetup(suite.storage, "../../../../../testrig/media")
}

func (suite *FiltersTestSuite) TearDownTest() {
testrig.StandardDBTeardown(suite.db)
testrig.StandardStorageTeardown(suite.storage)
testrig.StopWorkers(&suite.state)
}

func TestFiltersTestSuite(t *testing.T) {
suite.Run(t, new(FiltersTestSuite))
}
Loading